unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
private
public
end;
PDevBroadcastHdr = ^TDevBroadcastHdr;
TDevBroadcastHdr =
Packed Record
dbcd_size : DWORD;
dbcd_devicetype : DWORD;
dbcd_reserved : DWORD;
end;
var hWndProcHook: HHOOK=0;
Form1: TForm1;
implementation
{$R *.dfm}
function CallWndProc(Code: integer; wParam: WPARAM; lParam: LPARAM): LResult;
stdcall;
var pCWP: PCWPSTRUCT;
begin
if Code >= 0
then
begin
Result := CallNextHookEx(hWndProcHook, Code, wParam, lParam);
end else
begin
Result := CallNextHookEx(0, Code, wParam, lParam);
EXIT;
end;
pCWP := pointer(lParam);
if pCWP^.
message = WM_DEVICECHANGE
then
begin
case pCWP^.wParam
of
$8000 :
if PDevBroadcastHdr(pCWP^.lParam)^.dbcd_devicetype = $00000002
then
begin
//-- Fenster der Hostapplication suchen und an dieses posten,
//-- oder Host Application speichert Handle vor dem Laden der DLL in
//-- shared memory oder Registry.
//-- Elegant wäre hier eine IPC mit einem PIPE Client oder Socket Client
end;
$8004 :
if PDevBroadcastHdr(pCWP^.lParam)^.dbcd_devicetype = $00000002
then
begin
//-- Ergo wie oben
end;
else
pCWP.
message := WM_NULL;
//-- Message vernichten
end;
end;
end;
function CreateHook: integer;
stdcall;
begin
hWndProcHook := SetWindowsHookEx(WH_CALLWNDPROC, @CallWndProc, hInstance, 0);
result := hWndProcHook;
end;
function DeleteHook: integer;
stdcall;
begin
result := Ord(UnhookWindowsHookEx(hWndProcHook));
end;
function StartUp:integer;
stdcall;
begin
result:=0;
end;
procedure DLLEntryPoint(dwReason: DWORD);
begin
case dwReason
of
DLL_PROCESS_ATTACH: ;
//-- IPC init
DLL_PROCESS_DETACH: ;
//-- IPC release
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var HookResult : integer;
begin
IsMultithread := true;
DisableThreadLibraryCalls(hInstance);
DllProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
HookResult:=CreateHook;
end;
end.