سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

You can check this link.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx there is thoroughly explained about that argument

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top