LoadLibrary为什么总是返回NULL;DLLIMPORT再BCB中如何正确应用(200分)

  • 主题发起人 主题发起人 142857
  • 开始时间 开始时间
1

142857

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]http://www.vckbase.com/code/winsys/hook/trapkeys.zip
有TaskKeyHook.dll 我想通过DLL调用,禁止热键,请见以下代码:
为什么总是返回NULL值呢?
if (FileExists(ExtractFilePath(Application->ExeName)+"TaskKeyHook.DLL" )){
HINSTANCE hLib =::LoadLibrary("TaskKeyHook.DLL");
if( hLib==NULL ) //then
{ // throw
ShowMessage( "You must have TaskKeyHook.DLL installed to use this component" );
请各位大虾帮我看看。
另外,由于开发环境只有C++Builder,调用http://www.vckbase.com/code/winsys/hook/trapkeys.zip压缩文件
解压后的TaskKeyHook.h文件时
内容见下:#define DLLIMPORT __declspec(dllimport)
DLLIMPORT BOOL DisableTaskKeys(BOOL bEnable, BOOL bBeep);
DLLIMPORT BOOL AreTaskKeysDisabled();
可是,编译总出现
[Linker Error] Unresolved external 'DisableTaskKeys(int, int)' referenced from E:/U盘/UMAIN.OBJ
错误,请问如何解决。
 
静态调用时,需要有DLL的lib文件,方法如下:
C:/>implib DisableTaskKeys.lib DisableTaskKeys.dll
在项目中加入DisableTaskKeys.lib,即可完成静态调用。
如果采用动态调用,代码可参照下面的改一下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int rs;
AnsiString aUserName, aPswd;
//把要调用的方法定义为一种类型,就无需在后面用到时多次进行调制类型转换
typedef int (__stdcall *_Verify)(const char *, const char *);
HINSTANCE hDLL;
_Verify Verify;
hDLL = LoadLibrary("MyFunc.dll");
//如果调用DLL的是另一个DLL,尽量不要使用绝对路径
if (hDLL){
Verify = (_Verify)GetProcAddress(hDLL, "VerifyUser");
if (Verify){
aUserName = Edit1->Text;
aPswd = Edit2->Text;
rs = Verify(aCardSN.c_str(), aPswd.c_str());
if (rs)
::MessageBox(0, "用户有效!", "", MB_OK);
else
::MessageBox(0, "用户未通过验证!", "验证", MB_OK);
}else
::MessageBox(0, "函数导入失败!","",MB_ICONERROR);
}else
::MessageBox(0, "无法加载动态链接库!","",MB_ICONERROR);
FreeLibrary(hDLL);
}
 
TaskKeyHook.dll是用VC写的吗?有源码吗?看看输出函数是 export stdcall 还是别的。
VC与CB写的DLL格式经常无法互相调用
 
在::LoadLibrary("TaskKeyHook.DLL")中的文件名加上全路径,
也可把它放入系统的PATH指定的路径中
 
同意Sachow
 
////////////////////////////////////////////////////////////////
// MSDN Magazine -- September 2002
// If this code works, it was written by Paul DiLascia.
// If not, Ido
n't know who wrote it.
// Compiles with Visual Studio 6.0 and Visual Studio .NET on Windows XP.
//
// This file implements the low-level keyboard hook that traps the task keys.
//
#define _WIN32_WINNT 0x0500 // for KBDLLHOOKSTRUCT
#include <afxwin.h> // MFC core and standard components
#define DLLEXPORT __declspec(dllexport)
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////
// App (DLL) object
//
class CTaskKeyHookDll : public CWinApp {
public:
CTaskKeyHookDll() { }
~CTaskKeyHookDll() { }
} MyDll;
////////////////
// The section is SHARED among all instances of this DLL.
// A low-level keyboard hook is always a system-wide hook.
//
#pragma data_seg (".mydata")
HHOOK g_hHookKbdLL = NULL;
// hook handle
BOOL g_bBeep = FALSE; // beep on illegal key
#pragma data_seg ()
#pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it shared
/////////////////
// Low-level keyboard hook:
// Trap task-switching keys by returning without passing along.
//
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
{
KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;
if (nCode==HC_ACTION) {
BOOL bCtrlKeyDown =
GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);
if ((pkh->vkCode==VK_ESCAPE &amp;&amp;
bCtrlKeyDown) || // Ctrl+Esc
(pkh->vkCode==VK_TAB &amp;&amp;
pkh->flags &amp;
LLKHF_ALTDOWN) || // Alt+TAB
(pkh->vkCode==VK_ESCAPE &amp;&amp;
pkh->flags &amp;
LLKHF_ALTDOWN)|| // Alt+Esc
(pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN)) { // Start Menu
if (g_bBeep &amp;&amp;
(wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
MessageBeep(0);
// only beep ondo
wnstroke if requested
return 1;
// gobble it: go directly to jail,do
not pass go
}
}
return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
}
//////////////////
// Are task keys disabled--ie, is hook installed?
// Note: This assumes there's no other hook thatdo
es the same thing!
//
DLLEXPORT BOOL AreTaskKeysDisabled()
{
return g_hHookKbdLL != NULL;
}
//////////////////
// Disable task keys: install low-level kbd hook.
// Return whether currently disabled or not.
//
DLLEXPORT BOOL DisableTaskKeys(BOOL bDisable, BOOL bBeep)
{
if (bDisable) {
if (!g_hHookKbdLL) {
g_hHookKbdLL = SetWindowsHookEx(WH_KEYBOARD_LL,
MyTaskKeyHookLL, MyDll.m_hInstance, 0);
}
} else
if (g_hHookKbdLL != NULL) {
UnhookWindowsHookEx(g_hHookKbdLL);
g_hHookKbdLL = NULL;
}
g_bBeep = bBeep;
return AreTaskKeysDisabled();
}
 
if (FileExists(ExtractFilePath(Application->ExeName)+"TaskKeyHook.DLL" )){
HINSTANCE hLib =::LoadLibrary("TaskKeyHook.DLL");
改为:
if (FileExists(ExtractFilePath(Application->ExeName)+"TaskKeyHook.DLL" )){
HINSTANCE hLib =::LoadLibrary(ExtractFilePath(Application->ExeName)+"TaskKeyHook.DLL");
 
后退
顶部