Biatu

Triggering "Scan for Hardware Changes"

6 posts in this topic

#1 ·  Posted (edited)

Hello, I found this func in the forums, but it does not work. I keep getting errors. I could use devcon but I will be using the script in a limited space environment.

 

ScanForHardwareChanges()

Func ScanForHardwareChanges() 
    Local Const $CM_LOCATE_DEVNODE_NORMAL = 0
    Local Const $CR_SUCCESS = 0
    $status = DLLCall("cfgmgr32.dll","int","CM_Locate_DevNode", _
            "int*",0,"int",0,"int",$CM_LOCATE_DEVNODE_NORMAL)
    
    If Not ($status[0] = $CR_SUCCESS) Then
        MsgBox(4096,"","CM_Locate_DevNode failed: " & $status[0])
        Return 0
    EndIf
    
    $status = DLLCall("cfgmgr32.dll","int","CM_Reenumerate_DevNode","int",$status[1],"int",0)
    
    If Not ($status[0] = $CR_SUCCESS) Then
        MsgBox(4096,"","CM_Reenumerate_DevNode failed: " & $status[0])
       printf("CM_Reenumerate_DevNode failed: %x\n", status));
        Return 0
    EndIf

    Return 1
EndFunc
 

 

Edited by Biatu

What is what? What is what.

Share this post


Link to post
Share on other sites



#2 ·  Posted

Share this post


Link to post
Share on other sites

#3 ·  Posted

yes, when running script as in on Win10 I get Error 51, on windows 7 I get error 5 (even with requireAdministrator set from Au3Wrapper), and...

if I change this line back to what it originally was...

$status = DLLCall("cfgmgr32.dll","int","CM_Locate_DevNode", _
            "int_ptr",0,"int",0,"int",$CM_LOCATE_DEVNODE_NORMAL)

I get error 3


What is what? What is what.

Share this post


Link to post
Share on other sites

#4 ·  Posted

Hmm, I'm not much help here.  I couldn't get it to run on windows 7 either.  I got the same error (5) which seems to indicate either an invalid DEVNODE or DEVINST from what I found an a header file (cfgmgr32.h) online.

#define CR_INVALID_DEVNODE                0x00000005
#define CR_INVALID_DEVINST          	  CR_INVALID_DEVNODE

Error 51 seems to indicate an access denied error.

#define CR_ACCESS_DENIED                  0x00000033

 

I'm not too sure where to troubleshoot from here.  Hopefully someone more familiar will chime in.

Share this post


Link to post
Share on other sites

#5 ·  Posted

It works for me in Windows 10 x64 Enterprise (required #RequireAdmin otherwise error 51), but not in Windows 7 x64 Enterprise (error 5).  Same user account on both, with Admin rights.

Share this post


Link to post
Share on other sites

Bump, I've come across this feature again.

 

I thought to include SeDriverLoadPrivilege as MSDN suggests but still getting error 5 on Win7 and on Win10 I cannot tell if it works, however it does not throw an error.

Script:
 

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
#include <WinAPIProc.au3>
#include <SecurityConstants.au3>
#RequireAdmin
Global $aAdjust, $hToken
$hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ALL_ACCESS, $TOKEN_QUERY))
MsgBox(64,@Error,@Extended)
_WinAPI_AdjustTokenPrivileges($hToken, BitOR($SE_DEBUG_NAME,$SE_LOAD_DRIVER_NAME), $SE_PRIVILEGE_ENABLED, $aAdjust)
MsgBox(64,@Error,@Extended)
ScanForHardwareChanges()
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
MsgBox(64,@Error,@Extended)
_WinAPI_CloseHandle($hToken)
MsgBox(64,@Error,@Extended)
ScanForHardwareChanges()

Func ScanForHardwareChanges()
    Local Const $CM_LOCATE_DEVNODE_NORMAL = 0
    Local Const $CR_SUCCESS = 0
    $status = DLLCall("cfgmgr32.dll","int","CM_Locate_DevNode", _
            "int*",0,"int",0,"int",$CM_LOCATE_DEVNODE_NORMAL)

    If Not ($status[0] = $CR_SUCCESS) Then
        MsgBox(4096,"","CM_Locate_DevNode failed: " & $status[0])
        Return 0
    EndIf

    $status = DLLCall("cfgmgr32.dll","int","CM_Reenumerate_DevNode","int",$status[1],"int",0)

    If Not ($status[0] = $CR_SUCCESS) Then
        MsgBox(4096,"","CM_Reenumerate_DevNode failed: " & $status[0])
       ;printf("CM_Reenumerate_DevNode failed: %x\n", status));
        Return 0
    EndIf

    Return 1
EndFunc

 


What is what? What is what.

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