如何枚举出所有USB设备中找到U盘,并取得其逻辑盘符?

ynduanlian 2010-11-10 02:09:29
在网上查到一段枚举出所有USB设备中找到U盘的代码,但不知道如何把设备与逻辑盘符对应起来?

function SetupDiGetClassDevsA(ClassGuid: PGUID; const Enumerator: PAnsiChar;hwndParent: HWND; Flags: DWORD): HDEVINFO; stdcall;external 'SetupApi.dll';
function SetupDiEnumDeviceInfo(DeviceInfoSet: HDEVINFO;MemberIndex: DWORD; var DeviceInfoData: TSPDevInfoData): LongBool; stdcall;external 'SetupApi.dll';
function CM_Get_DevNode_Status(pulStatus: PULong; pulProblemNumber: PULong;dnDevInst: DWord; ulFlags: ULong): DWord; stdcall;external 'cfgmgr32.dll' name 'CM_Get_DevNode_Status';
function SetupDiGetDeviceRegistryPropertyA(DeviceInfoSet: HDEVINFO;const DeviceInfoData: TSPDevInfoData; Property_: DWORD;var PropertyRegDataType: DWORD; PropertyBuffer: PBYTE; PropertyBufferSize:DWORD;var RequiredSize: DWORD): LongBool; stdcall;external 'SetupApi.dll';
function CM_Request_Device_Eject(dnDevInst: DWord; out pVetoType: TPNPVetoType;pszVetoName: PChar; ulNameLength: ULong; ulFlags: ULong): DWord; stdcall;external 'SetupApi.dll' name 'CM_Request_Device_EjectA';

//获得设备信息
function GetRegistryProperty(PnPHandle: hDevInfo; DevData:TSPDevInfoData; Prop: DWord; Buffer: PChar; dwLength: DWord): Boolean;
var
aBuffer: array[0..256] of Char;
begin
dwLength := 0;
aBuffer[0] := #0;
SetupDiGetDeviceRegistryPropertyA(PnPHandle, DevData, Prop, Prop,PBYTE(@aBuffer[0]), SizeOf(aBuffer), dwLength);
StrCopy(Buffer, aBuffer);
Result := Buffer^ <> #0;
end;

//根据设备信息回复出设备的名称
function ConstructDeviceName(DeviceInfoSet: hDevInfo;DeviceInfoData:TSPDevInfoData; Buffer: PChar; dwLength: DWord): Boolean;
const
UnknownDevice = '<Unknown Device>';
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_DEVICEDESC, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASS, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASSGUID, Buffer, dwLength)) then
begin
dwLength := DWord(SizeOf(UnknownDevice));
Buffer := Pointer(LocalAlloc(LPTR, Cardinal(dwLength)));
StrCopy(Buffer, UnknownDevice);
end;
end;
end;
end;
Result := true;
end;

//遍历DevInfo,获得U盘在当前系统设备列表中的ID
function EnumAddDevices(ShowHidden: Boolean;DevInfo: hDevInfo): Boolean;
var
i, Status, Problem: DWord;
pszText: PChar;
DeviceInfoData:TSPDevInfoData;
begin
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
i := 0;
//遍历设备列表,查找USB存储设备信息
while SetupDiEnumDeviceInfo(DevInfo, i, DeviceInfoData) do
begin
inc(i);
//获取设备节点状态信息
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
break;
end;
try
GetMem(pszText, 256);
ConstructDeviceName(DevInfo, DeviceInfoData, pszText, DWord(nil)); //创建设备可见名称列表
if pos('USB Mass Storage Device',StrPas(pszText))<>0 then //比较字符串,找到USB存储设备
MyDevice_ID:=i-1; //得到USB存储设备在当前设备列表中的ID
这里怎么知道USB存储设备的逻辑盘符呢?
finally
FreeMem(pszText);
end;
end;
Result := true;
end;

//获取系统中所有设备信息到hDevInfo指针所指空间
function GetDevInfo(var hDevInfo:HDEVINFO): boolean;
begin
hDevInfo := SetupDiGetClassDevsA(nil, nil, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES);
Result := hDevInfo <> Pointer(INVALID_HANDLE_VALUE);
end;

//获得设备列表
procedure IniDevice;
begin
MyDevice_ID:=0;
DevInfo:= nil;
if not GetDevInfo(DevInfo) then
begin
ShowMessage('枚举设备失败!');
exit;
end;
EnumAddDevices(TRUE,DevInfo);
end;



...全文
756 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gyk120 2010-11-15
  • 打赏
  • 举报
回复
GetVolumeNameForVolumeMountPoint
应该是用这个函数吧?
ynduanlian 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
http://zhidao.baidu.com/question/74084037.html
[/Quote]
我的目换是想停用设备,所以寻找设备ID,上面的方案,只是列举了盘符,还是没有把盘符和设备ID对应啊

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