Jump to content

Enumerating Devices fails on Windows 10 (SetupDiGetDeviceRegistryProperty)


Recommended Posts

This code works as expected on Windows XP, but SetupDiGetDeviceRegistryProperty returns Error: Invalid Handle on Windows 10. Any idea why?

#Include <WinAPI.au3>

Global Const $DIGCF_DEFAULT          = 0x00000001  ;// only valid with DIGCF_DEVICEINTERFACE
Global Const $DIGCF_PRESENT          = 0x00000002
Global Const $DIGCF_ALLCLASSES       = 0x00000004
Global Const $DIGCF_PROFILE          = 0x00000008
Global Const $DIGCF_DEVICEINTERFACE  = 0x00000010
Global Const $SPDRP_DEVICEDESC                 = (0x00000000)  ;// DeviceDesc (R/W)
Global Const $SPDRP_HARDWAREID                 = (0x00000001)  ;// HardwareID (R/W)
Global Const $SPDRP_COMPATIBLEIDS              = (0x00000002)  ;// CompatibleIDs (R/W)
Global Const $SPDRP_UNUSED0                    = (0x00000003)  ;// unused
Global Const $SPDRP_SERVICE                    = (0x00000004)  ;// Service (R/W)
Global Const $SPDRP_UNUSED1                    = (0x00000005)  ;// unused
Global Const $SPDRP_UNUSED2                    = (0x00000006)  ;// unused
Global Const $SPDRP_CLASS                      = (0x00000007)  ;// Class (R--tied to ClassGUID)
Global Const $SPDRP_CLASSGUID                  = (0x00000008)  ;// ClassGUID (R/W)
Global Const $SPDRP_DRIVER                     = (0x00000009)  ;// Driver (R/W)
Global Const $SPDRP_CONFIGFLAGS                = (0x0000000A)  ;// ConfigFlags (R/W)
Global Const $SPDRP_MFG                        = (0x0000000B)  ;// Mfg (R/W)
Global Const $SPDRP_FRIENDLYNAME               = (0x0000000C)  ;// FriendlyName (R/W)
Global Const $SPDRP_LOCATION_INFORMATION       = (0x0000000D)  ;// LocationInformation (R/W)
Global Const $SPDRP_PHYSICAL_DEVICE_OBJECT_NAME= (0x0000000E)  ;// PhysicalDeviceObjectName ®
Global Const $SPDRP_CAPABILITIES               = (0x0000000F)  ;// Capabilities ®
Global Const $SPDRP_UI_NUMBER                  = (0x00000010)  ;// UiNumber ®
Global Const $SPDRP_UPPERFILTERS               = (0x00000011)  ;// UpperFilters (R/W)
Global Const $SPDRP_LOWERFILTERS               = (0x00000012)  ;// LowerFilters (R/W)
Global Const $SPDRP_BUSTYPEGUID                = (0x00000013)  ;// BusTypeGUID ®
Global Const $SPDRP_LEGACYBUSTYPE              = (0x00000014)  ;// LegacyBusType ®
Global Const $SPDRP_BUSNUMBER                  = (0x00000015)  ;// BusNumber ®
Global Const $SPDRP_ENUMERATOR_NAME            = (0x00000016)  ;// Enumerator Name ®
Global Const $SPDRP_SECURITY                   = (0x00000017)  ;// Security (R/W, binary form)
Global Const $SPDRP_SECURITY_SDS               = (0x00000018)  ;// Security (W, SDS form)
Global Const $SPDRP_DEVTYPE                    = (0x00000019)  ;// Device Type (R/W)
Global Const $SPDRP_EXCLUSIVE                  = (0x0000001A)  ;// Device is exclusive-access (R/W)
Global Const $SPDRP_CHARACTERISTICS            = (0x0000001B)  ;// Device Characteristics (R/W)
Global Const $SPDRP_ADDRESS                    = (0x0000001C)  ;// Device Address ®
Global Const $SPDRP_UI_NUMBER_DESC_FORMAT      = (0X0000001D)  ;// UiNumberDescFormat (R/W)
Global Const $SPDRP_DEVICE_POWER_DATA          = (0x0000001E)  ;// Device Power Data ®
Global Const $SPDRP_REMOVAL_POLICY             = (0x0000001F)  ;// Removal Policy ®
Global Const $SPDRP_REMOVAL_POLICY_HW_DEFAULT  = (0x00000020)  ;// Hardware Removal Policy ®
Global Const $SPDRP_REMOVAL_POLICY_OVERRIDE    = (0x00000021)  ;// Removal Policy Override (RW)
Global Const $SPDRP_INSTALL_STATE              = (0x00000022)  ;// Device Install State ®
Global Const $SPDRP_LOCATION_PATHS             = (0x00000023)  ;// Device Location Paths ®
Global Const $SPDRP_MAXIMUM_PROPERTY           = (0x00000024)  ;// Upper bound on ordinals

