为什么dll中的导出函数,在调用它的程序中无法导入,奇怪在dll中的导出函数有的正常,有的不正常,我都检查得快要疯了,都没办法,请高手
dll是一个键盘钩子,我想让dll和调用它的程序发消息通信或通过导出函数传字符。
hodll.h如下:
LRESULT __declspec(dllexport) __stdcall CALLBACK KeyboardProc(
int nCode,
WPARAM wParam,
LPARAM lParam);
BOOL __declspec(dllexport) __stdcall installhook();//此函数正常
BOOL __declspec(dllexport)__stdcall UnHook();//不正常
extern "C" _declspec(dllexport) char *sendstr();//不正常
class CHodllApp : public CWinApp
{
public:
BOOL ExitInstance ();
CHodllApp();
BOOL InitInstance ();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHodllApp)
//}}AFX_VIRTUAL
//{{AFX_MSG(CHodllApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//hodll.cpp如下:
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
#pragma data_seg()
HINSTANCE hins;
char szBuf[256];
char *p;
CString msg;
CString strMsg;
#define MAX_LEN 2
//#define WM_MY_MESSAGE (WM_USER+100)
////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CHodllApp, CWinApp)
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////////////
LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode))
{
switch(wParam)
{
case 'R':
/*DWORD result;
SendMessageTimeout(HWND_BROADCAST, // 目标窗口
WM_MY_MESSAGE, // 消息
0, // WPARAM
0, // LPARAM
SMTO_ABORTIFHUNG |
SMTO_NORMAL,
TIMEOUT_INTERVAL,
&result);*/
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
//AfxMessageBox("send r msg");
strMsg="r";
break;
case 'S':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="s";
break;
case 'T':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="t";
break;
case 'U':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="u";
break;
case 'V':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="v";
break;
case 'W':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="w";
break;
case 'X':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="x";
break;
case 'Y':
//PostMessage(hAim,WM_MY_MESSAGE,NULL,NULL);
strMsg="y";
break;
default:
break;
}
}
LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}
BOOL __declspec(dllexport)__stdcall installhook()
{
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
return TRUE;
}
extern "C" _declspec(dllexport) char *sendstr()
{
static char string[MAX_LEN];
strncpy(string,strMsg,sizeof(string)-1);
string[sizeof(string)-1]='\0';
return string;
}
BOOL __declspec(dllexport)__stdcall UnHook()
{
BOOL unhooked = UnhookWindowsHookEx(hkb);
return unhooked;
}
BOOL CHodllApp::InitInstance ()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
hins=AfxGetInstanceHandle();
GetModuleFileName(AfxGetInstanceHandle( ),szBuf,sizeof(szBuf));
p = szBuf;
while(strchr(p,'\\'))
{
p = strchr(p,'\\');
p++;
}
*p = '\0';
msg=szBuf;
return TRUE;
}
BOOL CHodllApp::ExitInstance ()
{
return TRUE;
}
CHodllApp::CHodllApp()
{
}
CHodllApp theApp;
//////////////////////////////
在调用它的程序中如果是静态调用,则出现函数UnHook();sendstr();,而installhook则正常。
在动态调用中,如下:
static HINSTANCE hinstDLL;
typedef BOOL (CALLBACK *inshook)();
inshook instkbhook;
typedef char* (*dsendstr)();
dsendstr lsendstr;
typedef BOOL (*dunhook)();
dunhook lunhook;
if(hinstDLL=LoadLibrary((LPCTSTR)"hodll.dll"))
{
lsendstr=(dsendstr)GetProcAddress(hinstDLL, "sendstr");
//lsendstr调试时为空
lunhook=(dunhook)GetProcAddress(hinstDLL, "UnHook");
//lunhook调试时为空
instkbhook=(inshook)GetProcAddress
(hinstDLL, "installhook");
//instkbhook调试时不为空,正常。
instkbhook();
}
为什么这样呢?我已经是晕了,找不出原因。高手指点指点了。