高分求助,delphi转bcb (hook相关) - C++ Builder / Windows SDK/API
unit UHook2;
interface
uses Windows, Messages, SysUtils, Controls, ComCtrls, DBGrids, CommCtrl;
procedure InstallHook; stdcall;
procedure UninstallHook; stdcall;
function GetHookedText: PChar; stdcall;
implementation
uses UShare;
var
ControlAtom: TAtom;
ControlAtomString: string;
RM_GetObjectInstance: DWORD; // registered window message
function FindControl(Handle: HWND): TWinControl;
var
OwningProcess: DWORD;
begin
Result := nil;
if (Handle <> 0) and (GetWindowThreadProcessId(Handle, OwningProcess) <> 0) and
(OwningProcess = GetCurrentProcessId) then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result := Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
else
Result := Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0));
end;
end;
function MsgWndProc(HWND: HWND; Msg: UINT; WParam: WParam; LParam: LParam): LRESULT; stdcall;
begin
case Msg of
CM_HOOKTEXT: Exit;
end;
Result := DefWindowProc(HWND, Msg, WParam, LParam);
end;
function GetMsgProc(Code: UINT; WParam: WParam; LParam: LParam): LRESULT; stdcall;
begin
if Code = HC_ACTION then
begin
end;
Result := CallNextHookEx(P^.hkMsg, Code, WParam, LParam);
end;
procedure InstallHook; stdcall;
var
dg: TDBGrid;
begin
if P^.hkMsg = 0 then
P^.hkMsg := SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance, 0);
P^.DestWnd := FindWindow('TABCForm', 'ABC');
P^.DestWnd := FindWindowEx(P^.DestWnd, 0, 'TDBGrid', nil);
P^.Text[0] := #0;
if P^.DestWnd <> 0 then
begin
dg := TDBGrid(FindControl(P^.DestWnd));
if dg <> nil then
begin
StrPLCopy(P^.Text, IntToStr(P^.DestWnd), 1024)
end
else
StrPLCopy(P^.Text, DateTimeToStr(Now), 1024);
end;
end;
procedure UninstallHook; stdcall;
begin
if P^.hkMsg <> 0 then
begin
UnhookWindowsHookEx(P^.hkMsg);
P^.hkMsg := 0;
end;
end;
function GetHookedText: PChar; stdcall;
begin
Result := P^.Text;
end;
var
DoClear: Boolean;
DestPID: DWORD;
DestProcess: Boolean = False;
UtilWindowClass: TWndClass = (
Style: 0;
lpfnWndProc: @MsgWndProc;
cbClsExtra: 0;
cbWndExtra: 0;
HInstance: 0;
hIcon: 0;
hCursor: 0;
hbrBackground: 0;
lpszMenuName: nil;
lpszClassName: 'THookcmMsgWindow');
initialization
hMapFile := OpenFileMapping(FILE_MAP_WRITE, False, cMapFileName);
DoClear := hMapFile = 0;
if hMapFile = 0 then
hMapFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
0, SizeOf(TShareData), cMapFileName);
P := MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, 0);
if DoClear then FillChar(P^, SizeOf(TShareData), 0);
ControlAtomString := Format('ControlOfs%.8X%.8X', [GetModuleHandle(nil), GetCurrentThreadId]);
ControlAtom := GlobalAddAtom(PChar(ControlAtomString));
RM_GetObjectInstance := RegisterWindowMessage(PChar(ControlAtomString));
if P^.DestWnd <> 0 then
begin
GetWindowThreadProcessId(P^.DestWnd, DestPID);
if DestPID = GetCurrentProcessId then
begin
DestProcess := True;
UtilWindowClass.HInstance := HInstance;
RegisterClass(UtilWindowClass);
P^.MsgWnd := CreateWindowEx(
WS_EX_TOOLWINDOW, // extended window style
UtilWindowClass.lpszClassName, // pointer to registered class name
UtilWindowClass.lpszClassName, // pointer to window name
WS_POPUP, // window style
0, // horizontal position of window
0, // vertical position of window
0, // window width
0, // window height
0, // handle to parent or owner window
0, // handle to menu, or child-window identifier
HInstance, // handle to application instance
nil); // pointer to window-creation data
//PostMessage(P^.HostWnd, CM_MSGWNDCREATED, P^.MsgWnd, 1);
end;
end;
finalization
if DestProcess then
begin
DestroyWindow(P^.MsgWnd);
//PostMessage(P^.HostWnd, CM_MSGWNDCREATED, P^.MsgWnd, 0);
end;
GlobalDeleteAtom(ControlAtom);
ControlAtomString := '';
UnmapViewOfFile(P);
CloseHandle(hMapFile);
end.
[解决办法]
[解决办法]
来学习下。
[解决办法]
来学习了
[解决办法]
不用转,直接添加到BCB的工程文件中就行
[解决办法]
关注
强烈关注
[解决办法]
支持妖哥来学习了 ..
[解决办法]
頂妖哥,學習
[解决办法]
妖哥都没放心上,说明这问题对妖哥来说小菜一碟
[解决办法]
邮件已发.
通过修正代码中一个小BUG, 在Win7下也可以正常读取了.
目前实现TestD.exe通过HookD.dll读取TargetD.exe的内容(Delphi)
TestC.exe通过HookC.dll读取TargetC.exe的内容(CBuilder)
由于VCL版本的不同以及CBuilder的Delphi的一些差别, 交差读取方面还存在问题. 尽管有资料说通过TObjectInstance结构, Delphi可以读取CBuilder生成的可执行文件中的StringGrid, 但是问题还是挺多. 需要持续研究.