Solved

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

Posted on 2004-04-15
2
669 Views
Last Modified: 2013-11-13
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
Comment
Question by:ksm
2 Comments
 
LVL 1

Accepted Solution

by:
trebrelborg earned 500 total points
ID: 10899936
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
 

Author Comment

by:ksm
ID: 10904049
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
recursion example 16 141
Customizing meta title for 404 page template in WordPress' TwentySixteen theme 2 93
ejb stateless example 2 25
Cygwin - GNU GPL License 1 37
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question