Win32 CreateWindow() 调用在子线程中挂起?

发布于 2024-08-11 21:49:50 字数 234 浏览 2 评论 0原文

我正在为 OpenGL 开发一个可移植层(为 Linux 和 Windows 抽象出 glX 和 wgl 的东西)...无论如何,它有一个创建窗口的方法...如果你不传入父级,你会得到一个带有框架的真实窗口...如果你传入一个父窗口,你会得到一个无边框、无框架的窗口...

这工作正常,只要我在 1 个线程上完成这一切...一旦另一个线程尝试创建子窗口时,应用程序在 win32 调用“CreateWindow()”时死锁。有什么想法吗?

I'm working on a portability layer for OpenGL (abstracts the glX and wgl stuff for Linux and Windows)... Anyhow, it has a method for creating a Window... If you don't pass in a parent, you get a real window with a frame... If you pass in a parent, you get a borderless, frameless window...

This works fine, as long as I do it all on 1 thread... As soon as another thread tries to create a child window, the app deadlocks in the win32 call "CreateWindow()". Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绻影浮沉 2024-08-18 21:49:50

这不是一个真正的答案,但由于很多人似乎认为 Win32 禁止在父线程之外的其他线程中创建子线程,所以我觉得有必要发布一个相反的演示。

下面的代码演示了在属于不同进程的父级上创建子窗口。它接受窗口句柄值作为命令行参数,并在该父窗口上创建子窗口。

// t.cpp

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

#define CLASS_NAME L"fykshfksdafhafgsakr452"


static LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch ( msg )
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(hwnd, &ps);
            EndPaint(hwnd, &ps);
            break;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}



int main( int argc, char* argv[] )
{
    HWND parent = (argc >= 2) ? (HWND)strtoul(argv[1], 0, 0) : (HWND)0;
    printf("parent: 0x%x\n", parent);

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = (HINSTANCE)GetModuleHandle(NULL);
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = (HBRUSH)(COLOR_ACTIVECAPTION + 1);
    if ( !RegisterClass(&wc) )
    {
        printf("%d: error %d\n", __LINE__, GetLastError());
        return 0;
    }

    const DWORD style = WS_CHILD | WS_VISIBLE;

    HWND hwnd = CreateWindow(CLASS_NAME, L"Test", style, 50, 50, 100, 100,
                             parent, 0, wc.hInstance, 0);

    if ( !hwnd )
    {
        printf("%d: error %d\n", __LINE__, GetLastError());
        return 0;
    }

    MSG msg;
    while ( GetMessage(&msg, 0, 0, 0) )
        DispatchMessage(&msg);

    return 0;
}

使用以下命令进行编译(使用 MSVC 命令行环境):

cl /EHsc /DUNICODE /D_UNICODE t.cpp user32.lib

然后使用 Spy++ 或其他一些工具来获取任何窗口的句柄值——例如记事本或您正在查看此站点的浏览器的句柄值。假设它是 0x00001234。然后使用 t.exe 0x1234 运行已编译的示例。使用 Ctrl-C 终止 t.exe(或仅关闭控制台窗口)。

This is not a real answer, but since so many people seem to believe that Win32 forbids creating children in other threads than the parent, I feel obliged to post a demonstration to the contrary.

The code below demonstrates creation of a child window on a parent belonging to a different process. It accepts a window handle value as a command-line parameter and creates a child window on that parent.

// t.cpp

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

#define CLASS_NAME L"fykshfksdafhafgsakr452"


static LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch ( msg )
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(hwnd, &ps);
            EndPaint(hwnd, &ps);
            break;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}



int main( int argc, char* argv[] )
{
    HWND parent = (argc >= 2) ? (HWND)strtoul(argv[1], 0, 0) : (HWND)0;
    printf("parent: 0x%x\n", parent);

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = (HINSTANCE)GetModuleHandle(NULL);
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = (HBRUSH)(COLOR_ACTIVECAPTION + 1);
    if ( !RegisterClass(&wc) )
    {
        printf("%d: error %d\n", __LINE__, GetLastError());
        return 0;
    }

    const DWORD style = WS_CHILD | WS_VISIBLE;

    HWND hwnd = CreateWindow(CLASS_NAME, L"Test", style, 50, 50, 100, 100,
                             parent, 0, wc.hInstance, 0);

    if ( !hwnd )
    {
        printf("%d: error %d\n", __LINE__, GetLastError());
        return 0;
    }

    MSG msg;
    while ( GetMessage(&msg, 0, 0, 0) )
        DispatchMessage(&msg);

    return 0;
}

Compile this with the following command (using MSVC command line environment):

cl /EHsc /DUNICODE /D_UNICODE t.cpp user32.lib

Then use Spy++ or some other tool to obtain handle value of any window -- e.g. of Notepad or the browser you're viewing this site in. Let's assume it's 0x00001234. Then run the compiled sample with t.exe 0x1234. Use Ctrl-C to terminate t.exe (or just close the console window).

面犯桃花 2024-08-18 21:49:50

当子窗口创建后,它可以通过SendMessage与父窗口交互。但是,请注意,与 PostMessage 不同,跨线程边界的 SendMessage 会阻塞线程。如果父窗口的线程正在等待子线程,而子线程试图创建一个父窗口在该线程中的窗口,那么就会出现死锁。

一般来说,我认为跨线程建立子父关系不是一个好主意。它很容易造成僵局。

When a child window is created, it can interact with the parent window via SendMessage. But, note that SendMessage across thread boundary blocks threads unlike PostMessage. If the parent window's thread is waiting for the child thread, and the child thread is trying to create a window whose parent is in that thread, then it's a deadlock.

In general, I don't think it's a good idea to make child-parent relationship across the threads. It can very easily make deadlock.

神经暖 2024-08-18 21:49:50

这里有很多回复说你不能尝试在不同的线程上拥有子窗口和父窗口,并且强调它不会工作。

如果是这样,Windows 将采取一些保护措施,并且当您尝试调用 CreateWindow 时就会失败。
现在,肯定存在可能导致重大问题的线程耦合问题,但是,考虑到这些限制,这是受支持的场景。

There are a lot of replies here saying that you MUST NOT attempt to have child and parent windows on different threads, and rather emphatically stating that it will not work.

If that was so, Windows would put some safeguards in and simply fail out when you attempted to call CreateWindow.
Now, there definitely are thread coupling issues that can cause major issues, but, that said with those constraints, this is a supported scenario.

作妖 2024-08-18 21:49:50

This is an interesting question: A lot of old school win32 guys have told me you CANNOT do this. In researching this, I found this forum: SendMessage(). My current theory is that CreateWindowEx() must send a message (via SendMessage(), so it blocks) to the parent window to ask for permission to exist (or at least to notify of its existence)... Anyhow, as long as that parent thread is free to process these messages, it all works...

习惯成性 2024-08-18 21:49:50

窗口与创建它的线程相关联(更具体地说,与该线程的消息队列相关联)。父窗口不能与其子窗口驻留在不同的线程中。

A window is tied to the thread that creates it (more specifically, to that thread's message queue). A parent window cannot reside in a different thread than its child windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文