创建进程执行 EXE
CreateProcess execute EXE
我有一个应用程序,其中用户将文件上传到远程服务器,接收此文件的同一服务器应运行此应用程序。我正在使用创建过程方法。问题是,文件目录已经在 std :: 字符串中定义,我很难将此目录作为参数传递给 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));
}
CreateProcess有两个版本:CreateProcessA和CreateProcessW(就像大多数类似的Windows API一样)。根据您是否启用了 Unicode,使用正确的版本。在这里,您需要先将 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));
您也可以尝试通过手动调用 CreateProcessA 来替换 CreateProcess,并像您在问题中尝试的那样传递字符串,但这样您将不支持宽字符:
if (!CreateProcessA(NULL, directory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));
相关文章:
- 执行用 C++ 编写的.exe(使用 mingw 编译器)时出现问题
- cmd.exe与Powershell中C++程序的不同执行时间
- 在 Linux 上没有 /proc/self/exe 的 C/C++ 运行时可执行文件的大小?
- 无法获取进程 ID 4 (ntoskrnl.exe) 的可执行路径
- 无法使用QProcess在cmd.exe中执行复制命令
- 如何在 windowsservercore Docker 映像上安装执行本机C++ DLL/EXE 所需的所有依赖二进制
- 如何在 Azure 应用服务中使用 cl.exe 编译 C++ 代码并生成可执行文件
- 为什么删除执行组件指针会导致"Program.exe has triggered a breakpoint"
- 如何使jse文件执行编码.exe并静默添加启动?Javascript将文件添加到启动?
- 从 Qt(release) 生成的文件执行.EXE文件时出错
- 程序'main.exe'运行失败:指定的可执行文件不是此操作系统平台的有效应用程序
- 在 Windows 命令行中运行.exe在使用 2 种相同方式执行时会产生不同的输出
- 执行AppData的外部EXE
- C ShellexeCuteex不会通过未从该EXE产生输出来执行参数
- 我的应用程序是从Visual Studio内部运行还是通过执行EXE文件运行
- 线程执行在IDE中和C 中的EXE应用程序中是否有所不同
- 基于 Visual Studio c++ 的 exe 不执行任何操作
- 无法与exe一起执行简单的Shell Script/Commonads
- 阻止基于md5哈希执行特定的.exe文件
- Java运行的exe执行不正确