【问题标题】:GetWindowLong() - Change in behaviour introduced with Creators Update breaks my win32 applicationGetWindowLong() - Creators Update 引入的行为改变破坏了我的 win32 应用程序
【发布时间】:2017-04-22 18:17:35
【问题描述】:

最近对 Windows 10 进行的创作者更新破坏了我使用 Win32 API GetWindowLong() 的应用程序代码。

在 Windows 10 Creator 更新之前,即使进程 B(主线程)被阻止,一个进程(比如进程 A)也能够在另一个进程的窗口句柄(比如进程 B)上调用 GetWindowWord()/GetWindowLong() API在某些系统调用中(例如等待释放互斥锁)。因此,尽管进程 B 被阻塞,进程 A 仍能够使用这些 API 成功查询进程 B 拥有的窗口的保留内存。

但是,在 Windows 10 上应用 Creator 更新后,当进程 B(主线程)被阻塞时,进程 A 在属于进程 B 的窗口上调用这些 API 时会被阻塞。

我通过创建 2 个代表进程 A 和进程 B 的独立 Win32 应用程序来模拟这种情况。在应用了 Creators Update 的 Windows 10 系统上,进程 A 在属于属于的窗口调用 GetWindowLong()/GetWindowWord() 时挂起进程 B (主线程)正在等待互斥锁。换句话说,对 GetWindowLong()/GetWindowWord() 的调用从未返回,从而导致进程 A 挂起。

但是,当我在没有 Creators Update 或更早版本(例如 Windows 7)的 Windows 10 系统上使用独立应用程序测试相同场景时,进程 A 中对 GetWindowLong()/GetWindowWord() API 的调用会返回即使进程 B 正在等待释放互斥体,也会成功。

为了演示上述问题,下面是进程 A 和进程 B 的代码。 要查看问题,运行进程A 和进程B。然后,找出进程B 的窗口的窗口句柄(例如使用Spy++),然后将其粘贴到进程A 的窗口的编辑字段中。然后点击确定。显示一个消息框,显示在进程 B 的窗口的额外内存中设置的 LONG 值(使用 SetWindowLong())。 到现在为止还挺好。 现在,转到进程 B 的窗口并通过单击“阻止”按钮使其挂起。这将使进程“B”(主 GUI 线程)等待永远不会被释放的互斥体,因此进程 B 将挂起。

现在,返回进程 A 的窗口并再次单击“确定”(假设编辑字段仍然具有您之前粘贴的进程 B 的相同窗口句柄)。

现在,这是行为上的不同:

在没有 Creators Update 的 Windows 10 和早期的 Windows 版本(如 Windows 7)上,和以前一样(即当进程 B 没有挂起时),一个消息框显示在进程 B 的窗口的额外内存中设置的 LONG 值(使用 SetWindowLong ()) 显示。

在带有 Creators Update 的 Windows 10 上,进程 A 挂起,因为使用进程 B 的窗口句柄对 SetWindowLong() 的调用永远不会返回使进程 A 挂起。

请建议我如何解决 Windows 10 Creators Update 上的这种行为变化,以免我的应用程序挂起。 任何想法/帮助将不胜感激。

这是进程 A 的代码。

/* Process A */
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>

int count = 0;
int count1 = 0;
TCHAR str[1000];

LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);

HWND g_hwndEdit, g_hwndButton;

#define ID_EDIT (3456)

#define ID_OK (3457)

TCHAR szWinName[] = TEXT("MyWin");

HINSTANCE g_hInst = NULL;

int WINAPI WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,LPSTR lpszArgs,int nWinMode)
{
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wcl;
    g_hInst = hThisInst;
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst;
    wcl.lpszClassName = szWinName;
    wcl.lpfnWndProc = WindowFunc;
    wcl.style = CS_HREDRAW|CS_VREDRAW;
    wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wcl.hIconSm = NULL;
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW);
    wcl.lpszMenuName = NULL;
    wcl.cbClsExtra = 0;
    wcl.cbWndExtra = 44;
    wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    if(!RegisterClassEx(&wcl)) return 0;
    hwnd = CreateWindowEx(
        WS_EX_WINDOWEDGE,
        szWinName,
        "Process A",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        HWND_DESKTOP,
        NULL,
        hThisInst,
        NULL
        );    
    ShowWindow(hwnd,nWinMode);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    LONG l;    
    HWND hwndOther = hwnd;
    char s[] = "Paste the window handle (in HEX) of Process B's window on which you wish to call GetWindowLong() in the edit field and click on OK.";

    HDC hdc;
    PAINTSTRUCT ps;
    static int cxClient = 0, cyClient = 0;

    char btnText[1001];

    switch(message){

    case WM_CREATE:

        g_hwndEdit = CreateWindow ("edit", NULL,
                         WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
                                   WS_BORDER | ES_LEFT,
                         200, 200, 200, 200, hwnd, (HMENU)ID_EDIT,
                         g_hInst, NULL) ;

        g_hwndButton = CreateWindow(                       
                        "Button",
                        "OK",
                        WS_CHILD|WS_VISIBLE,
                        500,
                        200,
                        150,
                        50,
                        hwnd,
                        (HMENU)ID_OK,
                        g_hInst,
                        NULL
                        );

        return 0;

    case WM_SIZE:

        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hwnd, &ps);

        TextOut(hdc, 10, 100, s, strlen(s));

        EndPaint(hwnd, &ps);

        return 0;


    case WM_COMMAND:
        {
            if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_OK)
            {
                GetWindowText(g_hwndEdit, btnText, 1000);
                sscanf(btnText, "%x", &hwndOther);
                l = GetWindowLong(hwndOther, 24);
                sprintf(str, "The LONG value at offset 24 of the window with handle 0x%x is %d.", hwndOther, l);
                MessageBox(hwnd, str, "", 0);
            }
        }

        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        return 0;        
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}

