Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Werte aus Gerätemanager ... (https://www.delphipraxis.net/172434-werte-aus-geraetemanager.html)

royw 3. Jan 2013 15:58

Werte aus Gerätemanager ...
 
Hi,

ich will einen bestimmten Wert/Eigenschaft von einer USB Webcam erhalten.

Der Wert ist in der DETAILS Karte des Geräts verfügbar : "Vom Bus gemeldete Gerätebeschreibung" .

Hat einer einen Hinweis, wie ich diesen aus meinem Delphi Programm ermitteln kann ?

Eventuelle über WMI ?

Vielen Dank schon mal im Voraus für eure Mühe :-)

DeddyH 3. Jan 2013 17:12

AW: Werte aus Gerätemanager ...
 
An WMI denke ich bei sowas auch als erstes: http://theroadtodelphi.wordpress.com...uter-hardware/

P.S.: Willkommen in der DP :dp:

royw 4. Jan 2013 08:04

AW: Werte aus Gerätemanager ...
 
ja, mittels WMI kann ich bereits einiges auslesen. Doch leider nicht den Wert den ich suche ...
ich suche die Eigenschaft : "Vom Bus gemeldete Gerätebeschreibung" .

Der Geräte Manager zeigt auch deutlich mehr Eigenschaften an als in der Win32_PnPEntity Class enthalten sind ...

Hat noch jemand eine Idee ?

UliBru 4. Jan 2013 08:29

AW: Werte aus Gerätemanager ...
 
siehe auch http://www.delphipraxis.net/1189115-post1.html

Aviator 4. Jan 2013 15:34

AW: Werte aus Gerätemanager ...
 
Hilft dir die Registry in diesem Fall nicht vielleicht auch weiter?

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\U SB

royw 4. Jan 2013 15:48

AW: Werte aus Gerätemanager ...
 
Nein leider nicht, da taucht der Wert leider nicht auf ...

royw 8. Jan 2013 13:09

AW: Werte aus Gerätemanager ...
 
So ich habe eine Lösung in C gefunden und diese nach Delphi (7) übersetzt : (ohne Jedi Zeugs)

Ihr braucht einen Button (Button1) und eine Listbox (ListBox1)

Der Code erhebt keinen Anspruch auf absolute Richtigkeit und Schönheit :-)


Delphi-Quellcode:
const
  MAX_PATH = 260;

//--------------------------------------------------------------
// Configuration Manager return status codes
//--------------------------------------------------------------

  CR_SUCCESS     =$00000000;

type
//
// Define type for reference to device information set
//
  THDEVINFO = Pointer;

//
// Device information structure (references a device instance
// that is a member of a device information set)
//
  PSP_DevInfo_Data = ^TSP_DevInfo_Data;
  SP_DEVINFO_DATA = packed record
    cbSize: DWORD;
    ClassGuid: TGUID;
    DevInst: DWORD; // DEVINST handle
    Reserved: LongWord;
  end;
  TSP_DevInfo_Data = SP_DEVINFO_DATA;

  PDEVPROPKEY = ^TDEVPROPKEY;
  DEVPROPKEY = packed record
    fmtid : TGUID ;
    pid : Pointer;
  end;
  TDEVPROPKEY = DEVPROPKEY;

  DEVPROPTYPE = Pointer;
  PCWSTR = PWCHAR;

  TDEVINST = DWord;

  TPOSVERSIONINFOW = ^TOSVERSIONINFOW;
  TOSVERSIONINFOW = packed record
    dwOSVersionInfoSize      : DWORD ;
    dwMajorVersion           : DWORD ;
    dwMinorVersion           : DWORD ;
    dwBuildNumber            : DWORD ;
    dwPlatformId             : DWORD ;
    szCSDVersion             : array[0..127] of wchar;
  end;


