NotifyServiceStatusChangeA
関数シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
DWORD NotifyServiceStatusChangeA(
SC_HANDLE hService,
SERVICE_NOTIFY dwNotifyMask,
SERVICE_NOTIFY_2A* pNotifyBuffer
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hService | SC_HANDLE | in | サービスまたはサービス制御マネージャーへのハンドル。サービスへのハンドルは OpenService または CreateService 関数によって返され、SERVICE_QUERY_STATUS アクセス権を持っている必要があります。サービス制御マネージャーへのハンドルは OpenSCManager 関数によって返され、SC_MANAGER_ENUMERATE_SERVICE アクセス権を持っている必要があります。詳細については、 Service Security and Access Rights を参照してください。 サービスごとに、未処理の通知要求は 1 つしか存在できません。 | ||||||||||||||||||||||
| dwNotifyMask | SERVICE_NOTIFY | in | 報告すべき状態変化の種類。このパラメーターには、次の値のうち 1 つ以上を指定できます。
| ||||||||||||||||||||||
| pNotifyBuffer | SERVICE_NOTIFY_2A* | in | コールバック関数へのポインターなど、通知情報を格納する SERVICE_NOTIFY 構造体へのポインター。この構造体は、コールバック関数が呼び出されるか、呼び出し元スレッドが通知要求をキャンセルするまで有効なままである必要があります。 同じバッファーパラメーターを指定して NotifyServiceStatusChange を複数回呼び出すことは、最初の呼び出しのコールバック関数がそのバッファーの使用を終えるか、最初の通知要求がキャンセルされるまで行わないでください。そうしないと、コールバック関数がどのバージョンのバッファーを受け取るかは保証されません。 Windows Vista: コールバック関数のアドレスは、読み込まれたモジュールのアドレス範囲内になければなりません。したがって、コールバック関数を実行時に生成されるコード(JIT コンパイラーによって生成されるマネージドコードなど)や、実行時に展開されるネイティブコードにすることはできません。この制限は、Windows Server 2008 および Windows Vista with SP1 で撤廃されました。 |
戻り値の型: DWORD
公式ドキュメント
指定したサービスが作成または削除されたとき、あるいはその状態が変化したときに、アプリケーションが通知を受け取れるようにします。(ANSI)
戻り値
関数が成功した場合、戻り値は ERROR_SUCCESS です。サービスが削除対象としてマークされている場合、戻り値は ERROR_SERVICE_MARKED_FOR_DELETE となり、サービスへのハンドルを閉じる必要があります。サービス通知がシステムの状態に対して大きく遅れている場合、関数は ERROR_SERVICE_NOTIFY_CLIENT_LAGGING を返します。この場合、クライアントは SCM へのハンドルを閉じ、新しいハンドルを開いてから、この関数をもう一度呼び出す必要があります。
関数が失敗した場合、戻り値は システムエラーコード のいずれかです。
解説(Remarks)
NotifyServiceStatusChange 関数は、サービスアプリケーションに関する通知を受け取るために使用できます。ドライバーサービスに関する通知を受け取るために使用することはできません。
サービスの状態が変化すると、システムは指定されたコールバック関数を、呼び出し元スレッドにキューイングされた非同期プロシージャ呼び出し (APC) として呼び出します。呼び出し元スレッドは、通知を受け取るためにアラート可能な待機状態に入る (たとえば SleepEx 関数を呼び出す) 必要があります。詳細については、Asynchronous Procedure Calls を参照してください。
NotifyServiceStatusChange が呼び出された時点でサービスが要求された状態のいずれかに既にある場合、コールバック関数は直ちにキューイングされます。同じサービスと状態を指定して次にこの関数が呼び出される時点までにサービスの状態が変化していない場合、コールバック関数は直ちにはキューイングされず、サービスが次に要求された状態に入ったときにキューイングされます。
NotifyServiceStatusChange 関数は、呼び出し元スレッドに対して THREAD_SET_CONTEXT アクセス権を指定して OpenThread 関数を呼び出します。呼び出し元スレッドがこのアクセス権を持っていない場合、NotifyServiceStatusChange は失敗します。呼び出し元スレッドが別のユーザーを偽装している場合、コンテキストを設定するのに十分な権限を持っていない可能性があります。
追加のスレッドを作成するよりも、待機を行うスレッドから NotifyServiceStatusChange を呼び出すほうが効率的です。
コールバック関数が呼び出された後、追加の通知を受け取るには、呼び出し元が NotifyServiceStatusChange を再度呼び出す必要があります。NotifyServiceStatusChange やその他の SCM 関数を含む Windows API の一部の関数はリモートプロシージャ呼び出し (RPC) を使用することに注意してください。これらの関数はアラート可能な待機操作を実行する可能性があるため、コールバック関数内から呼び出すのは安全ではありません。代わりに、コールバック関数は通知パラメーターを保存し、追加の処理はコールバックの外で行うべきです。
未処理の通知をキャンセルするには、CloseServiceHandle 関数を使用してサービスハンドルを閉じます。CloseServiceHandle が成功した後は、通知 APC はそれ以上キューイングされません。呼び出し元スレッドが、サービスハンドルを閉じることなく、また APC が生成されるまで待機することなく終了した場合、メモリリークが発生する可能性があります。
winsvc.h ヘッダーは、NotifyServiceStatusChange を、UNICODE プリプロセッサー定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング中立のエイリアスを、エンコーディング中立でないコードと混在して使用すると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
DWORD NotifyServiceStatusChangeA(
SC_HANDLE hService,
SERVICE_NOTIFY dwNotifyMask,
SERVICE_NOTIFY_2A* pNotifyBuffer
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint NotifyServiceStatusChangeA(
IntPtr hService, // SC_HANDLE
uint dwNotifyMask, // SERVICE_NOTIFY
IntPtr pNotifyBuffer // SERVICE_NOTIFY_2A*
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function NotifyServiceStatusChangeA(
hService As IntPtr, ' SC_HANDLE
dwNotifyMask As UInteger, ' SERVICE_NOTIFY
pNotifyBuffer As IntPtr ' SERVICE_NOTIFY_2A*
) As UInteger
End Function' hService : SC_HANDLE
' dwNotifyMask : SERVICE_NOTIFY
' pNotifyBuffer : SERVICE_NOTIFY_2A*
Declare PtrSafe Function NotifyServiceStatusChangeA Lib "advapi32" ( _
ByVal hService As LongPtr, _
ByVal dwNotifyMask As Long, _
ByVal pNotifyBuffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
NotifyServiceStatusChangeA = ctypes.windll.advapi32.NotifyServiceStatusChangeA
NotifyServiceStatusChangeA.restype = wintypes.DWORD
NotifyServiceStatusChangeA.argtypes = [
wintypes.HANDLE, # hService : SC_HANDLE
wintypes.DWORD, # dwNotifyMask : SERVICE_NOTIFY
ctypes.c_void_p, # pNotifyBuffer : SERVICE_NOTIFY_2A*
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
NotifyServiceStatusChangeA = Fiddle::Function.new(
lib['NotifyServiceStatusChangeA'],
[
Fiddle::TYPE_VOIDP, # hService : SC_HANDLE
-Fiddle::TYPE_INT, # dwNotifyMask : SERVICE_NOTIFY
Fiddle::TYPE_VOIDP, # pNotifyBuffer : SERVICE_NOTIFY_2A*
],
-Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn NotifyServiceStatusChangeA(
hService: *mut core::ffi::c_void, // SC_HANDLE
dwNotifyMask: u32, // SERVICE_NOTIFY
pNotifyBuffer: *mut SERVICE_NOTIFY_2A // SERVICE_NOTIFY_2A*
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi)]
public static extern uint NotifyServiceStatusChangeA(IntPtr hService, uint dwNotifyMask, IntPtr pNotifyBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_NotifyServiceStatusChangeA' -Namespace Win32 -PassThru
# $api::NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer)#uselib "ADVAPI32.dll"
#func global NotifyServiceStatusChangeA "NotifyServiceStatusChangeA" sptr, sptr, sptr
; NotifyServiceStatusChangeA hService, dwNotifyMask, varptr(pNotifyBuffer) ; 戻り値は stat
; hService : SC_HANDLE -> "sptr"
; dwNotifyMask : SERVICE_NOTIFY -> "sptr"
; pNotifyBuffer : SERVICE_NOTIFY_2A* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeA "NotifyServiceStatusChangeA" sptr, int, var ; res = NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer) ; hService : SC_HANDLE -> "sptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2A* -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeA "NotifyServiceStatusChangeA" sptr, int, sptr ; res = NotifyServiceStatusChangeA(hService, dwNotifyMask, varptr(pNotifyBuffer)) ; hService : SC_HANDLE -> "sptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2A* -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; DWORD NotifyServiceStatusChangeA(SC_HANDLE hService, SERVICE_NOTIFY dwNotifyMask, SERVICE_NOTIFY_2A* pNotifyBuffer) #uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeA "NotifyServiceStatusChangeA" intptr, int, var ; res = NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer) ; hService : SC_HANDLE -> "intptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2A* -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD NotifyServiceStatusChangeA(SC_HANDLE hService, SERVICE_NOTIFY dwNotifyMask, SERVICE_NOTIFY_2A* pNotifyBuffer) #uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeA "NotifyServiceStatusChangeA" intptr, int, intptr ; res = NotifyServiceStatusChangeA(hService, dwNotifyMask, varptr(pNotifyBuffer)) ; hService : SC_HANDLE -> "intptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2A* -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procNotifyServiceStatusChangeA = advapi32.NewProc("NotifyServiceStatusChangeA")
)
// hService (SC_HANDLE), dwNotifyMask (SERVICE_NOTIFY), pNotifyBuffer (SERVICE_NOTIFY_2A*)
r1, _, err := procNotifyServiceStatusChangeA.Call(
uintptr(hService),
uintptr(dwNotifyMask),
uintptr(pNotifyBuffer),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction NotifyServiceStatusChangeA(
hService: THandle; // SC_HANDLE
dwNotifyMask: DWORD; // SERVICE_NOTIFY
pNotifyBuffer: Pointer // SERVICE_NOTIFY_2A*
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'NotifyServiceStatusChangeA';result := DllCall("ADVAPI32\NotifyServiceStatusChangeA"
, "Ptr", hService ; SC_HANDLE
, "UInt", dwNotifyMask ; SERVICE_NOTIFY
, "Ptr", pNotifyBuffer ; SERVICE_NOTIFY_2A*
, "UInt") ; return: DWORD●NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer) = DLL("ADVAPI32.dll", "dword NotifyServiceStatusChangeA(void*, dword, void*)")
# 呼び出し: NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer)
# hService : SC_HANDLE -> "void*"
# dwNotifyMask : SERVICE_NOTIFY -> "dword"
# pNotifyBuffer : SERVICE_NOTIFY_2A* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn NotifyServiceStatusChangeA(
hService: ?*anyopaque, // SC_HANDLE
dwNotifyMask: u32, // SERVICE_NOTIFY
pNotifyBuffer: [*c]SERVICE_NOTIFY_2A // SERVICE_NOTIFY_2A*
) callconv(std.os.windows.WINAPI) u32;proc NotifyServiceStatusChangeA(
hService: pointer, # SC_HANDLE
dwNotifyMask: uint32, # SERVICE_NOTIFY
pNotifyBuffer: ptr SERVICE_NOTIFY_2A # SERVICE_NOTIFY_2A*
): uint32 {.importc: "NotifyServiceStatusChangeA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
uint NotifyServiceStatusChangeA(
void* hService, // SC_HANDLE
uint dwNotifyMask, // SERVICE_NOTIFY
SERVICE_NOTIFY_2A* pNotifyBuffer // SERVICE_NOTIFY_2A*
);ccall((:NotifyServiceStatusChangeA, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, UInt32, Ptr{SERVICE_NOTIFY_2A}),
hService, dwNotifyMask, pNotifyBuffer)
# hService : SC_HANDLE -> Ptr{Cvoid}
# dwNotifyMask : SERVICE_NOTIFY -> UInt32
# pNotifyBuffer : SERVICE_NOTIFY_2A* -> Ptr{SERVICE_NOTIFY_2A}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t NotifyServiceStatusChangeA(
void* hService,
uint32_t dwNotifyMask,
void* pNotifyBuffer);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer)
-- hService : SC_HANDLE
-- dwNotifyMask : SERVICE_NOTIFY
-- pNotifyBuffer : SERVICE_NOTIFY_2A*
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const NotifyServiceStatusChangeA = lib.func('__stdcall', 'NotifyServiceStatusChangeA', 'uint32_t', ['void *', 'uint32_t', 'void *']);
// NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer)
// hService : SC_HANDLE -> 'void *'
// dwNotifyMask : SERVICE_NOTIFY -> 'uint32_t'
// pNotifyBuffer : SERVICE_NOTIFY_2A* -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
NotifyServiceStatusChangeA: { parameters: ["pointer", "u32", "pointer"], result: "u32" },
});
// lib.symbols.NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer)
// hService : SC_HANDLE -> "pointer"
// dwNotifyMask : SERVICE_NOTIFY -> "u32"
// pNotifyBuffer : SERVICE_NOTIFY_2A* -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t NotifyServiceStatusChangeA(
void* hService,
uint32_t dwNotifyMask,
void* pNotifyBuffer);
C, "ADVAPI32.dll");
// $ffi->NotifyServiceStatusChangeA(hService, dwNotifyMask, pNotifyBuffer);
// hService : SC_HANDLE
// dwNotifyMask : SERVICE_NOTIFY
// pNotifyBuffer : SERVICE_NOTIFY_2A*
// 構造体/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);
int NotifyServiceStatusChangeA(
Pointer hService, // SC_HANDLE
int dwNotifyMask, // SERVICE_NOTIFY
Pointer pNotifyBuffer // SERVICE_NOTIFY_2A*
);
}@[Link("advapi32")]
lib LibADVAPI32
fun NotifyServiceStatusChangeA = NotifyServiceStatusChangeA(
hService : Void*, # SC_HANDLE
dwNotifyMask : UInt32, # SERVICE_NOTIFY
pNotifyBuffer : SERVICE_NOTIFY_2A* # SERVICE_NOTIFY_2A*
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef NotifyServiceStatusChangeANative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Void>);
typedef NotifyServiceStatusChangeADart = int Function(Pointer<Void>, int, Pointer<Void>);
final NotifyServiceStatusChangeA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<NotifyServiceStatusChangeANative, NotifyServiceStatusChangeADart>('NotifyServiceStatusChangeA');
// hService : SC_HANDLE -> Pointer<Void>
// dwNotifyMask : SERVICE_NOTIFY -> Uint32
// pNotifyBuffer : SERVICE_NOTIFY_2A* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NotifyServiceStatusChangeA(
hService: THandle; // SC_HANDLE
dwNotifyMask: DWORD; // SERVICE_NOTIFY
pNotifyBuffer: Pointer // SERVICE_NOTIFY_2A*
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'NotifyServiceStatusChangeA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NotifyServiceStatusChangeA"
c_NotifyServiceStatusChangeA :: Ptr () -> Word32 -> Ptr () -> IO Word32
-- hService : SC_HANDLE -> Ptr ()
-- dwNotifyMask : SERVICE_NOTIFY -> Word32
-- pNotifyBuffer : SERVICE_NOTIFY_2A* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let notifyservicestatuschangea =
foreign "NotifyServiceStatusChangeA"
((ptr void) @-> uint32_t @-> (ptr void) @-> returning uint32_t)
(* hService : SC_HANDLE -> (ptr void) *)
(* dwNotifyMask : SERVICE_NOTIFY -> uint32_t *)
(* pNotifyBuffer : SERVICE_NOTIFY_2A* -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("NotifyServiceStatusChangeA" notify-service-status-change-a :convention :stdcall) :uint32
(h-service :pointer) ; SC_HANDLE
(dw-notify-mask :uint32) ; SERVICE_NOTIFY
(p-notify-buffer :pointer)) ; SERVICE_NOTIFY_2A*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NotifyServiceStatusChangeA = Win32::API::More->new('ADVAPI32',
'DWORD NotifyServiceStatusChangeA(HANDLE hService, DWORD dwNotifyMask, LPVOID pNotifyBuffer)');
# my $ret = $NotifyServiceStatusChangeA->Call($hService, $dwNotifyMask, $pNotifyBuffer);
# hService : SC_HANDLE -> HANDLE
# dwNotifyMask : SERVICE_NOTIFY -> DWORD
# pNotifyBuffer : SERVICE_NOTIFY_2A* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f NotifyServiceStatusChangeW (Unicode版) — サービスの状態変化を非同期で通知するよう登録する。