[HOOK有关问题]附源码,来个大牛,关于CAD的
[HOOK问题]附源码,来个大牛,关于CAD的
先上代码(Codeblocks+MinGW+wxWidgets):
DLL部分:
main.h:
main.cpp部分:
wxtest.exe部分:
先上代码(Codeblocks+MinGW+wxWidgets):
DLL部分:
main.h:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void SomeFunction(const LPCSTR sometext);
int SetHook();
int StopHook();
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
main.cpp部分:
#include "main.h"
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <string>
HINSTANCE globalInst; ///不用了
HWND previewwnd = NULL; // 上次鼠标所指的窗口句柄
HWND glhDisplayWnd=NULL;
// a sample exported function
void SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
globalInst=hinstDLL;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
return TRUE; // succesful
}
}
HHOOK hook=NULL;
///获取句柄已经没有问题了,接下来就处理wParam中的消息
///不能稍微移动一下鼠标就产生一堆的消息
LRESULT CALLBACK MouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
LPMOUSEHOOKSTRUCT pmousehook=(MOUSEHOOKSTRUCT FAR*)lParam;
char buffer[256];
FILE *out;
if(wParam==WM_LBUTTONDOWN)
{
HWND targetwnd=pmousehook->hwnd;
::GetWindowText(targetwnd,buffer,256);
out=fopen("1.txt","a+");
sprintf(buffer,"%s\t%x\r\n",buffer,targetwnd);
fwrite(buffer,strlen(buffer),1,out);
fclose(out);
memset(buffer,0,strlen(buffer));
}
return CallNextHookEx(hook,nCode,wParam,lParam);
}
int SetHook()
{
hook=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("D:\\Temp\\dll\\bin\\Debug\\dll.dll"),0);
if(hook==NULL)
{
printf("SetWindowsHookEx error:%d\n",GetLastError());
return -1;
}
return 0;
}
int StopHook()
{
if(UnhookWindowsHookEx(hook)==0)
{
printf("UnhookWindowsHookEx error:%d\n",GetLastError());
return -1;
}
return 0;
}
wxtest.exe部分:
#include <wx/wx.h>
#include <wx/icon.h>
#include <wx/textctrl.h>
////////////////////////////////////////
#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include "\..\temp\dll\main.h"
////////////////////////////////////////
typedef void(WINAPI *pfunction)(const LPCSTR sometext);
typedef int(WINAPI *pfsethook)();
typedef int(WINAPI *pfstophook)();
///为每一个窗口申请一个类
class myapp : public wxApp
{
public:
virtual bool OnInit();
};
class myframe : public wxFrame
{
public:
myframe(const wxString& title,const wxPoint& pos,const wxSize& size);
DECLARE_EVENT_TABLE()
};
IMPLEMENT_APP(myapp) ///声明类