Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uFlashEvents;
- (*
- Flash Events
- Author: HEX0x29A
- Date : 21.10.2015
- *)
- interface
- type
- TOnFlashEvent = procedure(Drive: WideChar);
- procedure SetOnFlashInsertEvent(Event: TOnFlashEvent);
- procedure SetOnFlashRemoveEvent(Event: TOnFlashEvent);
- implementation
- uses
- Windows, Messages;
- const
- DBT_DEVICEARRIVAL = $8000;
- DBT_DEVICEREMOVECOMPLETE = $8004;
- DBT_DEVTYP_VOLUME = $0002;
- MainWndClass = '{85B54B0C-50E1-4FAD-BCF9-A818138C30AF}';
- type
- PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
- DEV_BROADCAST_HDR = packed record
- dbch_size: DWORD;
- dbch_devicetype: DWORD;
- dbch_reserved: DWORD;
- end;
- PDevBroadcastVolume = ^TDevBroadcastVolume;
- TDevBroadcastVolume = packed record
- dbcv_size: DWORD;
- dbcv_devicetype: DWORD;
- dbcv_reserved: DWORD;
- dbcv_unitmask: DWORD;
- dbcv_flags: WORD;
- end;
- var
- Handle: HWND;
- WndClass: tagWNDCLASSW;
- OnFlashInsert: TOnFlashEvent;
- OnFlashRemove: TOnFlashEvent;
- procedure Destroy();
- begin
- DestroyWindow(Handle);
- UnRegisterClass(MainWndClass, HInstance);
- end;
- function GetDrive(pDBVol: PDevBroadcastVolume): WideChar;
- var
- i: Byte;
- Maske: DWORD;
- begin
- Result := WideChar(#0);
- Maske := pDBVol^.dbcv_unitmask;
- for i := 0 to 25 do
- begin
- if (Maske and 1) = 1 then
- Result := WideChar(i + Ord('A'));
- Maske := Maske shr 1;
- end;
- end;
- procedure OnDeviceChange(var Msg: TMessage);
- begin
- with Msg do
- case wParam of
- DBT_DEVICEARRIVAL:
- if (PDevBroadcastHdr(lParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME) and Assigned(OnFlashInsert) then
- OnFlashInsert(GetDrive(PDevBroadcastVolume(lParam)));
- DBT_DEVICEREMOVECOMPLETE:
- if (PDevBroadcastHdr(lParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME) and Assigned(OnFlashRemove) then
- OnFlashRemove(GetDrive(PDevBroadcastVolume(lParam)));
- end;
- end;
- procedure SetOnFlashInsertEvent(Event: TOnFlashEvent);
- begin
- OnFlashInsert := Event;
- end;
- procedure SetOnFlashRemoveEvent(Event: TOnFlashEvent);
- begin
- OnFlashRemove := Event;
- end;
- function WindowProc(hWin: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
- var
- Msg: TMessage;
- begin
- Result := 0;
- case uMsg of
- WM_DEVICECHANGE:
- begin
- Msg.Msg := uMsg;
- Msg.wParam := wParam;
- Msg.lParam := lParam;
- OnDeviceChange(Msg);
- Result := Msg.Result;
- end;
- WM_DESTROY, WM_CLOSE:
- Destroy();
- else
- Result := DefWindowProc(hWin, uMsg, wParam, lParam);
- end;
- end;
- procedure Create();
- begin
- with WndClass do
- begin
- lpfnWndProc := @WindowProc;
- hInstance := SysInit.HInstance;
- lpszClassName := MainWndClass;
- end;
- RegisterClassW(WndClass);
- Handle := CreateWindowExW(0, MainWndClass, nil, 0, 0, 0, 0, 0, 0, 0, HInstance, nil);
- ShowWindow(Handle, SW_HIDE);
- end;
- initialization
- Create();
- finalization
- Destroy();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement