由于一些程序是对话框的模式,且有些没有处理隐藏消息,这时候用ShellExecute,或者ShellExecuteEx隐藏方式运行第三方程序,会是失败的。此时使用下面方法,则可以:
BOOL Run(const wchar_t *pszApp)
{
if (NULL == pszApp)
{
return FALSE;
}
TCHAR szDesktop[256] = L"Desktop";
HDESK hDesk = CreateDesktop(szDesktop, NULL, NULL, 0, GENERIC_ALL|DESKTOP_CREATEWINDOW, NULL);
if(hDesk)
{
STARTUPINFO si = {sizeof(si)};
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.lpDesktop = szDesktop;
PROCESS_INFORMATION pi = {0};
TCHAR szTempApp[256] = {0};
wcsncpy(szTempApp, pszApp, sizeof(szTempApp) / sizeof(TCHAR) - 1);
if (CreateProcess(NULL, szTempApp, NULL, NULL, FALSE, 0, NULL,NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
CloseDesktop(hDesk);
return TRUE;
}
return FALSE;
}