【发布时间】:2014-09-04 08:29:33
【问题描述】:
我正在尝试使用 CreateProcess() 函数制作一个简单的应用程序控制器。程序接收到将由套接字执行的程序的路径并将其存储到 char[] 变量中,然后将变量发送给将执行它的函数。
我得到的错误是
Client: Received data is: C:\Windows\System32\calc.exe
Server: Bytes received: 30.
CreateProcess failed (123).
(2) = ERROR_FILE_NOT_FOUND
我尝试使用双斜杠 (//),但收到错误 (123)
Client: Received data is: C:\\Windows\\System32\\calc.exe
Server: Bytes received: 33.
CreateProcess failed (123).
(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
}
以及启动进程的函数:
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 );
}
你能告诉我哪里出了问题,我想是关于变量类型的问题,但我找不到错误。
提前致谢。
【问题讨论】:
-
在调用 CreateProcess() 之前调试 'cmd' 参数的输出,它可能会被破坏
标签: c++ windows sockets createprocess