What's wrong with this code, part 11: Launching notepad.

Anyway, I ran into this problem while I was writing some stuff at work.  I've restructured it to remove the "work-ness" of the code, but the problem still exists.

It's short, but sweet:

#include <stdio.h>#include <windows.h>#define PROCESS_NAME L"C:\\WINDOWS\\NOTEPAD.EXE"int _tmain(int argc, _TCHAR* argv[]){    PROCESS_INFORMATION processInformation = {0};    STARTUPINFO startupInfo = {0};    DWORD status;    if (GetFileAttributesW(PROCESS_NAME) == INVALID_FILE_ATTRIBUTES)    {        wprintf(L"Can't find " PROCESS_NAME L"; error %d, aborting\n", GetLastError());        return(1);    }    startupInfo.cb = sizeof(startupInfo);    if (!CreateProcessW(NULL, PROCESS_NAME, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInformation))    {        status = GetLastError();    }    wprintf(L"Status launching " PROCESS_NAME L" is %d\n", status);    return 0;}

Some things that are NOT wrong with this code:

  1. The code is unicode-only.  It is not intended to work in non unicode environments.
  2. The code expects that the system directory is C:\WINDOWS.  This is not a bug, it's by design.  The real code was more complicated this simplified version, but both show the same bug.

It's possible that you won't see the bug if you take it and compile it.  But it is still there, and if you compile it in the NT build environment, you'll see the problem (you may also see the problem with current versions of Visual Studio, I was able to reproduce the problem quite easily.