Почему при отлове WM_DEVICECHANGE возникает ошибка определения devicetype???
Код:
program Project;
const
KERNEL32 = 'kernel32.dll';
USER32 = 'user32.dll';
ClassName = 'KL';
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
DBTF_Remove = $0000;
DBT_DevTyp_Volume = $0002;
WS_VISIBLE = $10000000;
WS_CHILD = $40000000;
WM_DEVICECHANGE = 537;
//GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
//DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class
type
DWORD = INTEGER;
UINT = INTEGER;
HWND = INTEGER;
WPARAM = INTEGER;
LPARAM = INTEGER;
LRESULT = INTEGER;
HINST = INTEGER;
HHOOK = INTEGER;
BOOL = BOOLEAN;
HMODULE = INTEGER;
TPoint = packed record
X : Longint;
Y : Longint;
end;
TWndClassEx = packed record
cbSize : Integer;
style : Integer;
lpfnWndProc : Pointer;
cbClsExtra : Integer;
cbWndExtra : Integer;
hInstance : integer;
hIcon : Integer;
hCursor : Integer;
hbrBackground : Integer;
lpszMenuName : PChar;
lpszClassName : PChar;
hIconSm : Integer;
end;
TMSG = packed record
hwnd : Integer;
message : integer;
wParam : Longint;
lParam : Longint;
time : integer;
pt : TPoint;
end;
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
msg : TMsg;
WinClass : TWndClassEx;
top : integer;
left : integer;
width : integer;
height : integer;
hInstance: integer;
hw : hwnd;
bu : hwnd;
drive : pchar;
function DefWindowProc(hWnd: Integer; Msg: UINT; wParam: Integer; lParam: Integer): Integer; stdcall; external user32 name 'DefWindowProcA';
function RegisterClassEx(const WndClass: TWndClassEx): Word; stdcall; external user32 name 'RegisterClassExA';
function GetMessage(var lpMsg: TMsg; hWnd: integer; wMsgFilterMin, wMsgFilterMax: integer): boolean; stdcall; external user32 name 'GetMessageA';
function TranslateMessage(const lpMsg: TMsg): boolean; stdcall; external user32 name 'TranslateMessage';
function DispatchMessage(const lpMsg: TMsg): integer; stdcall; external user32 name 'DispatchMessageA';
function CreateWindowEx(dwExStyle: integer; lpClassName: PChar; lpWindowName: PChar; dwStyle: integer; X, Y, nWidth, nHeight: Integer; hWndParent: integer; hMenu: integer; hInstance: integer; lpParam: Pointer): integer; stdcall; external user32 name 'CreateWindowExA';
function GetModuleHandle(lpModuleName: PChar): HMODULE; stdcall; external kernel32 name 'GetModuleHandleA';
function GetDrive(pDBVol: PDevBroadcastVolume): pchar;
var
i: Byte;
Maske: DWORD;
begin
if (pDBVol^.dbcv_flags and DBTF_Remove) = DBTF_Remove then
begin
Maske := pDBVol^.dbcv_unitmask;
for i := 0 to 25 do
begin
if (Maske and 1) = 1 then
Result := PChar(i + Ord('A'));
Maske := Maske shr 1;
end;
end;
end;
function WndProc(hwnd, wmsg, wparam, lparam: integer): integer; stdcall;
var
devType: Integer;
Datos: PDevBroadcastHdr;
begin
if wparam = 123 then halt;
if wmsg = WM_DEVICECHANGE then
begin
Datos := PDevBroadcastHdr(Msg.lParam);
devType := Datos^.dbch_devicetype;
if devtype = DBT_DevTyp_Volume then halt;
end
else
Result:=DefWindowProc(hwnd, wmsg, wparam, lparam);
end;
Procedure CreateMyFofm;
begin
with WinClass do
begin
cbSize:=SizeOf(WinClass); //
lpszClassName:=ClassName; //Имя
lpfnWndProc:=@WndProc; //Поцедура обрботки
cbClsExtra:=0; //Параметр
cbWndExtra:=0; //Параметр
hInstance:=0; //Хендл Проги
lpszMenuName:=nil; //Парамерт
style:=0; //Стили никчему
hbrBackground:=5; //Цвет окна белый
end;
RegisterClassEx(WinClass);
top:=100;
left:=100;
width:= 0;
height:= 65;
hw:=CreateWindowEx(0,ClassName,'KL',WS_VISIBLE,left,top,width,height,0,0,hInstance,nil);
Bu:=CreateWindowEx(0,'Button','Откл',WS_VISIBLE or WS_CHILD,5,5,100,25,hw,123,0,nil);
end;
begin
CreateMyFofm;
hInstance:=GetModuleHandle(0);//Определяем хендл
while GetMessage(Msg,0,0,0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end.