Thread: replacing system with CreateProcess

  1. #1
    Registered User
    Join Date
    Jan 2007

    replacing system with CreateProcess

    I have some code that starts up an executable with system(). It works fine except that it shows up a command prompt for a little while.

    We want to get rid of that, so I changed the code to use CreateProcess. Now no window shows up but the program crashes. Anyone know what Im doing wrong?

    int doCmdLineCall(std::string command) const
        //return system(command.c_str());
        // mimic return value of system() to not change error handling behaviour
        STARTUPINFO info = {0};
        PROCESS_INFORMATION procInfo = {0};
        BOOL ret = FALSE;
        std::string::size_type i = command.find_first_of(' ');
        std::string cmd;
        std::string param;
        if (i != std::string::npos)
            cmd = command.substr(0, i);
            param = command.substr(i, command.length());
            info.cb = sizeof(info);
            info.dwFlags = STARTF_USESHOWWINDOW;
            info.wShowWindow = SW_HIDE;
            ret = CreateProcessA(cmd.c_str(), (char *)param.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &info, &procInfo);
            if (ret)
                WaitForSingleObject(procInfo.hProcess, (DWORD)-1L);
                DWORD exitcode;
                GetExitCodeProcess(procInfo.hProcess, &exitcode);
                ret = (intptr_t)(int)exitcode;
        return 0;// system(command.c_str());

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Crashes where? On what line? With what error? Have you single-stepped it with a debugger? Does it get to execute the CreateProcessA function at all or does it crash while evaluating the param.c_str() parameter? If it executes, what return value do you get? Either that function should return an error, or it should execute the program. A crash tends to suggest that you just aren't sending it the correct parameters at all or you have other problems like NULL dereferences etc.

    But without an error message or at least a line number that it crashes on, it's hard to help.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    CreateProcess function

    CreateProcess expects a non-const param as its second parameter as it does try to edit it in some circumstances. Have you tried copying it into a buffer and trying?

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    I guess the called program is expecting argc to be at least 2 and argv[1] to be non-null, the code you have only create an argc of 1 and hence, a NULL argv[1]. Boom.

  5. #5
    Registered User
    Join Date
    Jan 2007
    The program didnt crash in this function, somehow the executable wasn't started which made the system crash.

    The fix was to pass NULL for first parameter of CreateProcess and the full commandline as second. Not really sure why, probable something to do with what adeyblue said.
    CreateProcessA(NULL, (char *)command.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &info, &procInfo);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateProcess() in C
    By sidhuram in forum Windows Programming
    Replies: 4
    Last Post: 09-01-2010, 12:54 AM
  2. Replies: 6
    Last Post: 10-31-2009, 04:10 AM
  3. Replacing system("PAUSE")
    By jk1998 in forum C++ Programming
    Replies: 19
    Last Post: 05-13-2007, 09:07 AM
  4. CreateProcess
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 05-12-2002, 06:45 AM
  5. CreateProcess()
    By Newfie in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2002, 07:31 AM