Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want run command line batch file with createprocessA().
Because I want run cmd line without visible.
I have to hide cmd window.
Here is my code:


C++
PROCESS_INFORMATION pInfo;
	FillMemory(&pInfo, sizeof(PROCESS_INFORMATION), 0); 
	STARTUPINFOA sInfo;
	FillMemory(&sInfo, sizeof(STARTUPINFOA), 0); 
	sInfo.cb = sizeof(STARTUPINFOA);
	sInfo.lpReserved = NULL;
	sInfo.lpReserved2 = NULL;
	sInfo.cbReserved2 = 0;
	sInfo.lpDesktop = NULL;
	sInfo.lpTitle = NULL;
	sInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
	sInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
	sInfo.hStdOutput =  GetStdHandle(STD_OUTPUT_HANDLE);
	sInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
	sInfo.wShowWindow = SW_HIDE;
	BOOL ret = ::CreateProcessA("c:\\windows\\system32\\cmd.exe", "c:\\windows\\shot.bat", NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);


I tried it in cmd window like this.
cmd /c c:\\windows\\shot.bat
It works well but.
CreateProcessA() returns 1 and it does not take batch file action.
It does not work.


I need help.
Posted
Updated 1-Mar-14 14:22pm
v8

This article is exactly what you want;

Running console applications silently[^]

I have tested with batch file and works exactly as described.
 
Share this answer
 
Comments
kenffff 2-Mar-14 1:59am    
thank you.
[no name] 2-Mar-14 3:19am    
No problem. The author of the article may appreciate your vote.
You need to use either /c or /k; using these options the text behind it executed as a command in the shell that is started.
Quote cmd /?:
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]

/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
 
Share this answer
 
Comments
kenffff 1-Mar-14 10:32am    
I want hide cmd window.
thanks
kenffff 1-Mar-14 19:41pm    
I've tried this.
CreateProcessA("c:\\windows\\system32\\cmd.exe","/c c:\\windows\\shot.bat",....);
batch file does not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900