CreateProcess的第二个参数应该是什么

What should the second parameter of CreateProcess be?

本文关键字:是什么 参数 第二个 CreateProcess      更新时间:2023-10-16

我正在尝试使用CreateProcess()启动服务器。这是代码:

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;
}

我从文件中不明白第二个参数应该是什么。你能帮我吗?谢谢

来自MSDN:

lpCommandLine[in,out,optional]

要执行的命令行。此字符串的最大长度为32768个字符,包括Unicode终止的null性格如果lpApplicationName为NULL,则的模块名称部分lpCommandLine仅限于MAX_PATH字符。

此函数的Unicode版本CreateProcessW可以修改此字符串的内容。因此,此参数不能是指向只读内存的指针(如常量变量或文字字符串)。如果此参数是一个常量字符串,则函数可能导致访问违规。

lpCommandLine参数可以为NULL在这种情况下,函数使用lpApplicationName指向的字符串作为命令行。

所以NULL至少在那里是可以的。只要你不通过辩论。

您可以使用它将参数传递给由第一个参数定义的.exe:

例如,调用cmd.exe,然后运行脚本或使用zip实用程序:

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);
}