Danyfirex

Get Portable Devices

3 posts in this topic

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

4 people like this

Share this post


Link to post
Share on other sites



#2 ·  Posted

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

  • Similar Content

    • nikink
      By nikink
      Hi all,
      I have a bit of code that works on my old Win10 PC, that fails on my new Win10 PC, and I think the only significant difference is the version of Autoit - old PC has 3.3.12, new has 3.3.14.
      I couldn't find anything mentioned in the change logs though, so perhaps I'm wrong.
      Anyway, the code to replicate my issue is:
      Test('username', 'DOMAIN') ; THIS ERRORS: ;Test('localun', 'DOMAIN') ; THIS ERRORS: ;Test(' ', ' ') ; THIS ERRORS: ;Test('', '') ; THIS ERRORS: ;Test('localun', '') ; THIS ERRORS: ;Test('', 'DOMAIN') Func Test($un, $dom) $compName = 'PCNAME' $FullName = '.' $Description = '.' ; get the WIM object $objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2") ; get default user full name and description $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") If IsObj($objAccount) Then $FullName = $objAccount.FullName $Description = $objAccount.Description EndIf ConsoleWrite($FullName & @CRLF) ConsoleWrite($Description & @CRLF) Return EndFunc  
      On my old PC this code will output just . and . for each of those line currently commented out. Which is fine.
      On my new PC any of those commented out lines of code cause an error, and the script won't even compile.
      $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'") $objAccount = $objWMIService^ ERROR I'm very much a newb with the WMI stuff and objects, but it looks like the .Get property is failing when either $un or $dom aren't valid in v3.3.14, whereas in 3.3.12 the .Get would fail to return an object, which is then caught by the If statement.
      Am I on track with this? Is there some new/better way to code the example so that 3.3.14 will compile it?
    • Marc
      By Marc
      Hi Folks,
      I've been very fond of portable software. Some of the portable programs are quite large, so to make the using fancier, I started to compress the portable applications into a WinRar SFX Archive which extracts the software to the tempdir, starts the software and afterward deletes the whole temp Dir.
      But it got cumbersome to manually extract the icon from the exe, make all necessary settings in the WinRar GUI - so I wrote this script. Nothing spectacular, but (in my eyes) very useful. After that, I added some other options to make it a little more versatile.
      Features:
      if we have a 32Bit and a 64Bit version, it can launch the correct Version (32Bit or 64Bit) fitting to the system the SFX is started on (contains a small exe file "universal_launcher.exe" which expects two parameters  for the 32Bit.exe and 64Bit.exe, checks the current system for 64Bit and then runs the correct file). Powerbasic Sourcecode of the exe file is included, of course. automatically extracts the icon from the chosen exe and uses it as icon for the SFX if the chosen exe file has a version number in it, the version is put in the resulting SFX's filename can just extract the files to a user-chosen directory OR can just extract the files to the program files directory (and, optionally, create a desktop shortcut) OR extract the files to a temp directory and run an exe Have fun with it
      best regards,
      Marc

      MakeSFX.zip
    • nacerbaaziz
      By nacerbaaziz
      Hi all
      I want a way to get the last key pressed.
      I have a program that works with keyboard shortcuts and I want to  give the permission for the user to edit shortcut keys depending on what suits him
      i  want to make read-only edit box and the program writes the latest shortcut key pressed
      Please help me,
      greetings to all
      And thanks in advance
    • nacerbaaziz
      By nacerbaaziz
      hello guys
      how are you؟
      I hope to be fine.
      I have a question  please
      how do I get the menu item that was pressed without that contains a variable؟
      For example I have a menu  of Favorites and I want the script recognizes the existing path in the pressed item
      i'll repeat to tell the item does not contain a variable
      Is there any solution
      if you want to explain more I could write an example of what I want.
      Greetings to all,
      thanks in advance
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I have an urgent question, please help.
      How do I select all the text in the edit or input
      And how to get the currently selected text
      Please help urgently
      Greetings and thanks for all