正确得程序应该为下面得程序。
原理见我和Pipi.,cAkk得讨论:
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=198085
library HookWriteFile;
uses
SysUtils,
Classes,
windows,
messages;
type PCommonData=^TCommonData;
TCommonData = record
FileName:array [0..255] of char;
FileHandle:THandle;
end;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
commonData
CommonData;
HMapFile:THandle;
procedure MapCommonData;
var FirstCall: Boolean;
begin
HMapFile:=OpenFileMapping(FILE_MAP_WRITE, False, 'sjhdfasdfasdfasd');
FirstCall:=(HMapFile = 0);
if FirstCall then
begin
HMapFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TCommonData),'sjhdfasdfasdfasd');
end;
CommonData:= MapViewOfFile(HMapFile, FILE_MAP_WRITE, 0, 0, 0);
if FirstCall then FillChar(CommonData^, SizeOf(TCommonData), 0);
CommonData^.FileName:= 'c:/CPhrase.TBL'#0;
end;
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if ((lParam and _KeyPressMask) = 0) and
(GetKeyState(vk_Control) < 0) and (wParam = Ord('B')) then
begin
Result := 1;
commonData^.FileHandle:= FileOpen(commonData^.FileName, fmOpenRead + fmShareDenyNone);
if commonData^.FileHandle < 0 then
MessageBox(0, 'End of Init. Openbb file Error' ,'DEBUG',0)
else
MessageBox(0, 'OK' ,'DEBUG',0);
FileClose(commonData^.FileHandle);
end;
end;
function EnableHotKeyHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookHandler,
HInstance,
0);
Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then DisableHotKeyHook;
ExitProc := procSaveExit;
end;
exports
EnableHotKeyHook,
DisableHotKeyHook;
begin
MapCommonData;
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.