Jump to content
Sign in to follow this  
dariel

Help with WMI object

Recommended Posts

dariel

Hi, how are everyone, first of all sorry for my bad english. So going to the question I am having some dificulties with my script, i am making a program that detects when some usb memory its connected and retrieve some information from it, when the WM_DEVICECHANGE ocurre i call an script that retrieve information using the object wmi, but they gave me an error with the wmi object, if i call the function manually its work fine, so the problem its when it called from that function, below its an example that i make to show the problem.

#include <GUIConstantsEx.au3>
#include <String.au3>

Global Const $WM_DEVICECHANGE = 0x0219
Global Const $DBT_DEVICEARRIVAL = 0x8000 ; Found New Hardware
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ; The device is disconnected

Global Const $DBT_DEVNODES_CHANGED = 0x0007
Global Const $DBT_DEVTYP_OEM = 0x00000000 ; oem-defined device type
Global Const $DBT_DEVTYP_DEVNODE = 0x00000001 ; devnode number
Global Const $DBT_DEVTYP_VOLUME = 0x00000002; logical partition
Global Const $DBT_DEVTYP_PORT = 0x00000003 ; serial, parallel
Global Const $DBT_DEVTYP_NET = 0x00000004 ; network resource

Global $k = 0
;~ getnfo('f:')
$Gui = GUICreate("WM_DEVICECHANGE", 370, 140)
GUICtrlCreateLabel('WM_DEVICECHANGE function is performed when connecting or disconnecting devices to the system. Try inserting the stick', 5, 5, 360, 70)

GUISetState()
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")

Do
Until GUIGetMsg() = -3

Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
    $k += 1
    WinSetTitle($Gui, '', 'Connection ' & $k)

    If ($wParam = $DBT_DEVICEARRIVAL) Or ($wParam = $DBT_DEVICEREMOVECOMPLETE) Then
        Local $DEV_BROADCAST_VOLUME = DllStructCreate("int dbcvsize;int dbcvdevicetype;int dbcvreserved;int dbcvunitmask;" & _
                "ushort dbcvflags", $lParam)
        Local $iDriveType = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvdevicetype")
    Else
        Return $GUI_RUNDEFMSG
    EndIf

    ; If the device is not a logical drive, the output of the function
    If $iDriveType <> $DBT_DEVTYP_VOLUME Then Return $GUI_RUNDEFMSG

    Local $iMask = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvunitmask")
    $iMask = Log($iMask) / Log(2)

    Local $iDrive = Chr(65 + $iMask) & ":"

    Switch $wParam
        Case $DBT_DEVICEARRIVAL ; detection device
            TrayTip("WM_DEVICECHANGE", "device connected. Drive: "&$iDrive&" Info: "& @CRLF&getnfo($iDrive), 5, 1)
        Case $DBT_DEVICEREMOVECOMPLETE ; Disconnecting
            TrayTip("WM_DEVICECHANGE", "device disconnected", 5, 2)
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Func getnfo($iDrive)
            $PNPid = RegRead("HKLM\SYSTEM\MountedDevices", "\DosDevices\" & $iDrive)
            $PNPid = _HexToString(StringReplace(StringReplace($PNPid, "0x", ""), "00", ""))
            $PNPid = StringSplit($PNPid, '#', 2)
            $PNPid = $PNPid[2]

    Global $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
local $colItems = ""

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity where DeviceID like 'USB\\%'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $PNPidChk = $objItem.DeviceID
            $PNPidChk = StringTrimLeft($PNPidChk, StringInStr($PNPidChk, "\", 0, -1)) ;Return Ugly Full PNPDeviceID
            If $PNPidChk = StringTrimRight($PNPid,2) Then
                $DevId = $objItem.DeviceID
                $DevId = StringMid($DevId, StringInStr($DevId, "Vid"), 17)
                $tMemHid=$PNPid&'|'&$DevId
                return $tMemHid
                ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $tMemHid = ' & $tMemHid & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
            EndIf
        Next
    EndIf
;~     _ArrayDisplay($aMemHids)
EndFunc

 

Edited by JLogan3o13

Share this post


Link to post
Share on other sites
JLogan3o13

Moved to the appropriate forum, not sure how we can make this text in the DEV forum any bigger/more clear:

Quote

Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Also, please use code tags (<> on the toolbar) in the future. It gives a nice scrolling code box and makes things much easier to read.


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

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
Sign in to follow this  

×