【问题标题】:Why are the ShellExecuteEx() parameters not working?为什么 ShellExecuteEx() 参数不起作用?
【发布时间】:2015-03-17 09:20:47
【问题描述】:

假设我希望程序“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 的可变参数的信息,因此我找不到对此的正确解释。

有没有人知道如何解决这个问题? 提前致谢!

【问题讨论】:

    标签: c++ mfc shellexecute shellexecuteex


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

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

    问题很可能是这段代码:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多