我在使用C++的windows.h頭文件中引入了CreatProcess函數的hev問題。每當我嘗試傳遞一個包含cmd命令的TCHAR變量時,它會返回錯誤:CreateProcess failed(2)。 ,爲此我正在等待您的解釋和解決方案。爲什麼命令不能用CreateProcess執行
考慮下面的代碼:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain(int argc, TCHAR *argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if(argc != 2)
{
printf("Usage: %s [cmdline]\n", argv[0]);
return 0;
}
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
通知:當我啓動與指定它的路徑的應用程序..它工作得很好像=> 「C:\ code.exe」;
在這種情況下,你的應用程序不工作?你在說:code.exe C:\ Code.exe正在工作? –
以防將DOS命令作爲參數傳遞 – afr0ck