我有一个应用程序,用户将文件上传到远程服务器,接收此文件的同一服务器应运行此应用程序。我使用的是CreateProcess方法。问题是,文件目录已经在std ::string中定义了,我很难将这个目录作为参数传递给CreateProcess。
如何确保将此目录传递给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));
}发布于 2015-01-13 09:46:27
CreateProcess有两个版本: CreateProcessA和CreateProcessW (就像大多数类似的windows APIs一样)。是否使用正确的版本取决于您是否启用了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并传递cstring来替换CreateProcess,就像您在问题中尝试做的那样,但是您将不支持宽字符:
if (!CreateProcessA(NULL, directory.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi));https://stackoverflow.com/questions/27913696
复制相似问题