サービスを列挙 <TOP>
サービス(参照:http://yougo.ascii24.com/gh/13/001302.html)を列挙します。WindowsNT3.1以降
OpenSCManager サービスマネージャの指定されたデータベースを開く
EnumServicesStatus サービスを列挙
CloseServiceHandle サービスのハンドルを閉じる
lstrcpy 文字列をコピーする
コントロールパネル → 管理ツール → サービス で確認しています。
'================================================================ '= サービスを列挙 '= WindowsNT3.1以降
'= (OpenSCManager2.bas) '================================================================ #include "Windows.bi" #define ERROR_MORE_DATA 234 #define SERVICE_ACTIVE &H1 'Active状態のサービスを列挙 #define SERVICE_INACTIVE &H2 'SERVICE_STOPPED状態のサービスを列挙 #define SC_MANAGER_ENUMERATE_SERVICE &H4'SCMデータベースにインストールされているサービスを列挙するために必要なEnumServicesStatus(Ex)の呼び出しを有効にする #define SERVICE_WIN32_OWN_PROCESS &H10 '単体で動作するサービス #define SERVICE_WIN32_SHARE_PROCESS &H20'他のサービスと伴に動作するサービス #define SERVICE_WIN32 (&H10 Or &H20) '(SERVICE_WIN32_OWN_PROCESS Or SERVICE_WIN32_SHARE_PROCESS) #define vbNullString ByVal 0 '値0の文字列。値0を持つ文字列。空文字列ではない 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 ENUM_SERVICE_STATUS lpServiceName As Long lpDisplayName As Long ServiceStatus As SERVICE_STATUS End Type ' 指定されたコンピュータ上のサービス制御マネージャとの接続を確立し、サービス制御マネージャの指定されたデータベースを開く Declare Function Api_OpenSCManager& Lib "advapi32" Alias "OpenSCManagerA" (ByVal lpMachineName$, ByVal lpDatabaseName$, ByVal dwDesiredAccess&) ' サービス制御マネージャ(Service Control Manager:SCM)の指定されたデータベース内のサービスを列挙 Declare Function Api_EnumServicesStatus& Lib "advapi32" Alias "EnumServicesStatusA" (ByVal hSCManager&, ByVal dwServiceType&, ByVal dwServiceState&, lpServices As Any, ByVal cbBufSize&, pcbbNeeded&, lpReturned&, lpResumeHandle&) ' サービスコントロールマネージャオブジェクトまたはサービスオブジェクトへの指定されたハンドルを閉じる Declare Function Api_CloseServiceHandle& Lib "advapi32" Alias "CloseServiceHandle" (ByVal hSCObject&) ' 文字列をコピーする Declare Function Api_lstrcpy& Lib "kernel32" Alias "lstrcpyA" (ByVal szDest$, ByVal szcSource&) Var Shared List1 As Object List1.Attach GetDlgItem("List1") : List1.SetFontSize 12 '================================================================ '= Chr$(0)を取り除く '================================================================ Declare Function TrimNull(sInput As String) As String Function TrimNull(sInput As String) As String Var ZeroPos As Integer ZeroPos = InStr(1, sInput, Chr$(0)) If ZeroPos > 0 Then TrimNull = Left$(sInput, ZeroPos - 1) Else TrimNull = sInput End If End Function '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() Var hSCM As Long Var ess(261) As ENUM_SERVICE_STATUS Var InfoBuffer As Long Var ServiceName As String * 250 Var ByteNeed As Long Var ServiceRet As Long Var NextUnreadEntry As Long Var StructNeed As Long Var Ret As Long Var i As Long Var txt As String hSCM = Api_OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ENUMERATE_SERVICE) NextUnreadEntry = 0 Ret = Api_EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ByVal 0, 0, ByteNeed, ServiceRet, NextUnreadEntry) StructNeed = ByteNeed / Len(ess(0)) + 1 InfoBuffer = StructNeed * Len(ess(0)) NextUnreadEntry = 0 Ret = Api_EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ess(0), InfoBuffer, ByteNeed, ServiceRet, NextUnreadEntry) List1.Resetcontent List1.AddString "All registered services:" List1.AddString "------------------------" For i = 0 To ServiceRet - 1 Ret = Api_lstrcpy(ServiceName, ess(i).lpServiceName) txt = TrimNull(ServiceName) & " - " Ret = Api_lstrcpy(ServiceName, ess(i).lpDisplayName) txt = txt & TrimNull(ServiceName) List1.AddString txt Next i Ret = Api_CloseServiceHandle(hSCM) End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End