G
Guest
I am attempting to get information from the Win32 API QueryServiceConfig. I
keep getting a 1421 error. I need to know what I am doing wrong. Here is
the code:
<DllImport("advapi32.dll", CharSet:=CharSet.Auto,
entrypoint:="QueryServiceConfigA")> _
Public Shared Function QueryServiceConfig(ByVal hService As Integer, _
ByRef lpServiceConfig As IntPtr, _
ByVal cbBufSize As Long, ByRef pcbBytesNeeded As Long) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure QUERY_SERVICE_CONFIG
Public dwServiceType As Integer
Public dwStartType As Integer
Public dwErrorControl As Integer
<MarshalAs(UnmanagedType.LPTStr)> Public lpBinaryPathName As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpLoadOrderGroup As String
Public dwTagId As Integer
<MarshalAs(UnmanagedType.LPTStr)> Public lpDependencies As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpServiceStartName As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpDisplayName As String
End Structure
Public Function RetrieveUserName(ByVal sServiceName As String) As String
Dim iSCManagerHandle As Integer
Dim iSCManagerLockHandle As Integer
Dim iServiceHandle As Integer
Dim bCloseService As Boolean
Dim bUnlockSCManager As Boolean
Dim bCloseSCManager As Boolean
Dim iScActionsPointer As New IntPtr
Try
iSCManagerHandle = OpenSCManager(vbNullString, vbNullString, _
ServiceControlManagerType.SC_MANAGER_ALL_ACCESS)
If iSCManagerHandle < 1 Then
Throw New Exception("Unable to open the Services Manager.")
End If
iSCManagerLockHandle = LockServiceDatabase(iSCManagerHandle)
If iSCManagerLockHandle < 1 Then
Throw New Exception("Unable to lock the Services Manager.")
End If
iServiceHandle = OpenService(iSCManagerHandle, sServiceName, _
ACCESS_TYPE.SERVICE_QUERY_CONFIG)
If iServiceHandle < 1 Then
Throw New Exception("Unable to open the Service for
modification.")
End If
'Allocate memory for struct.
Dim oQSC As New QUERY_SERVICE_CONFIG
Dim bQuery As Boolean
Dim lBytesNeeded As Long
bQuery = QueryServiceConfig(iServiceHandle, oQSC, 0, lBytesNeeded)
MsgBox(Marshal.GetLastWin32Error())
If Not bQuery Then
Throw New Exception("Unable to query the Service settings.")
End If
Catch ex As Exception
Throw ex
Finally
'Once you are done you should close/release the handles to the
service()
'and the Service Control Manager, as well as free the memory
that(held)
'the array of SC_ACTION structures.
Marshal.FreeHGlobal(iScActionsPointer)
If iServiceHandle > 0 Then
bCloseService = CloseServiceHandle(iServiceHandle)
End If
bUnlockSCManager = UnlockServiceDatabase(iSCManagerLockHandle)
If iSCManagerHandle > 0 Then
bCloseSCManager = CloseServiceHandle(iSCManagerHandle)
End If
End Try
End Function
Note: I get a valid handle from the OpenService call.
keep getting a 1421 error. I need to know what I am doing wrong. Here is
the code:
<DllImport("advapi32.dll", CharSet:=CharSet.Auto,
entrypoint:="QueryServiceConfigA")> _
Public Shared Function QueryServiceConfig(ByVal hService As Integer, _
ByRef lpServiceConfig As IntPtr, _
ByVal cbBufSize As Long, ByRef pcbBytesNeeded As Long) As Boolean
End Function
<StructLayout(LayoutKind.Sequential)> _
Public Structure QUERY_SERVICE_CONFIG
Public dwServiceType As Integer
Public dwStartType As Integer
Public dwErrorControl As Integer
<MarshalAs(UnmanagedType.LPTStr)> Public lpBinaryPathName As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpLoadOrderGroup As String
Public dwTagId As Integer
<MarshalAs(UnmanagedType.LPTStr)> Public lpDependencies As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpServiceStartName As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpDisplayName As String
End Structure
Public Function RetrieveUserName(ByVal sServiceName As String) As String
Dim iSCManagerHandle As Integer
Dim iSCManagerLockHandle As Integer
Dim iServiceHandle As Integer
Dim bCloseService As Boolean
Dim bUnlockSCManager As Boolean
Dim bCloseSCManager As Boolean
Dim iScActionsPointer As New IntPtr
Try
iSCManagerHandle = OpenSCManager(vbNullString, vbNullString, _
ServiceControlManagerType.SC_MANAGER_ALL_ACCESS)
If iSCManagerHandle < 1 Then
Throw New Exception("Unable to open the Services Manager.")
End If
iSCManagerLockHandle = LockServiceDatabase(iSCManagerHandle)
If iSCManagerLockHandle < 1 Then
Throw New Exception("Unable to lock the Services Manager.")
End If
iServiceHandle = OpenService(iSCManagerHandle, sServiceName, _
ACCESS_TYPE.SERVICE_QUERY_CONFIG)
If iServiceHandle < 1 Then
Throw New Exception("Unable to open the Service for
modification.")
End If
'Allocate memory for struct.
Dim oQSC As New QUERY_SERVICE_CONFIG
Dim bQuery As Boolean
Dim lBytesNeeded As Long
bQuery = QueryServiceConfig(iServiceHandle, oQSC, 0, lBytesNeeded)
MsgBox(Marshal.GetLastWin32Error())
If Not bQuery Then
Throw New Exception("Unable to query the Service settings.")
End If
Catch ex As Exception
Throw ex
Finally
'Once you are done you should close/release the handles to the
service()
'and the Service Control Manager, as well as free the memory
that(held)
'the array of SC_ACTION structures.
Marshal.FreeHGlobal(iScActionsPointer)
If iServiceHandle > 0 Then
bCloseService = CloseServiceHandle(iServiceHandle)
End If
bUnlockSCManager = UnlockServiceDatabase(iSCManagerLockHandle)
If iSCManagerHandle > 0 Then
bCloseSCManager = CloseServiceHandle(iSCManagerHandle)
End If
End Try
End Function
Note: I get a valid handle from the OpenService call.