; Retrieve handle to Setup API dll
$dll = DllOpen("setupapi.dll")
If @ERROR Then MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)

; Returns a handle to a device information set that contains requested device information elements for a local machine
$result = Dllcall($dll,"hwnd", "SetupDiGetClassDevs", "ptr", 0, "ptr", 0, "hwnd", 0,"dword", BitOr($DIGCF_PRESENT, $DIGCF_ALLCLASSES, $DIGCF_PROFILE))
If @ERROR Then
    MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)
Else
    ; Check for WinAPI error
    $WinAPI_Error = _WinAPI_GetLastError()
    If $WinAPI_Error <> 0 Then
        MsgBox(0,"SetupDiGetClassDevs","Error " & _WinAPI_GetLastError() & @CRLF & _WinAPI_GetLastErrorMessage()) ;Error 13: The data is invalid.
    Else
;~         Debug($result)
        $hDevInfo = $result[0]
    EndIf
EndIf

; Defines a device instance that is a member of a device information set.
$tSP_DEVINFO_DATA = DllStructCreate("DWORD cbSize;int Data1;ushort Data2;ushort Data3;byte Data4[8];DWORD DevInst;INT_PTR Reserved")
If @ERROR Then MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)

$pSP_DEVINFO_DATA = DllStructGetPtr($tSP_DEVINFO_DATA)
$tSP_DEVINFO_DATA_Size = DllStructGetSize($tSP_DEVINFO_DATA)
;~ ConsoleWrite("$tSP_DEVINFO_DATA_Size: " & $tSP_DEVINFO_DATA_Size & @CRLF) ;16 bytes

; Store pointer to GUID
DllStructSetData($tSP_DEVINFO_DATA,"cbSize",$tSP_DEVINFO_DATA_Size)

; Loop until error is set
$wIndex = 0

While 1
    ConsoleWrite("Index: " & $wIndex & @CRLF)

    ; Returns a SP_DEVINFO_DATA structure that specifies a device information element in a device information set.
    $result = DllCall($dll,"int", "SetupDiEnumDeviceInfo", "hwnd", $hDevInfo, "long", $wIndex, "ptr", $pSP_DEVINFO_DATA)
    If @ERROR Then
        MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)
        Exit
    Else
;~         Debug($result)
        ; Check for WinAPI error
        $WinAPI_Error = _WinAPI_GetLastError()
        If $WinAPI_Error <> 0 Then
            MsgBox(0,"SetupDiEnumDeviceInfo","Error " & _WinAPI_GetLastError() & @CRLF & _WinAPI_GetLastErrorMessage())
            Exit
        EndIf
    EndIf

    If $result[0] = 0 Then ContinueLoop

    ; SPDRP_CLASS - Retrieves a specified Plug and Play device property.
    $result2 = Dllcall($dll,"int", "SetupDiGetDeviceRegistryProperty", "dword", $hDevInfo, "ptr", $pSP_DEVINFO_DATA, "dword", $SPDRP_CLASS, "dword*", 0, "str", "", "dword", 2048, "dword*", 0)
    If @ERROR Then
        MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)
    Else
        ;Check for WinAPI error
        $WinAPI_Error = _WinAPI_GetLastError()
        If $WinAPI_Error <> 0 Then
;~             MsgBox(0,"SetupDiGetDeviceRegistryProperty","Error " & _WinAPI_GetLastError() & @CRLF & _WinAPI_GetLastErrorMessage()) ;Error 13: The data is invalid.
        EndIf
;~         Debug($result2)
    EndIf

    ; SPDRP_FRIENDLYNAME - Retrieves a specified Plug and Play device property.
    $result3 = Dllcall($dll,"int", "SetupDiGetDeviceRegistryProperty", "dword", $hDevInfo, "ptr", $pSP_DEVINFO_DATA, "dword", $SPDRP_FRIENDLYNAME, "dword*", 0, "str", "", "dword", 2048, "dword*", 0)
    If @ERROR Then
        MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)
    Else
        ; Check for WinAPI error
        $WinAPI_Error = _WinAPI_GetLastError()
        If $WinAPI_Error <> 0 Then
