Click here to Skip to main content
15,908,013 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: gethostbyaddr problem Pin
David Crow17-Sep-03 9:33
David Crow17-Sep-03 9:33 
GeneralRe: gethostbyaddr problem Pin
ns17-Sep-03 9:54
ns17-Sep-03 9:54 
GeneralRe: gethostbyaddr problem Pin
David Crow17-Sep-03 10:08
David Crow17-Sep-03 10:08 
GeneralPrint help easy question Pin
Binayak17-Sep-03 8:50
Binayak17-Sep-03 8:50 
GeneralRe: Print help easy question Pin
vcplusplus17-Sep-03 9:32
vcplusplus17-Sep-03 9:32 
GeneralCharacter Set of a String Pin
hckygirloh17-Sep-03 8:50
hckygirloh17-Sep-03 8:50 
GeneralRe: Character Set of a String Pin
Daniel Turini17-Sep-03 9:42
Daniel Turini17-Sep-03 9:42 
GeneralGlobal Hook with a Callback Pin
tsprang17-Sep-03 8:49
tsprang17-Sep-03 8:49 
Hi all,

I'm working on a project where I have a native (unmanaged) DLL that establishes itself as a filter for system-wide (global) mouse processing. The functional bit of code is this:

<br />
extern "C" __declspec(dllexport)<br />
HHOOK Hook(PFN_MOUSE_MOVE_CALLBACK callback)<br />
{<br />
	if (!_hooked)<br />
	{<br />
		_hooked = true;<br />
		_mouseMoveCallback = callback;<br />
		_hhk = SetWindowsHookEx(WH_MOUSE, SystemMouseProc, _hinstance, 0);<br />
		return _hhk;<br />
	}<br />
	else<br />
		return (HHOOK)NULL;<br />
}<br />


The argument is intended to be used in the actual mouse processing as follows:

<br />
LRESULT CALLBACK SystemMouseProc (int code, WPARAM wparam, LPARAM lparam)<br />
{<br />
	if (code >= 0)<br />
	{<br />
		MOUSEHOOKSTRUCT* mouse = (MOUSEHOOKSTRUCT*)lparam;<br />
<br />
		if (_mouseMoveCallback != NULL)<br />
			_mouseMoveCallback(mouse->pt.x, mouse->pt.y);<br />
<br />
	}<br />
	return CallNextHookEx(_hhk, code, wparam, lparam);<br />
}<br />


