请问我的程序错在哪里?为什么只能保存20字节(100分)

  • 主题发起人 主题发起人 ajim
  • 开始时间 开始时间
A

ajim

Unregistered / Unconfirmed
GUEST, unregistred user!
function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
AssignFile(f,'c:/1.txt');
Rewrite(f);
recOK:=1;
Result:=0;
if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then recOK:=0
else if iCode = HC_SYSMODALOFF then recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then begin

Write(f,pEventMSG(lParam)^);
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog>=1000 then begin
UnHookWindowsHookEx(hHook);
end;
end;
system.closefile(f);
end;
 
我試過了,沒有問題呀.應該沒有問題.
調試的時候,你設個斷點,看看lparam的內容是什麽吧.

 
但是在文件里保存只能是20字节亚。坑定有错的
 
用文件流来试试看,还有你的pEventMSG类型是?
 
pEventMSG
windows hook 的类型之一
 
我再帮你试试,晚上再给你答案.
 
很感谢您,不管成不成功都谢谢了。delphi是我起步的地方。希望大家都能像你一样在别人起步的时候帮一吧
 
EVENTMSG = packed record
message: UINT;
paramL: UINT;
paramH: UINT;
time: DWORD;
hwnd: HWND;
end;
一个EVENT变量的大小为,4*5=20个字节,是对的呀?本来就只有这么小.
Write(f,pEventMSG(lParam)^);//你的程序中只写入了一个EVENTMSG类型的变量当
然只有20个字节了.
 
还有呀!
Rewrite(f);你在这儿重建了文件,删除了以前的所有内容.
所以永远都是只有20个字节.

根据你的意思,是不是想记录1000个消息,那你应该用reset(f)
 
你也太谦虚了吧,才起步就来用HOOK?呵呵!
交个朋友吧.

Email:zengxiaoxun@hotmail.com
QQ:8468307
 
真的是呀。那么怎么样记录n个呢 我好像试过了,不行呀。你看我做的切入点是不是错了
unit playback;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
SaveDialog1: TSaveDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
f:file of EVENTMSG;
Form1: TForm1;
EventArr:array[0..1000]of EVENTMSG;
EventLog,PlayLog,hHook,hPlay,recOK,canPlay:Integer;
bDelay:Bool;
implementation
{$R *.DFM}


function PlayProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
canPlay:=1;
Result:=0;
if iCode < 0 then //必须将消息传递到钩子链的下一个处理程序
Result := CallNextHookEx(hPlay,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then canPlay:=0
else if iCode = HC_SYSMODALOFF then canPlay:=1
else if ((canPlay =1 )and(iCode=HC_GETNEXT)) then begin
if bDelay then begin
bDelay:=False;
Result:=50;
end;
pEventMSG(lParam)^:=EventArr[PlayLog];
end
else if ((canPlay = 1)and(iCode = HC_SKIP))then begin
bDelay := True;
PlayLog:=PlayLog+1;
end;
if PlayLog>=EventLog then begin
UNHookWindowsHookEx(hPlay);
end;
end;
function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
AssignFile(f,'c:/1.txt');
Rewrite(f);
recOK:=1;
Result:=0;
if iCode < 0 then
Result := CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then recOK:=0
else if iCode = HC_SYSMODALOFF then recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then begin

Write(f,pEventMSG(lParam)^);
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog>=1000 then begin
UnHookWindowsHookEx(hHook);
end;
end;
system.closefile(f);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='纪录';
Button2.Caption:='停止';
Button3.Caption:='回放';
Button2.Enabled:=False;
Button3.Enabled:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EventLog:=0;
//建立键盘、鼠标操作消息纪录链
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnHookWindowsHookEx(hHook);
hHook:=0;
Button1.Enabled:=True;
Button2.Enabled:=False;
Button3.Enabled:=True;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
PlayLog:=0;
//建立键盘鼠标操作消息纪录回放链
hPlay:=SetwindowsHookEx(WH_JOURNALPLAYBACK,PlayProc,
HInstance,0);
Button3.Enabled:=False;
end;
end.
 
还有reset(f)也是一样的:(
 
不是你那样纪录的,随便给你改了改。
var
hJNextHook: hHook;
Form1: TForm1;

EventArr:array[0..1000]of EVENTMSG;
EventLog:Integer;


PlayLog:Integer;
hHook,hPlay:Integer;
recOK:Integer;


canPlay:Integer;
bDelay:Bool;
f : file of EVENTMSG;
KIFile : THandle;
isRecording, isPlaying, CanRecord: Boolean;
implementation

{$R *.DFM}
function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
begin
recOK:=1;
Result:=0;
if iCode < 0 then
Result := CallNextHookEx(hJNextHook,iCode,wParam,lParam)
else if iCode = HC_SYSMODALON then
recOK:=0
else if iCode = HC_SYSMODALOFF then
recOK:=1
else if ((recOK>0) and (iCode = HC_ACTION)) then begin
Write(f,pEventMSG(lParam)^);
EventArr[EventLog]:=pEventMSG(lParam)^;
EventLog:=EventLog+1;
if EventLog>=1000 then begin
UnHookWindowsHookEx(hHook);
end;
end;
end;

procedure RecordJournal(FileName: string);
begin
if hJNextHook <> 0 then exit;
hJNextHook := SetWindowsHookEx(WH_JOURNALRECORD,
HookProc, Hinstance, 0);
if hJNextHook <> 0 then
begin
AssignFile(f, FileName);
ReWrite(f);
isRecording := true;
CanRecord := true;
end ;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
RecordJournal('c:/1.txt');
EventLog:=0;
//hHook:=SetwindowsHookEx(WH_JOURNALRECORD,@HookProc,HInstance,0);
Button2.Enabled:=True;
Button1.Enabled:=False;
end;
行不行?
 
谢谢您了我用用看
 
哈哈。可以这样:
reset(f);
seek(f,filesize(f));
就可以了。
 
to:BeginDelphi,
好像不可以呀 我早试过了。你用过了吗?
 
给你段原码:
program Writefile;
{$i-}//没有文件不然会异常退出
uses windows;
var F:file of eventmsg;
msg:EventMsg;
I:Integer;
begin
for I:=1 to 30 do
begin
assign(f,'c:/a.dat');
reset(f);//这个try不到,只能{$I-}
if IOResult>0 then rewrite(f);
seek(f,filesize(f));
write(f,msg);
close(f);
end;
end.

生成的文件有600字节,可以呀。你可能没有用对。
 
还没有搞定么?你留个email,我发给你一个很好的纪录日志钩子的例子
 
接受答案了.
 
后退
顶部