function  SetupDiGetDeviceProperty(DeviceInfoSet: THDEVINFO; DeviceInfoData: PSP_DEVINFO_DATA; const PropertyKey: PDEVPROPKEY; var PropertyType:DEVPROPTYPE; PropertyBuffer:PBYTE;PropertyBufferSize:DWORD; RequiredSize:PDWORD; Flags:DWORD): BOOL; stdcall; external 'Setupapi.DLL' name 'SetupDiGetDevicePropertyW';
function  SetupDiGetClassDevsW(const ClassGuid: PGUID; Enumerator: PCWSTR; hwndParent: HWND; Flags: DWORD): THDEVINFO; stdcall; external 'Setupapi.DLL' name 'SetupDiGetClassDevsW';
function  SetupDiEnumDeviceInfo(DeviceInfoSet: THDEVINFO; MemberIndex: DWORD; DeviceInfoData: PSP_DEVINFO_DATA): BOOL; stdcall; external 'Setupapi.DLL' name 'SetupDiEnumDeviceInfo';
function  CM_Get_Device_IDW(DeviceInstanceHandle: TDEVINst; Buffer:PCWSTR; Bufferlen : ULONG; ulFlags:ULONG): DWORD; stdcall; external 'Setupapi.DLL' name 'CM_Get_Device_IDW';
function  SetupDiGetDeviceRegistryPropertyW(DeviceInfoSet: THDEVINFO; const DeviceInfoData: SP_DevInfo_Data; Property_: DWORD; var PropertyRegDataType: DWORD; PropertyBuffer: PBYTE; PropertyBufferSize: DWORD; var RequiredSize: DWORD): BOOL; stdcall; external 'Setupapi.DLL' name 'SetupDiGetDeviceRegistryPropertyW';
function  GetVersionExW(OsVersion:TPOSVERSIONINFOW): BOOL; stdcall; external 'Kernel32.dll' name 'GetVersionExW';

//
// Flags controlling what is included in the device information set built
// by SetupDiGetClassDevs
//
const  // DIGCF_DEFAULT          = $00000001; // only valid with DIGCF_DEVICEINTERFACE
        DIGCF_PRESENT          = $00000002;
        DIGCF_ALLCLASSES       = $00000004;
        DIGCF_PROFILE          = $00000008;
        DIGCF_DEVICEINTERFACE  = $00000010;
        INVALID_HANDLE_VALUE   = DWORD($FFFFFFFF);

        MAX_DEVICE_ID_LEN      = 200;

        SPDRP_DEVICEDESC                 = ($00000000) ; // DeviceDesc (R/W) ;

        DEVPKEY_Device_BusReportedDeviceDesc : TDEVPROPKEY = (fmtid : '{540b947e-8b40-45bc-a8a2-6a0b894cbda2}' ; pid : pointer(4) );    // DEVPROP_TYPE_STRING

procedure TForm1.Button1Click(Sender: TObject);
var GUID_CAM         : TGUID;
    hDevInfo         : THDEVINFO ;
    DeviceInfoData   : TSP_DEVINFO_DATA ;
    PDeviceInfoData  : PSP_DEVINFO_DATA ;
    successful : Bool;
    i,r:DWORD;
    Buffer:array [0..MAX_DEVICE_ID_LEN] of WCHAR;
    dwSize, dwPropertyRegDataType : DWORD;
    szDesc : array[0..1023] of char;
    OsVersion : TOSVERSIONINFOW;
    BusReportedDescr_Key : TDEVPROPKEY;
    ulPropertyType : DEVPROPTYPE;
begin
  GUID_CAM := StringToGUID('{6bdd1fc6-810f-11d0-bec7-08002be2092f}');          // [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}] > "Class"="Image"
  hDevInfo := SetupDiGetClassDevsW (@GUID_CAM, nil, 0, DIGCF_PRESENT);
  if dword(hDevInfo)<>INVALID_HANDLE_VALUE then
  begin
    Listbox1.Items.Add('hDevInfo : '+inttohex(dword(hDevInfo),8));
    i:=0;
    PDeviceInfoData := @DeviceInfoData;
    repeat
      DeviceInfoData.cbSize := sizeof(TSP_DEVINFO_DATA);
      successful := SetupDiEnumDeviceInfo(hDevInfo, i, @DeviceInfoData);
      if successful then
      begin
        r := CM_Get_Device_IDW(DeviceInfoData.DevInst,@Buffer , MAX_PATH, 0);
        if r = CR_SUCCESS then
        begin
          Listbox1.Items.Add('CM_GET_DEVICE : '+widechartostring(@Buffer));
          if (SetupDiGetDeviceRegistryPropertyW (hDevInfo, DeviceInfoData, SPDRP_DEVICEDESC, dwPropertyRegDataType, @szDesc, sizeof(szDesc), dwSize)) then
          begin
            Listbox1.Items.Add('SetupDiGetDeviceRegistryPropertyW : '+widechartostring(@szDesc));
            OsVersion.dwOSVersionInfoSize := sizeof(TOSVERSIONINFOW);
            if (GetVersionExW(@OsVersion)) then
            begin
              Listbox1.Items.Add('GetVersionExW : '+widechartostring(@OsVersion.szCSDVersion));
              if (OsVersion.dwBuildNumber > 7000) then
              begin // mind. Windows Vista
                Listbox1.Items.Add('GetVersionExW : >= Windows Vista');
                if (SetupDiGetDeviceProperty (hDevInfo, @DeviceInfoData, @DEVPKEY_Device_BusReportedDeviceDesc, ulPropertyType, @szDesc, sizeof(szDesc), @dwSize,0)) then
                begin
                  Listbox1.Items.Add('SetupDiGetDeviceProperty : BusReportedDescription = "'+widechartostring(@szDesc)+'"');
                end else Listbox1.Items.Add('SetupDiGetDeviceProperty : ERROR ');
              end else Listbox1.Items.Add('GetVersionExW : < Windows Vista ');
            end else Listbox1.Items.Add('GetVersionExW : ERROR ');
          end else Listbox1.Items.Add('SetupDiGetDeviceRegistryPropertyW : ERROR ');
        end else Listbox1.Items.Add('CM_GET_DEVICE : ERROR ('+inttohex(r,8)+')');
        inc(i);
      end ;
    until not successful;
  end else Listbox1.Items.Add('hDevInfo : ERROR ('+inttohex(dword(hDevInfo),8)+')');
