• Advertisement
Sign in to follow this  

Shutting down a hidden window in a dll

This topic is 3389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my application I need to create a hidden window in a dll. I create the window in a separate thread. I have a few questions about destroying the window. 1. Is using PostMessage(m_hwnd, WM_QUIT, 0, 0); to close the window the correct way? When I send the message it does exit the message loop and the thread ends, so it seems correct to me. Since I'm destroying the window from another thread I assume this is the best way or not? 2. Do I need to process the WM_CLOSE message? Or is that for the X button? This is a hidden window so it doesn't have an X button to press. MSDN just says "sent as a signal that a window or an application should terminate" not where that signal comes from. 3. Do I need the DestroyWindow(m_hwnd); call after the message loop exits or is that unnecessary? Since it's exited the message loop do I assume the Window has already been destroyed? When is a WM_DESTROY message received? Again it's the fact it's a hidden window that is confusing me a bit. 4. Any comments on the overall design?
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			hInstance = (HINSTANCE) hModule;
			DisableThreadLibraryCalls(hInstance);
			break;
		case DLL_PROCESS_DETACH:
			if(m_hwnd)
			{
				PostMessage(m_hwnd, WM_QUIT, 0, 0);
				m_hwnd = 0;
			}
			break;
	}
	return TRUE;
}

DWORD WINAPI create_window(LPVOID lpParam)
{
	int exitcode = 1;
	WNDCLASS wc = { 0 };

	// initialize the description of the window class
	wc.lpszClassName 	= window_class;
	wc.hInstance 		= hInstance;
	wc.lpfnWndProc		= winproc;

	// register the class; fail if we can't
	if (!RegisterClass(&wc))
		goto error;

	// create a window
	m_hwnd = CreateWindowEx(
	WINDOW_STYLE_EX,
	window_class,
	window_name,
	WINDOW_STYLE,
	0, 0,
	1, 1,
	NULL,
	NULL,
	hInstance,
	NULL);

	if (m_hwnd == NULL)
		goto error;

	MSG msg;

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

	exitcode = msg.wParam;

error:
	if(m_hwnd)
	{
		DestroyWindow(m_hwnd);
		m_hwnd = 0;
	}

	UnregisterClass(window_class, hInstance);

	return exitcode;
}

MYDLL_API int __stdcall initialize()
{
	DWORD dwThreadId = NULL, dwThrdParam = 1;

	m_hThread = CreateThread(NULL, 0, create_window, &dwThrdParam, 0, &dwThreadId);

	if (m_hThread == NULL) 
		return 0;

	return 1;
}

MYDLL_API int __stdcall shutdown()
{
	if(m_hwnd)
	{
		PostMessage(m_hwnd, WM_QUIT, 0, 0);
		m_hwnd = 0;
	}

	return 1;
}

LRESULT CALLBACK winproc(HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	switch(message)
	{
	case WM_CLOSE:
		DestroyWindow(wnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(wnd, message, wparam, lparam);
	}

	return 0;	
}

Share this post


Link to post
Share on other sites
Advertisement
To destroy a window you need to call DestroyWindow. Sending/Posting WM_DESTROY is NOT destroying a window.
DestroyWindow might not work if it's called from a different thread.

Running out of the message loop is also not destroying a window, it merely stops all message processing for it.

Share this post


Link to post
Share on other sites
Send the WM_CLOSE message to the Window, the default is to call DestroyWindow from inside the correct thread.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement