Page 1 of 1
Run commands of Command Prompt
Posted: Sat Sep 26, 2009 7:11 pm
by Haruks
Hello,
I am trying learn the PureBasic...
Currently I know write on CMD (Command Prompt of Windows)
But now I want learn the PureBasic, but as I am starting, if I can run commands of CMD on PB this will help me a lot!

!
> Someone know how can i run commands of CMD on the PB?
I think in use the "OpenFile" to open a .BAT, but this open a CMD window...
Can someone help me?
(Sorry for my English, I'm learning too =] )
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 7:25 pm
by thanos
> Someone know how can i run commands of CMD on the PB?
I think in use the "OpenFile" to open a .BAT, but this open a CMD window...
Could you give an example of the commands you want to run?
Regards.
Thanos
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 7:33 pm
by Kaeru Gaman
if you want to execute a shell command, start command.com with RunProgram and pass the command as Parameter$.
if you want to execute batch files, start them with RunProgram.
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 7:41 pm
by Haruks
Hello thanos, thanks for your reply!
Examples of commands:
> Create a local user with password:
net user USER PASSWORD /add /expires:never
> Add user to a group:
net localgroup GROUP USER /add
> Create a registry key:
reg add "HKLM\SOFTWARE\Example" /v VALUE /t REG_DWORD /d 0 /f
> Query Services:
sc query SERVICE
Now I thought of one thing... I can use the "OpenFile" to run the command and use the parameters... i will test this now!
because the commands "NET" "REG" and "SC" is files on C:\Windows\System32
- Haruks
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 7:43 pm
by Haruks
Kaeru Gaman wrote:if you want to execute a shell command, start command.com with RunProgram and pass the command as Parameter$.
if you want to execute batch files, start them with RunProgram.
yes... I will test this!
Thanks Thanos and Kaeru Gaman
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 8:00 pm
by thanos
Just a snippet, if the PATH includes the reg.exe directory:
Code: Select all
Param$ = "ADD " + Chr(34) + "HKLM\SOFTWARE\Example" + Chr(34) + " /v VALUE /t REG_DWORD /d 0 /f"
Result = RunProgram("reg", Param$, "")
Debug Result
if it is not in the PATH you should add the directory which contains the reg.exe:
Code: Select all
Param$ = "ADD " + Chr(34) + "HKLM\SOFTWARE\Example" + Chr(34) + " /v VALUE /t REG_DWORD /d 0 /f"
Result = RunProgram("reg", Param$, "C:\Windows\System32")
Debug Result
Regards
Thanos
Re: Run commands of Command Prompt
Posted: Sat Sep 26, 2009 8:23 pm
by Haruks
Thanos
then, when I will use
" I need create a variable...
Thanks for this information!!
Only a question:
on the code below, the
,"" is the path correct? (C:\Windows\System32 by default)
Code: Select all
OpenConsole()
PrintN("Query the service BROWSER")
RunProgram("sc.exe", "query browser","")
input()
End
Re: Run commands of Command Prompt
Posted: Sun Sep 27, 2009 9:46 am
by thanos
No. I think that the "WorkingDirectory" is the program's directory by default.
If you want to run the sc.exe, the file must be either in the program's directory or in one of the PATH's directories.
Otherwise you must include the directory in the WorkingDirectory parameter. E.g. The directory which contains tour program is the C:\CodeBase and the sc.exe is in the C:\Haruks directory which is not included in the PATH variable:
Code: Select all
RunProgram("sc.exe", "query browser", "C:\Haruks")
Regards.
Thanos
Re: Run commands of Command Prompt
Posted: Sun Sep 27, 2009 6:37 pm
by p2hicy
Code: Select all
ImportC "msvcrt.lib"
system(str.p-ascii)
EndImport
OpenConsole()
system("echo oh hai")
system("pause")
That oughta do it. Now you can issue your commands directly as if you would type them into a command prompt window.
Re: Run commands of Command Prompt
Posted: Mon Sep 28, 2009 2:34 am
by Haruks
> thanos
Humm... don't need use the path C:\windows\system32 for run a command... so, when i use the "RunProgram" this is equal if I use the "Run" (of the windows: start>run) but we can use many options!! (run hide for example...)
> p2hicy
Ual! Thanks a lot!!!!
one question about this lib.. Can I run command with this lib without show any screen?
for example:
RunProgram("sc.exe", "query browser","",#PB_Program_Hide)
Re: Run commands of Command Prompt
Posted: Mon Sep 28, 2009 7:11 am
by p2hicy
If you can live with a short pop-up of the console window, you can use this:
Code: Select all
ImportC "msvcrt.lib"
system(str.p-ascii)
EndImport
a$ = Space(500)
If OpenConsole()
If GetConsoleTitle_(@a$, Len(a$))
ShowWindow_(FindWindow_(0, a$), #SW_HIDE)
EndIf
system("sc query browser")
EndIf
Re: Run commands of Command Prompt
Posted: Tue Sep 29, 2009 12:27 am
by Haruks
Where i get more information about the lib MSVCRT?
Thanks!
Re: Run commands of Command Prompt
Posted: Tue Sep 29, 2009 6:14 pm
by thanos
Haruks wrote:Where i get more information about the lib MSVCRT?
Thanks!
Try this link:
http://msdn.microsoft.com/en-us/library ... S.80).aspx
Regards
Thanos