サービスの状態および構成を取得          <TOP>


CloseServiceHandle サービスコントロールマネージャオブジェクトまたはサービスオブジェクトへの指定されたハンドルを閉じる
QueryServiceStatus 指定されたサービスの現在のステータスを取得
OpenService 既存のサービスのハンドルを開く
OpenSCManager 指定されたコンピュータ上のサービス制御マネージャとの接続を確立し、サービス制御マネージャの指定されたデータベースを開く
QueryServiceConfig 指定されたサービスの構成パラメータを取得
CopyMemory 文字列をコピーする
lstrcpy ある位置から別の位置にメモリブロックを移動
 

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services で確認できます。

 

'================================================================
'= サービスの状態および構成を取得
'=    (OpenSCManager3.bas)
'=    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
'================================================================
#include "Windows.bi"

Type SERVICE_STATUS
    dwServiceType             As Long
    dwCurrentState            As Long
    dwControlsAccepted        As Long
    dwWin32ExitCode           As Long
    dwServiceSpecificExitCode As Long
    dwCheckPoint              As Long
    dwWaitHint                As Long
End Type

Type QUERY_SERVICE_CONFIG
    dwServiceType      As Long
    dwStartType        As Long
    dwErrorControl     As Long
    lpBinaryPathName   As Long
    lpLoadOrderGroup   As Long
    dwTagId            As Long
    lpDependencies     As Long
    lpServiceStartName As Long
    lpDisplayName      As Long
End Type

#define SERVICE_ACCEPT_PAUSE_CONTINUE &H2 '
#define SERVICE_ACCEPT_SHUTDOWN &H4       '
#define SERVICE_ACCEPT_STOP &H1           '
#define SERVICE_CONTINUE_PENDING &H5      '
#define SERVICE_PAUSE_PENDING &H6         '
#define SERVICE_PAUSED &H7                '
#define SERVICE_RUNNING &H4               '
#define SERVICE_START_PENDING &H2         '
#define SERVICE_STOP_PENDING &H3          '
#define SERVICE_STOPPED &H1               '
#define SERVICE_INTERROGATE &H80          '
#define SC_MANAGER_CONNECT &H1            '
#define GENERIC_READ -2147483648          '読み込みモード(&H80000000)
#define ERROR_INSUFFICIENT_BUFFER 122     'システム コールに渡されるデータ領域が小さい

' サービスコントロールマネージャオブジェクトまたはサービスオブジェクトへの指定されたハンドルを閉じる
Declare Function Api_CloseServiceHandle& Lib "advapi32" Alias "CloseServiceHandle" (ByVal hSCObject&)

' 指定されたサービスの現在のステータスを取得
Declare Function Api_QueryServiceStatus& Lib "advapi32" Alias "QueryServiceStatus" (ByVal hService&, lpServiceStatus As SERVICE_STATUS)

' 既存のサービスのハンドルを開く
Declare Function Api_OpenService& Lib "advapi32" Alias "OpenServiceA" (ByVal hSCManager&, ByVal lpServiceName$, ByVal dwDesiredAccess&)

' 指定されたコンピュータ上のサービス制御マネージャとの接続を確立し、サービス制御マネージャの指定されたデータベースを開く
Declare Function Api_OpenSCManager& Lib "advapi32" Alias "OpenSCManagerA" (ByVal lpMachineName$, ByVal lpDatabaseName$, ByVal dwDesiredAccess&)

' 指定されたサービスの構成パラメータを取得
Declare Function Api_QueryServiceConfig& Lib "advapi32" Alias "QueryServiceConfigA" (ByVal hService&, lpServiceConfig As Byte, ByVal cbBufSize&, pcbBytesNeeded&)

' 文字列をコピーする
Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy&)

' ある位置から別の位置にメモリブロックを移動
Declare Function Api_lstrcpy& Lib "Kernel32" Alias "lstrcpyA" (ByVal lpString1$, ByVal lpString2&)

