ShellExecute freezes on Windows XP when working with two processes

Viewed 38

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. Second process spinning in CommandLoop.

Then I read this message in second process with ReadMessage for milliseconds. But when I call ShellExecute function in second process, this process freezes for 30 seconds only on Windows XP. On Windows 7, 8, 10 this code works correctly. What's could be wrong?

1 Answers

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. No makes of virus scanners is going to spend time making them faster on XP.

Related