Advertisement
HEX0x29A

uFlashEvents

Oct 20th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.93 KB | None | 0 0
  1. unit uFlashEvents;
  2. (*
  3. Flash Events
  4. Author: HEX0x29A
  5. Date  : 21.10.2015
  6. *)
  7. interface
  8.  
  9. type
  10.   TOnFlashEvent = procedure(Drive: WideChar);
  11.  
  12. procedure SetOnFlashInsertEvent(Event: TOnFlashEvent);
  13.  
  14. procedure SetOnFlashRemoveEvent(Event: TOnFlashEvent);
  15.  
  16. implementation
  17.  
  18. uses
  19.   Windows, Messages;
  20.  
  21. const
  22.   DBT_DEVICEARRIVAL = $8000;
  23.   DBT_DEVICEREMOVECOMPLETE = $8004;
  24.   DBT_DEVTYP_VOLUME = $0002;
  25.   MainWndClass = '{85B54B0C-50E1-4FAD-BCF9-A818138C30AF}';
  26.  
  27. type
  28.   PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
  29.  
  30.   DEV_BROADCAST_HDR = packed record
  31.     dbch_size: DWORD;
  32.     dbch_devicetype: DWORD;
  33.     dbch_reserved: DWORD;
  34.   end;
  35.  
  36.   PDevBroadcastVolume = ^TDevBroadcastVolume;
  37.  
  38.   TDevBroadcastVolume = packed record
  39.     dbcv_size: DWORD;
  40.     dbcv_devicetype: DWORD;
  41.     dbcv_reserved: DWORD;
  42.     dbcv_unitmask: DWORD;
  43.     dbcv_flags: WORD;
  44.   end;
  45.  
  46. var
  47.   Handle: HWND;
  48.   WndClass: tagWNDCLASSW;
  49.   OnFlashInsert: TOnFlashEvent;
  50.   OnFlashRemove: TOnFlashEvent;
  51.  
  52. procedure Destroy();
  53. begin
  54.   DestroyWindow(Handle);
  55.   UnRegisterClass(MainWndClass, HInstance);
  56. end;
  57.  
  58. function GetDrive(pDBVol: PDevBroadcastVolume): WideChar;
  59. var
  60.   i: Byte;
  61.   Maske: DWORD;
  62. begin
  63.   Result := WideChar(#0);
  64.   Maske := pDBVol^.dbcv_unitmask;
  65.   for i := 0 to 25 do
  66.   begin
  67.     if (Maske and 1) = 1 then
  68.       Result := WideChar(i + Ord('A'));
  69.     Maske := Maske shr 1;
  70.   end;
  71. end;
  72.  
  73. procedure OnDeviceChange(var Msg: TMessage);
  74. begin
  75.   with Msg do
  76.     case wParam of
  77.       DBT_DEVICEARRIVAL:
  78.         if (PDevBroadcastHdr(lParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME) and Assigned(OnFlashInsert) then
  79.           OnFlashInsert(GetDrive(PDevBroadcastVolume(lParam)));
  80.       DBT_DEVICEREMOVECOMPLETE:
  81.         if (PDevBroadcastHdr(lParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME) and Assigned(OnFlashRemove) then
  82.           OnFlashRemove(GetDrive(PDevBroadcastVolume(lParam)));
  83.     end;
  84. end;
  85.  
  86. procedure SetOnFlashInsertEvent(Event: TOnFlashEvent);
  87. begin
  88.   OnFlashInsert := Event;
  89. end;
  90.  
  91. procedure SetOnFlashRemoveEvent(Event: TOnFlashEvent);
  92. begin
  93.   OnFlashRemove := Event;
  94. end;
  95.  
  96. function WindowProc(hWin: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  97. var
  98.   Msg: TMessage;
  99. begin
  100.   Result := 0;
  101.   case uMsg of
  102.     WM_DEVICECHANGE:
  103.       begin
  104.         Msg.Msg := uMsg;
  105.         Msg.wParam := wParam;
  106.         Msg.lParam := lParam;
  107.         OnDeviceChange(Msg);
  108.         Result := Msg.Result;
  109.       end;
  110.     WM_DESTROY, WM_CLOSE:
  111.       Destroy();
  112.   else
  113.     Result := DefWindowProc(hWin, uMsg, wParam, lParam);
  114.   end;
  115. end;
  116.  
  117. procedure Create();
  118. begin
  119.   with WndClass do
  120.   begin
  121.     lpfnWndProc := @WindowProc;
  122.     hInstance := SysInit.HInstance;
  123.     lpszClassName := MainWndClass;
  124.   end;
  125.   RegisterClassW(WndClass);
  126.   Handle := CreateWindowExW(0, MainWndClass, nil, 0, 0, 0, 0, 0, 0, 0, HInstance, nil);
  127.   ShowWindow(Handle, SW_HIDE);
  128. end;
  129.  
  130. initialization
  131.   Create();
  132.  
  133. finalization
  134.   Destroy();
  135.  
  136. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement