我做的一个取得鼠标左键的坐标的程序,只能在本程序的界面上点击时得到坐标,测试后我发现,在dll里得到了坐标似乎是传不出来。
dll中:
_declspec( dllexport ) long x;
_declspec( dllexport ) long y;LRESULT CALLBACK MouseProc( int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
MOUSEHOOKSTRUCT* m_pM;
CPoint point;
if (nCode < 0) // do not process the message
return CallNextHookEx(m_ghHook, nCode, wParam, lParam);
if(wParam==WM_LBUTTONDOWN)
{
char s[12];
m_pM=(MOUSEHOOKSTRUCT*)lParam;
point=m_pM->pt;
x=point.x;
y=point.y;
sprintf(s,"%d",x);
// AfxMessageBox(s);
}
return CallNextHookEx(m_ghHook, nCode, wParam, lParam);}BOOL CAgentHookApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
hins=AfxGetInstanceHandle();
return CWinApp::InitInstance();
}
BOOL __declspec(dllexport)__stdcall SetHook()
{
m_ghHook=SetWindowsHookEx(WH_MOUSE,MouseProc,hins,0);
return TRUE;
}BOOL UnHook()
{
if(UnhookWindowsHookEx(m_ghHook))
return TRUE;
else
return FALSE;
}
def:
EXPORTS
; Explicit exports can go here
MouseProc;
SetHook;
UnHook;
x DATA;
y DATA;
调用:
HOOKPROC hkprcMousMsg;
static HHOOK hhookSysMsg;
int * p;
int x;
if(hinstDLL=LoadLibrary((LPCTSTR)"AgentHook.dll"))
{
dll中:
_declspec( dllexport ) long x;
_declspec( dllexport ) long y;LRESULT CALLBACK MouseProc( int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
MOUSEHOOKSTRUCT* m_pM;
CPoint point;
if (nCode < 0) // do not process the message
return CallNextHookEx(m_ghHook, nCode, wParam, lParam);
if(wParam==WM_LBUTTONDOWN)
{
char s[12];
m_pM=(MOUSEHOOKSTRUCT*)lParam;
point=m_pM->pt;
x=point.x;
y=point.y;
sprintf(s,"%d",x);
// AfxMessageBox(s);
}
return CallNextHookEx(m_ghHook, nCode, wParam, lParam);}BOOL CAgentHookApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
hins=AfxGetInstanceHandle();
return CWinApp::InitInstance();
}
BOOL __declspec(dllexport)__stdcall SetHook()
{
m_ghHook=SetWindowsHookEx(WH_MOUSE,MouseProc,hins,0);
return TRUE;
}BOOL UnHook()
{
if(UnhookWindowsHookEx(m_ghHook))
return TRUE;
else
return FALSE;
}
def:
EXPORTS
; Explicit exports can go here
MouseProc;
SetHook;
UnHook;
x DATA;
y DATA;
调用:
HOOKPROC hkprcMousMsg;
static HHOOK hhookSysMsg;
int * p;
int x;
if(hinstDLL=LoadLibrary((LPCTSTR)"AgentHook.dll"))
{
x=*p;
}
{
pt.x = x;
pt.y = y;
}
在dll里面用全局变量保存坐标
在导出函数里面返回全局变量的置就可以乐或者这样也行
p=(int *)GetProcAddress(hinstDLL, "x");
x=*p;
long x;
long y;
调用x=*p;时,DLL被加载到了当前进程得到的当然是本窗口的坐标。
若要得到所有的鼠标坐标,要将x y声名为所有实例共享的.