⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 用DELPHI获取U盘序列号。 通过此功能可以把系统放在U盘上
💻 PAS
字号:
unit   Unit1;   
    
  interface   
    
  uses   
      Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
      Dialogs,   StdCtrls;

  {   
  typedef   struct   _USB_DEVICE_DESCRIPTOR   {   
      UCHAR     bLength   ;   
      UCHAR     bDescriptorType   ;   
      USHORT     bcdUSB   ;   
      UCHAR     bDeviceClass   ;   
      UCHAR     bDeviceSubClass   ;   
      UCHAR     bDeviceProtocol   ;   
      UCHAR     bMaxPacketSize0   ;   
      USHORT     idVendor   ;   
      USHORT     idProduct   ;   
      USHORT     bcdDevice   ;   
      UCHAR     iManufacturer   ;
      UCHAR     iProduct   ;   
      UCHAR     iSerialNumber   ;   
      UCHAR     bNumConfigurations   ;   
  }   
    
    
  const   
          DIGCF_PRESENT   =   2;   
          DIGCF_INTERFACEDEVICE   =   16;   
  type   
    
    PSP_DEVINFO_DATA   =   ^SP_DEVINFO_DATA;   
    SP_DEVINFO_DATA   =   packed   record   
          cbSize   :DWORD;   
          ClassGuid   :TGUID;   
          DevInst   :DWORD   ;         //   DEVINST   handle   
          Reserved   :DWORD;   
    end;   
    
      HDEVINFO   =   Pointer;
      pulong   =   ^ulong;   
    
      PSP_INTERFACE_DEVICE_DATA   =   ^SP_INTERFACE_DEVICE_DATA;   
      SP_INTERFACE_DEVICE_DATA   =   packed   record   
            cbSize   :ulong;   
            InterfaceClassGuid   :   TGuid;   
            Flags     :ulong;   
            Reserved   :ulong;   
      end;   
    
      PSP_INTERFACE_DEVICE_DETAIL_DATA   =   ^PSP_INTERFACE_DEVICE_DETAIL_DATA;   
      SP_DEVICE_INTERFACE_DETAIL_DATA   =   packed   record   
          cbSize   :DWORD;   
          DevicePath:   Char;   
      end;   
    
    
    PUSB_DEVICE_DESCRIPTOR   =   ^USB_DEVICE_DESCRIPTOR;   
    USB_DEVICE_DESCRIPTOR     =   packed   record   
        bLength   :UCHAR;   
        bDescriptorType   :UCHAR;   
        bcdUSB   :WORD;   
        bDeviceClass   :UCHAR;   
        bDeviceSubClass   :UCHAR;   
        bDeviceProtocol   :UCHAR;   
        bMaxPacketSize0   :UCHAR;   
        idVendor   :Word;   
        idProduct   :Word;   
        bcdDevice   :Word;   
        iManufacturer   :UCHAR;   
        iProduct   :UCHAR;   
        iSerialNumber   :UCHAR;   
        bNumConfigurations   :UCHAR;   
      end;   
    
  function   SetupDiGetDeviceInterfaceDetail(
        DeviceInfoSet   :   HDEVINFO;   
        DeviceInterfaceData:     PSP_INTERFACE_DEVICE_DATA;   
        DeviceInterfaceDetailData:     PSP_INTERFACE_DEVICE_DETAIL_DATA;   
        DeviceInterfaceDetailDataSize   :   DWORD;   
        RequiredSize   :   PDWORD;   
        DeviceInfoData   :   PSP_DEVINFO_DATA   
          ):   BOOL;   stdcall;   external   'SETUPAPI.dll'   name   'SetupDiGetDeviceInterfaceDetailA';
    
  function   SetupDiGetClassDevsEx(   
            ClassGuid   :PGUID;   
          Enumerator   :PChar;   
          hwndParent   :HWND;   
          Flags   :   DWORD;   
          MachineName   :PChar;   
          Reserved   :Pointer   
          ):   HDEVINFO;   stdcall;   external   'SETUPAPI.dll'   name   'SetupDiGetClassDevsExA';   
    
  function   SetupDiGetClassDevs(   
          ClassGuid   :PGUID   ;   
          Enumerator:   PChar;   
          hwndParent:   HWND;   
          Flags   :   DWORD
          ):   HDEVINFO;   stdcall;   external   'SETUPAPI.dll'   name   'SetupDiGetClassDevsA';   
    
  function   SetupDiEnumDeviceInterfaces(
              DeviceInfoSet   :HDEVINFO;   
              DeviceInfoData   :PSP_DEVINFO_DATA;   
              InterfaceClassGuid   :PGUID;   
              MemberIndex   :DWORD;   
              DeviceInterfaceData   :PSP_INTERFACE_DEVICE_DATA   
          ):   BOOL;   stdcall;   external   'SETUPAPI.dll'   name   'SetupDiEnumDeviceInterfaces';   
    
  function   SetupDiDestroyDeviceInfoList(   
                DeviceInfoSet   :HDEVINFO   
          ):BOOL;   stdcall;   external   'SETUPAPI.dll'   name   'SetupDiEnumDeviceInterfaces';   
    
  type   
      TOnGetStrEvent   =   procedure(AStr   :string)   of   object;   
    
  //以下代码可枚举所有设备   
    
  const   //USBIODS_GUID   :   TGuid   =   '{745a17a0-74d3-11d0-b6fe-00a0c90f57da}';   
              USBIODS_GUID   :   TGuid   =   '{a5dcbf10-6530-11d2-901f-00c04fb951ed}';   
    
  type   
      TForm1   =   class(TForm)   
          Button1:   TButton;   
          Memo1:   TMemo;   
          procedure   Button1Click(Sender:   TObject);   
      private   
          {   Private   declarations   }   
      public
          {   Public   declarations   }   
      end;   
    
  var   
      Form1:   TForm1;   
    
  implementation   
    
  {$R   *.dfm}   
    
  procedure   TForm1.Button1Click(Sender:   TObject);   
  var   b   :integer;
          bRst   :BOOL;
          hInfo   :HDEVINFO;
  //         USBIODS_GUID   :TGuid;   
          deviceInterfaceData   :SP_INTERFACE_DEVICE_DATA;   
          DeviceDetailData   :SP_DEVICE_INTERFACE_DETAIL_DATA;
          predictedLength   :   ULONG;
          requiredLength     :ULONG;   
          nCount:   integer;   
  begin
          Memo1.Clear;   
    
  {         USBIODS_GUID.D1   :=   $a5dcbf10;   
          USBIODS_GUID.D2   :=   $6530;   
          USBIODS_GUID.D3   :=   $11d2;   
          USBIODS_GUID.D4[0]   :=   $90;   
          USBIODS_GUID.D4[1]   :=   $1f;   
          USBIODS_GUID.D4[2]   :=   $00;   
          USBIODS_GUID.D4[3]   :=   $c0;   
          USBIODS_GUID.D4[4]   :=   $4f;
          USBIODS_GUID.D4[5]   :=   $b9;   
          USBIODS_GUID.D4[6]   :=   $51;   
          USBIODS_GUID.D4[7]   :=   $ed;   
    }   
  //   USBIODS_GUID   :=   '{a5dcbf10-6530-11d2-901f-00c04fb951ed}';   
    
          predictedLength   :=   0;   
          requiredLength   :=   0;   
          hInfo   :=   nil;   
          hInfo   :=   SetupDiGetClassDevs(@USBIODS_GUID,   nil,   0,
                                        DIGCF_PRESENT   OR   DIGCF_INTERFACEDEVICE);
          showmessage(string(hInfo));
          bRst   :=   true;   
          nCount   :=   0;
          deviceInterfaceData.cbSize   :=   Sizeof(SP_INTERFACE_DEVICE_DATA);   
          DeviceDetailData.cbSize   :=   sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);   

          if   hInfo   =   nil   then
          begin   
              showmessage('failed   0');   
              exit;
          end;   
    
          while   bRst   do   
          begin   
              deviceInterfaceData.cbSize   :=   Sizeof(deviceInterfaceData);
              bRst   :=   SetupDiEnumDeviceInterfaces(hInfo,   nil,   @USBIODS_GUID,
                                     nCount,   @deviceInterfaceData);
              if   bRst   then   
              begin
                    SetupDiGetDeviceInterfaceDetail(hInfo,   @deviceInterfaceData,
                                            nil,   0,   @requiredLength,   nil);
                    predictedLength   :=   requiredLength;   
                    DeviceDetailData.cbSize   :=   sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
                    bRst   :=   SetupDiGetDeviceInterfaceDetail(hInfo,   @deviceInterfaceData,
                                           @DeviceDetailData,   predictedLength,   @requiredLength,   nil);
                    if   bRst   then   
                    begin
                  //       showmessage('ok'+inttostr(nCount));
                      /////   pszDevicePath[nCount]   :=   string(PChar(@(DeviceDetailData.DevicePath)));
                        showmessage(string(PChar(@(DeviceDetailData.DevicePath))));
                      ////   Memo1.Lines.Add(pszDevicePath[nCount]);   
          //               Memo1.Lines.Add(string(PChar(@(DeviceDetailData.DevicePath))));
                        Inc(nCount);
                    end;
              end;
          end;   //end   while
          showmessage('finished');
  //       SetupDiDestroyDeviceInfoList(hInfo);   
  end;   
    
    
  end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -