C++ CreateProcess无法从Windows 7上的套接字接收路径(64)

Ika*_*ary 3 c++ sockets windows createprocess

我正在尝试使用CreateProcess()函数创建一个简单的应用程序控制器.程序接收程序的te路径,该路径将由套接字执行并将其存储到char []变量中,稍后它将变量发送给将执行它的函数.

我得到的错误是

Client: Received data is: C:\Windows\System32\calc.exe
Server: Bytes received: 30.
CreateProcess failed (123).
Run Code Online (Sandbox Code Playgroud)

(2)= ERROR_FILE_NOT_FOUND

我尝试使用doble slash(//)并收到错误(123)

Client: Received data is: C:\\Windows\\System32\\calc.exe
Server: Bytes received: 33.
CreateProcess failed (123).
Run Code Online (Sandbox Code Playgroud)

(123)= ERROR_INVALID_NAME

接收程序执行路径的函数:

bytesRecv = recv(m_socket, recvbuf, 200, 0);

if (bytesRecv == SOCKET_ERROR)
   printf("Server: recv() error %ld.\n", WSAGetLastError());
else
{
   printf("\nClient: Received data is: %s\n", recvbuf);
   printf("Server: Bytes received: %ld.\n", bytesRecv );
   NewProcess(1,LPWSTR(recvbuf)); // <---- Call to NewProcess function with path
}
Run Code Online (Sandbox Code Playgroud)

和启动过程的功能:

void NewProcess(int count,LPWSTR cmd)
{
    LPTSTR concatenation = _T(" ");
    LPTSTR cmdArgs = NULL;


    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    si.wShowWindow = SW_HIDE;
    si.dwFlags = STARTF_USESHOWWINDOW;

    // Start the child process. 

    if( !CreateProcess( NULL,       // Program full path
    cmd,                    // Arguments
    NULL,                       // Process handle not inheritable
    NULL,                       // Thread handle not inheritable
    FALSE,                      // Set handle inheritance to FALSE
    0,                          // No creation flags
    NULL,                       // Use parent's environment block
    NULL,                       // Use parent's starting directory 
    &si,                        // Pointer to STARTUPINFO structure
    &pi )                       // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.

    WaitForSingleObject( pi.hProcess, INFINITE );
    printf("\nProcess ID: %d Terminated!",pi.dwProcessId);

    // Close process and thread handles.

    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我什么是错的,我认为是变量类型的东西,但我找不到错误.

提前致谢.

Dav*_*nan 6

问题出在这里:

LPWSTR(recvbuf)
Run Code Online (Sandbox Code Playgroud)

您已将缓冲区转换为指向宽字符数组的指针,但事实并非如此.我们可以告诉你,因为在你写之前:

printf("\nClient: Received data is: %s\n", recvbuf);
Run Code Online (Sandbox Code Playgroud)

这意味着它recvbuf是一个指向8位ANSI字符数组的指针.使用CreateProcessA或从ANSI转换为UTF-16.

你应该从中汲取的教训是,每次施放角色阵列时,你很可能会犯错.编译器可能会反对你通过,recvbuf因为它正确地确定recvbuf了格式错误.通过强制转换,你只是简单地压缩编译器并对其撒谎.你的演员不是recvbuf一个LPWSTR.它仍然存在LPSTR,但您告诉编译器忽略该错误.

您需要确保以recvbufnull结尾.如果传输失败,并且recvbuf未终止空值,则表明存在缓冲区溢出情况.

最后,转义反斜杠只是在源代码中执行的操作.