NotifyServiceStatusChangeW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
DWORD NotifyServiceStatusChangeW(
SC_HANDLE hService,
SERVICE_NOTIFY dwNotifyMask,
SERVICE_NOTIFY_2W* 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_2W* | in | コールバック関数へのポインターなど、通知情報を格納する SERVICE_NOTIFY 構造体へのポインター。この構造体は、コールバック関数が呼び出されるか、呼び出し側スレッドが通知要求をキャンセルするまで有効である必要があります。 最初の呼び出しのコールバック関数がバッファーの使用を終えるか、最初の通知要求がキャンセルされるまで、同じバッファーパラメーターを指定して NotifyServiceStatusChange を複数回呼び出さないでください。そうしないと、コールバック関数が受け取るバッファーのバージョンが保証されません。 Windows Vista: コールバック関数のアドレスは、ロード済みモジュールのアドレス範囲内になければなりません。したがって、コールバック関数を実行時に生成されるコード(JIT コンパイラーが生成するマネージドコードなど)や、実行時に展開されるネイティブコードにすることはできません。この制限は Windows Server 2008 および Windows Vista with SP1 で撤廃されました。 |
戻り値の型: DWORD
公式ドキュメント
指定したサービスが作成または削除されたとき、あるいはその状態が変化したときに、アプリケーションが通知を受け取れるようにします。(Unicode)
戻り値
関数が成功すると、戻り値は 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 (Unicode / -W)
#include <windows.h>
DWORD NotifyServiceStatusChangeW(
SC_HANDLE hService,
SERVICE_NOTIFY dwNotifyMask,
SERVICE_NOTIFY_2W* pNotifyBuffer
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint NotifyServiceStatusChangeW(
IntPtr hService, // SC_HANDLE
uint dwNotifyMask, // SERVICE_NOTIFY
IntPtr pNotifyBuffer // SERVICE_NOTIFY_2W*
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function NotifyServiceStatusChangeW(
hService As IntPtr, ' SC_HANDLE
dwNotifyMask As UInteger, ' SERVICE_NOTIFY
pNotifyBuffer As IntPtr ' SERVICE_NOTIFY_2W*
) As UInteger
End Function' hService : SC_HANDLE
' dwNotifyMask : SERVICE_NOTIFY
' pNotifyBuffer : SERVICE_NOTIFY_2W*
Declare PtrSafe Function NotifyServiceStatusChangeW Lib "advapi32" ( _
ByVal hService As LongPtr, _
ByVal dwNotifyMask As Long, _
ByVal pNotifyBuffer 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
NotifyServiceStatusChangeW = ctypes.windll.advapi32.NotifyServiceStatusChangeW
NotifyServiceStatusChangeW.restype = wintypes.DWORD
NotifyServiceStatusChangeW.argtypes = [
wintypes.HANDLE, # hService : SC_HANDLE
wintypes.DWORD, # dwNotifyMask : SERVICE_NOTIFY
ctypes.c_void_p, # pNotifyBuffer : SERVICE_NOTIFY_2W*
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
NotifyServiceStatusChangeW = Fiddle::Function.new(
lib['NotifyServiceStatusChangeW'],
[
Fiddle::TYPE_VOIDP, # hService : SC_HANDLE
-Fiddle::TYPE_INT, # dwNotifyMask : SERVICE_NOTIFY
Fiddle::TYPE_VOIDP, # pNotifyBuffer : SERVICE_NOTIFY_2W*
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "advapi32")]
extern "system" {
fn NotifyServiceStatusChangeW(
hService: *mut core::ffi::c_void, // SC_HANDLE
dwNotifyMask: u32, // SERVICE_NOTIFY
pNotifyBuffer: *mut SERVICE_NOTIFY_2W // SERVICE_NOTIFY_2W*
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint NotifyServiceStatusChangeW(IntPtr hService, uint dwNotifyMask, IntPtr pNotifyBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_NotifyServiceStatusChangeW' -Namespace Win32 -PassThru
# $api::NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer)#uselib "ADVAPI32.dll"
#func global NotifyServiceStatusChangeW "NotifyServiceStatusChangeW" wptr, wptr, wptr
; NotifyServiceStatusChangeW hService, dwNotifyMask, varptr(pNotifyBuffer) ; 戻り値は stat
; hService : SC_HANDLE -> "wptr"
; dwNotifyMask : SERVICE_NOTIFY -> "wptr"
; pNotifyBuffer : SERVICE_NOTIFY_2W* -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeW "NotifyServiceStatusChangeW" sptr, int, var ; res = NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer) ; hService : SC_HANDLE -> "sptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2W* -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeW "NotifyServiceStatusChangeW" sptr, int, sptr ; res = NotifyServiceStatusChangeW(hService, dwNotifyMask, varptr(pNotifyBuffer)) ; hService : SC_HANDLE -> "sptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2W* -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; DWORD NotifyServiceStatusChangeW(SC_HANDLE hService, SERVICE_NOTIFY dwNotifyMask, SERVICE_NOTIFY_2W* pNotifyBuffer) #uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeW "NotifyServiceStatusChangeW" intptr, int, var ; res = NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer) ; hService : SC_HANDLE -> "intptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2W* -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD NotifyServiceStatusChangeW(SC_HANDLE hService, SERVICE_NOTIFY dwNotifyMask, SERVICE_NOTIFY_2W* pNotifyBuffer) #uselib "ADVAPI32.dll" #cfunc global NotifyServiceStatusChangeW "NotifyServiceStatusChangeW" intptr, int, intptr ; res = NotifyServiceStatusChangeW(hService, dwNotifyMask, varptr(pNotifyBuffer)) ; hService : SC_HANDLE -> "intptr" ; dwNotifyMask : SERVICE_NOTIFY -> "int" ; pNotifyBuffer : SERVICE_NOTIFY_2W* -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procNotifyServiceStatusChangeW = advapi32.NewProc("NotifyServiceStatusChangeW")
)
// hService (SC_HANDLE), dwNotifyMask (SERVICE_NOTIFY), pNotifyBuffer (SERVICE_NOTIFY_2W*)
r1, _, err := procNotifyServiceStatusChangeW.Call(
uintptr(hService),
uintptr(dwNotifyMask),
uintptr(pNotifyBuffer),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction NotifyServiceStatusChangeW(
hService: THandle; // SC_HANDLE
dwNotifyMask: DWORD; // SERVICE_NOTIFY
pNotifyBuffer: Pointer // SERVICE_NOTIFY_2W*
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'NotifyServiceStatusChangeW';result := DllCall("ADVAPI32\NotifyServiceStatusChangeW"
, "Ptr", hService ; SC_HANDLE
, "UInt", dwNotifyMask ; SERVICE_NOTIFY
, "Ptr", pNotifyBuffer ; SERVICE_NOTIFY_2W*
, "UInt") ; return: DWORD●NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer) = DLL("ADVAPI32.dll", "dword NotifyServiceStatusChangeW(void*, dword, void*)")
# 呼び出し: NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer)
# hService : SC_HANDLE -> "void*"
# dwNotifyMask : SERVICE_NOTIFY -> "dword"
# pNotifyBuffer : SERVICE_NOTIFY_2W* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "advapi32" fn NotifyServiceStatusChangeW(
hService: ?*anyopaque, // SC_HANDLE
dwNotifyMask: u32, // SERVICE_NOTIFY
pNotifyBuffer: [*c]SERVICE_NOTIFY_2W // SERVICE_NOTIFY_2W*
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc NotifyServiceStatusChangeW(
hService: pointer, # SC_HANDLE
dwNotifyMask: uint32, # SERVICE_NOTIFY
pNotifyBuffer: ptr SERVICE_NOTIFY_2W # SERVICE_NOTIFY_2W*
): uint32 {.importc: "NotifyServiceStatusChangeW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
uint NotifyServiceStatusChangeW(
void* hService, // SC_HANDLE
uint dwNotifyMask, // SERVICE_NOTIFY
SERVICE_NOTIFY_2W* pNotifyBuffer // SERVICE_NOTIFY_2W*
);ccall((:NotifyServiceStatusChangeW, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, UInt32, Ptr{SERVICE_NOTIFY_2W}),
hService, dwNotifyMask, pNotifyBuffer)
# hService : SC_HANDLE -> Ptr{Cvoid}
# dwNotifyMask : SERVICE_NOTIFY -> UInt32
# pNotifyBuffer : SERVICE_NOTIFY_2W* -> Ptr{SERVICE_NOTIFY_2W}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t NotifyServiceStatusChangeW(
void* hService,
uint32_t dwNotifyMask,
void* pNotifyBuffer);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer)
-- hService : SC_HANDLE
-- dwNotifyMask : SERVICE_NOTIFY
-- pNotifyBuffer : SERVICE_NOTIFY_2W*
-- 構造体/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 NotifyServiceStatusChangeW = lib.func('__stdcall', 'NotifyServiceStatusChangeW', 'uint32_t', ['void *', 'uint32_t', 'void *']);
// NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer)
// hService : SC_HANDLE -> 'void *'
// dwNotifyMask : SERVICE_NOTIFY -> 'uint32_t'
// pNotifyBuffer : SERVICE_NOTIFY_2W* -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
NotifyServiceStatusChangeW: { parameters: ["pointer", "u32", "pointer"], result: "u32" },
});
// lib.symbols.NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer)
// hService : SC_HANDLE -> "pointer"
// dwNotifyMask : SERVICE_NOTIFY -> "u32"
// pNotifyBuffer : SERVICE_NOTIFY_2W* -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t NotifyServiceStatusChangeW(
void* hService,
uint32_t dwNotifyMask,
void* pNotifyBuffer);
C, "ADVAPI32.dll");
// $ffi->NotifyServiceStatusChangeW(hService, dwNotifyMask, pNotifyBuffer);
// hService : SC_HANDLE
// dwNotifyMask : SERVICE_NOTIFY
// pNotifyBuffer : SERVICE_NOTIFY_2W*
// 構造体/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);
int NotifyServiceStatusChangeW(
Pointer hService, // SC_HANDLE
int dwNotifyMask, // SERVICE_NOTIFY
Pointer pNotifyBuffer // SERVICE_NOTIFY_2W*
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun NotifyServiceStatusChangeW = NotifyServiceStatusChangeW(
hService : Void*, # SC_HANDLE
dwNotifyMask : UInt32, # SERVICE_NOTIFY
pNotifyBuffer : SERVICE_NOTIFY_2W* # SERVICE_NOTIFY_2W*
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef NotifyServiceStatusChangeWNative = Uint32 Function(Pointer<Void>, Uint32, Pointer<Void>);
typedef NotifyServiceStatusChangeWDart = int Function(Pointer<Void>, int, Pointer<Void>);
final NotifyServiceStatusChangeW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<NotifyServiceStatusChangeWNative, NotifyServiceStatusChangeWDart>('NotifyServiceStatusChangeW');
// hService : SC_HANDLE -> Pointer<Void>
// dwNotifyMask : SERVICE_NOTIFY -> Uint32
// pNotifyBuffer : SERVICE_NOTIFY_2W* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function NotifyServiceStatusChangeW(
hService: THandle; // SC_HANDLE
dwNotifyMask: DWORD; // SERVICE_NOTIFY
pNotifyBuffer: Pointer // SERVICE_NOTIFY_2W*
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'NotifyServiceStatusChangeW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "NotifyServiceStatusChangeW"
c_NotifyServiceStatusChangeW :: Ptr () -> Word32 -> Ptr () -> IO Word32
-- hService : SC_HANDLE -> Ptr ()
-- dwNotifyMask : SERVICE_NOTIFY -> Word32
-- pNotifyBuffer : SERVICE_NOTIFY_2W* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let notifyservicestatuschangew =
foreign "NotifyServiceStatusChangeW"
((ptr void) @-> uint32_t @-> (ptr void) @-> returning uint32_t)
(* hService : SC_HANDLE -> (ptr void) *)
(* dwNotifyMask : SERVICE_NOTIFY -> uint32_t *)
(* pNotifyBuffer : SERVICE_NOTIFY_2W* -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("NotifyServiceStatusChangeW" notify-service-status-change-w :convention :stdcall) :uint32
(h-service :pointer) ; SC_HANDLE
(dw-notify-mask :uint32) ; SERVICE_NOTIFY
(p-notify-buffer :pointer)) ; SERVICE_NOTIFY_2W*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $NotifyServiceStatusChangeW = Win32::API::More->new('ADVAPI32',
'DWORD NotifyServiceStatusChangeW(HANDLE hService, DWORD dwNotifyMask, LPVOID pNotifyBuffer)');
# my $ret = $NotifyServiceStatusChangeW->Call($hService, $dwNotifyMask, $pNotifyBuffer);
# hService : SC_HANDLE -> HANDLE
# dwNotifyMask : SERVICE_NOTIFY -> DWORD
# pNotifyBuffer : SERVICE_NOTIFY_2W* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f NotifyServiceStatusChangeA (ANSI版) — サービスの状態変化を非同期で通知するよう登録する。