-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgserver.pas
172 lines (144 loc) · 3.94 KB
/
msgserver.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
unit msgserver;
interface
uses
Windows, Messages, Classes;
type
PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
DEV_BROADCAST_DEVICEINTERFACE = record
dbcc_size: DWORD;
dbcc_devicetype: DWORD;
dbcc_reserved: DWORD;
dbcc_classguid: TGUID;
dbcc_name: short;
end;
const
GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class
type
TMsgserverEvent = procedure(var Msg: TMessage) of object;
TUsbNotifyProc = procedure(Sender: TObject) of object;
TMsgDataEvent = procedure(var Msg: TWMCopyData) of object;
THotkeyEvent = procedure(var key: word) of object;
Twinmsg = packed record
msg: Cardinal;
wparam: longint;
lparam: LongInt;
end;
TMsgServer = class
public
handle: hwnd;
WindowMsg: Cardinal;
OnMessage: TMsgserverEvent;
OnData: TMsgDataEvent;
OnHotkey: THotkeyEvent;
active: Boolean;
protected
procedure WMDeviceChange(var Msg: TMessage); dynamic;
private
NotifyHandle: Pointer;
FOnUSBArrival: TUsbNotifyProc;
FOnUSBRemove: TUsbNotifyProc;
procedure WndMethod(var Msg: TMessage);
function RegisterDevNotification: Boolean;
public
constructor Create;
destructor free;
published
property OnDeviceArrival: TUsbNotifyProc read FOnUSBArrival write FOnUSBArrival;
property OnDeviceRemove: TUsbNotifyProc read FOnUSBRemove write FOnUSBRemove;
end;
implementation
uses
entrypoint;
function TMsgServer.RegisterDevNotification: Boolean;
var
dbi: DEV_BROADCAST_DEVICEINTERFACE;
Size: Integer;
begin
Result := False;
try
Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
ZeroMemory(@dbi, Size);
dbi.dbcc_size := Size;
dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved := 0;
dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name := 0;
NotifyHandle := RegisterDeviceNotification(handle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);
finally
end;
if Assigned(NotifyHandle) then
Result := True;
end;
constructor TMsgServer.Create;
begin
handle := AllocateHWnd(WndMethod);
RegisterDevNotification;
active := True;
end;
destructor TMsgServer.Free;
begin
active := false;
if Assigned(NotifyHandle) then
UnregisterDeviceNotification(NotifyHandle);
DeallocateHWnd(handle);
end;
procedure TMsgServer.WMDeviceChange(var Msg: TMessage);
var
// devType: Integer;
Datos: PDevBroadcastHdr;
begin
if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
begin
Datos := PDevBroadcastHdr(Msg.lParam);
// devType := Datos^.dbch_devicetype;
// if devType = DBT_DEVTYP_DEVICEINTERFACE then
// begin
if Msg.wParam = DBT_DEVICEARRIVAL then
begin
if Assigned(FOnUSBArrival) then
FOnUSBArrival(Self);
end
else
begin
if Assigned(FOnUSBRemove) then
FOnUSBRemove(Self);
end;
// end;
end;
end;
procedure TMsgServer.WndMethod(var Msg: TMessage);
begin
Msg.Result := DefWindowProc(handle, Msg.Msg, Msg.wParam, Msg.lParam);
if not active then
exit;
case Msg.msg of
WM_HOTKEY:
begin
if assigned(OnHotkey) then
OnHotkey(Msg.WParamLo);
end;
WM_COPYDATA:
begin
if Assigned(OnData) then
OnData(TWMcopydata(Msg));
exit;
end;
WM_DEVICECHANGE:
begin
WMDeviceChange(Msg);
exit;
end;
end;
if Assigned(OnMessage) then
OnMessage(Msg);
end;
end.