System message unhandled exception


Problem Found:

The crash occurs within the call DispatchMessage( &msg ), what is the proper use of this function?

Old message below

I'm trying to compile my first 64bit Windows application using Microsoft Visual Studio 2012 Ultimate and the Windows SDK that came with it (I've done 64bit programs on other systems in the past) and I am getting an Unhandled Exception crash at runtime with this block of code exactly at the line if (msg.message == WM_QUIT) and I can't figure it out at all, the program works fine in 32bit.

All the program does is start up and then run this block as a method, it gets to this point before it crashes.

I am compiling and running this on Windows 7.

MSG msg;

while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
    // No message translation because we don't use WM_CHAR and it would conflict with our
    // deadkey handling.

    //if ( ExternalWindow && msg.hwnd == HWnd ) {
        //WndProc(HWnd, msg.message, msg.wParam, msg.lParam);
    //} else {
        DispatchMessage( &msg );

    if ( msg.message == WM_QUIT ) {
        Close = true;

I am linking with these libraries: kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

I'm sorry I can't provide more indepth information I am rather unfamiliar with the Windows SDK and as I said before, this is my first 64bit Windows application (It works fine with 32bit, so I'm guessing it's pointer size/storage issues?).

EDIT: This is also non-unicode so it is calling PeekMessageA() as well as DispatchMessageA().

EDIT2: WndProc() simply reads user input, it is an empty function at the moment and my breakpoints tell me it doesn't get called. ExternalWindow is false and HWnd is valid as the windows pops up and displays before the crash.

EDIT3: Here's the contents of msg, message, wParam and lParam remain the same each run, obviously the points, the time and the window position changes with each launch.

msg     {msg=0x0000031f wp=0x0000000000000001 lp=0x0000000000000000}
hdnw    0x00000000004a2594 {unused=-842150451 }
message 799
wParam  1
lParam  0
time    29882662
pt      {x=1479 y=581}`

EDIT4: I dummied out the function (return void on line one), of course the application runs perfectly fine only without the ability to close it, but I noticed that the window title is no longer being set, so I guess that this error is with the creation of the window, I will post my code for that in a moment.

asked on Stack Overflow Jul 20, 2013 by Felix Jones • edited Jul 20, 2013 by Ken White

1 Answer


You have corrupted some memory. This is the root cause. Win32 layer keeps some of its data in the memory of the process. Once you corrupt it, Win32 functions may crash.

Remove everything from your application and leave just the message pump. This should work. After that add stuffs in small chunks. If you will be careful enough, you will notice what part introduces memory corruption.

answered on Stack Overflow Jul 21, 2013 by Kirill Kobelev • edited Sep 12, 2015 by Kirill Kobelev

User contributions licensed under CC BY-SA 3.0