[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

Example of how to use QueryServiceConfig2 API call in Visual Basic 6

Hi:

Anybody have an example of how to use the QueryServiceConfig2 API call in VB 6?  I'm looking to get the service description.  I'd like to use this API call rather than look brute force into the registry.

Thanks.
ksm
0
ksm
Asked:
ksm
1 Solution
 
trebrelborgCommented:
Hi ksm,

here is my way...

1) Create a "Class Module" named clsWin32GetServiceName with following code inside

'----------------------------------------------------------------------
Option Explicit

Private Const GENERIC_READ = &H80000000
Private Const SERVICE_QUERY_CONFIG = &H1

Private Type SERVICE_DESCRIPTION_STRUCT
    'max 1024 bytes; ptr|string)
    lpDescription As Long
    szDescription As String * 1020
End Type

Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _
    (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
    ByVal dwDesiredAccess As Long) As Long

Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
    (ByVal hSCManager As Long, ByVal lpServiceName As String, _
    ByVal dwDesiredAccess As Long) As Long
   
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long

Private Declare Function QueryServiceConfig2 Lib "advapi32.dll" Alias "QueryServiceConfig2A" _
 (ByVal hService As Long, ByVal dwInfoLevel As Long, ByRef SD As SERVICE_DESCRIPTION_STRUCT, _
 ByVal cbBufSize As Long, ByRef pcbBytesNeeded As Long) _
 As Long
 

Public Function GetServiceDescription(ByVal serviceName As String) As String
    Dim hSCM As Long
    Dim hSVC As Long
    Dim SD_STRUCT As SERVICE_DESCRIPTION_STRUCT
    Dim cbDescription As Long
   
    ' get into scmanager on localhost, active database, purpose is read
    hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_READ)
    If hSCM <> 0 Then
        ' open service by name
        hSVC = OpenService(hSCM, serviceName, SERVICE_QUERY_CONFIG)
        If hSVC <> 0 Then
            ' query for description (remember: max 1024 bytes; ptr|string)
            If QueryServiceConfig2(hSVC, SERVICE_QUERY_CONFIG, SD_STRUCT, Len(SD_STRUCT), cbDescription) Then
                ' determining the length is little tricky since its count is based on unicode
                GetServiceDescription = Left(SD_STRUCT.szDescription, _
                    ((cbDescription - Len(SD_STRUCT.lpDescription)) / 2) - 1)   '-1 for trailing nullchar
            Else
                GetServiceDescription = "QueryServiceConfig2 failed; check serviceName parameter"
            End If
            CloseServiceHandle hSVC
        Else
            GetServiceDescription = "OpenService failed!"
        End If
        CloseServiceHandle hSCM
    Else
        GetServiceDescription = "OpenSCManager failed!"
    End If
End Function
'----------------------------------------------------------------------


2) Create a (Test) Form
- place a Textbox (Text1) on it
- place a Command-Button (Command1) on it with following code in the Click-Event

'----------------------------------------------------------------------
Private Sub Command1_Click()

    Dim W32ServiceName As clsWin32GetServiceName
    Set W32ServiceName = New clsWin32GetServiceName

    ' call with the Servicename entered in Text1    
    MsgBox "Description of Servicename is '" & W32ServiceName.GetServiceDescription(Text1) & "'"
   
    Set W32ServiceName = Nothing

End Sub
'----------------------------------------------------------------------

3) Have fun reading Service-Descriptions ;-)


treb
0
 
ksmAuthor Commented:
treb:

Works quite well.  I changed two things: (1) I ran as a .bas module and simply called out GetServiceDescription directly and (2) the (-1) minus 1 for the null character caused problems with the description was blank.  But that was an easy fix.

Thanks a lot!  Great help.

ksm
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now