Solved

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

Posted on 2004-04-15
2
647 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now