library Project1;
{ 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
ExceptionLog,
Windows,
SysUtils,
Messages,
Classes,
Dialogs,
Controls,
ComCtrls;
{$R *.res}
const
MAP_FILE_NAME = 'MAP_FILE_ZHAO';
type
PShareMem = ^TShareMem;
TShareMem = packed record
HookHandle: HHOOK;
end;
var
hMapHandle: Cardinal;
pShareMemHook: PShareMem;
bFirst: Boolean = True;
function GetMsgProc(nCode: Integer; wParam, lParam: Integer): Integer; stdcall;
var
hFormHandle,hControlbarHandle,hToolButtonHandle:THandle;
twcFormControl:TWinControl;
i:Integer;
begin
if bFirst then
begin
bFirst := False;
//添加自己的代码
end;
Result := CallNextHookEx(pShareMemHook^.HookHandle, nCode, wParam, lParam);
end;
function SetHook(ThreadId: Cardinal): Boolean; stdcall;
begin
Result := False;
if ThreadId <> 0 then
begin
if pShareMemHook <> nil then
begin
pShareMemHook^.HookHandle := SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc,
Hinstance, ThreadId);
if pShareMemHook^.HookHandle <> 0 then
begin
Result := True;
PostThreadMessage(ThreadId, WM_NULL, 0, 0);
end;
end;
end
else
begin
Result := UnhookWindowsHookEx(pShareMemHook^.HookHandle);
pShareMemHook^.HookHandle := 0;
end;
end;
procedure LibraryProc(Reason: Integer);
begin
case Reason of
DLL_PROCESS_ATTACH:
begin
hMapHandle := CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE
or SEC_COMMIT, 0, SizeOf(TShareMem), MAP_FILE_NAME);
if hMapHandle <> 0 then
begin
pShareMemHook := MapViewOfFile(hMapHandle, FILE_MAP_WRITE, 0, 0,
SizeOf(TShareMem));
end
end;
DLL_PROCESS_DETACH:
begin
if pShareMemHook <> nil then
UnmapViewOfFile(pShareMemHook);
if hMapHandle <> 0 then
CloseHandle(hMapHandle);
end;
DLL_THREAD_ATTACH: ;
DLL_THREAD_DETACH: ;
end;
end;
exports
SetHook;
begin
DLLProc := @LibraryProc;
LibraryProc(DLL_PROCESS_ATTACH);
end.