[comp.windows.ms.programmer] Problems w/ GETMESSAGE Windows Hook

newcomb@altair.la.locus.com (Chris Newcomb) (12/05/90)

I'm having problems using the WH_GETMESSAGE Windows hook in a DLL.  The hook
installs successfully and works correctly, but the system crashes and reboots
itself when the DLL exits.

I don't have any problems with the WH_CALLWNDPROC hook--replace all instances
of WH_GETMESSAGE with WH_CALLWNDPROC in the source below, and everything works.
The DLL consists of one source file (listed below) and the LIBENTRY.OBJ DLL
entry module provided with the SDK.  The source is compiled with:

cl -c -ASw -Gsw -Os


Here is the source file:

#include <windows.h>


void FAR PASCAL	GMHook(int, WORD, DWORD);	/*  Hook function	*/

FARPROC	lpNextGMHook;		/*  Next GETMESSAGE Hook function	*/


int FAR PASCAL
LibMain(hInstance, wDataSeg, nHeapSize, lpCmdLine)
HANDLE	hInstance;
WORD	wDataSeg;
WORD	nHeapSize;
LPSTR	lpCmdLine;
{
	/**
		I also tried passing in the return value of
			GetProcAddress(hInstance, "GMHook")
		instead of just passing in GMHook, but that didn't seem to make
		any difference.
	**/
	lpNextGMHook = SetWindowsHook(WH_GETMESSAGE, GMHook);
	return(TRUE);
}


void FAR PASCAL
WEP(nParameter)
int	nParameter;
{
	switch (nParameter) {

	    case WEP_FREE_DLL:
		/**
			We need to free all resources that the DLL is using,
			since we're dying but Windows isn't.

			This seems to be where all Hell breaks loose.
		**/
		UnhookWindowsHook(WH_GETMESSAGE, GMHook);
		return;

	    default:
		return;
	}
}


void FAR PASCAL
GMHook(nCode, wParam, lParam)
int	nCode;
WORD	wParam;
DWORD	lParam;
{
	/**
		Don't do anything with the message, just pass it on to the
		next guy.
	**/
	(void)DefHookProc(nCode, wParam, lParam, &lpNextGMHook);
}


/**
	Stub function to allow an application to load/run this DLL.
**/
void FAR PASCAL
foo()
{
}
------------------------------------------------------------------------------
Here is the .DEF file:

LIBRARY		HOOKLIB

DESCRIPTION	'Hook Test Library'

EXETYPE		WINDOWS

STUB		'WINSTUB.EXE'


CODE	PRELOAD FIXED
DATA	PRELOAD SINGLE FIXED		; Fixed data segment for the Hook


HEAPSIZE	0


EXPORTS
	foo		@1		; Stub function
	GMHook		@2		; Hook function
	WEP		@3	RESIDENTNAME
------------------------------------------------------------------------------

So...anybody got any ideas?  I'm stumped on this one.

Chris