CreateProcess running EXE

2

I have an application, where the user uploads a file to the remote server, this same server when receiving this file should run this application. I'm using the CreateProcess method. The problem is, the file directory is already defined in a std :: string, and I'm having trouble passing this directory as a parameter to CreateProcess.

How do I proceed so that this directory can be passed to the CreateProcess without errors?

EDIT: Now the code compiles, but the file does not execute ...

//o cliente envia remotamente o diretorio onde sera salvo o arquivo
socket_setup.SEND_BUFFER("\nDiretorio remoto para upload: ");
char *dirUP_REMOTE = socket_setup.READ_BUFFER();
std::string DIRETORIO_UP = dirUP_REMOTE; // variavel onde se armazena o diretorio remoto


    //depois do upload essa é a validação para execução do arquivo
if (!strcmp(STRCMP_EXECUTE, EXECUTE_TIME_YES))
{
    std::wstring temp(directory.begin(), directory.end());

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    CreateProcess(NULL, (LPWSTR)temp.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
    
asked by anonymous 13.01.2015 / 02:27

2 answers

4

The CreateProcess signature is:

BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);

Notice that LPTSTR is a pointer to a string of WCHAR (16 bits per character), but std :: string uses char (8 bits per character). So one solution is to use std :: wstring or convert your std :: string to LPTSTR .

If directory is std :: wstring, you can use directory.c_str() itself.

Or you can convert your std :: string to std :: wstring and then use it, for example:

std::string str = "c:\Windows\System32\calc.exe"; // EXEMPLO
std::wstring temp(str.begin(), str.end());

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CreateProcess(NULL, (LPWSTR) temp.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

Despite this, , I think it would be advisable to use std :: wstring soon in your case, because it seems your compiler is configured to use the Windows API with UNICODE.

    
13.01.2015 / 02:37
0

Friend, the answer above is correct . make sure that you are doing the validations before CreateProcess . You are uploading to a remote server, make sure the file was properly handled upon reaching the server before you attempt to run it. Also check that it has been closed "fclose (file)" before creating the process if it will not be impossible to execute.

    
13.01.2015 / 18:34