Q
qi_jianzhou
Unregistered / Unconfirmed
GUEST, unregistred user!
程序很简单
就是其它的程序中输入什么,我这里的 Memo 中就显示他的输入(当然不是汉字了 ^_^)
我用自定义消息的方式给主程序发消息,让其显示,击键保存在内存映象文件呢,主程序读内存映象来显示数据
==================== dll =========================
library KDll;
uses
SysUtils,
windows,
dialogs,
messages,
Classes;
const wm_k = wm_user+10;
type
PKeyInfo = ^TKeyInfo;
TKeyInfo = record
k:word;
end;
var
HH:HHOOK;
KeyInfoKeyInfo;
mh:THandle;
function KeyHookProc(nCode:integer;wparam,lparam:integer):integer;stdcall;
var
h:THandle;
begin
result := 0;
if nCode<0 then
windows.CallNextHookEx(hh,nCode,wparam,lparam)
else
begin
if ncode = windows.HC_Action then
begin
h := FindWindow(nil,pchar('KeyHook'));
// 内存映象
mh := windows.OpenFileMapping(windows.PAGE_READWRITE,false,pchar('KeyTest1'));
keyInfo := windows.MapViewOfFile(mh,windows.FILE_MAP_ALL_ACCESS,0,0,0);
keyInfo^.k := wparam; // 赋值 --> 这里没有赋值成功, 不知为什么
windows.UnmapViewOfFile(keyInfo);
closeHandle(mh);
sendmessage(h,wm_k,wparam,lparam); // 发送消息
end;
end;
end;
procedure HookOn;stdcall;
begin
hh := windows.SetWindowsHookEx(windows.WH_KEYBOARD,@keyHookProc,Hinstance,0);
end;
procedure HookOff;stdcall;
begin
windows.UnhookWindowsHookEx(hh);
end;
exports
HookOn,HookOff;
begin
end.
=================== 主程序 ====================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
// 自定认一个消息
const wm_k = wm_user+10;
type
// 自定义结构
PKeyInfo = ^TKeyInfo;
TKeyInfo = record
k:word;
end;
TFormKeyHook = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure WMK(var msg:Tmessage);message wm_K;
public
end;
procedure HookOn;stdcall;external 'KDll.dll';
procedure HookOff;stdcall;external 'KDll.dll';
var
FormKeyHook: TFormKeyHook;
Mh:THandle;
KeyInfoKeyinfo;
implementation
{$R *.dfm}
procedure TFormKeyHook.Button1Click(Sender: TObject);
begin
HookOn;
mh := windows.CreateFileMapping($ffffffff,nil,
windows.PAGE_READWRITE,0,sizeof(TKeyInfo),pchar('KeyTest1'));
KeyInfo:= windows.MapViewOfFile(mh,windows.FILE_MAP_ALL_ACCESS,0,0,0);
//keyInfo^.k := mh;
end;
procedure TFormKeyHook.Button2Click(Sender: TObject);
begin
HookOff;
windows.UnmapViewOfFile(keyInfo);
closeHandle(mh);
end;
procedure TFormKeyHook.WMK(var msg:Tmessage);
begin
if msg.Msg = wm_k then
memo1.Lines.Add(char(Keyinfo^.k)); // 显示按键
end;
end.
==================================================
当我用 内存映象时,就不能 hook 了,但一去掉内存映象
直接
procedure TFormKeyHook.WMK(var msg:Tmessage);
begin
if msg.Msg = wm_k then
memo1.Lines.Add('aaa') ; // 这样显示,就没有问题
end;
不明白为什么用了内存映象就会不能 Hook ,请高手指点
万分感激
就是其它的程序中输入什么,我这里的 Memo 中就显示他的输入(当然不是汉字了 ^_^)
我用自定义消息的方式给主程序发消息,让其显示,击键保存在内存映象文件呢,主程序读内存映象来显示数据
==================== dll =========================
library KDll;
uses
SysUtils,
windows,
dialogs,
messages,
Classes;
const wm_k = wm_user+10;
type
PKeyInfo = ^TKeyInfo;
TKeyInfo = record
k:word;
end;
var
HH:HHOOK;
KeyInfoKeyInfo;
mh:THandle;
function KeyHookProc(nCode:integer;wparam,lparam:integer):integer;stdcall;
var
h:THandle;
begin
result := 0;
if nCode<0 then
windows.CallNextHookEx(hh,nCode,wparam,lparam)
else
begin
if ncode = windows.HC_Action then
begin
h := FindWindow(nil,pchar('KeyHook'));
// 内存映象
mh := windows.OpenFileMapping(windows.PAGE_READWRITE,false,pchar('KeyTest1'));
keyInfo := windows.MapViewOfFile(mh,windows.FILE_MAP_ALL_ACCESS,0,0,0);
keyInfo^.k := wparam; // 赋值 --> 这里没有赋值成功, 不知为什么
windows.UnmapViewOfFile(keyInfo);
closeHandle(mh);
sendmessage(h,wm_k,wparam,lparam); // 发送消息
end;
end;
end;
procedure HookOn;stdcall;
begin
hh := windows.SetWindowsHookEx(windows.WH_KEYBOARD,@keyHookProc,Hinstance,0);
end;
procedure HookOff;stdcall;
begin
windows.UnhookWindowsHookEx(hh);
end;
exports
HookOn,HookOff;
begin
end.
=================== 主程序 ====================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
// 自定认一个消息
const wm_k = wm_user+10;
type
// 自定义结构
PKeyInfo = ^TKeyInfo;
TKeyInfo = record
k:word;
end;
TFormKeyHook = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure WMK(var msg:Tmessage);message wm_K;
public
end;
procedure HookOn;stdcall;external 'KDll.dll';
procedure HookOff;stdcall;external 'KDll.dll';
var
FormKeyHook: TFormKeyHook;
Mh:THandle;
KeyInfoKeyinfo;
implementation
{$R *.dfm}
procedure TFormKeyHook.Button1Click(Sender: TObject);
begin
HookOn;
mh := windows.CreateFileMapping($ffffffff,nil,
windows.PAGE_READWRITE,0,sizeof(TKeyInfo),pchar('KeyTest1'));
KeyInfo:= windows.MapViewOfFile(mh,windows.FILE_MAP_ALL_ACCESS,0,0,0);
//keyInfo^.k := mh;
end;
procedure TFormKeyHook.Button2Click(Sender: TObject);
begin
HookOff;
windows.UnmapViewOfFile(keyInfo);
closeHandle(mh);
end;
procedure TFormKeyHook.WMK(var msg:Tmessage);
begin
if msg.Msg = wm_k then
memo1.Lines.Add(char(Keyinfo^.k)); // 显示按键
end;
end.
==================================================
当我用 内存映象时,就不能 hook 了,但一去掉内存映象
直接
procedure TFormKeyHook.WMK(var msg:Tmessage);
begin
if msg.Msg = wm_k then
memo1.Lines.Add('aaa') ; // 这样显示,就没有问题
end;
不明白为什么用了内存映象就会不能 Hook ,请高手指点
万分感激