Reputation: 377
I have to make a simple program with visual studio, using Windows APIs. My code works well in debug mode, however, it doesn't work well in release mode and I couldn't figure it why. I cut & paste the part where my program crashed. Here's that part.
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
int _tmain(int argc, TCHAR * argv[])
{
TCHAR cmdString[] = "notepad.exe";
STARTUPINFO si = { 0, };
PROCESS_INFORMATION pi;
si.cb = sizeof(si);
BOOL ret = TRUE;
CreateProcess(NULL, cmdString, NULL, NULL, TRUE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
_tprintf(_T("Error = {%d}\n", GetLastError()));
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
system("pause");
return 0;
}
Simple program which opens notepad.exe, right? Works well in debug mode, but notepad won't open in release mode(Program ends without opening notepad.exe).
I tried to find solution in S.O, like this link but It doesn't help me much.
Why CreateProcess() doesn't work properly in release-mode?
Upvotes: 0
Views: 194
Reputation: 161
My guess is that it's all about compiler optimization as this post can tell you way better than me.
Since CreateProcess is returning (on success) nonzero.
Try using it this way :
BOOL ret = TRUE;
if(!CreateProcess(NULL, cmdString, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
printf("Error = {%d}", GetLastError());
ret = FALSE;
You'll even have some details on the error.
Upvotes: 2