Createprocess running EXE

Asked

Viewed 197 times

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 set in an Std::string , and I’m having difficulty passing this directory as a parameter to Createprocess.

How should I proceed so that this directory can be passed to Createprocess without errors?

EDITING: Now the code compiles, but the file is not executed...

//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);
}
  • What difficulties you’re having?

  • if (!Createprocess(NULL,/directory.c_str()/, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)); in this argument passage for the create process /directory.c_str()

  • Yes, but what happens? Build or run error?

2 answers

2


The signature of Createprocess 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 a solution is to use Std::wstring or convert your Std::string to LPTSTR.


If directory for Std::wstring, you can use the directory.c_str() even.

Or you can convert your Std::string to Std::wstring and then use it, 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 logo in your case, as it seems your compiler is configured to use the Windows API with UNICODE.

  • how I do the conversion from Std::string to LPSTR?

  • found this method LPSTR s = const_cast<char *>(directory.c_str()); but now the Createprocess signature changes

  • I edited the answer. Take a look there.

  • i tried using Std::wstring temp(directory.Begin(), directory.end(); if (!Createprocess(NULL, temp, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));

  • You have to use temp.c_str() in the function.

  • I am having the following error: argument of type "const wchar_t *" is incompatible with Parameter of type "LPWSTR"

  • Now compile, but the file does not run. : (

Show 2 more comments

0

Friend, the answer above is correct. make sure you’re doing the right ones validations before the Createprocess. You’re making upload to a remote server, make sure that the file was duly treated when arriving at the server before you try to run it. Also check if it has been closed "fclose(file)" before the creation of process, if its execution will not be impossible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.