QueryServiceLockStatusA
関数シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
BOOL QueryServiceLockStatusA(
SC_HANDLE hSCManager,
QUERY_SERVICE_LOCK_STATUSA* lpLockStatus, // optional
DWORD cbBufSize,
DWORD* pcbBytesNeeded
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hSCManager | SC_HANDLE | in | サービス制御マネージャーデータベースへのハンドル。このハンドルは OpenSCManager 関数によって返され、SC_MANAGER_QUERY_LOCK_STATUS アクセス権を持っている必要があります。詳細については、 Service Security and Access Rights を参照してください。 |
| lpLockStatus | QUERY_SERVICE_LOCK_STATUSA* | outoptional | 指定したデータベースのロック状態と、そのメンバーが指す文字列を受け取る QUERY_SERVICE_LOCK_STATUS 構造体へのポインター。 |
| cbBufSize | DWORD | in | lpLockStatus パラメーターが指すバッファーのサイズ(バイト単位)。 |
| pcbBytesNeeded | DWORD* | out | 関数が失敗した場合に、すべてのロック状態情報を返すために必要なバイト数を受け取る変数へのポインター。 |
戻り値の型: BOOL
公式ドキュメント
指定したサービス制御マネージャーデータベースのロック状態を取得します。(ANSI)
戻り値
関数が成功した場合、戻り値は 0 以外の値です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
次のエラーコードは、サービス制御マネージャーによって設定されることがあります。その他のエラーコードは、サービス制御マネージャーから呼び出されるレジストリ関数によって設定されることがあります。
| 戻りコード | 説明 |
|---|---|
| ハンドルが SC_MANAGER_QUERY_LOCK_STATUS アクセス権を持っていません。 | |
| ロック状態情報が lpLockStatus バッファーに収まる量を超えています。すべての情報を取得するために必要なバイト数は、pcbBytesNeeded パラメーターに返されます。lpLockStatus には何も書き込まれません。 | |
| 指定したハンドルが無効です。 |
解説(Remarks)
QueryServiceLockStatus 関数は、指定したデータベースがロックされているかどうかを示す QUERY_SERVICE_LOCK_STATUS 構造体を返します。データベースがロックされている場合、この構造体はロックを所有するユーザーのアカウント名と、ロックが保持されている時間の長さを提供します。
プロセスは、サービス制御マネージャーデータベースのロックの所有権を取得するために LockServiceDatabase 関数を呼び出し、ロックを解放するために UnlockServiceDatabase 関数を呼び出します。
winsvc.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして QueryServiceLockStatus を定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不整合につながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
BOOL QueryServiceLockStatusA(
SC_HANDLE hSCManager,
QUERY_SERVICE_LOCK_STATUSA* lpLockStatus, // optional
DWORD cbBufSize,
DWORD* pcbBytesNeeded
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool QueryServiceLockStatusA(
IntPtr hSCManager, // SC_HANDLE
IntPtr lpLockStatus, // QUERY_SERVICE_LOCK_STATUSA* optional, out
uint cbBufSize, // DWORD
out uint pcbBytesNeeded // DWORD* out
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function QueryServiceLockStatusA(
hSCManager As IntPtr, ' SC_HANDLE
lpLockStatus As IntPtr, ' QUERY_SERVICE_LOCK_STATUSA* optional, out
cbBufSize As UInteger, ' DWORD
<Out> ByRef pcbBytesNeeded As UInteger ' DWORD* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hSCManager : SC_HANDLE
' lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out
' cbBufSize : DWORD
' pcbBytesNeeded : DWORD* out
Declare PtrSafe Function QueryServiceLockStatusA Lib "advapi32" ( _
ByVal hSCManager As LongPtr, _
ByVal lpLockStatus 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
QueryServiceLockStatusA = ctypes.windll.advapi32.QueryServiceLockStatusA
QueryServiceLockStatusA.restype = wintypes.BOOL
QueryServiceLockStatusA.argtypes = [
wintypes.HANDLE, # hSCManager : SC_HANDLE
ctypes.c_void_p, # lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* 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')
QueryServiceLockStatusA = Fiddle::Function.new(
lib['QueryServiceLockStatusA'],
[
Fiddle::TYPE_VOIDP, # hSCManager : SC_HANDLE
Fiddle::TYPE_VOIDP, # lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out
-Fiddle::TYPE_INT, # cbBufSize : DWORD
Fiddle::TYPE_VOIDP, # pcbBytesNeeded : DWORD* out
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn QueryServiceLockStatusA(
hSCManager: *mut core::ffi::c_void, // SC_HANDLE
lpLockStatus: *mut QUERY_SERVICE_LOCK_STATUSA, // QUERY_SERVICE_LOCK_STATUSA* 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 QueryServiceLockStatusA(IntPtr hSCManager, IntPtr lpLockStatus, uint cbBufSize, out uint pcbBytesNeeded);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_QueryServiceLockStatusA' -Namespace Win32 -PassThru
# $api::QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)#uselib "ADVAPI32.dll"
#func global QueryServiceLockStatusA "QueryServiceLockStatusA" sptr, sptr, sptr, sptr
; QueryServiceLockStatusA hSCManager, varptr(lpLockStatus), cbBufSize, varptr(pcbBytesNeeded) ; 戻り値は stat
; hSCManager : SC_HANDLE -> "sptr"
; lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> "sptr"
; cbBufSize : DWORD -> "sptr"
; pcbBytesNeeded : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global QueryServiceLockStatusA "QueryServiceLockStatusA" sptr, var, int, var ; res = QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded) ; hSCManager : SC_HANDLE -> "sptr" ; lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> "var" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global QueryServiceLockStatusA "QueryServiceLockStatusA" sptr, sptr, int, sptr ; res = QueryServiceLockStatusA(hSCManager, varptr(lpLockStatus), cbBufSize, varptr(pcbBytesNeeded)) ; hSCManager : SC_HANDLE -> "sptr" ; lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> "sptr" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL QueryServiceLockStatusA(SC_HANDLE hSCManager, QUERY_SERVICE_LOCK_STATUSA* lpLockStatus, DWORD cbBufSize, DWORD* pcbBytesNeeded) #uselib "ADVAPI32.dll" #cfunc global QueryServiceLockStatusA "QueryServiceLockStatusA" intptr, var, int, var ; res = QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded) ; hSCManager : SC_HANDLE -> "intptr" ; lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> "var" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL QueryServiceLockStatusA(SC_HANDLE hSCManager, QUERY_SERVICE_LOCK_STATUSA* lpLockStatus, DWORD cbBufSize, DWORD* pcbBytesNeeded) #uselib "ADVAPI32.dll" #cfunc global QueryServiceLockStatusA "QueryServiceLockStatusA" intptr, intptr, int, intptr ; res = QueryServiceLockStatusA(hSCManager, varptr(lpLockStatus), cbBufSize, varptr(pcbBytesNeeded)) ; hSCManager : SC_HANDLE -> "intptr" ; lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> "intptr" ; cbBufSize : DWORD -> "int" ; pcbBytesNeeded : DWORD* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procQueryServiceLockStatusA = advapi32.NewProc("QueryServiceLockStatusA")
)
// hSCManager (SC_HANDLE), lpLockStatus (QUERY_SERVICE_LOCK_STATUSA* optional, out), cbBufSize (DWORD), pcbBytesNeeded (DWORD* out)
r1, _, err := procQueryServiceLockStatusA.Call(
uintptr(hSCManager),
uintptr(lpLockStatus),
uintptr(cbBufSize),
uintptr(pcbBytesNeeded),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction QueryServiceLockStatusA(
hSCManager: THandle; // SC_HANDLE
lpLockStatus: Pointer; // QUERY_SERVICE_LOCK_STATUSA* optional, out
cbBufSize: DWORD; // DWORD
pcbBytesNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'QueryServiceLockStatusA';result := DllCall("ADVAPI32\QueryServiceLockStatusA"
, "Ptr", hSCManager ; SC_HANDLE
, "Ptr", lpLockStatus ; QUERY_SERVICE_LOCK_STATUSA* optional, out
, "UInt", cbBufSize ; DWORD
, "Ptr", pcbBytesNeeded ; DWORD* out
, "Int") ; return: BOOL●QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded) = DLL("ADVAPI32.dll", "bool QueryServiceLockStatusA(void*, void*, dword, void*)")
# 呼び出し: QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)
# hSCManager : SC_HANDLE -> "void*"
# lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* 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 QueryServiceLockStatusA(
hSCManager: ?*anyopaque, // SC_HANDLE
lpLockStatus: [*c]QUERY_SERVICE_LOCK_STATUSA, // QUERY_SERVICE_LOCK_STATUSA* optional, out
cbBufSize: u32, // DWORD
pcbBytesNeeded: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;proc QueryServiceLockStatusA(
hSCManager: pointer, # SC_HANDLE
lpLockStatus: ptr QUERY_SERVICE_LOCK_STATUSA, # QUERY_SERVICE_LOCK_STATUSA* optional, out
cbBufSize: uint32, # DWORD
pcbBytesNeeded: ptr uint32 # DWORD* out
): int32 {.importc: "QueryServiceLockStatusA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
int QueryServiceLockStatusA(
void* hSCManager, // SC_HANDLE
QUERY_SERVICE_LOCK_STATUSA* lpLockStatus, // QUERY_SERVICE_LOCK_STATUSA* optional, out
uint cbBufSize, // DWORD
uint* pcbBytesNeeded // DWORD* out
);ccall((:QueryServiceLockStatusA, "ADVAPI32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{QUERY_SERVICE_LOCK_STATUSA}, UInt32, Ptr{UInt32}),
hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)
# hSCManager : SC_HANDLE -> Ptr{Cvoid}
# lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> Ptr{QUERY_SERVICE_LOCK_STATUSA}
# cbBufSize : DWORD -> UInt32
# pcbBytesNeeded : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t QueryServiceLockStatusA(
void* hSCManager,
void* lpLockStatus,
uint32_t cbBufSize,
uint32_t* pcbBytesNeeded);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)
-- hSCManager : SC_HANDLE
-- lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out
-- cbBufSize : DWORD
-- pcbBytesNeeded : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const QueryServiceLockStatusA = lib.func('__stdcall', 'QueryServiceLockStatusA', 'int32_t', ['void *', 'void *', 'uint32_t', 'uint32_t *']);
// QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)
// hSCManager : SC_HANDLE -> 'void *'
// lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> 'void *'
// cbBufSize : DWORD -> 'uint32_t'
// pcbBytesNeeded : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
QueryServiceLockStatusA: { parameters: ["pointer", "pointer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded)
// hSCManager : SC_HANDLE -> "pointer"
// lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* 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 QueryServiceLockStatusA(
void* hSCManager,
void* lpLockStatus,
uint32_t cbBufSize,
uint32_t* pcbBytesNeeded);
C, "ADVAPI32.dll");
// $ffi->QueryServiceLockStatusA(hSCManager, lpLockStatus, cbBufSize, pcbBytesNeeded);
// hSCManager : SC_HANDLE
// lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* 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 QueryServiceLockStatusA(
Pointer hSCManager, // SC_HANDLE
Pointer lpLockStatus, // QUERY_SERVICE_LOCK_STATUSA* optional, out
int cbBufSize, // DWORD
IntByReference pcbBytesNeeded // DWORD* out
);
}@[Link("advapi32")]
lib LibADVAPI32
fun QueryServiceLockStatusA = QueryServiceLockStatusA(
hSCManager : Void*, # SC_HANDLE
lpLockStatus : QUERY_SERVICE_LOCK_STATUSA*, # QUERY_SERVICE_LOCK_STATUSA* 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 QueryServiceLockStatusANative = Int32 Function(Pointer<Void>, Pointer<Void>, Uint32, Pointer<Uint32>);
typedef QueryServiceLockStatusADart = int Function(Pointer<Void>, Pointer<Void>, int, Pointer<Uint32>);
final QueryServiceLockStatusA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<QueryServiceLockStatusANative, QueryServiceLockStatusADart>('QueryServiceLockStatusA');
// hSCManager : SC_HANDLE -> Pointer<Void>
// lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> Pointer<Void>
// cbBufSize : DWORD -> Uint32
// pcbBytesNeeded : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function QueryServiceLockStatusA(
hSCManager: THandle; // SC_HANDLE
lpLockStatus: Pointer; // QUERY_SERVICE_LOCK_STATUSA* optional, out
cbBufSize: DWORD; // DWORD
pcbBytesNeeded: Pointer // DWORD* out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'QueryServiceLockStatusA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "QueryServiceLockStatusA"
c_QueryServiceLockStatusA :: Ptr () -> Ptr () -> Word32 -> Ptr Word32 -> IO CInt
-- hSCManager : SC_HANDLE -> Ptr ()
-- lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> Ptr ()
-- cbBufSize : DWORD -> Word32
-- pcbBytesNeeded : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let queryservicelockstatusa =
foreign "QueryServiceLockStatusA"
((ptr void) @-> (ptr void) @-> uint32_t @-> (ptr uint32_t) @-> returning int32_t)
(* hSCManager : SC_HANDLE -> (ptr void) *)
(* lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* 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 ("QueryServiceLockStatusA" query-service-lock-status-a :convention :stdcall) :int32
(h-scmanager :pointer) ; SC_HANDLE
(lp-lock-status :pointer) ; QUERY_SERVICE_LOCK_STATUSA* 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 $QueryServiceLockStatusA = Win32::API::More->new('ADVAPI32',
'BOOL QueryServiceLockStatusA(HANDLE hSCManager, LPVOID lpLockStatus, DWORD cbBufSize, LPVOID pcbBytesNeeded)');
# my $ret = $QueryServiceLockStatusA->Call($hSCManager, $lpLockStatus, $cbBufSize, $pcbBytesNeeded);
# hSCManager : SC_HANDLE -> HANDLE
# lpLockStatus : QUERY_SERVICE_LOCK_STATUSA* optional, out -> LPVOID
# cbBufSize : DWORD -> DWORD
# pcbBytesNeeded : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f QueryServiceLockStatusW (Unicode版) — SCMデータベースのロック状態を取得する。
- f LockServiceDatabase — SCMデータベースをロックする。
- f OpenSCManagerA — サービス制御マネージャーへの接続を確立しハンドルを取得する。
- f UnlockServiceDatabase — ロックしたSCMデータベースのロックを解除する。