Win32 API 日本語リファレンス
ホームSystem.Services › QueryServiceConfigA

QueryServiceConfigA

関数
サービスの構成情報を取得する。
DLLADVAPI32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

BOOL QueryServiceConfigA(
    SC_HANDLE hService,
    QUERY_SERVICE_CONFIGA* lpServiceConfig,   // optional
    DWORD cbBufSize,
    DWORD* pcbBytesNeeded
);

パラメーター

名前方向説明
hServiceSC_HANDLEinサービスへのハンドル。このハンドルは OpenService または CreateService 関数によって返され、SERVICE_QUERY_CONFIG アクセス権を持っている必要があります。詳細については、 Service Security and Access Rights を参照してください。
lpServiceConfigQUERY_SERVICE_CONFIGA*outoptional

サービス構成情報を受け取るバッファーへのポインター。データの形式は QUERY_SERVICE_CONFIG 構造体です。

この配列の最大サイズは 8K バイトです。必要なサイズを判別するには、このパラメーターに NULL を、cbBufSize パラメーターに 0 を指定します。関数は失敗し、GetLastErrorERROR_INSUFFICIENT_BUFFER を返します。必要なサイズは pcbBytesNeeded パラメーターで受け取られます。

cbBufSizeDWORDinlpServiceConfig パラメーターが指すバッファーのサイズ(バイト単位)。
pcbBytesNeededDWORD*out関数が ERROR_INSUFFICIENT_BUFFER で失敗した場合に、すべての構成情報を格納するために必要なバイト数を受け取る変数へのポインター。

戻り値の型: BOOL

公式ドキュメント

指定したサービスの構成パラメーターを取得します。(ANSI)

戻り値

関数が成功した場合、戻り値は 0 以外の値です。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

次のエラーコードはサービス制御マネージャーによって設定される場合があります。その他のコードは、サービス制御マネージャーから呼び出されるレジストリ関数によって設定される場合があります。

戻りコード 説明
ERROR_ACCESS_DENIED
ハンドルが SERVICE_QUERY_CONFIG アクセス権を持っていません。
ERROR_INSUFFICIENT_BUFFER
lpServiceConfig バッファーに収まる量を超えるサービス構成情報があります。すべての情報を取得するために必要なバイト数は pcbBytesNeeded パラメーターで返されます。lpServiceConfig には何も書き込まれません。
ERROR_INVALID_HANDLE
指定したハンドルが無効です。

解説(Remarks)

QueryServiceConfig 関数は、特定のサービスについてレジストリに保持されているサービス構成情報を返します。この構成情報は、まずサービス制御プログラムが CreateService 関数を使用して設定します。この情報は、サービス構成プログラムが ChangeServiceConfig 関数を使用して更新している場合があります。

構成情報が最後に変更された時点でサービスが実行中であった場合、 QueryServiceConfig が返す情報は、サービスの現在の構成を反映しません。代わりに、次回サービスが実行されるときの構成を反映します。ただし、DisplayName キーはこの例外です。DisplayName キーが変更されると、サービスが実行中かどうかにかかわらず、即座に有効になります。

例については、 Querying a Service's Configuration を参照してください。

メモ

winsvc.h ヘッダーは QueryServiceConfig をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