;~             MsgBox(0,"SetupDiGetDeviceRegistryProperty","Error " & _WinAPI_GetLastError() & @CRLF & _WinAPI_GetLastErrorMessage()) ;Error 13: The data is invalid.
        EndIf
;~         Debug($result3)
    EndIf

    ; SPDRP_DEVICEDESC - Retrieves a specified Plug and Play device property.
    $result4 = Dllcall($dll,"int", "SetupDiGetDeviceRegistryProperty", "dword", $hDevInfo, "ptr", $pSP_DEVINFO_DATA, "dword", $SPDRP_DEVICEDESC, "dword*", 0, "str", "", "dword", 2048, "dword*", 0)
    If @ERROR Then
        MsgBox(0,"","@ERROR: " & @ERROR & @CRLF & "@EXTENDED: " & @EXTENDED)
    Else
        ; Check for WinAPI error
        $WinAPI_Error = _WinAPI_GetLastError()
        If $WinAPI_Error <> 0 Then
;~             MsgBox(0,"SetupDiGetDeviceRegistryProperty","Error " & _WinAPI_GetLastError() & @CRLF & _WinAPI_GetLastErrorMessage()) ;Error 13: The data is invalid.
        EndIf
;~         Debug($result4)
    EndIf

    ConsoleWrite("Class name: " & $result2[5] & @CRLF)
    ConsoleWrite("Friendly name: " & $result3[5] & @CRLF)
    ConsoleWrite("Device description: " & $result4[5] & @CRLF)
    ConsoleWrite(@CRLF)

    $wIndex += 1
WEnd

; Close handle to Setup API dll
DllClose($dll)

; Dump array to console
Func Debug($aArray)
    For $X = 0 to Ubound($aArray)-1
        ConsoleWrite("["&$X&"]: " & $aArray[$X] & @CRLF)
    Next
    ConsoleWrite(@CRLF)
EndFunc

 

