Jump to content

Get Portable Devices


Danyfirex
 Share

Recommended Posts

Well an Implementation of IPortableDeviceManager Interface to get all Portable Device conected to our pc.

 

#include <Array.au3>
#include <WinAPICom.au3>
Opt("MustDeclareVars", 1)

Global Const $sCLSID_PortableDeviceManager = "{0af10cec-2ecd-4b92-9581-34f6ae0637f3}"
Global Const $sIID_IPortableDeviceManager = "{a1567595-4c2f-4574-a6fa-ecef917b9a40}"
Global Const $sTagIPortableDeviceManager = "GetDevices hresult(ptr;dword*); RefreshDeviceList hresult(); " & _
        "GetDeviceFriendlyName hresult(wstr;wstr;dword*); GetDeviceDescription hresult(wstr;wstr;dword*); " & _
        "GetDeviceManufacturer hresult(wstr;wstr;dword*); GetDeviceManufacturer hresult(wstr;wstr;ptr;dword*dword*); " & _
        "GetPrivateDevices hresult(ptr;dword*)"

Global Enum $eDevID, $eDevName, $eDevManufacturer, $eDevDescription


Local $aPnPDevices = GetPortableDevices()
If IsArray($aPnPDevices) Then _ArrayDisplay($aPnPDevices)

;~ Success: Return 0
;~ Failure: Return 2DArray [n][4] |;[n][0]$eDevID, [n][1]$eDevName, [n][2]$eDevManufacturer,[n][3] $eDevDescription
Func GetPortableDevices()
    Local $aDevicesInfo[0][0] ;[n][0]$eDevID, [n][1]$eDevName, [n][2]$eDevManufacturer,[n][3] $eDevDescription
    Local $oPortableDeviceManager = 0
    Local $SizeofArray = 0
    Local $hr = 0x80004005 ;E_Fail Just to Initialized <0
    Local $taPnPDeviceIDs = 0
    Local $tName = 0
    $oPortableDeviceManager = ObjCreateInterface($sCLSID_PortableDeviceManager, $sIID_IPortableDeviceManager, $sTagIPortableDeviceManager)
    If Not IsObj($oPortableDeviceManager) Then Return 0

    If FAILED($oPortableDeviceManager.GetDevices(Null, $SizeofArray)) Then Return 0
    If $SizeofArray < 1 Then Return 0
    $taPnPDeviceIDs = DllStructCreate("ptr[" & $SizeofArray & "]")
    If FAILED($oPortableDeviceManager.GetDevices(DllStructGetPtr($taPnPDeviceIDs), $SizeofArray)) Then Return 0

    ReDim $aDevicesInfo[$SizeofArray][4]

    For $i = 0 To $SizeofArray - 1
        $tName = DllStructCreate("wchar[512]", DllStructGetData($taPnPDeviceIDs, 1, $i + 1))
        $aDevicesInfo[$i][$eDevID] = DllStructGetData($tName, 1)
        $aDevicesInfo[$i][$eDevName] = _GetFriendlyName($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $aDevicesInfo[$i][$eDevManufacturer] = _GetDeviceManufacturer($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $aDevicesInfo[$i][$eDevDescription] = _GetDeviceDescription($oPortableDeviceManager, $aDevicesInfo[$i][$eDevID])
        $tName = 0
        _WinAPI_CoTaskMemFree(DllStructGetData($taPnPDeviceIDs, 1, $i + 1))
    Next
    Return $aDevicesInfo

EndFunc   ;==>GetPortableDevices



Func _GetDeviceManufacturer($oInterface, $PnPDeviceID)
    Local $sString = ""
    $oInterface.GetDeviceManufacturer($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetDeviceManufacturer


Func _GetDeviceDescription($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceDescription($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetDeviceDescription


Func _GetFriendlyName($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceFriendlyName($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetFriendlyName

Func _GetProperty($oInterface, $PnPDeviceID)
    Local $sString = ""
    Local Const $Size = 128
    $oInterface.GetDeviceFriendlyName($PnPDeviceID, $sString, 128)
    Return $sString
EndFunc   ;==>_GetProperty

Func FAILED($hr)
    Return ($hr < 0)
EndFunc   ;==>FAILED

 

Saludos

Link to comment
Share on other sites

  • 2 years later...

I know it's more than 2 years later and I hope my question is not to stupid.

Is there a way to enumerate the WPD Device Properties for the devices returned by this script? I don't know enough programming to explain this properly but I would be interested in the properties listed at https://msdn.microsoft.com/en-us/library/windows/hardware/ff597865(v=vs.85).aspx for each portable device returned by this script. I would be interested in WPD_DEVICE_FIRMWARE_VERSION and WPD_DEVICE_MODEL besides other properties. I can see my phones and tablets but I can't even get started to get from here (get the phone) to there (get the firmware version).

Any hint would be appreciated.

Link to comment
Share on other sites

  • 3 weeks later...

Here you can find some more info on this ...

https://www.pdq.com/blog/where-can-i-find-a-device-s-firmware-version-in-wmi/ 

#RequireAdmin

Func GetFirmware() ;$id
Local $FirmWare, $strComputer = "."


Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$objFirmWare = $objWMIService.ExecQuery ("“SELECT * FROM MSDeviceUI_FirmwareRevision")

For $objItem In $objFirmWare
    ConsoleWrite("Active:" & $objItem.Active & @CRLF)
    ConsoleWrite("FirmwareRevision:" & $objItem.FirmwareRevision & @CRLF)
    ConsoleWrite("InstanceName:" & $objItem.InstanceName & @CRLF)
Next

EndFunc

But you need some extra tool : DevCON in conjunction with this script.

Not tested it tough just translated it for your purpose...

Rgds

ptrex

Link to comment
Share on other sites

  • 8 months later...

Hi, I am usually a lurker, however I can't seem to find anything usable with the limited amount of skills that I currently have, so I decided to ask for help !

Is it possible to use this script to retrieve the WPD_DEVICE_POWER_LEVEL property of a connected device ? 

If so, could you tell me how to achieve that ?

 

Thanks in advance

SomeBald

Link to comment
Share on other sites

Hello. I think you need to use some more interface like IPortableDeviceProperties and IPortableDeviceContent.

 

Saludos

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

×
×
  • Create New...