HELP - Process not dying

Started by
10 comments, last by 21st Century Moose 13 years ago

#include <windows.h>

const LPCWSTR className = L"Window1";
const LPCWSTR titleName = L"Window Title";

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

//fill up wc
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = className;
wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wc.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wc.hIconSm = wc.hIcon;
wc.hInstance = hInstance;

//register wc
RegisterClassEx(&wc);

DWORD windowStyle = (WS_OVERLAPPEDWINDOW | WS_VISIBLE) & ~WS_THICKFRAME;
hWnd = CreateWindow(className, titleName, windowStyle, 300,300,300,300, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

MSG msg;
while(true)
{
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

//do game logic here
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT); //Adds WM_QUIT to the message queue
break; //indicating that it is to gquit in the future
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


This is a code for creating an empty window. My problem is, when I click the 'x' button to exit, the window closes but the process is not dying so i have to manually kill the process in the windows task manager.

Can anyone tell me what is wrong with this code? Thanks!
Advertisement
I don't know Win32, but I'm suspicious of you checking for quit messages outside the PeekMessage() loop.
Adding to what rip-off said, if there are two messages in the queue, and in this order: WM_QUIT, SOME_RANDOM_THING, it won't work as you expect. And because you use an infinite while loop instead of one working off a modifiable variable, it seems the would-be obvious solution was obfuscated. Such things happen when you take a shortcut and it's why i never use such a loop.

I googled up a thread on another site that suggests DispatchMessage will never actually be called. Read it to find out why.
@rip-off
Yes, the problem is in the if(msg.message == WM_QUIT). The program does not enter into the if statement, I checked it. Thank you.
@Splinter of Chaos
The link you posted has a different context. The DispatchMessage is not called because the program uses GetMessage() with returns 0 when WM_QUIT is get from the queue. In my case, I'm using PeekMessage() and will always call the DispatchMessage() when there is a message in the queue.

But yeah, I got your point in the earlier statement. I should just use a variable flag to check when WM_QUIT is in the queue. Thanks!
Ooops! I tried doing this



MSG msg;
bool quitFlag = false;
while(true)
{
while(PeekMessage(&msg, hWnd, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if(msg.message == WM_QUIT)
{
quitFlag = true;
}
}

if(quitFlag)
break;

//do game logic here
}


But still the process is not ending. Isn't the PostQuitMessage() supposed to insert the WM_QUIT message to the message queue. It seems that the WM_QUIT is never inserted.

I also tried this code to check whether the WndProc() receives the WM_QUIT message. But the message box is not showing, which means it never receives the WM_QUIT;



LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_QUIT:
MessageBox(NULL,
L"QUITTING",
L"QUITTING",
MB_ICONEXCLAMATION | MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
WM_QUIT isn't sent to a particular window. Your message loop never receives it because it only handles messages specific to the window you created. Replace the hWnd parameter with 0.
wow thanks, that actually worked. What is actually happening when I pass 0?
Did you try looking at the documentation for PeekMessage()?
[color=#1C2837][size=2]
WM_QUIT isn't sent to a particular window.[/quote]
[color=#1C2837][size=2]

[color=#1C2837][size=2]If it is not sent to a particular window, where was it sent?
As mentioned in the documentation for PostQuitMessage() it's sent to the thread message queue.

This topic is closed to new replies.

Advertisement