www.pudn.com > 截取程序消息的DLL和使用例子GetMSG.rar > main.c


#include  
#include  
#include  
 
#include "getmsg.h" 
#define WM_KULER (WM_USER+1101) 
#define WM_ELSE (WM_USER+1102) 
HINSTANCE hInstance; 
 
BOOL WINAPI DllMain(HANDLE hModule, DWORD, LPVOID); 
LRESULT CALLBACK MyGetMsgProc(INT hc, WPARAM wParam, LPARAM lParam); 
LRESULT CALLBACK MyCallWndProc(INT hc, WPARAM wParam, LPARAM lParam); 
 
BOOL HookInstalled = 0 ; // State Table of my hooks 
 
HHOOK hhookMsg =NULL, hhookWnd =NULL; 
 
__declspec( dllexport ) HWND g_hWnd =NULL; 
__declspec( dllexport ) UINT g_msg =0; 
__declspec( dllexport ) char g_caption[256]; 
__declspec( dllexport ) char g_className[50]; 
 
//--------------------------------------------------------------------------- 
// DllMain 
//--------------------------------------------------------------------------- 
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) 
{ 
	FILE *fp =NULL; 
    hInstance = hModule; 
 
    switch(dwReason) 
	{ 
	case DLL_PROCESS_ATTACH: 
		if((fp =fopen("c:\\share.txt", "r")) !=NULL) 
		{ 
			fscanf(fp, "%d %d %s %s", &g_hWnd, &g_msg, g_className, g_caption); 
			fclose(fp); 
		} 
		//MessageBox(NULL, g_className, g_caption, MB_OK); 
		break; 
	case DLL_PROCESS_DETACH: 
		break; 
    default: 
      break; 
	} 
 
	return TRUE; 
} 
 
//--------------------------------------------------------------------------- 
int __stdcall InstGetMsg(BOOL fInstall, char *pclass, char *pcaption, UINT msg, HWND hWnd) 
{ 
  FILE *fp; 
 
  if ( fInstall )  
  { 
    if(pclass ==NULL) 
		g_className[0] =0; 
	else 
		strcpy(g_className, pclass); 
 
	if(pcaption ==NULL) 
		g_caption[0] =0; 
	else 
		strcpy(g_caption, pcaption); 
	g_hWnd =hWnd; 
	g_msg =msg; 
	if((fp =fopen("c:\\share.txt", "w")) !=NULL) 
	{ 
		fprintf(fp, "%d %d %s %s", g_hWnd, g_msg, g_className, g_caption); 
		fclose(fp); 
	} 
	if(HookInstalled) return FALSE; 
    hhookWnd = SetWindowsHookEx(WH_CALLWNDPROC, MyCallWndProc,  
				  hInstance, 0); 
    hhookMsg = SetWindowsHookEx(WH_GETMESSAGE, MyGetMsgProc,  
				  hInstance, 0); 
    HookInstalled = TRUE; 
  } 
  else  
  { 
    if(!HookInstalled) return FALSE; 
    UnhookWindowsHookEx(hhookMsg);     
    UnhookWindowsHookEx(hhookWnd);     
    HookInstalled = FALSE; 
  }                               
  return TRUE; 
} 
 
LRESULT CALLBACK MyGetMsgProc(INT hc, WPARAM wParam, LPARAM lParam) 
{ 
	PMSG pmsg; 
	HWND hwnd; 
	char *pclass =NULL, *pcaption =NULL; 
 
	pmsg = (PMSG)lParam; 
	if (hc >= 0 && pmsg && pmsg->hwnd) 
	{ 
		if(pmsg->message ==g_msg) 
		{ 
			if(g_className[0] !=0) pclass =g_className; 
			if(g_caption[0] !=0) pcaption =g_caption; 
 
			hwnd =FindWindow(pclass, pcaption); 
			if(g_hWnd && pmsg->hwnd ==hwnd) 
			{ 
				PostMessage(g_hWnd, WM_KULER/*pmsg->message*/, pmsg->wParam, pmsg->lParam); 
				//return 0; 
			} 
		} 
		else 
		{ 
			PostMessage(g_hWnd, WM_ELSE/*pmsg->message*/, pmsg->wParam, pmsg->lParam); 
		} 
	} 
 
	return CallNextHookEx(NULL, hc, wParam, lParam); 
} 
 
LRESULT CALLBACK MyCallWndProc(INT hc, WPARAM wParam, LPARAM lParam) 
{ 
    PCWPSTRUCT pcwps; 
	HWND hwnd; 
	char *pclass =NULL, *pcaption =NULL; 
 
    pcwps = (PCWPSTRUCT)lParam; 
 
    if (hc >= 0 && pcwps && pcwps->hwnd) 
    { 
		if(pcwps->message ==g_msg) 
		{ 
			if(g_className[0] !=0) pclass =g_className; 
			if(g_caption[0] !=0) pcaption =g_caption; 
 
			hwnd =FindWindow(pclass, pcaption); 
			if(hwnd) 
				MessageBeep(0); 
			if(pcwps->hwnd ==hwnd) 
			{ 
				SendMessage(g_hWnd, WM_KULER/*pcwps->message*/, pcwps->wParam, pcwps->lParam); 
				//return 0; 
			} 
		} 
		else 
		{ 
			PostMessage(g_hWnd, WM_ELSE/*pmsg->message*/, pcwps->wParam, pcwps->lParam); 
		} 
 
    } 
 
    return CallNextHookEx(NULL, hc, wParam, lParam); 
}