这是进程 B 的代码:

/* Process B */
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>

int count = 0;
int count1 = 0;
TCHAR str[1000];

LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);

TCHAR szWinName[] = TEXT("MyWin");

HINSTANCE g_hInst = NULL;

HANDLE g_hThread, g_hMutex;

HWND g_hwndButton;

#define ID_BUTTON (3456)

//worker thread fn
DWORD WINAPI ThreadFunc(LPVOID p)
{
    g_hMutex = CreateMutex(NULL, TRUE, "HELLO_MUTEX");
    // this worker thread now owns the above created mutex and goes into an infinite loop so that
    // the mutex is never released
    while (1){}
    return 0;
}

// main (GUI) thread
int WINAPI WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,LPSTR lpszArgs,int nWinMode)
{    

    HANDLE hThread;
    DWORD threadld;
    // create a worker thread that will create a mutex and then will go into an infinite loop making sure that the mutex is never released
    // and thus when the main (GUI) thread calls WaitForSingleObject() on this mutex handle, it is going to block forever.
    hThread = CreateThread(NULL,
                            0,
                            ThreadFunc,
                            0,
                            0,
                            &threadld);

    // make the main (GUI) thread sleep for 5 secs so that by the time it wakes up, the worker thread will have created the mutex and gone into an infinite loop
    Sleep(5000);
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wcl;
    g_hInst = hThisInst;
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst;
    wcl.lpszClassName = szWinName;
    wcl.lpfnWndProc = WindowFunc;
    wcl.style = CS_HREDRAW|CS_VREDRAW;
    wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wcl.hIconSm = NULL;
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW);
    wcl.lpszMenuName = NULL;
    wcl.cbClsExtra = 0;
    wcl.cbWndExtra = 44;
    wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    if(!RegisterClassEx(&wcl)) return 0;
    hwnd = CreateWindowEx(
        WS_EX_WINDOWEDGE,
        szWinName,
        "Process B",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        HWND_DESKTOP,
        NULL,
        hThisInst,
        NULL
        );
    SetWindowLong(hwnd, 24, 135678);
    ShowWindow(hwnd,nWinMode);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    char strr[1000];    
    char s[] = "Click on the \"Block\" button below to make the main (GUI) thread block by waiting on a mutex forever since the mutex will never be released.";
    HWND hwndOther = hwnd;

    HDC hdc;
    PAINTSTRUCT ps;
    static int cxClient = 0, cyClient = 0;

    switch(message){

    case WM_CREATE:


        sprintf(strr, "Window created - handle is %x.\n", hwnd);
        OutputDebugString(strr);

        g_hwndButton = CreateWindow(                       
                        "Button",
                        "Block",
                        WS_CHILD|WS_VISIBLE,
                        10,
                        120,
                        50,
                        50,
                        hwnd,
                        (HMENU)ID_BUTTON,
                        g_hInst,
                        NULL
                        );

        return 0;

    case WM_SIZE:

        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hwnd, &ps);

        TextOut(hdc, 10, 100, s, strlen(s));

        EndPaint(hwnd, &ps);

        return 0;

    case WM_COMMAND:
        {
            if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_BUTTON)
            {                
                MessageBox(hwnd, "Main (GUI) Thread going in blocking state by waiting for mutex forever now", "", 0);
                WaitForSingleObject(g_hMutex, INFINITE);              
            }
        }

        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        return 0;        
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}

【问题讨论】:

  • 如果这确实是 CU 中发生的变化,那么您将无能为力。
  • 真正的gui线程一定不能等待而“挂起”。他必须永久运行消息循环。所以解决方案非常简单 - 不等待,不处理消息,在 gui 线程中
  • 感谢 RbMm 为我指明了正确的方向。您的建议暗示使用 MsgWaitForMultipleObjects() 不会使进程 B 中的 GUI 线程挂起,因此在进程 B 的窗口句柄上的进程 A 中生成的 GetWindowLong() 将正常返回而不会挂起。

标签: windows winapi visual-c++


【解决方案1】:

我想我找到了在进程 B 中使用 MsgWaitForMultipleObjects() 的解决方案,这样除了等待互斥体之外,它还会继续在队列中查找消息。 这样一来,在进程A中对进程B的窗口句柄进行的GetWindowLong()调用就会正常返回而不会阻塞,问题就解决了。

