CreateProcess doesn't pass command line arguments

前端 未结 8 2178
梦谈多话
梦谈多话 2020-12-01 10:20

Hello I have the following code but it isn\'t working as expected, can\'t figure out what the problem is.

Basically, I\'m executing a process (a .NET process) and pa

8条回答
  •  不知归路
    2020-12-01 11:10

    If the first parameter to CreateProcess() is non-NULL, it will use that to locate the image to launch.

    If it is NULL, it will parser the 2nd argument to try to get the executable to launch from the 1st token.

    In either case, the C runtime will use the second argument to populate the argv array. So the first token from that parameter shows up in argv[0].

    You probably want something like the following (I've change the smtp.exe program to echoargs.exe - a simple utility I have to help figure out just this kind of issue):

    int main(int argc, char* argv[])
    {
        PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
    
        STARTUPINFO StartupInfo; //This is an [in] parameter
        char cmdArgs[] = "echoargs.exe name@example.com";
    
        ZeroMemory(&StartupInfo, sizeof(StartupInfo));
        StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
    
    
        if(CreateProcess("C:\\util\\echoargs.exe", cmdArgs, 
            NULL,NULL,FALSE,0,NULL,
            NULL,&StartupInfo,&ProcessInfo))
        { 
            WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
            CloseHandle(ProcessInfo.hThread);
            CloseHandle(ProcessInfo.hProcess);
    
            printf("Yohoo!");
        }  
        else
        {
            printf("The process could not be started...");
        }
    
        return 0;
    }
    

    Here's the output I get from that program:

    echoargs.exe name@example.com
    [0]: echoargs.exe
    [1]: name@example.com
    
    Yohoo!
    

提交回复
热议问题