简体   繁体   English

使用两个进程时,ShellExecute 在 Windows XP 上冻结

[英]ShellExecute freezes on Windows XP when working with two processes

Imagine that you have two processes: one sends commands and working in administrative mode, another reads them and executes them.假设您有两个进程:一个发送命令并在管理模式下工作,另一个读取并执行它们。

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);
            }
        }       
    }
}

From first process I send message with SendMessage for milliseconds.从第一个过程开始,我用 SendMessage 发送消息几毫秒。 Second process spinning in CommandLoop.在 CommandLoop 中旋转的第二个进程。

Then I read this message in second process with ReadMessage for milliseconds.然后我在第二个过程中用 ReadMessage 读取了这条消息,持续了几毫秒。 But when I call ShellExecute function in second process, this process freezes for 30 seconds only on Windows XP.但是当我在第二个进程中调用 ShellExecute function 时,此进程仅在 Windows XP 上冻结 30 秒。 On Windows 7, 8, 10 this code works correctly.在 Windows 7、8、10 上,此代码可以正常工作。 What's could be wrong?有什么问题?

One common issue is virus scanners.一个常见问题是病毒扫描程序。 Windows XP of course is long out of support, so performance on such systems is generally considered a non-issue. Windows XP 当然早就不支持了,所以在这种系统上的性能通常被认为不是问题。 No makes of virus scanners is going to spend time making them faster on XP.没有任何品牌的病毒扫描程序会花时间让它们在 XP 上运行得更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM