Показать сообщение отдельно
  #12  
Старый 12.09.2023, 09:14
infopol infopol вне форума
Прохожий
 
Регистрация: 03.06.2021
Сообщения: 35
Версия Delphi: Delphi 7
Репутация: 10
По умолчанию c procedure EnumDevices(var List:TStrings) разобрался

Код:
procedure EnumDevices(var List:TStrings);
var
  InfoData:SP_DEVINFO_DATA;
  I,A,Size,DType:DWORD;
  Buffer,PC:PChar;
  GUID:TGUID;
begin
 LoadSetupApi();
      List:=TStringList.Create;
 LoadHid();
  I:=0;
//  CM_Enumerate_Classes(I,GUID,0);
 // abort;
  while True do begin

    if CM_Enumerate_Classes(I,GUID,0)=CR_NO_SUCH_VALUE then Break;
    DevInfo:=SetupDiGetClassDevs(@GUID,nil,0,DIGCF_PRESENT);
// abort;
    if DevInfo<>HDEVINFO(INVALID_HANDLE_VALUE) then
    try
    //  exit;
      InfoData.cbSize:=SizeOf(InfoData);
      A:=0;
      Buffer:=nil;
      //Exit;
      try
        while SetupDiEnumDeviceInfo(DevInfo,A,InfoData) do begin
          Size:=0;

          while not SetupDiGetDeviceRegistryProperty(DevInfo,InfoData,SPDRP_HARDWAREID,DType,Pointer(Buffer),Size,Size)
           do
          begin

         // Exit;
            if GetLastError=ERROR_INSUFFICIENT_BUFFER then
              ReallocMem(Buffer,Size)
            else
              Exit;
            end;
          PC:=Buffer;
        // exit;
          while PC^<>#0 do begin

            List.Add(String(PC));
          PC:=StrEnd(PC)+1;
          end;

      //    Exit;
          Inc(A);
        end;
      finally

//            break;
          // Exit;
          FreeMem(Buffer);
    end;
 //   Exit;
    finally
  //  Exit;
                     SetupDiDestroyDeviceInfoList(@DevInfo);
      //       SetupDiDestroyDeviceInfoList(DevInfo);
    end;
    Inc(I);
  end;
  UnLoadSetupApi();
  UnLoadHid();
end;
а вот procedure ScanForHIDdevices( Var DeviceList : THIDDeviceList;
TargetVID, TargetPID : DWord); ничего не выдает


Код:
procedure ScanForHIDdevices( Var DeviceList : THIDDeviceList;
    TargetVID, TargetPID  : DWord);
Var
  HID_GUIid     : TGUID;
  spdid        : TSPDeviceInterfaceData;
  pSpDidd      : PSPDEVICEINTERFACEDETAILDATAA;
  spddd        : TSPDevInfoData;
  HIDinfo       : HDEVINFO;
  CurIdx       : Integer;
  dwSize       : DWord;
