如何用delphi获取U盘ID
答案:2 悬赏:0 手机版
解决时间 2021-03-16 05:29
- 提问者网友:爱了却不能说
- 2021-03-16 02:17
如何用delphi获取U盘ID
最佳答案
- 五星知识达人网友:一秋
- 2021-03-16 03:14
有USB的单元,调用一下里面的方法就行,但好像只能在XP电脑上获取
全部回答
- 1楼网友:春色三分
- 2021-03-16 04:11
unit unit1;
interface
uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, stdctrls,registry;
type
tform1 = class(tform)
label1: tlabel;
procedure formcreate(sender: tobject);
private
{ private declarations }
public
{ public declarations }
procedure checkdevice(var msg:tmessage); message wm_devicechange;
end;
var
form1: tform1;
flag:boolean;
const
digcf_present = $00000002;
digcf_deviceinterface = $00000010;
anysize_array = 1;
setupapi = 'setupapi.dll';
type
hdevinfo = pointer;
ulong_ptr = dword;
pspdevinfodata = ^tspdevinfodata;
sp_devinfo_data = packed record
cbsize: dword;
classguid: tguid;
devinst: dword;
reserved: ulong_ptr;
end;
{$externalsym sp_devinfo_data}
tspdevinfodata = sp_devinfo_data;
pspdeviceinterfacedata = ^tspdeviceinterfacedata;
sp_device_interface_data = packed record
cbsize: dword;
interfaceclassguid: tguid;
flags: dword;
reserved: ulong_ptr;
end;
{$externalsym sp_device_interface_data}
tspdeviceinterfacedata = sp_device_interface_data;
pspdeviceinterfacedetaildataa = ^tspdeviceinterfacedetaildataa;
sp_device_interface_detail_data_a = packed record
cbsize: dword;
devicepath: array[0..anysize_array - 1] of ansichar;
end;
{$externalsym sp_device_interface_detail_data_a}
tspdeviceinterfacedetaildataa = sp_device_interface_detail_data_a;
implementation
{$r *.dfm}
function setupdigetclassdevsa(classguid: pguid; const enumerator: pansichar;
hwndparent: hwnd; flags: dword): hdevinfo; stdcall; external setupapi;
function setupdienumdeviceinterfaces(deviceinfoset: hdevinfo;
deviceinfodata: pspdevinfodata; const interfaceclassguid: tguid;
memberindex: dword; var deviceinterfacedata: tspdeviceinterfacedata): bool; stdcall; external setupapi;
{$externalsym setupdienumdeviceinterfaces}
function setupdigetdeviceinterfacedetaila(deviceinfoset: hdevinfo;
deviceinterfacedata: pspdeviceinterfacedata;
deviceinterfacedetaildata: pspdeviceinterfacedetaildataa;
deviceinterfacedetaildatasize: dword; var requiredsize: dword;
device: pspdevinfodata): bool; stdcall; external setupapi;
function setupdidestroydeviceinfolist(deviceinfoset: hdevinfo): bool; stdcall; external setupapi;
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;
procedure tform1.checkdevice(var msg: tmessage);
const
dbt_devicearrival = $8000;
dbt_deviceremovecomplete = $8004;
dbt_devnodes_changed = $0007;
var i,j:longint; ssss,idd:string;
begin
if flag then exit;
case msg.wparam of
dbt_devicearrival:begin
for i:=1 to 26 do begin
if directoryexists(chr(64+i)+':\') and (getdrivetype(pchar(chr(64+i)+':\')) = drive_removable)
then begin
showmessage('');
end;
end;
end;//增加了新硬件
dbt_deviceremovecomplete:begin
showmessage('') ;//移除硬件
end;
dbt_devnodes_changed:
begin
showmessage('');
end;
end;
end;
end.以上代码在增加硬件时检测有多少个盘符,将新增的记录下来,移除硬件时将记录删去就好了
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