在HOOK中打开文件(100分)

  • 主题发起人 主题发起人 xddl
  • 开始时间 开始时间
X

xddl

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个键盘HOOK,希望按动某一热键后可以打开指定的文件,
但是只有当此HOOK在前景时可以正常打开,HOOK不在前景时就打不开,
是何原因??
谢谢指点
 
在别的进程空间里面,你可以发一个消息回自己的进程处理
 
Pipi,请做进一步解释,别的进程不可能由我控制,我可以把程序放在下面:


function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
sPhraseFileName = 'CPhrase.TBL';
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;
hPhraseFile := FileOpen(sPhraseFileName, fmOpenRead + fmShareDenyNone);
if hPhraseFile < 0 then
MessageBox(0, 'End of Init. Openbb file Error' ,'DEBUG',0)
else
MessageBox(0, 'OK' ,'DEBUG',0);
FileClose(hPhraseFile);
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;
 
正确得程序应该为下面得程序。
原理见我和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:PCommonData;
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.
 
谢谢,沈前卫,我实验了你的办法,很好。
再次表示感谢。
 
后退
顶部