当前位置:学Delphi网文档资料技术资料其他

delphi获取U盘序列号

减小字体 增大字体 作者:不详  来源:转载  发布时间:2011-03-09 15:55:59
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);

[1] [2]  下一页

Tags:

作者:不详
  • 好的评价 如果您觉得此文章好,就请您
      100%(2)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

广告位置B