{"id":9673,"date":"2011-09-12T07:00:00","date_gmt":"2011-09-12T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/09\/12\/is-this-a-really-bug-with-createwindowex-or-am-i-just-confused\/"},"modified":"2011-09-12T07:00:00","modified_gmt":"2011-09-12T07:00:00","slug":"is-this-a-really-bug-with-createwindowex-or-am-i-just-confused","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110912-00\/?p=9673","title":{"rendered":"Is this a really bug with CreateWindowEx or am I just confused?"},"content":{"rendered":"<p>\nSomebody sent me email pointing out strange behavior in the\n<code>MessageBox<\/code> function if you fail a window creation\nby returning &minus;1 from the <code>WM_CREATE<\/code> message.\nOn the other hand, returning <code>FALSE<\/code> from\n<code>WM_NCCREATE<\/code> seems to work just fine.\n&#8220;So why the difference with <code>WM_CREATE<\/code>?&#8221;\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\nLRESULT CALLBACK\nWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\n{\n    switch(uMsg)\n    {\n        case WM_CREATE:\n            return -1;\n        case WM_DESTROY:\n            PostQuitMessage(0);\n            return 0;\n    }\n    return DefWindowProc(hWnd, uMsg, wParam, lParam);\n}\nint APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev,\n    LPSTR lpCmdLine, int nShowCmd)\n{\n    MSG msg;\n    HWND hWnd;\n    WNDCLASS wc = { 0 };\n    wc.lpfnWndProc   = WndProc;\n    wc.hInstance     = hInst;\n    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);\n    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);\n    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\n    wc.lpszClassName = \"TestApp\";\n    if(!RegisterClass(&amp;wc)){\n        MessageBox(NULL, \"Error creating class\",\n            \"Test App\", MB_ICONERROR);\n        return 1;\n    }\n    hWnd = CreateWindow(wc.lpszClassName, \"Test App\",\n        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,\n        CW_USEDEFAULT, CW_USEDEFAULT, NULL, 0, hInst, NULL);\n    if(!hWnd){\n        MessageBox(NULL, \"Error creating window\",\n            \"Test App\", MB_ICONERROR);\n        return 1;\n    }\n    ShowWindow(hWnd, nShowCmd);\n    UpdateWindow(hWnd);\n    while(GetMessage(&amp;msg, NULL, 0, 0))\n    {\n        TranslateMessage(&amp;msg);\n        DispatchMessage(&amp;msg);\n    }\n    return (int)msg.wParam;\n}\n<\/pre>\n<p>\nYou already know enough to solve this puzzle.\nYou just need to connect the dots.\n<\/p>\n<p>\n(In fact, the person who sent me this topic did so\na year after\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/05\/20\/8520129.aspx\">\nI already answered it<\/a>.\nBut I&#8217;m repeating it here because the original answer\n<a HREF=\"http:\/\/www.adrianmccarthy.com\/blog\/?p=3\">\nwas accidentally destroyed<\/a>.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Somebody sent me email pointing out strange behavior in the MessageBox function if you fail a window creation by returning &minus;1 from the WM_CREATE message. On the other hand, returning FALSE from WM_NCCREATE seems to work just fine. &#8220;So why the difference with WM_CREATE?&#8221; #include &lt;windows.h&gt; LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-9673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Somebody sent me email pointing out strange behavior in the MessageBox function if you fail a window creation by returning &minus;1 from the WM_CREATE message. On the other hand, returning FALSE from WM_NCCREATE seems to work just fine. &#8220;So why the difference with WM_CREATE?&#8221; #include &lt;windows.h&gt; LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=9673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9673\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=9673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=9673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=9673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}