Вот полный код, основанный в http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx и решение hmjd :
#include <stdio.h>
#include <Windows.h>
int main()
{
const size_t stringSize = 1000;
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD exit_code;
char commandLine[stringSize] = "C:\\myDir\\someExecutable.exe param1 param2";
WCHAR wCommandLine[stringSize];
mbstowcs (wCommandLine, commandLine, stringSize);
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
wCommandLine, // 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 -1;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &exit_code);
printf("the execution of: \"%s\"\nreturns: %d\n", commandLine, exit_code);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
(работает как консольное приложение VS2005 в Windows XP)
Спасибо. Это то, что я искал. Похоже, мне нужно будет использовать CreateProcess(), чтобы начать эту вещь. Эта функция заполняет структуру PROCESS_INFORMATION, которая имеет РУЧКУ, которая мне нужна для использования с GetExitCodeProcess(). –
Когда вы закончите с ручкой, не забудьте закрыть его с помощью функции «CloseHandle». –
@ ЕвгенияРябцева, спасибо, я забыл добавить это. Обновлено. – hmjd