pdwSize       : DWord;

  SymbolicLink : String;
 // DevHandle    : THandle;
 // HidAttrs     : THIDDAttributes;
  FoundIdx     : Integer;
  Info         : THIDUSBDeviceInfo;

  Function GetHidDeviceInfo( Symlink : PChar) : THIDUSBDeviceInfo;
  Var
    pstr          : pWideChar;
    preparsedData : PHIDPPreparsedData;
    hidCaps       : THIDPCaps;
  Begin
 //  abort;
 LoadHid();
    FillChar(Result, SizeOf( Result), 0);
    Result.SymLink := SymLink+ #0;
    GetMem( pstr, 512);
 //   abort;
    DevHandle := CreateFile( Symlink,
                             GENERIC_READ or GENERIC_WRITE,
                             FILE_SHARE_READ or FILE_SHARE_WRITE,
                             nil,
                             OPEN_EXISTING,
                             0,
                             0);


    If DevHandle <> INVALID_HANDLE_VALUE then
    begin
      If HidD_GetAttributes( DevHandle,
                             HidAttrs) then
      begin
        result.VID           := HidAttrs.VendorID;
        result.PID           := HidAttrs.ProductID;
        result.VersionNumber := HidAttrs.VersionNumber;
      end;

      If HidD_GetManufacturerString( DevHandle, pstr, 512) then
        Result.ManufacturerString := pStr;

      If HidD_GetProductString( DevHandle, pstr, 512) then
        Result.ProductString := pStr;

      If HidD_GetSerialNumberString( DevHandle, pstr, 512) then
        Result.SerialNumberString := pStr;

      { Set Input buffer size }
      HidD_SetNumInputBuffers( DevHandle,
                               HIDUSB_COUNTOFINTERRUPTBUFFERS);

      { Get capabilities }
      HidD_GetPreparsedData( DevHandle, preparsedData);

      if HidD_GetPreparsedData( DevHandle, preparsedData)


       then
      begin
        HidP_GetCaps( preparsedData, hidCaps);
        Result.BufferSize := hidCaps.OutputReportByteLength;
      end
      else
       Result.BufferSize := 11;

      closeHandle( DevHandle);
    end;
    FreeMem( pStr);
  End;

Begin
 LoadHid();

    LoadSetupApi();

  FoundIdx   := 0;
  DeviceList := Nil;
  { Get GUID of hid class }
//ShowMessage(GUIDToString( HID_GUIid));
  //abort;
  HidD_GetHidGuid( HID_GUIid);
//ShowMessage(GUIDToString( HID_GUIid));

// abort;
  { Get a list of devices belonging to HID class }
  HIDinfo :=  SetupDiGetClassDevs( @HID_GUIid,
                                  nil,
                                  GetDesktopWindow(),
                                  DIGCF_DEVICEINTERFACE or DIGCF_PRESENT);

  { Go through list of devices }


  If thandle(HIDinfo) <> INVALID_HANDLE_VALUE then
  begin
    CurIdx := 0;
    spdid.cbSize := SizeOf(spdid);
    While SetupDiEnumDeviceInterfaces( HIDinfo,
                                       nil,
                                       HID_GUIid,
                                       curIdx,
                                        spdid) do
    begin
      dwSize := 0;
      { Get device path for Createfile calls }


      SetupDiGetDeviceInterfaceDetail( HIDinfo,
                                       @spdid,
                                       nil,
                                       dwSize,
                                       pdwSize,
                                       nil);

      If pdwSize > 0 then
      begin
        GetMem(pSpDidd, pdwSize);
        pSpDidd^.cbSize := SizeOf( TSPDEVICEINTERFACEDETAILDATAA);
        spddd.cbSize    := SizeOf(spddd);
        If SetupDiGetDeviceInterfaceDetail( HIDinfo,
                                            @spdid,
                                            pSpDidd,
                                            dwSize,
                                            pdwSize,
                                            @spddd) then
        begin
          SymbolicLink := PChar( @(pSpDidd^.DevicePath));

          { Get information about the device (Vendor and
            Product IDs, Strings, ...) }
          FillChar(info, SizeOf(Info), 0);
          Info        := GetHidDeviceInfo( @(pSpDidd^.DevicePath));
          Info.Handle := INVALID_HANDLE_VALUE;

          { check if VID/PID match targets }
          If (Info.VID = TargetVID) AND
             (Info.PID = TargetPID) then
          begin
            { Add Devices to result list }
            SetLength(DeviceList, FoundIdx + 1);
            DeviceList[foundIdx] := Info;
            Inc(FoundIdx);
          end
          else  // list all HID devices if no target is specified
            If (TargetVID = 0) AND (TargetPID = 0) then
            begin
              { Add Devices to result list }
              SetLength( DeviceList, FoundIdx + 1);
              DeviceList[FoundIdx] := Info;

              Inc(FoundIdx);
            end;
        end;

        FreeMem( pSpDidd);
      end;
      inc(CurIdx);
    end;
    SetupDiDestroyDeviceInfoList( HIDinfo);
  end;
End;
Ответить с цитированием