0

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?

Hashnut
  • 367
  • 3
  • 18
  • 1
    When you say "doesn't work in release mode", do you mean that the *compiler* gives you an error when building? If so, is that message you show *the full* and *complete* copy-pasted output? Otherwise, do you mean that you get an errors when running the program? – Some programmer dude Oct 01 '18 at 10:26
  • 3
    This looks more like a debugger diagnostic suggesting that with optimizations enabled variable has been optimized away and is not inspectable. – user7860670 Oct 01 '18 at 10:30
  • @Someprogrammerdude Thanks for the comment. I added some details to clarify the issue. – Hashnut Oct 01 '18 at 10:43
  • @VTT Thanks for the comment! What exactly does it mean that 'variable has been optimized away?' -> I found this quote, "When a variable has been "optimized away," it just means that it's not being modified in the context of the current scope." in [this link](https://stackoverflow.com/questions/31503178/how-get-value-of-variable-that-has-been-optimized-out), though this issue is about JS. But there is only single 'main' scope in the code above... + I turned off optimization option in VS2017, but still notepad won't open :( – Hashnut Oct 01 '18 at 10:47
  • 3
    You already know the value of isRun. Never, never write a "it did not work" error message. An absolute minimum is to use GetLastError(). – Hans Passant Oct 01 '18 at 10:55
  • 3
    Code after edit completely lacks error handling... – user7860670 Oct 01 '18 at 11:13
  • 1
    @TonyAhn so what was the error number returned by `GetLastEror()`? – Jabberwocky Oct 01 '18 at 11:29
  • @Jabberwocky It returned 208. – Hashnut Oct 01 '18 at 11:37

1 Answers1

2

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.

Kianii
  • 141
  • 1
  • 9
  • Thank you for the answer. By the way, when I debugged the program, It just bypass 'if' statement, even though I tried 'step into'. So I removed 'if' statement and just use printf to get error code. – Hashnut Oct 01 '18 at 11:39
  • 2
    @TonyAhn the value returned by [`GetError()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx) only makes sense if `CreateProcess` returned `FALSE`. – Jabberwocky Oct 01 '18 at 11:42
  • Yes, you're absolutely right. But if `CreateProcess` returns TRUE, then notepad.exe should open, right? Weird thing is that `CreateProcess` returns TRUE while notepad.exe won't open. (Only in the release mode. Works properly in debug mode) – Hashnut Oct 01 '18 at 11:45
  • GetLastError return 0 on success. Another thing, for TCHAR : TCHAR cmdString[] = L"notepad.exe"; Didn't u forget the L before your litteral**S** String**S** ? (Same for the error if you're using _tprintf) – Kianii Oct 01 '18 at 11:52