delphi获取优盘的ID


本文整理自网络,侵删。

 

delphi获取优盘的ID

function GetUSBDiskID(const DiskID: string; var PID: string): Boolean;
var
USBGuid: TGUID;
USBHandle: HDEVINFO;
Success: LongBool;
Devn: Integer;
DevData: TSPDevInfoData;
DeviceInterfaceData: TSPDeviceInterfaceData;
FunctionClassDeviceData: PSPDeviceInterfaceDetailDataA;
BytesReturned: DWORD;
Reg: TRegistry;
RegData: array of Char;
i, RegSize: Integer;
Str, USBPath: string;
begin
Result := false;
Pid := '';
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('SYSTEM\MountedDevices', false);
RegSize := Reg.GetDataSize(Format('\DosDevices\%s', [DiskID]));
SetLength(RegData, RegSize + 1);
Reg.ReadBinaryData(Format('\DosDevices\%s', [DiskID]), RegData[0], RegSize + 1);
for i := 0 to RegSize - 1 do
if RegData[i] <> #0 then
Str := Str + RegData[i];
Str := Copy(Str, Pos('#RemovableMedia#', Str) + 16, Length(Str));
Str := Copy(Str, 1, Pos('RM', Str) - 2);
Str := UpperCase(Str); Reg.CloseKey;
USBGuid := StringToGUID('{53f56307-b6bf-11d0-94f2-00a0c91efb8b}');
USBHandle := SetupDiGetClassDevsA(@USBGuid, nil, 0, DIGCF_PRESENT or DIGCF_DEVICEINTERFACE);
if USBHandle = Pointer(INVALID_HANDLE_VALUE) then Exit; Devn := 0;
repeat DeviceInterfaceData.cbSize := SizeOf(TSPDeviceInterfaceData);
Success := SetupDiEnumDeviceInterfaces(USBHandle, nil, USBGuid, Devn, DeviceInterfaceData);
if Success then
begin
DevData.cbSize := SizeOf(DevData);
BytesReturned := 0;
SetupDiGetDeviceInterfaceDetailA(USBHandle, @DeviceInterfaceData, nil, 0, BytesReturned, @DevData);
if (BytesReturned <> 0) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
FunctionClassDeviceData := AllocMem(BytesReturned);
FunctionClassDeviceData^.cbSize := SizeOf(TSPDeviceInterfaceDetailDataA);
if SetupDiGetDeviceInterfaceDetailA(USBHandle, @DeviceInterfaceData, FunctionClassDeviceData, BytesReturned, BytesReturned, @DevData) then
begin
USBPath := StrPas(PChar(@FunctionClassDeviceData.DevicePath));
if Reg.OpenKeyReadOnly(Format('SYSTEM\CurrentControlSet\Enum%s', [StringReplace(Copy(USBPath, 4, Pos('{', USBPath) - 5), '#', '\', [rfReplaceAll])])) then
if UpperCase(Reg.ReadString('ParentIdPrefix')) = Str then
begin
Delete(USBPath, 1, Pos('#', USBPath));
PID := Copy(USBPath, Pos('#', USBPath) + 1, Length(USBPath));
PID := Copy(PID, 1, Pos('#{', PID) - 1);
PID := UpperCase(StringReplace(PID, '&', '', [rfReplaceAll]));
Result := True;
Break;
end;
Reg.CloseKey; Inc(Devn);
end;
FreeMem(FunctionClassDeviceData);
end;
end;
until not Success;
SetupDiDestroyDeviceInfoList(USBHandle);
finally
Reg.Free;
end;
end;
end.

相关阅读 >>

Delphi adoquery 开启本地缓存

Delphi实现解析百度搜索结果link?url=

Delphi 字符串截取

Delphi byte、pbyte、tbytes的转换

Delphi程序只允许运行一个实例的三种方法

Delphi抓屏代码

Delphi xe 如何实现("再按一次退出") 然后退出程序

Delphi sendmessage postmessage 原理和区别

Delphi httpresponsestrings

Delphi 获取指定当前目录下指定文件扩展名所有文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...