The problem I'm hitting is that because it's a global hook, this DLL is "injected" (to use Microsoft's verbiage) into each process after it's initially installed by my EXE (which happens to be managed code, though I don't think it affects anything). This makes sense and is in fact what I want, EXCEPT for the fact that I want my callback to work when the thread of execution changes.

When the implementation of SystemMouseProc is changed to just dump to a file, everything's great:

<br />
		FILE* stream;<br />
		if ((stream = fopen("c:\\hook.txt", "a+")) != NULL)<br />
		{<br />
			fprintf(stream, "x: %d, y: %d\n", mouse->pt.x, mouse->pt.y);<br />
			fclose(stream);<br />
		}<br />


Heaps of mouse coordinates.

I guess what I'm trying to do is get some shared chunk of memory between all "instances" (is that the right word?) of my DLL so that regardless of the current process it's running in, my desired callback function can always be called. Right now, if the mouse is over any other program than the original caller, _mouseMoveCallback != NULL evaluates to false and nothing happens.

I've considered falling back on the stream method and just connecting a network socket to localhost between the DLL and calling EXE, but that really didn't seem like the best way to go if it could be helped.

Here's all the code for the DLL. TIA if you can help!

-T

<br />
#include "stdafx.h"<br />
#include "stdio.h"		// for debugging.<br />
<br />
/* Friggin C function definitions */<br />
LRESULT CALLBACK SystemMouseProc (int code, WPARAM wparam, LPARAM lparam);<br />
<br />
/* Other defs */<br />
typedef void (__stdcall* PFN_MOUSE_MOVE_CALLBACK)(int, int);<br />
<br />
/* Globals */<br />
HHOOK											_hhk = 0;<br />
HINSTANCE									_hinstance = 0;<br />
bool 											_hooked = false;<br />
PFN_MOUSE_MOVE_CALLBACK		_mouseMoveCallback = NULL;<br />
<br />
/* Entry to the app */<br />
BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)<br />
{<br />
	if (ul_reason_for_call == DLL_PROCESS_ATTACH)<br />
	{<br />
		if (_hinstance == 0)<br />
		{<br />
			_hinstance = (HINSTANCE)hModule;<br />
			FILE* stream;<br />
			if ((stream = fopen("c:\\hook.txt", "a+")) != NULL)<br />
			{<br />
				fprintf(stream, "Process Attached %d\n", (int)_hinstance);<br />
				fclose(stream);<br />
			}<br />
		}<br />
	}<br />
	return TRUE;<br />
}<br />
<br />
extern "C" __declspec(dllexport)<br />
HHOOK Hook(PFN_MOUSE_MOVE_CALLBACK callback)<br />
{<br />
	if (!_hooked)<br />
	{<br />
		_hooked = true;<br />
		_mouseMoveCallback = callback;<br />
		_hhk = SetWindowsHookEx(WH_MOUSE, SystemMouseProc, _hinstance, 0);<br />
		return _hhk;<br />
	}<br />
	else<br />
		return (HHOOK)NULL;<br />
}<br />
<br />
extern "C" __declspec(dllexport)<br />
void Unhook()<br />
{<br />
	if (_hooked)<br />
		UnhookWindowsHookEx(_hhk);<br />
<br />
	return;<br />
}<br />
<br />
/* My system hook */<br />
LRESULT CALLBACK SystemMouseProc (int code, WPARAM wparam, LPARAM lparam)<br />
{<br />
	if (code >= 0)<br />
	{<br />
		MOUSEHOOKSTRUCT* mouse = (MOUSEHOOKSTRUCT*)lparam;<br />
<br />
		if (_mouseMoveCallback != NULL)<br />
			_mouseMoveCallback(mouse->pt.x, mouse->pt.y);<br />
<br />
		//FILE* stream;<br />
		//if ((stream = fopen("c:\\hook.txt", "a+")) != NULL)<br />
		//{<br />
		//	fprintf(stream, "x: %d, y: %d\n", mouse->pt.x, mouse->pt.y);<br />
		//	fclose(stream);<br />
		//}<br />
	}<br />
	return CallNextHookEx(_hhk, code, wparam, lparam);<br />
}<br />

GeneralGUI Compiling Trouble Pin
doncab17-Sep-03 8:35
doncab17-Sep-03 8:35 
GeneralRe: GUI Compiling Trouble Pin
ZoogieZork17-Sep-03 9:47
ZoogieZork17-Sep-03 9:47 
GeneralRe: GUI Compiling Trouble Pin
doncab17-Sep-03 12:09
doncab17-Sep-03 12:09 
GeneralRe: GUI Compiling Trouble Pin
ZoogieZork17-Sep-03 12:51
ZoogieZork17-Sep-03 12:51 
Generalyour comments on CSocket and CAsyncSocket Pin
R. Thomas17-Sep-03 7:29
R. Thomas17-Sep-03 7:29 
GeneralRe: your comments on CSocket and CAsyncSocket Pin
Rickard Andersson2017-Sep-03 7:42
Rickard Andersson2017-Sep-03 7:42 
GeneralImage::Save Problem! Does anyone know how to solve this puzzle? Thanks a lot! Pin
TeleStar17-Sep-03 7:10
TeleStar17-Sep-03 7:10 
GeneralRe: Image::Save Problem! Does anyone know how to solve this puzzle? Thanks a lot! Pin
Steve S18-Sep-03 1:52
Steve S18-Sep-03 1:52 
GeneralYes, I had used the code on MSDN, but it doesn't work! Pin
TeleStar18-Sep-03 11:21
TeleStar18-Sep-03 11:21 
QuestionMFC-&gt;controls-&gt;extended styls? Pin
udiams17-Sep-03 5:36
udiams17-Sep-03 5:36 
AnswerRe: MFC-&gt;controls-&gt;extended styls? Pin
David Crow17-Sep-03 5:38
David Crow17-Sep-03 5:38 
AnswerRe: MFC-&gt;controls-&gt;extended styls? Pin
Alvaro Mendez17-Sep-03 6:02
Alvaro Mendez17-Sep-03 6:02 
AnswerRe: MFC-&gt;controls-&gt;extended styls? Pin
Mike Dimmick17-Sep-03 6:08
Mike Dimmick17-Sep-03 6:08 
GeneralSTL and Efficiency Pin
Anonymous17-Sep-03 5:13
Anonymous17-Sep-03 5:13 
GeneralRe: STL and Efficiency Pin
HalfWayMan17-Sep-03 5:40
HalfWayMan17-Sep-03 5:40 
GeneralRe: STL and Efficiency Pin
Mike Dimmick17-Sep-03 5:44
Mike Dimmick17-Sep-03 5:44 
GeneralRe: STL and Efficiency Pin
Andrew Walker17-Sep-03 5:45
Andrew Walker17-Sep-03 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.