This code was originally posted by weaponx [url=https://www.autoitscript.com/forum/topic/77375-resolved-device-listing-via-setupapidll/?do=findComment&comment=561887]here.

Link to comment
Share on other sites

works for me:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
Index: 0
Class name: HIDClass
Friendly name: 
Device description: HID-konformes, vom Hersteller definiertes Ger崍

Index: 1
Class name: System
Friendly name: 
Device description: Hauptplatinenressourcen

Index: 2
Class name: System
Friendly name: 
Device description: Hauptplatinenressourcen

Index: 3
Class name: System
Friendly name: 
Device description: Legacyger崍

Index: 4
Class name: HIDClass
Friendly name: 
Device description: HID-konformer Touchscreen

Index: 5
Class name: Battery
Friendly name: 
Device description: Microsoft ACPI-Kontrollmethodenkompatibler Akku

Index: 6
Class name: Mouse
Friendly name: 
Device description: HID-konforme Maus

Index: 7
Class name: System
Friendly name: 
Device description: System CMOS/Echtzeituhr

Index: 8
Class name: Keyboard
Friendly name: 
Device description: PS/2-Standardtastatur

Index: 9
Class name: PrintQueue
Friendly name: Stammdruckwarteschlange
Device description: Lokale Druckwarteschlange

Index: 10
Class name: System
Friendly name: 
Device description: Volume-Manager

Index: 11
Class name: AudioEndpoint
Friendly name: Mikrofon (Realtek High Definition Audio)
Device description: Audioendpunkt

Index: 12
Class name: USB
Friendly name: 
Device description: USB-Massenspeicherger崍

Index: 13
Class name: System
Friendly name: 
Device description: Eingebetteter, Microsoft ACPI-konformer Controller

Index: 14
Class name: DiskDrive
Friendly name: HUAWEI TF CARD Storage USB Device
Device description: Laufwerk

Index: 15
Class name: System
Friendly name: 
Device description: Systemzeitgeber

Index: 16
Class name: System
Friendly name: 
Device description: Microsoft Basic Display Driver

Index: 17
Class name: System
Friendly name: 
Device description: ACPI-Energiezustandknopf

Index: 18
Class name: Net
Friendly name: Remote NDIS based Internet Sharing Device #2
Device description: Remote NDIS based Internet Sharing Device

Index: 19
Class name: SoftwareDevice
Friendly name: Microsoft-ݢergangsadapterbus von IPv4 nach IPv6
Device description: Generisches Softwareger崍

Index: 20
Class name: System
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series Platform Control Unit - LPC: Bridge to Intel Legacy Block - 0F1C

Index: 21
Class name: Net
Friendly name: Microsoft Teredo Tunneling Adapter
Device description: Microsoft-Teredo-Tunneling-Adapter

Index: 22
Class name: System
Friendly name: 
Device description: ACPI-Thermozone

Index: 23
Class name: Processor
Friendly name: Intel(R) Celeron(R) CPU  N2806  @ 1.60GHz
Device description: Intel-Prozessor

Index: 24
Class name: Processor
Friendly name: Intel(R) Celeron(R) CPU  N2806  @ 1.60GHz
Device description: Intel-Prozessor

Index: 25
Class name: USB
Friendly name: 
Device description: USB-Verbundger崍

Index: 26
Class name: System
Friendly name: 
Device description: Intel(R) Trusted Execution Engine Interface 

Index: 27
Class name: System
Friendly name: 
Device description: Hochpr廩sionsereigniszeitgeber

Index: 28
Class name: System
Friendly name: 
Device description: Intel(R)-Energiemodul-Plug-In

Index: 29
Class name: System
Friendly name: 
Device description: Busenumerator f��erbundger嵥

Index: 30
Class name: System
Friendly name: 
Device description: Microsoft virtueller Datentr娥renumerator

Index: 31
Class name: Mouse
Friendly name: 
Device description: ELAN Input Device

Index: 32
Class name: USB
Friendly name: 
Device description: Generic USB Hub

Index: 33
Class name: Net
Friendly name: Microsoft ISATAP Adapter
Device description: Microsoft-ISATAP-Adapter

Index: 34
Class name: Battery
Friendly name: 
Device description: Microsoft-Netzteil

Index: 35
Class name: SCSIAdapter
Friendly name: 
Device description: Microsoft-Controller f��peicherpl嵺e

Index: 36
Class name: AudioEndpoint
Friendly name: Lautsprecher (Realtek High Definition Audio)
Device description: Audioendpunkt

Index: 37
Class name: System
Friendly name: 
Device description: High Definition Audio-Controller

Index: 38
Class name: Net
Friendly name: 
Device description: Microsoft Kerneldebugger-Netzwerkadapter

Index: 39
Class name: Image
Friendly name: HD WebCam
Device description: USB-Videoger崍

Index: 40
Class name: PrintQueue
Friendly name: Microsoft XPS Document Writer
Device description: Lokale Druckwarteschlange

Index: 41
Class name: VolumeSnapshot
Friendly name: 
Device description: Standard-Volumeschattenkopie

Index: 42
Class name: HIDClass
Friendly name: 
Device description: USB-Eingabeger崍

Index: 43
Class name: Firmware
Friendly name: 
Device description: Systemfirmware

Index: 44
Class name: System
Friendly name: 
Device description: ACPI-Deckel

Index: 45
Class name: PrintQueue
Friendly name: Microsoft Print to PDF
Device description: Lokale Druckwarteschlange

Index: 46
Class name: System
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series PCI Express - Root Port 3 - 0F4C

Index: 47
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 48
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 49
Class name: SoftwareDevice
Friendly name: Bluetooth
Device description: Generisches Softwareger崍

Index: 50
Class name: System
Friendly name: 
Device description: UMBus-Stamm-Busenumerator

Index: 51
Class name: System
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series Platform Control Unit - SMBus Port - 0F12

Index: 52
Class name: SoftwareDevice
Friendly name: Microsoft Radio Device Enumeration Bus
Device description: Generisches Softwareger崍

Index: 53
Class name: System
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series PCI Express - Root Port 1 - 0F48

Index: 54
Class name: SoftwareDevice
Friendly name: Microsoft Device Association Root Enumerator
Device description: Generisches Softwareger崍

Index: 55
Class name: Computer
Friendly name: 
Device description: ACPI x64-basierter PC

Index: 56
Class name: System
Friendly name: 
Device description: Microsoft Windows-Verwaltungsschnittstelle f��CPI

Index: 57
Class name: hdc
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series AHCI - 0F23

Index: 58
Class name: System
Friendly name: 
Device description: Stammkomplex f��CI-Express

Index: 59
Class name: Net
Friendly name: Virtueller Microsoft-Adapter f��irektes WiFi
Device description: Virtueller Microsoft-Adapter f��irektes WiFi

Index: 60
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 61
Class name: USB
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series EHCI USB - 0F34

Index: 62
Class name: PrintQueue
Friendly name: Fax
Device description: Lokale Druckwarteschlange

Index: 63
Class name: System
Friendly name: 
Device description: ACPI-Einschaltknopf

Index: 64
Class name: System
Friendly name: 
Device description: Microsoft ACPI-konformes System

Index: 65
Class name: 
Friendly name: 
Device description: 

Index: 66
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 67
Class name: SoftwareDevice
Friendly name: WiFi
Device description: Generisches Softwareger崍

Index: 68
Class name: System
Friendly name: 
Device description: Microsoft Basic Render Driver

Index: 69
Class name: USB
Friendly name: 
Device description: USB-Root-Hub

Index: 70
Class name: WPD
Friendly name: D:\
Device description: TF CARD Storage 

Index: 71
Class name: System
Friendly name: 
Device description: Programmierbarer Interruptcontroller

Index: 72
Class name: System
Friendly name: 
Device description: Microsoft UEFI-konformes System

Index: 73
Class name: PrintQueue
Friendly name: Send To OneNote 2013
Device description: Lokale Druckwarteschlange

Index: 74
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 75
Class name: System
Friendly name: 
Device description: ACPI-Schalter

Index: 76
Class name: HIDClass
Friendly name: 
Device description: HID-konformes, vom Hersteller definiertes Ger崍

Index: 77
Class name: Display
Friendly name: 
Device description: Intel(R) HD Graphics

Index: 78
Class name: Net
Friendly name: 
Device description: Von Microsoft gehosteter, virtueller Netzwerkadapter

Index: 79
Class name: Volume
Friendly name: 
Device description: Standardvolume

Index: 80
Class name: USB
Friendly name: 
Device description: Generic USB Hub

Index: 81
Class name: SoftwareDevice
Friendly name: Microsoft GS Wavetable Synthesizer
Device description: Generisches Softwareger崍

Index: 82
Class name: MEDIA
Friendly name: 
Device description: Intel(R) Display-Audio

Index: 83
Class name: System
Friendly name: 
Device description: Intel(R) Pentium(R) processor N- and J-series / Intel(R) Celeron(R) processor N- and J-series SoC Transaction Router - 0F00

Index: 84
Class name: System
Friendly name: 
Device description: Enumerator f��irtuelle NDIS-Netzwerkadapter

Index: 85
Class name: DiskDrive
Friendly name: ST500LT012-1DG142
Device description: Laufwerk

Index: 86
Class name: Monitor
Friendly name: 
Device description: PnP-Monitor (Standard)

Index: 87
Class name: HIDClass
Friendly name: 
Device description: HID-kompatible Drahtlos-Empf寧ersteuerelemente

Index: 88
Class name: MEDIA
Friendly name: 
Device description: Realtek High Definition Audio

Index: 89
Class name: Bluetooth
Friendly name: 
Device description: Bluetooth USB Module

Index: 90
Class name: System
Friendly name: 
Device description: Microsoft-Systemverwaltungs-BIOS-Treiber

Index: 91
Class name: System
Friendly name: 
Device description: PnP-Softwareger嵥-Enumerator

Index: 92
Class name: System
Friendly name: 
Device description: Qualcomm Atheros Bluetooth Bus

Index: 93
Class name: System
Friendly name: 
Device description: IWD Bus Enumerator

Index: 94
Class name: USB
Friendly name: 
Device description: USB-Verbundger崍

Index: 95
Class name: HIDClass
Friendly name: 
Device description: Launch Manager Wireless Device

Index: 96
Class name: System
Friendly name: 
Device description: Remote Desktop Device Redirector Bus

Index: 97
Class name: Net
Friendly name: 
Device description: Qualcomm Atheros AR956x-Drahtlosnetzwerkadapter

Index: 98
Class name: HIDClass
Friendly name: 
Device description: HID-konformes, vom Hersteller definiertes Ger崍

Index: 99
Class name: HIDClass
Friendly name: 
Device description: HID-konformes, vom Hersteller definiertes Ger崍

Index: 100
+>11:31:01 AutoIt3.exe ended.rc:0
+>11:31:01 AutoIt3Wrapper Finished.

only problem are the german umlaute:

Device description: HID-konformes, vom Hersteller definiertes Ger崍
;should be:
Device description: HID-konformes, vom Hersteller definiertes Gerät

i use autoit 3.3.14.2 on win10 x64 machine.

Edited by AutoBert
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...