N
newzhang2009
Unregistered / Unconfirmed
GUEST, unregistred user!
现在正开发一个项目,有一个dll注入其他程序,主程序(exe)如何与dll通信,我考虑用MessageHook,但总也试不成,请高手指导。
**************************************************************
dll程序中:
const
wm_mymessage=wm_user+456;
var
HookHandle:HHook;
//钩子回调函数
function TestHookProc(Code:Integer;WParam:WParam;Msg:LongInt):LRESULT;stdcall;
begin
if Code=HC_ACTION then
begin
if PMsg(Msg)^.message=wm_mymessage then
begin
showmessage('ok');
end;
end;
Result := CallNextHookEx(HookHandle, Code, WParam, Longint(@Msg));
end;//钩子回调函数
procedure EntryPoint(Reason: dword);
begin
HookHandle:=SetWindowsHookEx(WH_CALLWNDPROC,@TestHookProc,0,GetCurrentThreadId);
end;
begin
DLLProc := @EntryPoint;
EntryPoint(DLL_PROCESS_ATTACH);
end;
**************************************************************************
主程序(exe):
const
wm_mymessage=wm_user+456;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Inject;
var
ProcessHandle: THandle;
Process32: TProcessEntry32;
ProcessSnapshot: THandle;
begin
ProcessSnapshot := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Process32.dwSize := SizeOf(TProcessEntry32);
Process32First(ProcessSnapshot, Process32);
repeat
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, Process32.th32ProcessID);
if ProcessHandle <> 0 then
begin
if Process32.th32ProcessID=776 then //钩住指定ID的程序,测试用
begin
InjectLibrary(ProcessHandle, ExtractFilePath(ParamStr(0)) + 'hook.dll');
end;
end;
CloseHandle(ProcessHandle);
until not (Process32Next(ProcessSnapshot, Process32));
CloseHandle(ProcessSnapshot);
end;
procedure unInject;
var
ProcessHandle: THandle;
Process32: TProcessEntry32;
ProcessSnapshot: THandle;
begin
ProcessSnapshot := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Process32.dwSize := SizeOf(TProcessEntry32);
Process32First(ProcessSnapshot, Process32);
repeat
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, Process32.th32ProcessID);
if ProcessHandle <> 0 then
begin
if Process32.th32ProcessID=776 then //钩住指定ID的程序,测试用
begin
SendMessage(ProcessHandle, wm_mymessage, 0, 0);
end;
end;
CloseHandle(ProcessHandle);
until not (Process32Next(ProcessSnapshot, Process32));
CloseHandle(ProcessSnapshot);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Inject;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
unInject;
end;
**************************************************************
dll程序中:
const
wm_mymessage=wm_user+456;
var
HookHandle:HHook;
//钩子回调函数
function TestHookProc(Code:Integer;WParam:WParam;Msg:LongInt):LRESULT;stdcall;
begin
if Code=HC_ACTION then
begin
if PMsg(Msg)^.message=wm_mymessage then
begin
showmessage('ok');
end;
end;
Result := CallNextHookEx(HookHandle, Code, WParam, Longint(@Msg));
end;//钩子回调函数
procedure EntryPoint(Reason: dword);
begin
HookHandle:=SetWindowsHookEx(WH_CALLWNDPROC,@TestHookProc,0,GetCurrentThreadId);
end;
begin
DLLProc := @EntryPoint;
EntryPoint(DLL_PROCESS_ATTACH);
end;
**************************************************************************
主程序(exe):
const
wm_mymessage=wm_user+456;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Inject;
var
ProcessHandle: THandle;
Process32: TProcessEntry32;
ProcessSnapshot: THandle;
begin
ProcessSnapshot := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Process32.dwSize := SizeOf(TProcessEntry32);
Process32First(ProcessSnapshot, Process32);
repeat
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, Process32.th32ProcessID);
if ProcessHandle <> 0 then
begin
if Process32.th32ProcessID=776 then //钩住指定ID的程序,测试用
begin
InjectLibrary(ProcessHandle, ExtractFilePath(ParamStr(0)) + 'hook.dll');
end;
end;
CloseHandle(ProcessHandle);
until not (Process32Next(ProcessSnapshot, Process32));
CloseHandle(ProcessSnapshot);
end;
procedure unInject;
var
ProcessHandle: THandle;
Process32: TProcessEntry32;
ProcessSnapshot: THandle;
begin
ProcessSnapshot := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Process32.dwSize := SizeOf(TProcessEntry32);
Process32First(ProcessSnapshot, Process32);
repeat
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, Process32.th32ProcessID);
if ProcessHandle <> 0 then
begin
if Process32.th32ProcessID=776 then //钩住指定ID的程序,测试用
begin
SendMessage(ProcessHandle, wm_mymessage, 0, 0);
end;
end;
CloseHandle(ProcessHandle);
until not (Process32Next(ProcessSnapshot, Process32));
CloseHandle(ProcessSnapshot);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Inject;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
unInject;
end;