我们有这个应用程序,它使用 mshtml.dll 和 rundll.32.exe 来打印特定数据。在 Windows 10 中,如果我选择“Microsoft Print to PDF”或“Microsoft XPS Document Writer”作为打印机,我的应用程序将挂起。当我通过任务管理器结束任务时,才会出现“将打印输出另存为”对话框。在实际的打印机中,没有问题。
这是它的代码片段。
parameter.Format(_T("%s\\mshtml.dll,PrintHTML \"%s\""), systemPath, strHtmlFileName);
SHELLEXECUTEINFO shPrintInfo = {0};
shPrintInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shPrintInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shPrintInfo.hwnd = hWnd;
shPrintInfo.lpVerb = NULL;
shPrintInfo.lpFile = _T("rundll32.exe");
shPrintInfo.lpParameters = parameter;
shPrintInfo.lpDirectory = NULL;
shPrintInfo.nShow = SW_NORMAL;
shPrintInfo.hInstApp = NULL;
ShellExecuteEx(&shPrintInfo);
if (shPrintInfo.hProcess != NULL)
{
::WaitForSingleObject(shPrintInfo.hProcess, INFINITE);
::CloseHandle(shPrintInfo.hProcess);
}
我已经隔离了应用程序挂起的代码,它位于 WaitForSingleObject() 调用中。它永远不会结束,它阻止了“将打印输出另存为”对话框的显示。如果我将 INFINITE 更改为 10000(10 秒),则会显示“将打印输出另存为”对话框,但如果我在实际打印机上测试打印输出并等待 10 秒时间,则不会打印任何数据,因为句柄或进程已关闭通过 CloseHandle() 调用。
希望你能帮助我。
按照 Jonathan Potter 的建议查看 MsgWaitForMultipleObjectsEx ,我通过关注此线程找到了解决方案 了解 MsgWaitForMultipleObjects