附钩子如下:function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export; var hWnd: Thandle; nLen: Integer; Buff: array[0..4095] of Char; filestream:TFilestream; begin if nCode<0 then begin Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam); Exit; end; if nCode=HCBT_DESTROYWND then begin hWnd := Thandle(wparam); GetClassName(hWnd, @Buff, 128); if CompareStr(Buff,'Notepad')=0 then begin hWnd := FindWindowEx(hWnd, 0, @Buff,nil ); nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff)); // 这儿保存Buff里的文字??此处如何做,下面的代码行不通 filestream:=TFilestream.Create('E:\b.txt',fmcreate); try filestream.WriteBuffer(buff,nlen); finally filestream.Free; end; end; end; ncode:=-1; Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam); end;
写文件byte用 blockwrite 文本用 writevar i:Integer; PackFile:File; FileName,JmString:String; TPByte:array[1..2] of Byte; begin AssignFile(PackFile,saveDir+FileName); Rewrite(PackFile,1); tryfor i:=1 to 2 do begin TPByte[i]:=Byte(TP[i]); blockwrite(PackFile,TPByte[i],1); end;finally closeFile(PackFile); end; end;
是在钩子中实现,初学,望赐教附钩子如下: function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export; var hWnd: Thandle; nLen: Integer; Buff: array[0..4095] of Char; filestream:TFilestream; begin if nCode <0 then begin ....... end; if nCode=HCBT_DESTROYWND then begin hWnd := Thandle(wparam); GetClassName(hWnd, @Buff, 128); if CompareStr(Buff,'Notepad')=0 then begin hWnd := FindWindowEx(hWnd, 0, @Buff,nil ); nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff)); // 这儿保存Buff里的文字??此处如何做?????? end; end;
end;
那位大哥大姐帮我看看吧!我照下面编写也行不通(在????处) function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export; var hWnd: Thandle; nLen: Integer; Buff: array[0..4095] of Char; PackFile:textFile; i:integer; begin if nCode <0 then begin Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam); Exit; end; if nCode=HCBT_DESTROYWND then begin hWnd := Thandle(wparam); GetClassName(hWnd, @Buff, 128); if CompareStr(Buff,'Notepad')=0 then begin hWnd := FindWindowEx(hWnd, 0, @Buff,nil ); nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff)); // 这儿保存Buff里的文字??此处如何做,下面的代码也行不通 begin AssignFile(PackFile,'E:\b.txt'); Rewrite(PackFile); try for i:=0 to nlen-1 do begin write(PackFile,buff[i]); end; finally closeFile(PackFile); end; end; end; end; ncode:=-1; Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam); end;
MEMO有一个保存内容到指定文本里。
这个比较简单的保存
var
hWnd: Thandle;
nLen: Integer;
Buff: array[0..4095] of Char;
filestream:TFilestream;
begin
if nCode<0 then
begin
Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
Exit;
end; if nCode=HCBT_DESTROYWND then
begin
hWnd := Thandle(wparam);
GetClassName(hWnd, @Buff, 128);
if CompareStr(Buff,'Notepad')=0 then
begin
hWnd := FindWindowEx(hWnd, 0, @Buff,nil );
nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff));
// 这儿保存Buff里的文字??此处如何做,下面的代码行不通
filestream:=TFilestream.Create('E:\b.txt',fmcreate);
try
filestream.WriteBuffer(buff,nlen);
finally
filestream.Free;
end;
end;
end;
ncode:=-1;
Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
end;
PackFile:File;
FileName,JmString:String;
TPByte:array[1..2] of Byte;
begin
AssignFile(PackFile,saveDir+FileName);
Rewrite(PackFile,1);
tryfor i:=1 to 2 do begin
TPByte[i]:=Byte(TP[i]);
blockwrite(PackFile,TPByte[i],1);
end;finally
closeFile(PackFile);
end;
end;
function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export;
var
hWnd: Thandle;
nLen: Integer;
Buff: array[0..4095] of Char;
filestream:TFilestream;
begin
if nCode <0 then
begin
.......
end;
if nCode=HCBT_DESTROYWND then
begin
hWnd := Thandle(wparam);
GetClassName(hWnd, @Buff, 128);
if CompareStr(Buff,'Notepad')=0 then
begin
hWnd := FindWindowEx(hWnd, 0, @Buff,nil );
nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff));
// 这儿保存Buff里的文字??此处如何做??????
end;
end;
end;
function CBTProc(nCode:integer; wParam: WParam; lParam: lparam): lresult;stdcall;export;
var
hWnd: Thandle;
nLen: Integer;
Buff: array[0..4095] of Char;
PackFile:textFile;
i:integer;
begin
if nCode <0 then
begin
Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
Exit;
end; if nCode=HCBT_DESTROYWND then
begin
hWnd := Thandle(wparam);
GetClassName(hWnd, @Buff, 128);
if CompareStr(Buff,'Notepad')=0 then
begin
hWnd := FindWindowEx(hWnd, 0, @Buff,nil );
nLen := GetWindowText(hWnd, @Buff, SizeOf(Buff));
// 这儿保存Buff里的文字??此处如何做,下面的代码也行不通
begin
AssignFile(PackFile,'E:\b.txt');
Rewrite(PackFile);
try
for i:=0 to nlen-1 do begin
write(PackFile,buff[i]);
end;
finally
closeFile(PackFile);
end;
end;
end;
end;
ncode:=-1;
Result := CallNextHookEx(hOldCbthook, nCode, wParam, lParam);
end;