[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]