Search code examples
c++windowswinapicreateprocesswin32-process

How to check Win32 CreateProcess() failed reason using ProcMon. exclude GetLastError()


I am having an issue with checking CreateProcess() failure reason, there was a code in production which doesn't log GetLastError() when CreateProcess() failed so i am running ProcMon to check the reason but unable to find the reason (Will procMon log the failure reason something like "C:\dummy.exe path not found or permission denied" ?).

Is there a way (tools ?) to check why CreateProcess() is failing without considering GetLastError() ?

I can't debug customer environment (no access to me) but I can change the code & provide new build & it takes long time due to process. i am currently looking for quick options available. Below is the sample code not exact production code.

int main()
{
    STARTUPINFO info = { sizeof(info) };
    PROCESS_INFORMATION processInfo;

    TCHAR dymmypath[_MAX_PATH] = _T("C:\\dummy.exe");
    static TCHAR TempPathString[_MAX_PATH];

    STARTUPINFO         si = { sizeof(si) };        //default set up
    PROCESS_INFORMATION pi;                     //data structure for CreateProcess

    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWMINIMIZED;

    if (!CreateProcess(dymmypath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, TempPathString, &si, &pi))
    {
        printf("Failed");
    }
    else {
        printf("Success");
    }

    return 0;
}

Solution

  • i am running ProcMon to check the reason but unable to find the reason (Will procMon log the failure reason something like "C:\dummy.exe path not found or permission denied" ?).

    Only if the request reaches the filesystem, ie to look for the EXE file, which in your case it sounds like it is not doing that, likely because CreateProcess() is failing to validate your input parameters before it reaches into the filesystem.

    Is there a way (tools ?) to check why CreateProcess() is failing without considering GetLastError() ?

    As others said, you could try attaching a debugger to your running app, and put a breakpoint in the CreateProcess function itself.

    Another option is to use a tool like API Monitor, which will show you the actual API calls your program makes, what their parameter values are, reported error codes, etc.

    I can't debug customer environment (no access to me) but I can change the code & provide new build

    Then that is what you should do. Fix your code to do proper logging of error codes, don't ignore them anymore.

    it takes long time due to process.

    Well, that is your own fault for not optimizing your build process better, or breaking up your app into more manageable pieces, etc.