Var Shared Edit1 As Object
Var Shared List1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
List1.Attach GetDlgItem("List1") : List1.SetFontSize 12
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var hSCM  As Long
    Var hSVC As Long
    Var ss As SERVICE_STATUS
    Var qsc As QUERY_SERVICE_CONFIG
    Var lBytesNeeded As Long
    Var sTemp As String
    Var pFileName As Long
    Var Ret As Long

    List1.ResetContent

    hSCM = Api_OpenSCManager(ByVal 0, ByVal 0, SC_MANAGER_CONNECT)
    If hSCM = 0 Then
        A% = MessageBox("", "Error", 0, 2)
    End If

    hSVC = Api_OpenService(hSCM, Trim$(Edit1.GetWindowText), GENERIC_READ)
    If hSVC = 0 Then
        A% = MessageBox("", "Error", 0, 2) 
        GoTo *CloseHandles
    End If

    Ret = Api_QueryServiceStatus(hSVC, ss)
    If Ret = 0 Then
        A% = MessageBox("", "Error", 0, 2)
        GoTo *CloseHandles
    End If

    Select Case ss.dwCurrentState
        Case SERVICE_STOPPED
            sTemp = "The Service is Stopped"
        Case SERVICE_START_PENDING
            sTemp = "The Service Being Started"
        Case SERVICE_STOP_PENDING
            sTemp = "The Service is in the process of being stopped"
        Case SERVICE_RUNNING
            sTemp = "The Service is Running"
        Case SERVICE_CONTINUE_PENDING
            sTemp = "The Service is in the process of being Continued"
        Case SERVICE_PAUSE_PENDING
            sTemp = "The Service is in the process of being Paused"
        Case SERVICE_PAUSED
            sTemp = "The Service is Paused"
        Case SERVICE_ACCEPT_STOP
            sTemp = "The Service is Stopped"
        Case SERVICE_ACCEPT_PAUSE_CONTINUE
            sTemp = "The Service is "
        Case SERVICE_ACCEPT_SHUTDOWN
            sTemp = "The Service is being Shutdown"
    End Select

    List1.AddString "[Service]"
    List1.AddString "  Status        : " & sTemp

    Dim abConfig(0) As Byte
    Ret = Api_QueryServiceConfig(hSVC, abConfig(0), 0, lBytesNeeded)

    Erase abConfig

    Var abConfig(lBytesNeeded) As Byte
    Ret = Api_QueryServiceConfig(hSVC, abConfig(0), lBytesNeeded, lBytesNeeded)

    CopyMemory qsc, abConfig(0), Len(qsc)

    List1.AddString "  Type          : " & Str$(qsc.dwServiceType)
    List1.AddString "  Start Type    : " & Str$(qsc.dwStartType)
    List1.AddString "  Error Control : " & Str$(qsc.dwErrorControl)

    sTemp = Space$(255)
    Ret = Api_lstrcpy(sTemp, qsc.lpBinaryPathName)
    List1.AddString "  Binary Path   : " & sTemp

    Ret = Api_lstrcpy(sTemp, qsc.lpDependencies)
    List1.AddString "  Dependencies  : " & sTemp

    Ret = Api_lstrcpy(sTemp, qsc.lpDisplayName)
    List1.AddString "  DisplayName   : " & sTemp

    Ret = Api_lstrcpy(sTemp, qsc.lpLoadOrderGroup)
    List1.AddString "  LoadOrderGroup: " & sTemp

    Ret = Api_lstrcpy(sTemp, qsc.lpServiceStartName)
    List1.AddString "  Start Name    : " & sTemp

*CloseHandles
    'サービスのハンドルをクローズ
    Ret = Api_CloseServiceHandle(hSVC)

    'サービスコントロールマネージャーのハンドルをクローズ
    Ret = Api_CloseServiceHandle(hSCM)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End