[英]ShellExecute freezes on Windows XP when working with two processes
假設您有兩個進程:一個發送命令並在管理模式下工作,另一個讀取並執行它們。
bool SendMessage(const std::wstring& message, HANDLE pipe)
{
DWORD written = 0;
int bytesToSend = (message.size() + 1) * sizeof(wchar_t);
WriteFile(pipe, message.c_str(), bytesToSend, &written, nullptr); // WinAPI
return written == bytesToSend;
}
std::wstring ReadMessage(HANDLE pipe)
{
std::wstring message;
wchar_t wch;
DWORD bytesRead = 0;
while (true)
{
if (!ReadFile(pipe, &wch, sizeof(wch), &bytesRead, NULL) || !(bytesRead == sizeof(wch))) // WinAPI
{
message.clear();
break;
}
if (wch)
{
message += wch;
}
else
{
break;
}
}
return message;
}
void CommandLoop(HANDLE pipe)
{
DWORD dummy = 0;
WriteFile(pipe, "", 1, &dummy, NULL); //pass the token
while (true)
{
std::wstring command = ReadMessage(pipe);
if (command.empty())
break;
std::wstring file;
std::wstring params;
// Some handling here
// ...
// You are here
if (ParseCommand(command, file, params))
{
INT_PTR shellResult = reinterpret_cast<INT_PTR>(::ShellExecute(nullptr, L"open", file.c_str(), params.c_str(), nullptr, SW_SHOW)); /// freeze here
if (shellResult > HINSTANCE_ERROR)
{
SendMessage(L"ok", pipe);
}
else
{
SendMessage(std::to_wstring(shellResult), pipe);
}
}
}
}
從第一個過程開始,我用 SendMessage 發送消息幾毫秒。 在 CommandLoop 中旋轉的第二個進程。
然后我在第二個過程中用 ReadMessage 讀取了這條消息,持續了幾毫秒。 但是當我在第二個進程中調用 ShellExecute function 時,此進程僅在 Windows XP 上凍結 30 秒。 在 Windows 7、8、10 上,此代碼可以正常工作。 有什么問題?
一個常見問題是病毒掃描程序。 Windows XP 當然早就不支持了,所以在這種系統上的性能通常被認為不是問題。 沒有任何品牌的病毒掃描程序會花時間讓它們在 XP 上運行得更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.