EnumServicesStatusExW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
BOOL EnumServicesStatusExW(
SC_HANDLE hSCManager,
SC_ENUM_TYPE InfoLevel,
ENUM_SERVICE_TYPE dwServiceType,
ENUM_SERVICE_STATE dwServiceState,
BYTE* lpServices, // optional
DWORD cbBufSize,
DWORD* pcbBytesNeeded,
DWORD* lpServicesReturned,
DWORD* lpResumeHandle, // optional
LPCWSTR pszGroupName // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hSCManager | SC_HANDLE | in | サービス制御マネージャーデータベースへのハンドル。このハンドルは OpenSCManager 関数によって返され、SC_MANAGER_ENUMERATE_SERVICE アクセス権を持っている必要があります。詳細については、 Service Security and Access Rights を参照してください。 | ||||||||||||||
| InfoLevel | SC_ENUM_TYPE | in | 返されるサービス属性。データベース内の各サービスの名前とサービス状態情報を取得するには、SC_ENUM_PROCESS_INFO を使用します。lpServices パラメーターは、 ENUM_SERVICE_STATUS_PROCESS 構造体の配列を受け取るバッファーへのポインターです。このバッファーは、構造体と、そのメンバーが指す文字列を保持できる十分な大きさである必要があります。 現在のところ、他の情報レベルは定義されていません。 | ||||||||||||||
| dwServiceType | ENUM_SERVICE_TYPE | in | 列挙するサービスの種類。このパラメーターには、次の値を 1 つ以上指定できます。
| ||||||||||||||
| dwServiceState | ENUM_SERVICE_STATE | in | 列挙するサービスの状態。このパラメーターには、次のいずれかの値を指定できます。
| ||||||||||||||
| lpServices | BYTE* | outoptional | 状態情報を受け取るバッファーへのポインター。このデータの形式は、InfoLevel パラメーターの値によって異なります。 この配列の最大サイズは 256K バイトです。必要なサイズを確認するには、このパラメーターに NULL を、cbBufSize パラメーターに 0 を指定します。関数は失敗し、GetLastError は ERROR_MORE_DATA を返します。pcbBytesNeeded パラメーターには、必要なサイズが格納されます。 Windows Server 2003 および Windows XP: この配列の最大サイズは 64K バイトです。この上限は、Windows Server 2003 with SP1 および Windows XP with SP2 以降で引き上げられました。 | ||||||||||||||
| cbBufSize | DWORD | in | lpServices パラメーターが指すバッファーのサイズ(バイト単位)。 | ||||||||||||||
| pcbBytesNeeded | DWORD* | out | バッファーが小さすぎる場合に、残りのサービスエントリを返すために必要なバイト数を受け取る変数へのポインター。 | ||||||||||||||
| lpServicesReturned | DWORD* | out | 返されたサービスエントリの数を受け取る変数へのポインター。 | ||||||||||||||
| lpResumeHandle | DWORD* | inoutoptional | 入力時には、列挙の開始位置を指定する変数へのポインター。 EnumServicesStatusEx 関数を最初に呼び出すときには、この値を 0 に設定する必要があります。出力時には、関数が成功した場合この値は 0 になります。ただし、関数が 0 を返し、 GetLastError 関数が ERROR_MORE_DATA を返す場合、この値は、追加データを取得するために EnumServicesStatusEx 関数を呼び出したときに次に読み取るべきサービスエントリを示します。 | ||||||||||||||
| pszGroupName | LPCWSTR | inoptional | ロード順序グループ名。このパラメーターが文字列の場合、列挙されるのは、その文字列で指定された名前を持つグループに属するサービスのみです。このパラメーターが空文字列の場合、いずれのグループにも属さないサービスのみが列挙されます。このパラメーターが NULL の場合、グループのメンバーシップは無視され、すべてのサービスが列挙されます。 |
戻り値の型: BOOL
公式ドキュメント
指定したサービス制御マネージャーデータベース内のサービスを列挙します。各サービスの名前と状態に加え、指定した情報レベルに応じた追加データが返されます。(Unicode)
戻り値
関数が成功した場合、戻り値は 0 以外になります。
関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。次のエラーが返される場合があります。
| Return code | Description |
|---|---|
| ハンドルが SC_MANAGER_ENUMERATE_SERVICE アクセス権を持っていません。 | |
| バッファーが小さすぎます。アクティブなデータベース内のすべてのデータを返すことができませんでした。pcbBytesNeeded パラメーターには、残りのエントリを受け取るために必要なバイト数が格納されます。 | |
| 不正なパラメーター値が使用されました。 | |
| ハンドルが無効です。 | |
| InfoLevel パラメーターにサポートされていない値が含まれています。 | |
| システムがシャットダウン中です。この関数は呼び出せません。 |
解説(Remarks)
呼び出し元があるサービスに対して SERVICE_QUERY_STATUS アクセス権を持っていない場合、そのサービスはクライアントに返されるサービスの一覧から暗黙的に除外されます。
winsvc.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして EnumServicesStatusEx を定義します。エンコーディング非依存のエイリアスの使用を、エンコーディング非依存でないコードと混在させると、不整合が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
BOOL EnumServicesStatusExW(
SC_HANDLE hSCManager,
SC_ENUM_TYPE InfoLevel,
ENUM_SERVICE_TYPE dwServiceType,
ENUM_SERVICE_STATE dwServiceState,
BYTE* lpServices, // optional
DWORD cbBufSize,
DWORD* pcbBytesNeeded,
DWORD* lpServicesReturned,
DWORD* lpResumeHandle, // optional
LPCWSTR pszGroupName // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool EnumServicesStatusExW(
IntPtr hSCManager, // SC_HANDLE
int InfoLevel, // SC_ENUM_TYPE
uint dwServiceType, // ENUM_SERVICE_TYPE
uint dwServiceState, // ENUM_SERVICE_STATE
IntPtr lpServices, // BYTE* optional, out
uint cbBufSize, // DWORD
out uint pcbBytesNeeded, // DWORD* out
out uint lpServicesReturned, // DWORD* out
IntPtr lpResumeHandle, // DWORD* optional, in/out
[MarshalAs(UnmanagedType.LPWStr)] string pszGroupName // LPCWSTR optional
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function EnumServicesStatusExW(
hSCManager As IntPtr, ' SC_HANDLE
InfoLevel As Integer, ' SC_ENUM_TYPE
dwServiceType As UInteger, ' ENUM_SERVICE_TYPE
dwServiceState As UInteger, ' ENUM_SERVICE_STATE
lpServices As IntPtr, ' BYTE* optional, out
cbBufSize As UInteger, ' DWORD
<Out> ByRef pcbBytesNeeded As UInteger, ' DWORD* out
<Out> ByRef lpServicesReturned As UInteger, ' DWORD* out
lpResumeHandle As IntPtr, ' DWORD* optional, in/out
<MarshalAs(UnmanagedType.LPWStr)> pszGroupName As String ' LPCWSTR optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hSCManager : SC_HANDLE
' InfoLevel : SC_ENUM_TYPE
' dwServiceType : ENUM_SERVICE_TYPE
' dwServiceState : ENUM_SERVICE_STATE
' lpServices : BYTE* optional, out
' cbBufSize : DWORD
' pcbBytesNeeded : DWORD* out
' lpServicesReturned : DWORD* out
' lpResumeHandle : DWORD* optional, in/out
' pszGroupName : LPCWSTR optional
Declare PtrSafe Function EnumServicesStatusExW Lib "advapi32" ( _
ByVal hSCManager As LongPtr, _
ByVal InfoLevel As Long, _
ByVal dwServiceType As Long, _
ByVal dwServiceState As Long, _
ByVal lpServices As LongPtr, _
ByVal cbBufSize As Long, _
ByRef pcbBytesNeeded As Long, _
ByRef lpServicesReturned As Long, _
ByVal lpResumeHandle As LongPtr, _
ByVal pszGroupName As LongPtr) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
EnumServicesStatusExW = ctypes.windll.advapi32.EnumServicesStatusExW
EnumServicesStatusExW.restype = wintypes.BOOL
EnumServicesStatusExW.argtypes = [
wintypes.HANDLE, # hSCManager : SC_HANDLE
ctypes.c_int, # InfoLevel : SC_ENUM_TYPE
wintypes.DWORD, # dwServiceType : ENUM_SERVICE_TYPE
wintypes.DWORD, # dwServiceState : ENUM_SERVICE_STATE
ctypes.POINTER(ctypes.c_ubyte), # lpServices : BYTE* optional, out
wintypes.DWORD, # cbBufSize : DWORD
ctypes.POINTER(wintypes.DWORD), # pcbBytesNeeded : DWORD* out
ctypes.POINTER(wintypes.DWORD), # lpServicesReturned : DWORD* out
ctypes.POINTER(wintypes.DWORD), # lpResumeHandle : DWORD* optional, in/out
wintypes.LPCWSTR, # pszGroupName : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
EnumServicesStatusExW = Fiddle::Function.new(
lib['EnumServicesStatusExW'],
[
Fiddle::TYPE_VOIDP, # hSCManager : SC_HANDLE
Fiddle::TYPE_INT, # InfoLevel : SC_ENUM_TYPE
-Fiddle::TYPE_INT, # dwServiceType : ENUM_SERVICE_TYPE
-Fiddle::TYPE_INT, # dwServiceState : ENUM_SERVICE_STATE
Fiddle::TYPE_VOIDP, # lpServices : BYTE* optional, out
-Fiddle::TYPE_INT, # cbBufSize : DWORD
Fiddle::TYPE_VOIDP, # pcbBytesNeeded : DWORD* out
Fiddle::TYPE_VOIDP, # lpServicesReturned : DWORD* out
Fiddle::TYPE_VOIDP, # lpResumeHandle : DWORD* optional, in/out
Fiddle::TYPE_VOIDP, # pszGroupName : LPCWSTR optional
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "advapi32")]
extern "system" {
fn EnumServicesStatusExW(
hSCManager: *mut core::ffi::c_void, // SC_HANDLE
InfoLevel: i32, // SC_ENUM_TYPE
dwServiceType: u32, // ENUM_SERVICE_TYPE
dwServiceState: u32, // ENUM_SERVICE_STATE
lpServices: *mut u8, // BYTE* optional, out
cbBufSize: u32, // DWORD
pcbBytesNeeded: *mut u32, // DWORD* out
lpServicesReturned: *mut u32, // DWORD* out
lpResumeHandle: *mut u32, // DWORD* optional, in/out
pszGroupName: *const u16 // LPCWSTR optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool EnumServicesStatusExW(IntPtr hSCManager, int InfoLevel, uint dwServiceType, uint dwServiceState, IntPtr lpServices, uint cbBufSize, out uint pcbBytesNeeded, out uint lpServicesReturned, IntPtr lpResumeHandle, [MarshalAs(UnmanagedType.LPWStr)] string pszGroupName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_EnumServicesStatusExW' -Namespace Win32 -PassThru
# $api::EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)#uselib "ADVAPI32.dll"
#func global EnumServicesStatusExW "EnumServicesStatusExW" wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr, wptr
; EnumServicesStatusExW hSCManager, InfoLevel, dwServiceType, dwServiceState, varptr(lpServices), cbBufSize, varptr(pcbBytesNeeded), varptr(lpServicesReturned), varptr(lpResumeHandle), pszGroupName ; 戻り値は stat
; hSCManager : SC_HANDLE -> "wptr"
; InfoLevel : SC_ENUM_TYPE -> "wptr"
; dwServiceType : ENUM_SERVICE_TYPE -> "wptr"
; dwServiceState : ENUM_SERVICE_STATE -> "wptr"
; lpServices : BYTE* optional, out -> "wptr"
; cbBufSize : DWORD -> "wptr"
; pcbBytesNeeded : DWORD* out -> "wptr"
; lpServicesReturned : DWORD* out -> "wptr"
; lpResumeHandle : DWORD* optional, in/out -> "wptr"
; pszGroupName : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global EnumServicesStatusExW "EnumServicesStatusExW" sptr, int, int, int, var, int, var, var, var, wstr ; res = EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName) ; hSCManager : SC_HANDLE -> "sptr" ; InfoLevel : SC_ENUM_TYPE -> "int" ; dwServiceType : ENUM_SERVICE_TYPE -> "int" ; dwServiceState : ENUM_SERVICE_STATE -> "int" ; lpServices : BYTE* optional, out -> "var" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "var" ; lpServicesReturned : DWORD* out -> "var" ; lpResumeHandle : DWORD* optional, in/out -> "var" ; pszGroupName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global EnumServicesStatusExW "EnumServicesStatusExW" sptr, int, int, int, sptr, int, sptr, sptr, sptr, wstr ; res = EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, varptr(lpServices), cbBufSize, varptr(pcbBytesNeeded), varptr(lpServicesReturned), varptr(lpResumeHandle), pszGroupName) ; hSCManager : SC_HANDLE -> "sptr" ; InfoLevel : SC_ENUM_TYPE -> "int" ; dwServiceType : ENUM_SERVICE_TYPE -> "int" ; dwServiceState : ENUM_SERVICE_STATE -> "int" ; lpServices : BYTE* optional, out -> "sptr" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "sptr" ; lpServicesReturned : DWORD* out -> "sptr" ; lpResumeHandle : DWORD* optional, in/out -> "sptr" ; pszGroupName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL EnumServicesStatusExW(SC_HANDLE hSCManager, SC_ENUM_TYPE InfoLevel, ENUM_SERVICE_TYPE dwServiceType, ENUM_SERVICE_STATE dwServiceState, BYTE* lpServices, DWORD cbBufSize, DWORD* pcbBytesNeeded, DWORD* lpServicesReturned, DWORD* lpResumeHandle, LPCWSTR pszGroupName) #uselib "ADVAPI32.dll" #cfunc global EnumServicesStatusExW "EnumServicesStatusExW" intptr, int, int, int, var, int, var, var, var, wstr ; res = EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName) ; hSCManager : SC_HANDLE -> "intptr" ; InfoLevel : SC_ENUM_TYPE -> "int" ; dwServiceType : ENUM_SERVICE_TYPE -> "int" ; dwServiceState : ENUM_SERVICE_STATE -> "int" ; lpServices : BYTE* optional, out -> "var" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "var" ; lpServicesReturned : DWORD* out -> "var" ; lpResumeHandle : DWORD* optional, in/out -> "var" ; pszGroupName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL EnumServicesStatusExW(SC_HANDLE hSCManager, SC_ENUM_TYPE InfoLevel, ENUM_SERVICE_TYPE dwServiceType, ENUM_SERVICE_STATE dwServiceState, BYTE* lpServices, DWORD cbBufSize, DWORD* pcbBytesNeeded, DWORD* lpServicesReturned, DWORD* lpResumeHandle, LPCWSTR pszGroupName) #uselib "ADVAPI32.dll" #cfunc global EnumServicesStatusExW "EnumServicesStatusExW" intptr, int, int, int, intptr, int, intptr, intptr, intptr, wstr ; res = EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, varptr(lpServices), cbBufSize, varptr(pcbBytesNeeded), varptr(lpServicesReturned), varptr(lpResumeHandle), pszGroupName) ; hSCManager : SC_HANDLE -> "intptr" ; InfoLevel : SC_ENUM_TYPE -> "int" ; dwServiceType : ENUM_SERVICE_TYPE -> "int" ; dwServiceState : ENUM_SERVICE_STATE -> "int" ; lpServices : BYTE* optional, out -> "intptr" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "intptr" ; lpServicesReturned : DWORD* out -> "intptr" ; lpResumeHandle : DWORD* optional, in/out -> "intptr" ; pszGroupName : LPCWSTR optional -> "wstr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procEnumServicesStatusExW = advapi32.NewProc("EnumServicesStatusExW")
)
// hSCManager (SC_HANDLE), InfoLevel (SC_ENUM_TYPE), dwServiceType (ENUM_SERVICE_TYPE), dwServiceState (ENUM_SERVICE_STATE), lpServices (BYTE* optional, out), cbBufSize (DWORD), pcbBytesNeeded (DWORD* out), lpServicesReturned (DWORD* out), lpResumeHandle (DWORD* optional, in/out), pszGroupName (LPCWSTR optional)
r1, _, err := procEnumServicesStatusExW.Call(
uintptr(hSCManager),
uintptr(InfoLevel),
uintptr(dwServiceType),
uintptr(dwServiceState),
uintptr(lpServices),
uintptr(cbBufSize),
uintptr(pcbBytesNeeded),
uintptr(lpServicesReturned),
uintptr(lpResumeHandle),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszGroupName))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction EnumServicesStatusExW(
hSCManager: THandle; // SC_HANDLE
InfoLevel: Integer; // SC_ENUM_TYPE
dwServiceType: DWORD; // ENUM_SERVICE_TYPE
dwServiceState: DWORD; // ENUM_SERVICE_STATE
lpServices: Pointer; // BYTE* optional, out
cbBufSize: DWORD; // DWORD
pcbBytesNeeded: Pointer; // DWORD* out
lpServicesReturned: Pointer; // DWORD* out
lpResumeHandle: Pointer; // DWORD* optional, in/out
pszGroupName: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'EnumServicesStatusExW';result := DllCall("ADVAPI32\EnumServicesStatusExW"
, "Ptr", hSCManager ; SC_HANDLE
, "Int", InfoLevel ; SC_ENUM_TYPE
, "UInt", dwServiceType ; ENUM_SERVICE_TYPE
, "UInt", dwServiceState ; ENUM_SERVICE_STATE
, "Ptr", lpServices ; BYTE* optional, out
, "UInt", cbBufSize ; DWORD
, "Ptr", pcbBytesNeeded ; DWORD* out
, "Ptr", lpServicesReturned ; DWORD* out
, "Ptr", lpResumeHandle ; DWORD* optional, in/out
, "WStr", pszGroupName ; LPCWSTR optional
, "Int") ; return: BOOL●EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName) = DLL("ADVAPI32.dll", "bool EnumServicesStatusExW(void*, int, dword, dword, void*, dword, void*, void*, void*, char*)")
# 呼び出し: EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)
# hSCManager : SC_HANDLE -> "void*"
# InfoLevel : SC_ENUM_TYPE -> "int"
# dwServiceType : ENUM_SERVICE_TYPE -> "dword"
# dwServiceState : ENUM_SERVICE_STATE -> "dword"
# lpServices : BYTE* optional, out -> "void*"
# cbBufSize : DWORD -> "dword"
# pcbBytesNeeded : DWORD* out -> "void*"
# lpServicesReturned : DWORD* out -> "void*"
# lpResumeHandle : DWORD* optional, in/out -> "void*"
# pszGroupName : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "advapi32" fn EnumServicesStatusExW(
hSCManager: ?*anyopaque, // SC_HANDLE
InfoLevel: i32, // SC_ENUM_TYPE
dwServiceType: u32, // ENUM_SERVICE_TYPE
dwServiceState: u32, // ENUM_SERVICE_STATE
lpServices: [*c]u8, // BYTE* optional, out
cbBufSize: u32, // DWORD
pcbBytesNeeded: [*c]u32, // DWORD* out
lpServicesReturned: [*c]u32, // DWORD* out
lpResumeHandle: [*c]u32, // DWORD* optional, in/out
pszGroupName: [*c]const u16 // LPCWSTR optional
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc EnumServicesStatusExW(
hSCManager: pointer, # SC_HANDLE
InfoLevel: int32, # SC_ENUM_TYPE
dwServiceType: uint32, # ENUM_SERVICE_TYPE
dwServiceState: uint32, # ENUM_SERVICE_STATE
lpServices: ptr uint8, # BYTE* optional, out
cbBufSize: uint32, # DWORD
pcbBytesNeeded: ptr uint32, # DWORD* out
lpServicesReturned: ptr uint32, # DWORD* out
lpResumeHandle: ptr uint32, # DWORD* optional, in/out
pszGroupName: WideCString # LPCWSTR optional
): int32 {.importc: "EnumServicesStatusExW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
int EnumServicesStatusExW(
void* hSCManager, // SC_HANDLE
int InfoLevel, // SC_ENUM_TYPE
uint dwServiceType, // ENUM_SERVICE_TYPE
uint dwServiceState, // ENUM_SERVICE_STATE
ubyte* lpServices, // BYTE* optional, out
uint cbBufSize, // DWORD
uint* pcbBytesNeeded, // DWORD* out
uint* lpServicesReturned, // DWORD* out
uint* lpResumeHandle, // DWORD* optional, in/out
const(wchar)* pszGroupName // LPCWSTR optional
);ccall((:EnumServicesStatusExW, "ADVAPI32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Int32, UInt32, UInt32, Ptr{UInt8}, UInt32, Ptr{UInt32}, Ptr{UInt32}, Ptr{UInt32}, Cwstring),
hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)
# hSCManager : SC_HANDLE -> Ptr{Cvoid}
# InfoLevel : SC_ENUM_TYPE -> Int32
# dwServiceType : ENUM_SERVICE_TYPE -> UInt32
# dwServiceState : ENUM_SERVICE_STATE -> UInt32
# lpServices : BYTE* optional, out -> Ptr{UInt8}
# cbBufSize : DWORD -> UInt32
# pcbBytesNeeded : DWORD* out -> Ptr{UInt32}
# lpServicesReturned : DWORD* out -> Ptr{UInt32}
# lpResumeHandle : DWORD* optional, in/out -> Ptr{UInt32}
# pszGroupName : LPCWSTR optional -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t EnumServicesStatusExW(
void* hSCManager,
int32_t InfoLevel,
uint32_t dwServiceType,
uint32_t dwServiceState,
uint8_t* lpServices,
uint32_t cbBufSize,
uint32_t* pcbBytesNeeded,
uint32_t* lpServicesReturned,
uint32_t* lpResumeHandle,
const uint16_t* pszGroupName);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)
-- hSCManager : SC_HANDLE
-- InfoLevel : SC_ENUM_TYPE
-- dwServiceType : ENUM_SERVICE_TYPE
-- dwServiceState : ENUM_SERVICE_STATE
-- lpServices : BYTE* optional, out
-- cbBufSize : DWORD
-- pcbBytesNeeded : DWORD* out
-- lpServicesReturned : DWORD* out
-- lpResumeHandle : DWORD* optional, in/out
-- pszGroupName : LPCWSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const EnumServicesStatusExW = lib.func('__stdcall', 'EnumServicesStatusExW', 'int32_t', ['void *', 'int32_t', 'uint32_t', 'uint32_t', 'uint8_t *', 'uint32_t', 'uint32_t *', 'uint32_t *', 'uint32_t *', 'str16']);
// EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)
// hSCManager : SC_HANDLE -> 'void *'
// InfoLevel : SC_ENUM_TYPE -> 'int32_t'
// dwServiceType : ENUM_SERVICE_TYPE -> 'uint32_t'
// dwServiceState : ENUM_SERVICE_STATE -> 'uint32_t'
// lpServices : BYTE* optional, out -> 'uint8_t *'
// cbBufSize : DWORD -> 'uint32_t'
// pcbBytesNeeded : DWORD* out -> 'uint32_t *'
// lpServicesReturned : DWORD* out -> 'uint32_t *'
// lpResumeHandle : DWORD* optional, in/out -> 'uint32_t *'
// pszGroupName : LPCWSTR optional -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
EnumServicesStatusExW: { parameters: ["pointer", "i32", "u32", "u32", "pointer", "u32", "pointer", "pointer", "pointer", "buffer"], result: "i32" },
});
// lib.symbols.EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName)
// hSCManager : SC_HANDLE -> "pointer"
// InfoLevel : SC_ENUM_TYPE -> "i32"
// dwServiceType : ENUM_SERVICE_TYPE -> "u32"
// dwServiceState : ENUM_SERVICE_STATE -> "u32"
// lpServices : BYTE* optional, out -> "pointer"
// cbBufSize : DWORD -> "u32"
// pcbBytesNeeded : DWORD* out -> "pointer"
// lpServicesReturned : DWORD* out -> "pointer"
// lpResumeHandle : DWORD* optional, in/out -> "pointer"
// pszGroupName : LPCWSTR optional -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t EnumServicesStatusExW(
void* hSCManager,
int32_t InfoLevel,
uint32_t dwServiceType,
uint32_t dwServiceState,
uint8_t* lpServices,
uint32_t cbBufSize,
uint32_t* pcbBytesNeeded,
uint32_t* lpServicesReturned,
uint32_t* lpResumeHandle,
const uint16_t* pszGroupName);
C, "ADVAPI32.dll");
// $ffi->EnumServicesStatusExW(hSCManager, InfoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pszGroupName);
// hSCManager : SC_HANDLE
// InfoLevel : SC_ENUM_TYPE
// dwServiceType : ENUM_SERVICE_TYPE
// dwServiceState : ENUM_SERVICE_STATE
// lpServices : BYTE* optional, out
// cbBufSize : DWORD
// pcbBytesNeeded : DWORD* out
// lpServicesReturned : DWORD* out
// lpResumeHandle : DWORD* optional, in/out
// pszGroupName : LPCWSTR optional
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
boolean EnumServicesStatusExW(
Pointer hSCManager, // SC_HANDLE
int InfoLevel, // SC_ENUM_TYPE
int dwServiceType, // ENUM_SERVICE_TYPE
int dwServiceState, // ENUM_SERVICE_STATE
byte[] lpServices, // BYTE* optional, out
int cbBufSize, // DWORD
IntByReference pcbBytesNeeded, // DWORD* out
IntByReference lpServicesReturned, // DWORD* out
IntByReference lpResumeHandle, // DWORD* optional, in/out
WString pszGroupName // LPCWSTR optional
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun EnumServicesStatusExW = EnumServicesStatusExW(
hSCManager : Void*, # SC_HANDLE
InfoLevel : Int32, # SC_ENUM_TYPE
dwServiceType : UInt32, # ENUM_SERVICE_TYPE
dwServiceState : UInt32, # ENUM_SERVICE_STATE
lpServices : UInt8*, # BYTE* optional, out
cbBufSize : UInt32, # DWORD
pcbBytesNeeded : UInt32*, # DWORD* out
lpServicesReturned : UInt32*, # DWORD* out
lpResumeHandle : UInt32*, # DWORD* optional, in/out
pszGroupName : UInt16* # LPCWSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef EnumServicesStatusExWNative = Int32 Function(Pointer<Void>, Int32, Uint32, Uint32, Pointer<Uint8>, Uint32, Pointer<Uint32>, Pointer<Uint32>, Pointer<Uint32>, Pointer<Utf16>);
typedef EnumServicesStatusExWDart = int Function(Pointer<Void>, int, int, int, Pointer<Uint8>, int, Pointer<Uint32>, Pointer<Uint32>, Pointer<Uint32>, Pointer<Utf16>);
final EnumServicesStatusExW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<EnumServicesStatusExWNative, EnumServicesStatusExWDart>('EnumServicesStatusExW');
// hSCManager : SC_HANDLE -> Pointer<Void>
// InfoLevel : SC_ENUM_TYPE -> Int32
// dwServiceType : ENUM_SERVICE_TYPE -> Uint32
// dwServiceState : ENUM_SERVICE_STATE -> Uint32
// lpServices : BYTE* optional, out -> Pointer<Uint8>
// cbBufSize : DWORD -> Uint32
// pcbBytesNeeded : DWORD* out -> Pointer<Uint32>
// lpServicesReturned : DWORD* out -> Pointer<Uint32>
// lpResumeHandle : DWORD* optional, in/out -> Pointer<Uint32>
// pszGroupName : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function EnumServicesStatusExW(
hSCManager: THandle; // SC_HANDLE
InfoLevel: Integer; // SC_ENUM_TYPE
dwServiceType: DWORD; // ENUM_SERVICE_TYPE
dwServiceState: DWORD; // ENUM_SERVICE_STATE
lpServices: Pointer; // BYTE* optional, out
cbBufSize: DWORD; // DWORD
pcbBytesNeeded: Pointer; // DWORD* out
lpServicesReturned: Pointer; // DWORD* out
lpResumeHandle: Pointer; // DWORD* optional, in/out
pszGroupName: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'EnumServicesStatusExW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "EnumServicesStatusExW"
c_EnumServicesStatusExW :: Ptr () -> Int32 -> Word32 -> Word32 -> Ptr Word8 -> Word32 -> Ptr Word32 -> Ptr Word32 -> Ptr Word32 -> CWString -> IO CInt
-- hSCManager : SC_HANDLE -> Ptr ()
-- InfoLevel : SC_ENUM_TYPE -> Int32
-- dwServiceType : ENUM_SERVICE_TYPE -> Word32
-- dwServiceState : ENUM_SERVICE_STATE -> Word32
-- lpServices : BYTE* optional, out -> Ptr Word8
-- cbBufSize : DWORD -> Word32
-- pcbBytesNeeded : DWORD* out -> Ptr Word32
-- lpServicesReturned : DWORD* out -> Ptr Word32
-- lpResumeHandle : DWORD* optional, in/out -> Ptr Word32
-- pszGroupName : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let enumservicesstatusexw =
foreign "EnumServicesStatusExW"
((ptr void) @-> int32_t @-> uint32_t @-> uint32_t @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint32_t) @-> (ptr uint32_t) @-> (ptr uint32_t) @-> (ptr uint16_t) @-> returning int32_t)
(* hSCManager : SC_HANDLE -> (ptr void) *)
(* InfoLevel : SC_ENUM_TYPE -> int32_t *)
(* dwServiceType : ENUM_SERVICE_TYPE -> uint32_t *)
(* dwServiceState : ENUM_SERVICE_STATE -> uint32_t *)
(* lpServices : BYTE* optional, out -> (ptr uint8_t) *)
(* cbBufSize : DWORD -> uint32_t *)
(* pcbBytesNeeded : DWORD* out -> (ptr uint32_t) *)
(* lpServicesReturned : DWORD* out -> (ptr uint32_t) *)
(* lpResumeHandle : DWORD* optional, in/out -> (ptr uint32_t) *)
(* pszGroupName : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("EnumServicesStatusExW" enum-services-status-ex-w :convention :stdcall) :int32
(h-scmanager :pointer) ; SC_HANDLE
(info-level :int32) ; SC_ENUM_TYPE
(dw-service-type :uint32) ; ENUM_SERVICE_TYPE
(dw-service-state :uint32) ; ENUM_SERVICE_STATE
(lp-services :pointer) ; BYTE* optional, out
(cb-buf-size :uint32) ; DWORD
(pcb-bytes-needed :pointer) ; DWORD* out
(lp-services-returned :pointer) ; DWORD* out
(lp-resume-handle :pointer) ; DWORD* optional, in/out
(psz-group-name (:string :encoding :utf-16le))) ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $EnumServicesStatusExW = Win32::API::More->new('ADVAPI32',
'BOOL EnumServicesStatusExW(HANDLE hSCManager, int InfoLevel, DWORD dwServiceType, DWORD dwServiceState, LPVOID lpServices, DWORD cbBufSize, LPVOID pcbBytesNeeded, LPVOID lpServicesReturned, LPVOID lpResumeHandle, LPCWSTR pszGroupName)');
# my $ret = $EnumServicesStatusExW->Call($hSCManager, $InfoLevel, $dwServiceType, $dwServiceState, $lpServices, $cbBufSize, $pcbBytesNeeded, $lpServicesReturned, $lpResumeHandle, $pszGroupName);
# hSCManager : SC_HANDLE -> HANDLE
# InfoLevel : SC_ENUM_TYPE -> int
# dwServiceType : ENUM_SERVICE_TYPE -> DWORD
# dwServiceState : ENUM_SERVICE_STATE -> DWORD
# lpServices : BYTE* optional, out -> LPVOID
# cbBufSize : DWORD -> DWORD
# pcbBytesNeeded : DWORD* out -> LPVOID
# lpServicesReturned : DWORD* out -> LPVOID
# lpResumeHandle : DWORD* optional, in/out -> LPVOID
# pszGroupName : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f EnumServicesStatusExA (ANSI版) — サービスとその状態を種類や状態で絞って列挙する。
- f EnumServicesStatusW — SCM内のサービスとその状態を列挙する。