Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MouseProc - Callbacks from a dll

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

I am trying to implement a global mouse hook. However i want to recieve the mouse messages to my app. I decided to implement a function in the dll that takes a function pointer to the callback proc in the apps code.

My question is can you pass function pointers to a dll and if the answer is yes to that then can you see anything wrong with the following code.

Code:
// Dll code
typedef LRESULT (CALLBACK* HOOKCALLBACKPTR)(int code, WPARAM wParam, LPARAM lParam, int idHook);

HOOKCALLBACKPTR pAppHookCallback = NULL;
HHOOK hMouse = NULL;

// Initialise the CALLBACK and Mouse Hook
DLLEXPORT BOOL SetAutoScrollCallback(HOOKCALLBACKPTR P)
{
    pAppHookCallback = P;
    
    if (NULL == (hMouse = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,hInstance,0)))
        return FALSE;
}

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    pAppHookCallback(nCode,wParam,lParam,WH_MOUSE);	
    return CallNextHookEx(hMouse,nCode,wParam,lParam);
}


// Application.cpp
typedef LRESULT (CALLBACK* HOOKCALLBACKPTR)(int code, WPARAM wParam, LPARAM lParam, int idHook);  // where idHook is the id of the calling hook (there could be more than one)

LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam,int idHook) 
{
    \\ TRACE("mouse hook here");
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    if (!SetAutoScrollCallback(HookProc)) 
        return 0;
}

Thanks in advance.

"Programmers are tools for converting caffeine into code - BSRF"
 
Hmmm, I've never considered doing that, but you really should not need to. The standard way of using hooks is to post messages back to your main window and have it handle them directly. Here's how I do it in a program where I use a keyboard hook:

Code:
HOOK_API LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(nCode >= 0 && (lParam & 0x80000000) == 0)
	{
		HWND hwnd = FindWindow("#32770", "My Window");
		PostMessage(hwnd, WM_USER + 100, wParam, lParam);
	}

	return CallNextHookEx(g_Hook, nCode, wParam, lParam);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top