我做的一个取得鼠标左键的坐标的程序,只能在本程序的界面上点击时得到坐标,测试后我发现,在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"))
{

解决方案 »

  1.   

    p=(int *)GetProcAddress(hinstDLL, "x");
    x=*p;
    }
      

  2.   

    应改定义一个函数,用这个函数返回x,y的值typedef void (WINAPI *PGetCursorPoint)(CPoint& pt);PGetCursorPoint pGetCursorPoint;pGetCursorPoint = (PGetCursorPoint)GetProcAddress(hinstDLL, "GetCursorPoint");CPoint pt;pGetCursorPoint(pt);
      

  3.   

    WINAPI void GetCursorPoint(CPoint& pt)
    {
        pt.x = x;
        pt.y = y;
    }
      

  4.   

    怎么不在定义一个导出函数呢
    在dll里面用全局变量保存坐标
    在导出函数里面返回全局变量的置就可以乐或者这样也行
    p=(int *)GetProcAddress(hinstDLL, "x");
    x=*p;
      

  5.   

    利用上面的也能返回值,但是只能返回本窗口的坐标。其它的在MouseProc能hook倒可以用AfxMessageBox(s)显示,但是在程序里得不到值
      

  6.   

    DLL可以加载到多个进程中,每一个进程中有一份变量的副本,如此例中的
    long x;
    long y;
    调用x=*p;时,DLL被加载到了当前进程得到的当然是本窗口的坐标。
    若要得到所有的鼠标坐标,要将x y声名为所有实例共享的.