-2

我正在使用 CreateProcessW 函数来创建一个进程。在这个函数中,传递第一个参数,即 lpApplicationName 和一些空格,如下所示:

在下面的路径(pszExePath)中有空格,因为这个过程没有创建。

pszExePath = L"C:\\Program Files\\Common Files\\Installer\\emrinst.exe";

我尝试使用以下几行来修剪空间,但我仍然面临这个问题。

pszExePath = L"\"";
pszExePath += L"C:\\Program Files\\Common Files\\Installer\\emrinst.exe";
pszExePath += L"\"";

如何从 CreateProcessW 函数的 lpApplicationName 中修剪空间?

以下是更新后的代码:

pszExePath = L"C:\\Program Files\\Common Files\\Installer\\emrinst.exe";
strCommandLine = "C:\\Testfolder\\Program Files\\Common Files\\Apps\\emarmain\\emr-test_folder\\Millinnium Files\\test\\test.inf";

std::wstring strFullPath = L"";
strFullPath += pszExePath;
strFullPath += pszCmdLine;


dwExitCode = ::CreateProcessW(NULL, (LPWSTR)strFullPath.c_str(),
            0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0,
            pszCurrentDirectory, &si, &pi);

我仍然收到错误,我认为它超过了第二个参数“lpCommandLine”的 32,768 个字符的大小。有什么办法可以增加尺寸吗?而且我的代码片段是否正确?

4

2 回答 2

3

正如您在文档中毫无疑问地阅读的那样:

如果您使用包含空格的长文件名,请使用带引号的字符串来指示文件名的结束位置和参数的开始位置;否则,文件名不明确。

所以你的那部分是正确的,但你不能像这样连接 C 字符串。相反,您可以这样做:

#include <string>

std::wstring cmdline = L"\"";
cmdline += L"C:\\Program Files\\Common Files\\Installer\\emrinst.exe";
cmdline += L"\"";

CreateProcess (NULL, &cmdline [0], ...);

或者,您可以将应用程序路径作为CreateProcess不带引号的第一个参数传递。

于 2019-10-12T18:19:53.747 回答
-1

遵循CreateProcess (Unicode) 的 MSDN 文档和问题中的代码:

1)在EXE路径和命令行中添加双引号。

pszExePath2) 在和之间添加空格strCommandLine

3) 注意:此解决方案需要 C++11,因为CreateProcess需要非只读字符串作为lpCommandLine参数。对于 C++ 的早期版本,使用_wcsdup复制字符串并调用free释放它。

    std::wstring pszExePath     = L"C:\\Program Files\\Common Files\\Installer\\emrinst.exe";
    std::wstring strCommandLine = L"C:\\Testfolder\\Program Files\\Common Files\\Apps\\emarmain\\emr-test_folder\\Millinnium Files\\test\\test.inf";

    std::wstring strFullPath = L"\"" + pszExePath + L"\"" + L" " + L"\"" + strCommandLine + L"\"";

    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof si;      

    BOOL result = ::CreateProcessW(NULL, &strFullPath[0], 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, pszCurrentDirectory, &si, &pi);    

    if (result)
    {
       CloseHandle(pi.hThread);
       CloseHandle(pi.hProcess);
    }

编辑: 或者,您可以尝试ShellExecuteW(注意两个路径中的双引号)。

std::wstring pszExePath = L"\"C:\\Program Files\\Common Files\\Installer\\emrinst.exe\"";
std::wstring strCommandLine = L"\"C:\\Testfolder\\Program Files\\Common Files\\Apps\\emarmain\\emr-test_folder\\Millinnium Files\\test\\test.inf\"";

ShellExecute(0, L"open", pszExePath.c_str(), strCommandLine.c_str(), pszCurrentDirectory, SW_NORMAL);
于 2019-10-13T17:13:07.757 回答