我找到了几个代码片段和.pas文件,可以检测USB闪存驱动器何时插入和拔出。有些提供各种好的信息,但是我需要的是设备的唯一序列号,而不是卷的序列号。
我的当前.pas文件(我不记得在哪里找到)似乎也可以检测SD卡(这很好)。如果你想看看,可以在这里找到它(它只返回驱动器号码和插入/拔出):
unit UsbDetector;
interface
uses Classes;
type
TUsbDriveChanged = procedure (Sender: TObject; Drive: string; Attached: boolean) of object;
procedure StartUsbDetector(NotifyProc: TUsbDriveChanged);
procedure StopUsbDetector;
implementation
uses Windows, Messages, Forms, SysUtils;
type
TUSBDetector = class(TObject)
private
fUsbDriveChanged: TUsbDriveChanged;
protected
procedure DeviceChanged(Msg: UINT; wParam, lParam: Longint);
procedure DoUsbDriveChanged(Drive: string; Attached: Boolean); dynamic;
public
constructor Create(NotifyProc: TUsbDriveChanged);
destructor Destroy; override;
property OnUsbDriveChanged: TUsbDriveChanged read fUsbDriveChanged;
end;
var mUSBDetector: TUSBDetector;
procedure StartUsbDetector(NotifyProc: TUsbDriveChanged);
begin
if not Assigned(mUsbDetector) then
mUsbDetector := TUsbDetector.Create(NotifyProc);
end;
procedure StopUsbDetector;
begin
FreeAndNil(mUsbDetector);
end;
{----------------------------------------------------------------------------}
// Device constants
const
DBT_DEVICEARRIVAL = $00008000;
DBT_DEVICEREMOVECOMPLETE = $00008004;
DBT_DEVTYP_VOLUME = $00000002;
// Device structs
type
_DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
DEV_BROADCAST_HDR = _DEV_BROADCAST_HDR;
TDevBroadcastHeader = DEV_BROADCAST_HDR;
PDevBroadcastHeader = ^TDevBroadcastHeader;
type
_DEV_BROADCAST_VOLUME = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: WORD;
end;
DEV_BROADCAST_VOLUME = _DEV_BROADCAST_VOLUME;
TDevBroadcastVolume = DEV_BROADCAST_VOLUME;
PDevBroadcastVolume = ^TDevBroadcastVolume;
var
fPrevWndProc: TFNWndProc = nil;
function UsbWndProc(hWnd: HWND; Msg: UINT; wParam, lParam: Longint): Longint; stdcall;
begin
Result := CallWindowProc(fPrevWndProc, hWnd, Msg, wParam, lParam);
if (Msg = WM_DEVICECHANGE) and (mUsbDetector <> nil) then
mUsbDetector.DeviceChanged(Msg, wParam, lParam);
end;
constructor TUSBDetector.Create(NotifyProc: TUsbDriveChanged);
begin
inherited Create;
fUsbDriveChanged := NotifyProc;
if not Assigned(fPrevWndProc) then
begin
fPrevWndProc := TFNWndProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(@UsbWndProc));
end;
end;
destructor TUSBDetector.Destroy;
begin
//SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(@fPrevWndProc));
inherited Destroy;
end;
procedure TUSBDetector.DeviceChanged(Msg: UINT; wParam, lParam: LongInt);
var
lpdbhHeader: PDevBroadcastHeader;
lpdbvData: PDevBroadcastVolume;
dwIndex: Integer;
lpszDrive: string;
begin
// Get the device notification header
lpdbhHeader := PDevBroadcastHeader(lParam);
// Handle the message
lpszDrive := '';
case WParam of
DBT_DEVICEARRIVAL: {a USB drive was connected}
begin
if lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME then
begin
lpdbvData := PDevBroadcastVolume(lParam);
for dwIndex := 0 to 25 do
begin
if (lpdbvData^.dbcv_unitmask shr dwIndex) = 1 then
begin
lpszDrive := lpszDrive + Chr(65 + dwIndex) + ':\';
break;
end;
end;
DoUsbDriveChanged(lpszDrive, True);
end;
end;
DBT_DEVICEREMOVECOMPLETE: {a USB drive was removed}
begin
if lpdbhHeader^.dbch_devicetype = DBT_DEVTYP_VOLUME then
begin
lpdbvData := PDevBroadcastVolume(lParam);
for dwIndex := 0 to 25 do
begin
if (lpdbvData^.dbcv_unitmask shr dwIndex) = 1 then
begin
lpszDrive := lpszDrive + Chr(65 + dwIndex) + ':\';
break;
end;
end;
DoUsbDriveChanged(lpszDrive, False);
end;
end;
end;
end;
procedure TUSBDetector.DoUsbDriveChanged(Drive: string; Attached: Boolean);
begin
if Assigned(fUsbDriveChanged) then
fUsbDriveChanged(Self, Drive, Attached);
end;
end.
顺便说一下,代码高亮出现了问题。
总之,在插入/拔出可移动设备时,获取驱动器号和其唯一序列号。或许可以将已有的代码与WMI调用“where Index=found_index”相结合。
****编辑!**** 我从RRUZ给出的代码中删除了“where”子句。我终于找到了如何处理数组,所以我使用它来查找Capabilities [i] = 7以获取所有可移动介质。现在我只需要将此代码与上面的代码连接起来。我考虑使用Index,但我不知道如何使用GetDrive MapInfo。如果您能提供一个获取驱动器号的示例,我的问题就解决了。