简体   繁体   English

CreateProcess执行EXE

[英]CreateProcess execute EXE

I have an application where the user uploads a file to the remote server, the same server to receive this file should run this application. 我有一个应用程序,用户在该应用程序中将文件上传到远程服务器,接收该文件的同一服务器应运行该应用程序。 I'm using the CreateProcess method. 我正在使用CreateProcess方法。 The problem is, the file directory is already defined in a std :: string, and I'm having difficulties to pass this directory as a parameter to the CreateProcess. 问题是,文件目录已经在std :: string中定义,并且我很难将此目录作为参数传递给CreateProcess。

How do I that this directory can be passed to the CreateProcess without errors? 如何将这个目录无误地传递到CreateProcess?

    //the client remotely sends the directory where the file will be saved
    socket_setup.SEND_BUFFER("\nRemote directory for upload: ");
    char *dirUP_REMOTE = socket_setup.READ_BUFFER();
    std::string DIRETORIO_UP = dirUP_REMOTE; // variable where it stores the remote directory


        //after uploading this is validation for executing file
if (!strcmp(STRCMP_EXECUTE, EXECUTE_TIME_YES))
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    std::wstring wdirectory;
    int slength = (int)directory.length() + 1;
    int len = MultiByteToWideChar(CP_ACP, 0, directory.c_str(), slength, 0, 0);
    wdirectory.resize(len);
    MultiByteToWideChar(CP_ACP, 0, directory.c_str(), slength, &wdirectory[0], len);
    if (!CreateProcess(NULL, wdirectory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));
}

There are two versions of CreateProcess: CreateProcessA and CreateProcessW (like most similar windows APIs). 有两种版本的CreateProcess:CreateProcessA和CreateProcessW(与大多数类似的Windows API一样)。 The right version is used depending on whether you have Unicode enabled. 根据是否启用了Unicode,使用正确的版本。 Here you need to convert your std::string to a std::wstring first, because that CreateProcess is actually a CreateProcessW. 在这里,您需要先将std :: string转换为std :: wstring,因为该CreateProcess实际上是CreateProcessW。

std::wstring wdirectory;
int slength = (int)directory.length() + 1;
int len = MultiByteToWideChar(CP_ACP, 0, directory.c_str(), slength, 0, 0); 
wdirectory.resize(len);
MultiByteToWideChar(CP_ACP, 0, directory.c_str(), slength, &wdirectory[0], len);
//...
if (!CreateProcess(NULL,(LPWSTR)wdirectory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));

You could also try replacing CreateProcess by a manual call to CreateProcessA and passing the cstring like you tried to do in the question, but then you won't support wide characters : 您也可以尝试通过手动调用CreateProcessA来替换CreateProcess,并像在问题中尝试的那样传递cstring,但是您将不支持宽字符:

if (!CreateProcessA(NULL, directory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM