ADVAPI Problem trying to access a Service

BlitzMax Forums/BlitzMax Programming/ADVAPI Problem trying to access a Service

andre72(Posted 2007) [#1]
Hi,

I try to control a service using BM.
The matter is that I get an Unhandled Memory Exeption Error.

The code:
SuperStrict

Import "-ladvapi32"

'API Constants
Const SERVICES_ACTIVE_DATABASE:String = "ServicesActive"
' Service Control
Const SERVICE_CONTROL_STOP:Int = $1
Const SERVICE_CONTROL_PAUSE:Int = $2
' Service State - for CurrentState
Const SERVICE_STOPPED:Int = $1
Const SERVICE_START_PENDING:Int = $2
Const SERVICE_STOP_PENDING:Int = $3
Const SERVICE_RUNNING:Int = $4
Const SERVICE_CONTINUE_PENDING:Int = $5
Const SERVICE_PAUSE_PENDING:Int = $6
Const SERVICE_PAUSED:Int = $7
'Service Control Manager object specific access types
Const STANDARD_RIGHTS_REQUIRED:Int = $F0000
Const SC_MANAGER_CONNECT:Int = $1
Const SC_MANAGER_CREATE_SERVICE:Int = $2
Const SC_MANAGER_ENUMERATE_SERVICE:Int = $4
Const SC_MANAGER_LOCK:Int = $8
Const SC_MANAGER_QUERY_LOCK_STATUS:Int = $10
Const SC_MANAGER_MODIFY_BOOT_CONFIG:Int = $20
Const SC_MANAGER_ALL_ACCESS:Int = $F003F 'STANDARD_RIGHTS_REQUIRED | SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_LOCK or SC_MANAGER_QUERY_LOCK_STATUS | SC_MANAGER_MODIFY_BOOT_CONFIG
'Service object specific access types
Const SERVICE_QUERY_CONFIG:Int = $1
Const SERVICE_CHANGE_CONFIG:Int = $2
Const SERVICE_QUERY_STATUS:Int = $4
Const SERVICE_ENUMERATE_DEPENDENTS:Int = $8
Const SERVICE_START:Int = $10
Const SERVICE_STOP:Int = $20
Const SERVICE_PAUSE_CONTINUE:Int = $40
Const SERVICE_INTERROGATE:Int = $80
Const SERVICE_USER_DEFINED_CONTROL:Int = $100
Const SERVICE_ALL_ACCESS:Int = $1FF 'STANDARD_RIGHTS_REQUIRED | SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_START | SERVICE_STOP | SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_USER_DEFINED_CONTROL

Type SERVICE_STATUS
Field dwServiceType:Int
Field dwCurrentState:Int
Field dwControlsAccepted:Int
Field dwWin32ExitCode:Int
Field dwServiceSpecificExitCode:Int
Field dwCheckPoint:Int
Field dwWaitHint:Int
End Type

Extern "Win32"
Function CloseServiceHandle:Int (hSCObject:Int) = "CloseServiceHandle@4"
Function ControlService:Int (hService:Int, dwControl:Int, lpServiceStatus:Int) = "ControlService@12"
Function OpenSCManager:Int (lpMachineName:Byte Ptr, lpDatabaseName:Byte Ptr, dwDesiredAccess:Int) = "OpenSCManagerA@12"
Function OpenService:Int (hSCManager:Int, lpServiceName:Byte Ptr, dwDesiredAccess:Int) = "OpenServiceA@12"
Function QueryServiceStatus (hService:Int, lpServiceStatus:SERVICE_STATUS) = "QueryServiceStatus@8"
Function StartService:Int (hService:Int, dwNumServiceArgs:Int, lpServiceArgVectors:Int) = "StartServiceA@12"
End Extern

Function ServiceStatus:Int(ComputerName:String, ServiceName:String)
Local ServiceStat:SERVICE_STATUS
Local hSManager:Int = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
DebugLog(hsManager)
If hSManager <> 0
Local hService:Int = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
DebugLog(hService)
If hService<>0
Local hServiceStatus:Int = QueryServiceStatus(hService, ServiceStat)
DebugLog(ServiceStat.dwCurrentState)
CloseServiceHandle hService
Endif
CloseServiceHandle hSManager
Endif
End Function

ServiceStatus "", "MySQL"

The error occurs at: Local hServiceStatus:Int = QueryServiceStatus(hService, ServiceStat)

I think the mistake is the way I give the parameter ServiceStat to the query.
Maybe somebody can show me the right way for?

Thanks,

Andre


Dreamora(Posted 2007) [#2]
pass a byte ptr instead of a type handle to the query, that should work.

you can not pass BM object out.


andre72(Posted 2007) [#3]
Hm, ok - it has to be Local ServiceStat:SERVICE_STATUS = New SERVICE_STATUS

But now I've the prob that dwCurrentState ever is 0 ...


andre72(Posted 2007) [#4]
Great Dreamora you're right.
Thanks it works fine now...