首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CreateProcess执行EXE

CreateProcess执行EXE
EN

Stack Overflow用户
提问于 2015-01-13 09:35:22
回答 1查看 1.2K关注 0票数 0

我有一个应用程序,用户将文件上传到远程服务器,接收此文件的同一服务器应运行此应用程序。我使用的是CreateProcess方法。问题是,文件目录已经在std ::string中定义了,我很难将这个目录作为参数传递给CreateProcess。

如何确保将此目录传递给CreateProcess时不会出现错误?

代码语言:javascript
复制
    //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));
}
EN

回答 1

Stack Overflow用户

发布于 2015-01-13 09:46:27

CreateProcess有两个版本: CreateProcessA和CreateProcessW (就像大多数类似的windows APIs一样)。是否使用正确的版本取决于您是否启用了Unicode。这里,您需要首先将std::string转换为std::wstring,因为CreateProcess实际上是一个CreateProcessW。

代码语言:javascript
复制
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));

您也可以尝试用手动调用CreateProcessA并传递cstring来替换CreateProcess,就像您在问题中尝试做的那样,但是您将不支持宽字符:

代码语言:javascript
复制
if (!CreateProcessA(NULL, directory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27913696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档