?
Solved

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

Posted on 2004-04-15
2
Medium Priority
?
696 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
trebrelborg earned 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

743 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