What should the second parameter of CreateProcess be?

拟墨画扇 提交于 2020-01-05 08:51:24

问题


I am trying to start a server using CreateProcess(). Here is the Code:

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // TODO: Place code here.

    int result; 
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    CreateProcess("C:\\AP\\DatabaseBase\\dbntsrv.exe", "*** WHAT SHOULD I PUT HERE***", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    return 0;
}

I did not understand from the documentation what the 2nd parameter should be. Can you please help me with it? Thank You


回答1:


From MSDN:

lpCommandLine [in, out, optional]

The command line to be executed. The maximum length of this string is 32,768 characters, including the Unicode terminating null character. If lpApplicationName is NULL, the module name portion of lpCommandLine is limited to MAX_PATH characters.

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.

So NULL is OK there, at least. As soon as you don't pass arguments.




回答2:


You use it to pass arguments to the .exe defined by the first parameter:

An example would be to call the cmd.exe and then run a script or use a zip utility:

void runCmd(const tstring& cmdString, STARTUPINFO &si, PROCESS_INFORMATION &pi)
{
    ZeroMemory( &si, sizeof(si) );
    ZeroMemory( &pi, sizeof(pi) );
    si.cb = sizeof(si);

    tstring cmd_exe_path(win_dir);
    cmd_exe_path.append( _T("\\System32\\") ).append(CMD_PROCESS);

    tstring argline( _T("/c ") );
    argline += cmdString;

    tstring curr_dir( cmdString.substr( 0, cmdString.rfind( _T('.') ) ) );
    curr_dir.erase( curr_dir.find_last_of( _T("/\\") ) );
    size_t pos = curr_dir.find( _T("\"") );
    while(  pos != tstring::npos )
    {
        curr_dir.erase( pos, pos + 1 );
        pos = curr_dir.find( _T("\"") );
    }

    //USE FULL PATHS FOR SAFETY... Include wrapping quotes if spaces required in path
    LOG(LOG_INFO,_T("runCmd(): Calling %s %s Dir[ %s ]"),cmd_exe_path.c_str(),argline.c_str(), curr_dir.c_str());

    if( !CreateProcess( cmd_exe_path.c_str(), &argline[0], NULL, NULL, FALSE, CREATE_NEW_CONSOLE, 
                        NULL,curr_dir.c_str(),&si,&pi ) ) //this generates warning C6335 - resource leak... however handles should be closed by owner
    {
        DWORD dw = GetLastError(); 
        std::string error( "runCmd(): Failed to create Shutdown process - error code is " );
        error.append(boost::lexical_cast<std::string>(dw));
        LOG(LOG_INFO,error.c_str());
        throw std::exception(error.c_str());
    }

    LOG(LOG_INFO,"runCmd(): process starting with PID[%d] TID[%d]",pi.dwProcessId,pi.dwThreadId);
}


来源:https://stackoverflow.com/questions/9427008/what-should-the-second-parameter-of-createprocess-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!