end;

Wichard 24. Mär 2020 07:56

AW: Werte aus Gerätemanager ...
 
ich bekomme mit diesem Programm unter windows 10 (mit delphi XP4) leider nur hDevInfo angezeigt. Die Variable "successful" ist false.
Wie komme ich weiter.
Danke für jede Hilfe

Wichard

himitsu 24. Mär 2020 10:32

AW: Werte aus Gerätemanager ...
 
Wer hätte es gedacht, aber die "richtige" Fehlerbehandlung hat doch ein paar Vorteile.
siehe dazu im MSDN in den Return Values und manchmal auch in den Remarks, wo drin steht die die Rückgaben der jeweiligen WinAPIs auszuwerten sind, vor allem wie und wo man die detailierten Fehlermeldungen her bekommt.

Auch hab ich mal die nutzlosen Verschachtelungen aufgelöst und die Fehlerbehandlung vom ELSE zu den jeweiligen APIs geschoben,
sowie die Versionsprüfung an den Anfang, denn in der Schleife wird sich dieses Ergebnis garantiert nicht verändern.
Delphi-Quellcode:
var
  GUID_CAM : TGUID;
  hDevInfo : THDEVINFO;
  DeviceInfoData : TSP_DEVINFO_DATA;
  i, R: DWORD;
  Buffer:array [0..MAX_DEVICE_ID_LEN] of WCHAR;
  dwSize, dwPropertyRegDataType : DWORD;
  szDesc : array[0..1023] of char;
  OsVersion : TOSVERSIONINFOW;
  BusReportedDescr_Key : TDEVPROPKEY;
  ulPropertyType : DEVPROPTYPE;
begin
  Listbox1.Items.Clear;
  OsVersion.dwOSVersionInfoSize := sizeof(TOSVERSIONINFOW);
  if not GetVersionExW(@OsVersion) or (OsVersion.dwBuildNumber <= 7000) then
    raise Exception.Create('minimum Windows Vista');
  GUID_CAM := StringToGUID('{6bdd1fc6-810f-11d0-bec7-08002be2092f}'); // [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}] > "Class"="Image"
  hDevInfo := SetupDiGetClassDevsW(@GUID_CAM, nil, 0, DIGCF_PRESENT);
  if DWORD(hDevInfo) = INVALID_HANDLE_VALUE then
    RaiseLastOSError;
  i := -1;
  while True do begin
    Inc(i);
    DeviceInfoData.cbSize := sizeof(TSP_DEVINFO_DATA);
    if not SetupDiEnumDeviceInfo(hDevInfo, i, @DeviceInfoData) then
      if GetLastError = ERROR_NO_MORE_ITEMS then
        Break
      else
        GetLastOSError;
    R := CM_Get_Device_IDW(DeviceInfoData.DevInst, @Buffer, MAX_PATH, 0);
    if R <> CR_SUCCESS then
      raise Exception.CreateFmt('CR-ERROR %d (cfgmgr32.h)', [R]); // see https://docs.microsoft.com/en-us/windows/win32/api/cfgmgr32/
    if not SetupDiGetDeviceRegistryPropertyW(hDevInfo, DeviceInfoData, SPDRP_DEVICEDESC, dwPropertyRegDataType, @szDesc, sizeof(szDesc), dwSize) then
      RaiseLastOSError;
    if not SetupDiGetDeviceProperty(hDevInfo, @DeviceInfoData, @DEVPKEY_Device_BusReportedDeviceDesc, ulPropertyType, @szDesc, sizeof(szDesc), @dwSize,0) then
      RaiseLastOSError;
    Listbox1.Items.Add('SetupDiGetDeviceProperty : BusReportedDescription = "'+widechartostring(@szDesc)+'"');
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:03 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz