C ++ CreateProcess cannot get path from socket on Windows 7 (64)

I am trying to create a simple application controller using the CreateProcess () function. The program gets the te path of the program to be executed by the socket and stores it in the char [] variable, and then sends the variable to the function that will execute it.

The error I am getting is

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

      

(2) = ERROR_FILE_NOT_FOUND

I tried with doble slash (//) and I get error (123)

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

      

(123) = ERROR_INVALID_NAME

The function that gets the path to the program execution:

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
}

      

and a function that starts the process:

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 environment block
    NULL,                       // Use parent 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 );
}

      

Can you tell me what is wrong, I suppose it is something like variables, but I cannot find the error.

Thanks in advance.

+3


source to share


1 answer


The problem is here:

LPWSTR(recvbuf)

      

You have specified the buffer as a pointer to a wide character array, but it is not. We can say this because just before you wrote:

printf("\nClient: Received data is: %s\n", recvbuf);

      



This means it recvbuf

is a pointer to an 8-bit ANSI character array. Either use CreateProcessA

or convert from ANSI to UTF-16.

The lesson you should take away is that every time you cast an array of characters, you are most likely wrong. The compiler supposedly objected to the transfer recvbuf

because it correctly identified that it recvbuf

was in the wrong format. Casting you just suppress the compiler and lie to it. Your order does not recvbuf

be LPWSTR

. It is still LPSTR

, but you told the compiler to ignore this error.

You need to be sure it recvbuf

is null terminated. If the transfer fails and is recvbuf

not null terminated, then you have a buffer overflow condition.

Finally, backslash escaping is something you only do in your source code.

+6


source







All Articles