我对以三个(PID,VID,序列号)的理解是三个值,不知道大家如何理解的如何实现获取以上三个值,在网上查了很多,有用查注册表的方法,等,请各位提供解决方案,谢谢
解决方案 »
- 这样可以删除数据集中游标指定的记录?
- delphi如何实现深度拷贝
- 请问哪里有delphi6下载,急。
- 窗体在游戏里弹出,不失真!如何做!快快快!
- 在delphi xe2中用Windows.Beep(1000, 500)已经没用,在xe2中怎么可以放beep声音?
- 三层应用开发的高手们看过来:李维的分布式应用系统篇讲到了使用企业对象的问题,请问如何开发企业对象?
- 急!急!急!这样写一个txt文件为什么总说‘IOERROR’?60分奉上!60分奉上!60分奉上!
- 神啊,救救我吧!!到底哪个报表控件好用???
- 大家请帮忙,一个关于ADO在WINDOW 2000下的问题!??
- StringGrid上画直线问题,
- 這個單元在什麼時候執行?
- 过程 函数的问题。
unit getUkey;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Registry, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation {$R *.dfm}
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;
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.Button1Click(Sender: TObject);
var
Drv, Pid: string;
begin
Drv := ExtractFileDrive(ParamStr(0));
if GetDriveType(PChar(Drv + '\')) <> DRIVE_REMOVABLE then
Application.MessageBox('�Բ��������ѱ�����������������ʹ��!', 'Error', MB_ICONHAND)
else if GetUSBDiskID(Drv, Pid) then ShowMessage(Pid);
end;
end.
var
Locator: OleVariant;
WMI: OleVariant;
RET: OleVariant;
Enum: IEnumVariant;
Tmp: OleVariant;
Value: Cardinal;
begin
Result:=False;
Locator:=CreateOleObject('WbemScripting.SWbemLocator');
WMI:=Locator.ConnectServer('.','','','');
Ret := WMI.ExecQuery('SELECT * FROM Win32_USBHub');//这里Win32_USBHub为检测项
Enum:= IUnknown(RET._NewEnum) as IEnumVariant;
while (Enum.Next(1,Tmp, Value) = S_OK) do
begin
ShowMessage(Tmp.PNPDeviceID);
if Pos(CheckString,LowerCase(Tmp.PNPDeviceID))>0 then
begin
Result:=True;
break;
end;
ShowMessage(Tmp.PNPDeviceID);//Tmp为检测到返回的数据集合,其中PNPDeviceID为集合中的对象,包含了PID和VID码
end;
end;以上可以查找指定vid/pid的U盘设备,但不知道如何找出该设备的路径,或者知道路径比如为H:盘,如何判断H:盘U盘设备的vid/pid