Jump to content

Error 6: ERROR_INVALID_HANDLE


Recommended Posts

If _NTServices.au3 is from this old example then there are a number of issues in the implementation of the functions. For example that handles are stored as integer data types in DllCalls. This means that the UDF will not work on 64 bit. And all the functions seems to be the ansi version (A-functions). Today you would use the unicode version (W-functions). If you go through the entire UDF and corrects all the mistakes, I'm pretty sure it'll work.

Link to comment
Share on other sites

It takes a few months before I get access to a Windows 10 PC. Perhaps there is another who can look at it. I'm almost sure it'll work if the functions are implemented properly.

Link to comment
Share on other sites

Hello. As LarsJ says funtions are not implemented properly. So I wrote this example with correct implementation. (tested in windows 10 x64. Compiled as x86 and x64)

 

Global Const $sTagSERVICE_STATUS = "DWORD dwServiceType;" & _
        "DWORD dwCurrentState;" & _
        "DWORD dwControlsAccepted;" & _
        "DWORD dwWin32ExitCode;" & _
        "DWORD dwServiceSpecificExitCode;" & _
        "DWORD dwCheckPoint;" & _
        "DWORD dwWaitHint;"




Global $STANDARD_RIGHTS_REQUIRED = 0x000F0000
Global $SC_MANAGER_CONNECT = 0x0001
Global $SC_MANAGER_CREATE_SERVICE = 0x0002
Global $SC_MANAGER_ENUMERATE_SERVICE = 0x0004
Global $SC_MANAGER_LOCK = 0x0008
Global $SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
Global $SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020
Global $SC_MANAGER_ALL_ACCESS = BitOR($STANDARD_RIGHTS_REQUIRED, _
        $SC_MANAGER_CONNECT, _
        $SC_MANAGER_CREATE_SERVICE, _
        $SC_MANAGER_ENUMERATE_SERVICE, _
        $SC_MANAGER_LOCK, _
        $SC_MANAGER_QUERY_LOCK_STATUS, _
        $SC_MANAGER_MODIFY_BOOT_CONFIG)
Global Const $hAdvapi32 = DllOpen("advapi32.dll")

MsgBox(64, "Info", "Make sure youre running Admin")


Local $aRet = 0
Local $sServiceName = "Spooler"

$aRet = DllCall($hAdvapi32, "handle", "OpenSCManagerW", _
        "wstr", Null, _
        "wstr", Null, _
        "dword", $SC_MANAGER_ALL_ACCESS)

If $aRet[0] = 0 Then
    If $aRet[0] = 0 And Not @error Then ConsoleWrite("!Error in OpenSCManager" & @CRLF)
    Exit
EndIf

Local $hSCManager = $aRet[0]

;~ MsgBox(0, "", $hSCManager)
ConsoleWrite(">$hSCManager: " & $hSCManager & @CRLF)

$aRet = DllCall($hAdvapi32, "handle", "OpenServiceW", _
        "handle",$hSCManager, _
        "wstr", $sServiceName, _
        "long", $SC_MANAGER_ALL_ACCESS)

If $aRet[0] = 0 Then
    If $aRet[0] = 0 And Not @error Then ConsoleWrite("!Error in OpenServiceW" & @CRLF)
    Exit
EndIf

Local $hServ = $aRet[0]
;~ MsgBox(0, "", $hServ)
ConsoleWrite(">$hSCManager: " & $hServ & @CRLF)


Local $tStatus = DllStructCreate($sTagSERVICE_STATUS)

$aRet = DllCall($hAdvapi32, "bool", "QueryServiceStatus", _
        "handle", $hServ, _
        "ptr", DllStructGetPtr($tStatus))

If $aRet[0] = 0 Then
    If $aRet[0] = 0 And Not @error Then ConsoleWrite("!Error in QueryServiceStatus" & @CRLF)
    Exit
EndIf

ConsoleWrite("+Spooler Status: " & $tStatus.dwCurrentState & @CRLF)
MsgBox(0, "", "Spooler Status: " & $tStatus.dwCurrentState)


DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hServ)
DllCall($hAdvapi32, "bool", "CloseServiceHandle", "handle", $hSCManager)
DllClose($hAdvapi32)

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

  • Recently Browsing   0 members

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