Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

774 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