为什么 ShellExecuteEx() 参数不起作用?

Why are the ShellExecuteEx() parameters not working?

假设我希望程序 "C:\MyProgram.exe" 到 运行 的参数是两个变量。出现的问题是MyProgram只接收2个参数,而我明明传了3个参数

我的代码:

SHELLEXECUTEINFO    ShExecInfo = { 0 };
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;

ShExecInfo.lpFile = T("\"C:\MyProgram.exe\"");
ShExecInfo.lpParameters = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, 1500);
if(GetExitCodeProcess(ShExecInfo.hProcess, &exitCode)){
    MessageBox(_T("Conversion ") + file[i] + _T(" unsuccesful."), _T("TEST!"), MB_OK);
    succes = 0;
}

因为 Internet 上关于 ShellExecuteEx 的可变参数的信息不多,所以我找不到合适的解释。

你们中有人知道如何解决这个问题吗? 提前致谢!

你有没有检查MyProgram接收到的那两个参数,它们有什么值?

问题很有可能是这段代码:

ShExecInfo.lpParameters = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");

你没有说 dirfile[i] 有什么类型,但通常像这样添加 C 风格的字符串(TCHAR[]TCHAR* 仍然是 C 风格strings) 不会连接它们,如果这是您期望在这种情况下发生的情况。

检查ShExecInfo.lpParameters分配后包含的内容,方法是显示它或最好使用调试器。

仅仅是因为您的构造产生了一个临时对象并存储了指向它的指针(我猜是一个 CString),但是当您启动程序时该临时对象已经被销毁。

auto str = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");
ShExecInfo.lpParameters = str;
ShellExecuteEx(&ShExecInfo);