这是进程B的更新代码,变化是“阻止”按钮单击处理WM_COMMAND情况(进程A代码保持不变):

#include <windows.h>
#include <stdio.h>
#include <commctrl.h>

int count = 0;
int count1 = 0;
TCHAR str[1000];

LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);

TCHAR szWinName[] = TEXT("MyWin");

HINSTANCE g_hInst = NULL;

HANDLE g_hThread, g_hMutex;

HWND g_hwndButton;

#define ID_BUTTON (3456)

//worker thread fn
DWORD WINAPI ThreadFunc(LPVOID p)
{
    g_hMutex = CreateMutex(NULL, TRUE, "HELLO_MUTEX");
    // this worker thread now owns the above created mutex and goes into an infinite loop so that
    // the mutex is never released
    while (1){}
    return 0;
}

// main (GUI) thread
int WINAPI WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,LPSTR lpszArgs,int nWinMode)
{    

    HANDLE hThread;
    DWORD threadld;
    // create a worker thread that will create a mutex and then will go into an infinite loop making sure that the mutex is never released
    // and thus when the main (GUI) thread calls WaitForSingleObject() on this mutex handle, it is going to block forever.
    hThread = CreateThread(NULL,
                            0,
                            ThreadFunc,
                            0,
                            0,
                            &threadld);

    // make the main (GUI) thread sleep for 5 secs so that by the time it wakes up, the worker thread will have created the mutex and gone into an infinite loop
    Sleep(5000);
    HWND hwnd;
    MSG msg;
    WNDCLASSEX wcl;
    g_hInst = hThisInst;
    wcl.cbSize = sizeof(WNDCLASSEX);
    wcl.hInstance = hThisInst;
    wcl.lpszClassName = szWinName;
    wcl.lpfnWndProc = WindowFunc;
    wcl.style = CS_HREDRAW|CS_VREDRAW;
    wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wcl.hIconSm = NULL;
    wcl.hCursor = LoadCursor(NULL,IDC_ARROW);
    wcl.lpszMenuName = NULL;
    wcl.cbClsExtra = 0;
    wcl.cbWndExtra = 44;
    wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    if(!RegisterClassEx(&wcl)) return 0;
    hwnd = CreateWindowEx(
        WS_EX_WINDOWEDGE,
        szWinName,
        "Process B",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        HWND_DESKTOP,
        NULL,
        hThisInst,
        NULL
        );
    SetWindowLong(hwnd, 24, 135678);
    ShowWindow(hwnd,nWinMode);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

BOOL waitWithMessageLoop(HANDLE hMutex, BOOL &bExit)
{
    BOOL bContinue = TRUE;
    bExit = FALSE;
    while(bContinue)
    {
        DWORD dwReturn = ::MsgWaitForMultipleObjects(1, &hMutex, FALSE, INFINITE, QS_ALLINPUT);
        if(dwReturn == WAIT_OBJECT_0)
        {
            // our mutex got released
            bContinue = FALSE;
        }
        else if(dwReturn == WAIT_OBJECT_0 + 1)
        {
            MSG msg;
            while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                if (msg.message == WM_QUIT)
                {
                    bExit = TRUE;
                    bContinue = FALSE;
                    break;
                }
                ::TranslateMessage(&msg);
                ::DispatchMessage(&msg);
            }
        }
        else
        {
            // MsgWaitForMultipleObjects() returned error
            return FALSE;
        }
    }
    return TRUE;
}


LRESULT CALLBACK WindowFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    char strr[1000];    
    char s[] = "Click on the \"Block\" button below to make the main (GUI) thread block by waiting on a mutex forever since the mutex will never be released.";
    HWND hwndOther = hwnd;

    HDC hdc;
    PAINTSTRUCT ps;
    static int cxClient = 0, cyClient = 0;

    switch(message){

    case WM_CREATE:


        sprintf(strr, "Window created - handle is %x.\n", hwnd);
        OutputDebugString(strr);

        g_hwndButton = CreateWindow(                       
                        "Button",
                        "Block",
                        WS_CHILD|WS_VISIBLE,
                        10,
                        120,
                        50,
                        50,
                        hwnd,
                        (HMENU)ID_BUTTON,
                        g_hInst,
                        NULL
                        );

        return 0;

    case WM_SIZE:

        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);

        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hwnd, &ps);

        TextOut(hdc, 10, 100, s, strlen(s));

        EndPaint(hwnd, &ps);

        return 0;

    case WM_COMMAND:
        {
            if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_BUTTON)
            {                
                MessageBox(hwnd, "Main (GUI) Thread going in blocking state by waiting for mutex forever now", "", 0);
                // disable the "Block" button
                EnableWindow(g_hwndButton, FALSE);
                //WaitForSingleObject(g_hMutex, INFINITE);// do NOT use this as this cause the GetWindowLong() call made in Process A to hang
                BOOL bExit = FALSE;
                waitWithMessageLoop(g_hMutex, bExit);
                if (bExit)
                {
                    PostQuitMessage(0);
                }
            }
        }

        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        return 0;        
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}

谢谢, --阿努拉格。

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2018-12-16
    相关资源
    最近更新 更多