BOOL QueryServiceConfigA(
    SC_HANDLE hService,
    QUERY_SERVICE_CONFIGA* lpServiceConfig,   // optional
    DWORD cbBufSize,
    DWORD* pcbBytesNeeded
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool QueryServiceConfigA(
    IntPtr hService,   // SC_HANDLE
    IntPtr lpServiceConfig,   // QUERY_SERVICE_CONFIGA* optional, out
    uint cbBufSize,   // DWORD
    out uint pcbBytesNeeded   // DWORD* out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function QueryServiceConfigA(
    hService As IntPtr,   ' SC_HANDLE
    lpServiceConfig As IntPtr,   ' QUERY_SERVICE_CONFIGA* optional, out
    cbBufSize As UInteger,   ' DWORD
    <Out> ByRef pcbBytesNeeded As UInteger   ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hService : SC_HANDLE
' lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out
' cbBufSize : DWORD
' pcbBytesNeeded : DWORD* out
Declare PtrSafe Function QueryServiceConfigA Lib "advapi32" ( _
    ByVal hService As LongPtr, _
    ByVal lpServiceConfig As LongPtr, _
    ByVal cbBufSize As Long, _
    ByRef pcbBytesNeeded As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

QueryServiceConfigA = ctypes.windll.advapi32.QueryServiceConfigA
QueryServiceConfigA.restype = wintypes.BOOL
QueryServiceConfigA.argtypes = [
    wintypes.HANDLE,  # hService : SC_HANDLE
    ctypes.c_void_p,  # lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out
    wintypes.DWORD,  # cbBufSize : DWORD
    ctypes.POINTER(wintypes.DWORD),  # pcbBytesNeeded : DWORD* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
QueryServiceConfigA = Fiddle::Function.new(
  lib['QueryServiceConfigA'],
  [
    Fiddle::TYPE_VOIDP,  # hService : SC_HANDLE
    Fiddle::TYPE_VOIDP,  # lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out
    -Fiddle::TYPE_INT,  # cbBufSize : DWORD
    Fiddle::TYPE_VOIDP,  # pcbBytesNeeded : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn QueryServiceConfigA(
        hService: *mut core::ffi::c_void,  // SC_HANDLE
        lpServiceConfig: *mut QUERY_SERVICE_CONFIGA,  // QUERY_SERVICE_CONFIGA* optional, out
        cbBufSize: u32,  // DWORD
        pcbBytesNeeded: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool QueryServiceConfigA(IntPtr hService, IntPtr lpServiceConfig, uint cbBufSize, out uint pcbBytesNeeded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_QueryServiceConfigA' -Namespace Win32 -PassThru
# $api::QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
#uselib "ADVAPI32.dll"
#func global QueryServiceConfigA "QueryServiceConfigA" sptr, sptr, sptr, sptr
; QueryServiceConfigA hService, varptr(lpServiceConfig), cbBufSize, varptr(pcbBytesNeeded)   ; 戻り値は stat
; hService : SC_HANDLE -> "sptr"
; lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> "sptr"
; cbBufSize : DWORD -> "sptr"
; pcbBytesNeeded : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global QueryServiceConfigA "QueryServiceConfigA" sptr, var, int, var
; res = QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
; hService : SC_HANDLE -> "sptr"
; lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> "var"
; cbBufSize : DWORD -> "int"
; pcbBytesNeeded : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL QueryServiceConfigA(SC_HANDLE hService, QUERY_SERVICE_CONFIGA* lpServiceConfig, DWORD cbBufSize, DWORD* pcbBytesNeeded)
#uselib "ADVAPI32.dll"
#cfunc global QueryServiceConfigA "QueryServiceConfigA" intptr, var, int, var
; res = QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
; hService : SC_HANDLE -> "intptr"
; lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> "var"
; cbBufSize : DWORD -> "int"
; pcbBytesNeeded : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procQueryServiceConfigA = advapi32.NewProc("QueryServiceConfigA")
)

// hService (SC_HANDLE), lpServiceConfig (QUERY_SERVICE_CONFIGA* optional, out), cbBufSize (DWORD), pcbBytesNeeded (DWORD* out)
r1, _, err := procQueryServiceConfigA.Call(
	uintptr(hService),
	uintptr(lpServiceConfig),
	uintptr(cbBufSize),
	uintptr(pcbBytesNeeded),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function QueryServiceConfigA(
  hService: THandle;   // SC_HANDLE
  lpServiceConfig: Pointer;   // QUERY_SERVICE_CONFIGA* optional, out
  cbBufSize: DWORD;   // DWORD
  pcbBytesNeeded: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'QueryServiceConfigA';
result := DllCall("ADVAPI32\QueryServiceConfigA"
    , "Ptr", hService   ; SC_HANDLE
    , "Ptr", lpServiceConfig   ; QUERY_SERVICE_CONFIGA* optional, out
    , "UInt", cbBufSize   ; DWORD
    , "Ptr", pcbBytesNeeded   ; DWORD* out
    , "Int")   ; return: BOOL
●QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded) = DLL("ADVAPI32.dll", "bool QueryServiceConfigA(void*, void*, dword, void*)")
# 呼び出し: QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
# hService : SC_HANDLE -> "void*"
# lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> "void*"
# cbBufSize : DWORD -> "dword"
# pcbBytesNeeded : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "advapi32" fn QueryServiceConfigA(
    hService: ?*anyopaque, // SC_HANDLE
    lpServiceConfig: [*c]QUERY_SERVICE_CONFIGA, // QUERY_SERVICE_CONFIGA* optional, out
    cbBufSize: u32, // DWORD
    pcbBytesNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
proc QueryServiceConfigA(
    hService: pointer,  # SC_HANDLE
    lpServiceConfig: ptr QUERY_SERVICE_CONFIGA,  # QUERY_SERVICE_CONFIGA* optional, out
    cbBufSize: uint32,  # DWORD
    pcbBytesNeeded: ptr uint32  # DWORD* out
): int32 {.importc: "QueryServiceConfigA", stdcall, dynlib: "ADVAPI32.dll".}
pragma(lib, "advapi32");
extern(Windows)
int QueryServiceConfigA(
    void* hService,   // SC_HANDLE
    QUERY_SERVICE_CONFIGA* lpServiceConfig,   // QUERY_SERVICE_CONFIGA* optional, out
    uint cbBufSize,   // DWORD
    uint* pcbBytesNeeded   // DWORD* out
);
ccall((:QueryServiceConfigA, "ADVAPI32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{QUERY_SERVICE_CONFIGA}, UInt32, Ptr{UInt32}),
      hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
# hService : SC_HANDLE -> Ptr{Cvoid}
# lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> Ptr{QUERY_SERVICE_CONFIGA}
# cbBufSize : DWORD -> UInt32
# pcbBytesNeeded : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t QueryServiceConfigA(
    void* hService,
    void* lpServiceConfig,
    uint32_t cbBufSize,
    uint32_t* pcbBytesNeeded);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
-- hService : SC_HANDLE
-- lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out
-- cbBufSize : DWORD
-- pcbBytesNeeded : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const QueryServiceConfigA = lib.func('__stdcall', 'QueryServiceConfigA', 'int32_t', ['void *', 'void *', 'uint32_t', 'uint32_t *']);
// QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
// hService : SC_HANDLE -> 'void *'
// lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> 'void *'
// cbBufSize : DWORD -> 'uint32_t'
// pcbBytesNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  QueryServiceConfigA: { parameters: ["pointer", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded)
// hService : SC_HANDLE -> "pointer"
// lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> "pointer"
// cbBufSize : DWORD -> "u32"
// pcbBytesNeeded : DWORD* out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t QueryServiceConfigA(
    void* hService,
    void* lpServiceConfig,
    uint32_t cbBufSize,
    uint32_t* pcbBytesNeeded);
C, "ADVAPI32.dll");
// $ffi->QueryServiceConfigA(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded);
// hService : SC_HANDLE
// lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out
// cbBufSize : DWORD
// pcbBytesNeeded : DWORD* out
// 構造体/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.ASCII_OPTIONS);
    boolean QueryServiceConfigA(
        Pointer hService,   // SC_HANDLE
        Pointer lpServiceConfig,   // QUERY_SERVICE_CONFIGA* optional, out
        int cbBufSize,   // DWORD
        IntByReference pcbBytesNeeded   // DWORD* out
    );
}
@[Link("advapi32")]
lib LibADVAPI32
  fun QueryServiceConfigA = QueryServiceConfigA(
    hService : Void*,   # SC_HANDLE
    lpServiceConfig : QUERY_SERVICE_CONFIGA*,   # QUERY_SERVICE_CONFIGA* optional, out
    cbBufSize : UInt32,   # DWORD
    pcbBytesNeeded : UInt32*   # DWORD* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef QueryServiceConfigANative = Int32 Function(Pointer<Void>, Pointer<Void>, Uint32, Pointer<Uint32>);
typedef QueryServiceConfigADart = int Function(Pointer<Void>, Pointer<Void>, int, Pointer<Uint32>);
final QueryServiceConfigA = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<QueryServiceConfigANative, QueryServiceConfigADart>('QueryServiceConfigA');
// hService : SC_HANDLE -> Pointer<Void>
// lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> Pointer<Void>
// cbBufSize : DWORD -> Uint32
// pcbBytesNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function QueryServiceConfigA(
  hService: THandle;   // SC_HANDLE
  lpServiceConfig: Pointer;   // QUERY_SERVICE_CONFIGA* optional, out
  cbBufSize: DWORD;   // DWORD
  pcbBytesNeeded: Pointer   // DWORD* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'QueryServiceConfigA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "QueryServiceConfigA"
  c_QueryServiceConfigA :: Ptr () -> Ptr () -> Word32 -> Ptr Word32 -> IO CInt
-- hService : SC_HANDLE -> Ptr ()
-- lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> Ptr ()
-- cbBufSize : DWORD -> Word32
-- pcbBytesNeeded : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let queryserviceconfiga =
  foreign "QueryServiceConfigA"
    ((ptr void) @-> (ptr void) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* hService : SC_HANDLE -> (ptr void) *)
(* lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> (ptr void) *)
(* cbBufSize : DWORD -> uint32_t *)
(* pcbBytesNeeded : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("QueryServiceConfigA" query-service-config-a :convention :stdcall) :int32
  (h-service :pointer)   ; SC_HANDLE
  (lp-service-config :pointer)   ; QUERY_SERVICE_CONFIGA* optional, out
  (cb-buf-size :uint32)   ; DWORD
  (pcb-bytes-needed :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $QueryServiceConfigA = Win32::API::More->new('ADVAPI32',
    'BOOL QueryServiceConfigA(HANDLE hService, LPVOID lpServiceConfig, DWORD cbBufSize, LPVOID pcbBytesNeeded)');
# my $ret = $QueryServiceConfigA->Call($hService, $lpServiceConfig, $cbBufSize, $pcbBytesNeeded);
# hService : SC_HANDLE -> HANDLE
# lpServiceConfig : QUERY_SERVICE_CONFIGA* optional, out -> LPVOID
# cbBufSize : DWORD -> DWORD
# pcbBytesNeeded : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型