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

时间:2015-03-17 09:20:47

标签: c++ mfc shellexecute shellexecuteex

假设我想要该程序" 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;
}

由于使用ShellExecuteEx在互联网上没有关于变量参数的大量信息,我找不到合适的解释。

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您是否检查了MyProgram收到的两个参数,它们具有哪些值?

问题很可能是这段代码:

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

您没有说明dirfile[i]有哪些类型,但一般情况下添加C样式字符串(TCHAR[]TCHAR*仍为C样式字符串)不会连接它们,如果这是你期望在这种情况下发生的事情。

通过显示该分配,或者最好使用调试器,检查分配后ShExecInfo.lpParameters包含的内容。

相关问题