PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
69 Posts
Hardware
123 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
20 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


Coil Winding machine counter with Atmega8 and Reed relay | 77 Views | Rate 77
Coil Winding machine counter with Atmega8 and Reed relay
uRADMonitor - Online Radiation monitoring station | 15070 Views | Rate 69.77
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18713 Views | Rate 59.79
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4938 Views | Rate 55.48
NMEA GPS Library for AVR Microcontrollers
Tube: GP-5 (ГП-5) | 47 Views | Rate 47
Tube: GP-5 (ГП-5)
Programmatically Injecting Events on Android - Part 2 | 5190 Views | Rate 45.13
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4775 Views | Rate 44.63
Building a robot – Part 2
Simple Switched power Supplies | 16454 Views | Rate 42.3
Simple Switched power Supplies

 
  

CreateProcess, GetExitCodeProcess and application crash


By Radu Motisan Posted on October 20th, 2009 , 813 Views (Rate 0.62)



  




Recently I've been working on a C++ app that needed to start a second app and catch its exit code.

The exit code is the number returned by main() or WinMain() at the application exit point.

The two apps I'm using are Starter.exe and WMLoader.exe (that gets executed by the first). WMLoader is a very basic console application that only returns an error code (for testing purposes)

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
return 11;
}

To receive the exit code 11 in Starter.exe I need to:

1) call CreateProcess to start WMLoader.exe

2) WaitForSingleObject to wait until WMLoader terminates

3) call GetExitCodeProcess to get the exit code.

My inital code was:

DWORD                  code;
STARTUPINFO          si;
PROCESS_INFORMATION  pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

if (!CreateProcess(NULL, L"WMLoader.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
printf("CreateProcess() failed: %d\n", GetLastError());
exit(1);
}

if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED) {
printf("WaitForSingleObject() failed: %d\n", GetLastError());
exit(1);
}

if (!(GetExitCodeProcess(pi.hProcess, &code))) {
printf("GetExitCodeProcess() failed: %d\n", GetLastError());
exit(1);
}
printf("code=%d", code);

This resulted in Starter.exe crash with the following message:

After wasting a few minutes double checking everything, I found the problem. From MSDN, CreateProcess page:

pszCmdLine
[in, out] Pointer to a null-terminated string that specifies the command line to execute.
The system adds a null character to the command line, trimming the string if necessary, to indicate which file was used.

Meaning you can't use a constant for pszCmdLine.

I've also modified the code to start the second process in SUSPENDED mode and resume it after the successful CreateProcess call:
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

TCHAR szFile[MAX_PATH] = TEXT("WMLoader.exe");
if(!CreateProcess(
NULL, // No module name (use command line).
szFile,
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_SUSPENDED, // Create suspended.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION structure.
{
// Handle error.
printf("Createprocess failed (%d).\n", GetLastError());
}
else
{
// Resume the external process thread.
DWORD resumeThreadResult = ResumeThread(pi.hThread);
// ResumeThread() returns 1 which is OK
// (it means that the thread was suspended but then restarted)

// Wait for the external process to finish.
DWORD waitForSingelObjectResult = WaitForSingleObject(pi.hProcess, INFINITE);
// WaitForSingleObject() returns 0 which is OK.

// Get the exit code of the external process.
DWORD exitCode;
if(!GetExitCodeProcess(pi.hProcess, &exitCode))
{
// Handle error.
printf("GetExitCodeProcess failed (%d).\n", GetLastError());
}
else
{
printf("Application exitcode:%d.\n", exitCode);
}
}






  

More on PocketMagic:

Programmatically Injecting Events on Android - Part 1 | 10531 Views | Rate 26.93
Programmatically Injecting Events on Android - Part 1
Atmega8 and enc28J60 for ethernet support | 7515 Views | Rate 25.13
Atmega8 and enc28J60 for ethernet support
Tube: USN-5J29 | 426 Views | Rate 25.06
Tube: USN-5J29
How to set the AVR Fusebits | 1797 Views | Rate 23.64
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2133 Views | Rate 22.94
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Dual H-Bridge for controlling two motors | 1304 Views | Rate 21.73
Dual H-Bridge for controlling two motors

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string ZwwsI2 to the field below: