如何使用Createprocessa()在C ++中运行批处理文件 [英] How Can I Run Batch File With Createprocessa() In C++
问题描述
我想用createprocessA()运行命令行批处理文件。
因为我想运行cmd行而不可见。
我必须隐藏cmd窗口。
这是我的代码:
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:
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);
我在这样的cmd窗口试过它。
cmd /cc:\\windows\\shot.bat
效果不错但是。
CreateProcessA()返回1并且不执行批处理文件操作。
它不起作用。
我需要帮助。
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.
推荐答案
这篇文章正是你想要的;
以静默方式运行控制台应用程序 [ ^ ]
我已经使用批处理文件进行了测试,并且完全按照描述工作。
This article is exactly what you want;
Running console applications silently[^]
I have tested with batch file and works exactly as described.
您需要使用/ c
或/ k
;使用这些选项后面的文本作为启动的shell中的命令执行。
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.
启动Windows命令解释程序的新实例
CMD [/ A | / U] [/ Q] [/ D] [/ E:ON | / E:OFF] [/ F:ON | / F:OFF] [/ V:ON | / V:OFF]
[[/ S] [/ C | / K] string]
/ C执行string指定的命令然后终止
/ K执行string指定的命令但仍然是
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
这篇关于如何使用Createprocessa()在C ++中运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!