AceInfinity
Emeritus, Contributor
Source Code:
Result:
Thought I'd share a basic example of creating a process, and getting the exit code from the child process after it exits. This also features a function I created to grab the Win32 error code description from an integer, probably returned by GetLastError(), unless you call it with an explicit Win32 error integer. You can see it in effect, returning an error code description for 0x2 by putting junk data in for the filename used in the CreateProcess function call.
It seems access to the GetProcessId() function from the Win32 API depends on the _WIN32_WINNT #define based on my tests. I just remembered having issue with another function which required NT 5 being defined, so I was curious about testing the same here, incremented it to 6, and it worked lol. I could've looked in the headers myself, but I couldn't bother. I just wanted to mash up some functions here and there to give a fuller example of creating a child process with C++.
:thumbsup2:
Code:
[NO-PARSE]#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#define _WIN32_WINNT 0x0600 // GetProcessId
#include <windows.h>
#include <iostream>
int StartProcess( LPCTSTR lpApplicationName, LPTSTR lpCommandLine )
{
STARTUPINFO procStartInfo;
PROCESS_INFORMATION procInfo;
ZeroMemory( &procStartInfo, sizeof(procStartInfo) );
procStartInfo.cb = sizeof(procStartInfo);
ZeroMemory( &procInfo, sizeof(procInfo) );
// process could not be created.
if( !CreateProcess( lpApplicationName, lpCommandLine,
NULL, NULL, FALSE, 0, NULL, NULL, &procStartInfo, &procInfo ) )
{
return GetLastError();
}
// wait until process has finished processing initial and pending input.
WaitForInputIdle( procInfo.hProcess, INFINITE );
std::wcout << "Process awaiting input..." << std::endl;
DWORD pid = GetProcessId( procInfo.hProcess );
std::wcout << "PID: " << pid << std::endl;
DWORD pVer = GetProcessVersion( pid );
std::wcout << "Process Major Version: " << HIWORD( pVer ) << std::endl;
std::wcout << "Process Minor Version: " << LOWORD( pVer ) << std::endl;
int isWOW64;
IsWow64Process( procInfo.hProcess, &isWOW64 );
std::wcout << "WOW64 Process: " << ( isWOW64 ? "True" : "False" ) << std::endl;
// wait until the child process exits.
WaitForSingleObject( procInfo.hProcess, INFINITE );
// retrieve the exit code from the child process.
ULONG exitCode;
if ( GetExitCodeProcess(procInfo.hProcess, &exitCode) )
std::wcout << "Process terminated with exit code [" << exitCode << ']' << std::endl;
// close process and thread handles.
CloseHandle( procInfo.hProcess );
CloseHandle( procInfo.hThread );
return EXIT_SUCCESS;
}
bool LastWin32Error( int err_code, WCHAR *buffer, const int buffer_len )
{
return FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err_code, 0,
buffer, buffer_len, (va_list*)NULL );
}
int main()
{
const DWORD ARG_SIZE = 128;
WCHAR cmdFile[MAX_PATH] = L"C:\\Windows\\System32\\notepad.exe";
WCHAR cmdArgs[ARG_SIZE] = L"Z:\\fb.txt";
const DWORD bufferLen = 256;
WCHAR *buffer = new WCHAR[bufferLen];
if ( LastWin32Error( StartProcess( cmdFile, cmdArgs ), buffer, bufferLen ) )
std::wcout << "Result: " << buffer << std::endl;
delete [] buffer;
}[/NO-PARSE]
Result:
Code:
[NO-PARSE]Process awaiting input...
PID: 1852
Process Major Version: 6
Process Minor Version: 3
WOW64 Process: True
Process terminated with exit code [0]
Result: The operation completed successfully.
[Finished in 5.0s][/NO-PARSE]
Thought I'd share a basic example of creating a process, and getting the exit code from the child process after it exits. This also features a function I created to grab the Win32 error code description from an integer, probably returned by GetLastError(), unless you call it with an explicit Win32 error integer. You can see it in effect, returning an error code description for 0x2 by putting junk data in for the filename used in the CreateProcess function call.
It seems access to the GetProcessId() function from the Win32 API depends on the _WIN32_WINNT #define based on my tests. I just remembered having issue with another function which required NT 5 being defined, so I was curious about testing the same here, incremented it to 6, and it worked lol. I could've looked in the headers myself, but I couldn't bother. I just wanted to mash up some functions here and there to give a fuller example of creating a child process with C++.
:thumbsup2: