Question

Following the tutorial here, I decided to make a process class for C++ so that I did not have to constantly keep writing out the same code for starting a process. It does work starting the process, but when I pass a command line function, it does nothing. Example ("c:\\windows\\notepad.exe", "c:\\windows\\PFRO.txt"). What is the problem?

Note: format is just a basic formatting function, using vsprintf

class process
{
public:
    static BOOL __stdcall start(LPCSTR _Proc_name, LPSTR _Command_line = NULL, LPSECURITY_ATTRIBUTES _Proc_attrib = NULL,
        LPSECURITY_ATTRIBUTES _Thread_attrib = NULL, BOOL _Inherits_handles = FALSE, DWORD _Creation_flags = NULL,
        LPVOID _Environment = NULL, LPCSTR _Cur_directory = NULL)
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        ZeroMemory(&pi, sizeof(pi));
        if (!CreateProcess(_Proc_name, _Command_line, _Proc_attrib, _Thread_attrib, 
            _Inherits_handles, _Creation_flags, _Environment, _Cur_directory, &si, &pi))
        {
            fputs(format("process::start(...) failed [%d]\n", GetLastError()), stderr);
            return false;
        }
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        return true;
    }
};

int main()
{
    process::start("c:\\windows\\notepad.exe", "c:\\windows\\PFRO.txt");
    getchar();
}
Was it helpful?

Solution

When the command line parameter is parsed to provide arguments for a main function, the first token is taken to be the executable file. A called program might well try to open the second token as its file argument, and of course you there wasn't one.

The usual practice is to repeat the program name as the first token in the command line. For example

process::start("c:\\windows\\notepad.exe", "notepad c:\\windows\\PFRO.txt");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top