Win32 API 日本語リファレンス
ホームNetworkManagement.NetManagement › NetAlertRaise

NetAlertRaise

関数
登録されたクライアントにネットワークアラートを通知する。
DLLNETAPI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// NETAPI32.dll
#include <windows.h>

DWORD NetAlertRaise(
    LPCWSTR AlertType,
    void* Buffer,
    DWORD BufferSize
);

パラメーター

名前方向説明
AlertTypeLPCWSTRin

発生させるアラートクラス(アラートの種類)を指定する定数文字列へのポインターです。このパラメーターには、次の定義済みの値のいずれか、またはネットワークアプリケーション用のユーザー定義のアラートクラスを指定できます。アラートのイベント名には任意のテキスト文字列を使用できます。

名前 意味
ALERT_ADMIN_EVENT
管理者による対応が必要です。
ALERT_ERRORLOG_EVENT
エラーログにエントリが追加されました。
ALERT_MESSAGE_EVENT
ユーザーまたはアプリケーションがブロードキャストメッセージを受信しました。
ALERT_PRINT_EVENT
印刷ジョブが完了したか、印刷エラーが発生しました。
ALERT_USER_EVENT
アプリケーションまたはリソースが使用されました。
Buffervoid*in

割り込みメッセージを待ち受けているクライアントに送信するデータへのポインターです。データは固定長の STD_ALERT 構造体で始まり、その後に ADMIN_OTHER_INFOERRLOG_OTHER_INFOPRINT_OTHER_INFO、または USER_OTHER_INFO のいずれか 1 つの構造体による追加のメッセージデータが続く必要があります。最後に、バッファーには必要な可変長情報を含める必要があります。詳細については、後述の「解説」セクションのコードサンプルを参照してください。

呼び出し側のアプリケーションは、すべての構造体と可変データのメモリを割り当て、解放する必要があります。詳細については、 ネットワーク管理関数のバッファー を参照してください。

BufferSizeDWORDinメッセージバッファーのサイズ(バイト単位)です。

戻り値の型: DWORD

公式ドキュメント

NetAlertRaise 関数は、特定のイベントが発生したときに、登録されているすべてのクライアントに通知します。

戻り値

関数が成功した場合、戻り値は NERR_Success です。

関数が失敗した場合、戻り値はシステムエラーコードであり、次のいずれかのエラーコードになることがあります。考えられるすべてのエラーコードの一覧については、 システムエラーコード を参照してください。

戻り値 説明
ERROR_INVALID_PARAMETER
パラメーターが正しくありません。このエラーは、AlertEventName パラメーターが NULL または空の文字列である場合、Buffer パラメーターが NULL である場合、または BufferSize パラメーターが STD_ALERT 構造体のサイズに追加のメッセージデータ構造体の固定サイズを加えた値より小さい場合に返されます。
ERROR_NOT_SUPPORTED
要求はサポートされていません。このエラーは、Alerter サービスがサポートされていないため、Windows Vista 以降で返されます。

解説(Remarks)

NetAlertRaise 関数を正常に実行するために、特別なグループメンバーシップは必要ありません。

NetAlertRaise 関数を呼び出すときは、クライアントコンピューターで Alerter サービスが実行されている必要があります。実行されていない場合、関数は ERROR_FILE_NOT_FOUND で失敗します。

次のコードサンプルは、NetAlertRaise 関数を呼び出し、 STD_ALERT 構造体と ADMIN_OTHER_INFO 構造体を指定して、管理アラートを発生させる方法を示しています。まず、サンプルはメッセージバッファーのサイズを計算します。次に、 GlobalAlloc 関数を呼び出してバッファーを割り当てます。コードは、バッファーの STD_ALERT 部分および ADMIN_OTHER_INFO 部分のメンバーに値を割り当てます。サンプルは、 ALERT_OTHER_INFO マクロを呼び出して ADMIN_OTHER_INFO 構造体へのポインターを取得します。また、 ALERT_VAR_DATA マクロを呼び出して、バッファーの可変データ部分へのポインターを取得します。最後に、コードサンプルは GlobalFree 関数を呼び出して、バッファーに割り当てられたメモリを解放します。

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")

#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <lm.h>

const int ALERT_VAR_DATA_SIZE = 216;

int wmain(int argc, wchar_t *argv[])
{
   int nBufferSize;
   LPVOID pAlertOtherInfo;
   PSTD_ALERT pStdAlert;              // STD_ALERT structure
   PADMIN_OTHER_INFO pAdminOtherInfo; // ADMIN_OTHER_INFO structure
   LPVOID pVarData; 
   time_t now;
   DWORD dwResult;
   //
   // Check command line arguments.
   //
   if (argc != 2)
   {
      fwprintf(stderr, L"Usage: %s LogFileName\n", argv[0]);
      exit(1);
   }
   // Calculate the buffer size;
   //  then allocate the memory for the buffer.
   //
   nBufferSize  = sizeof(STD_ALERT) + ALERT_VAR_DATA_SIZE;
   pAlertOtherInfo = (LPVOID) GlobalAlloc(GPTR, nBufferSize);

   if (pAlertOtherInfo == NULL)
   {
       fwprintf(stderr, L"Unable to allocate memory\n");
       exit(1);
   }

   //
   // Assign values to the STD_ALERT portion of the buffer.
   //   (This is required when you call NetAlertRaise.)
   //
   pStdAlert = (PSTD_ALERT)pAlertOtherInfo;
   time( &now );
   pStdAlert->alrt_timestamp = (DWORD)now;
   wcscpy_s(pStdAlert->alrt_eventname, EVLEN + 1, ALERT_ADMIN_EVENT);
   wcscpy_s(pStdAlert->alrt_servicename, SNLEN + 1, argv[0]);
   //
   // Retrieve the pointer to the ADMIN_OTHER_INFO structure 
   //  that follows the STD_ALERT portion of the buffer.
   //   Do this by calling the ALERT_OTHER_INFO macro.
   //
   pAdminOtherInfo = (PADMIN_OTHER_INFO)ALERT_OTHER_INFO(pAlertOtherInfo);
   //
   // Assign values to the ADMIN_OTHER_INFO structure.
   //
   pAdminOtherInfo->alrtad_numstrings = 1;
   //
   // Error 2377, NERR_LogOverflow, indicates
   //  a log file is full.
   //
   pAdminOtherInfo->alrtad_errcode = 2377;
   //
   // Retrieve the pointer to the variable data portion
   //  of the buffer by calling the ALERT_VAR_DATA macro.
   //
   pVarData = (LPTSTR)ALERT_VAR_DATA(pAdminOtherInfo);
   //
   // Supply the log file name for error 2377.
   //
   wcsncpy_s((wchar_t*) pVarData, ALERT_VAR_DATA_SIZE/2, 
           argv[1],
           ALERT_VAR_DATA_SIZE/2 );
   //
   // Send an administrative alert by calling the
   //  NetAlertRaise function.
   //
   dwResult = NetAlertRaise(ALERT_ADMIN_EVENT,
                            pAlertOtherInfo,
                            nBufferSize);
   //
   // Display the results of the function call.
   //
   if (dwResult != NERR_Success)
      wprintf(L"NetAlertRaise failed: %d\n", dwResult);
   else
      wprintf(L"Administrative alert raised successfully.\n");
   //
   // Free the allocated memory.
   //
   GlobalFree(pAlertOtherInfo);

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

各言語での呼び出し定義

// NETAPI32.dll
#include <windows.h>

DWORD NetAlertRaise(
    LPCWSTR AlertType,
    void* Buffer,
    DWORD BufferSize
);
[DllImport("NETAPI32.dll", ExactSpelling = true)]
static extern uint NetAlertRaise(
    [MarshalAs(UnmanagedType.LPWStr)] string AlertType,   // LPCWSTR
    IntPtr Buffer,   // void*
    uint BufferSize   // DWORD
);
<DllImport("NETAPI32.dll", ExactSpelling:=True)>
Public Shared Function NetAlertRaise(
    <MarshalAs(UnmanagedType.LPWStr)> AlertType As String,   ' LPCWSTR
    Buffer As IntPtr,   ' void*
    BufferSize As UInteger   ' DWORD
) As UInteger
End Function
' AlertType : LPCWSTR
' Buffer : void*
' BufferSize : DWORD
Declare PtrSafe Function NetAlertRaise Lib "netapi32" ( _
    ByVal AlertType As LongPtr, _
    ByVal Buffer As LongPtr, _
    ByVal BufferSize As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

NetAlertRaise = ctypes.windll.netapi32.NetAlertRaise
NetAlertRaise.restype = wintypes.DWORD
NetAlertRaise.argtypes = [
    wintypes.LPCWSTR,  # AlertType : LPCWSTR
    ctypes.POINTER(None),  # Buffer : void*
    wintypes.DWORD,  # BufferSize : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('NETAPI32.dll')
NetAlertRaise = Fiddle::Function.new(
  lib['NetAlertRaise'],
  [
    Fiddle::TYPE_VOIDP,  # AlertType : LPCWSTR
    Fiddle::TYPE_VOIDP,  # Buffer : void*
    -Fiddle::TYPE_INT,  # BufferSize : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "netapi32")]
extern "system" {
    fn NetAlertRaise(
        AlertType: *const u16,  // LPCWSTR
        Buffer: *mut (),  // void*
        BufferSize: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("NETAPI32.dll")]
public static extern uint NetAlertRaise([MarshalAs(UnmanagedType.LPWStr)] string AlertType, IntPtr Buffer, uint BufferSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'NETAPI32_NetAlertRaise' -Namespace Win32 -PassThru
# $api::NetAlertRaise(AlertType, Buffer, BufferSize)
#uselib "NETAPI32.dll"
#func global NetAlertRaise "NetAlertRaise" sptr, sptr, sptr
; NetAlertRaise AlertType, Buffer, BufferSize   ; 戻り値は stat
; AlertType : LPCWSTR -> "sptr"
; Buffer : void* -> "sptr"
; BufferSize : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "NETAPI32.dll"
#cfunc global NetAlertRaise "NetAlertRaise" wstr, sptr, int
; res = NetAlertRaise(AlertType, Buffer, BufferSize)
; AlertType : LPCWSTR -> "wstr"
; Buffer : void* -> "sptr"
; BufferSize : DWORD -> "int"
; DWORD NetAlertRaise(LPCWSTR AlertType, void* Buffer, DWORD BufferSize)
#uselib "NETAPI32.dll"
#cfunc global NetAlertRaise "NetAlertRaise" wstr, intptr, int
; res = NetAlertRaise(AlertType, Buffer, BufferSize)
; AlertType : LPCWSTR -> "wstr"
; Buffer : void* -> "intptr"
; BufferSize : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	netapi32 = windows.NewLazySystemDLL("NETAPI32.dll")
	procNetAlertRaise = netapi32.NewProc("NetAlertRaise")
)

// AlertType (LPCWSTR), Buffer (void*), BufferSize (DWORD)
r1, _, err := procNetAlertRaise.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(AlertType))),
	uintptr(Buffer),
	uintptr(BufferSize),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function NetAlertRaise(
  AlertType: PWideChar;   // LPCWSTR
  Buffer: Pointer;   // void*
  BufferSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetAlertRaise';
result := DllCall("NETAPI32\NetAlertRaise"
    , "WStr", AlertType   ; LPCWSTR
    , "Ptr", Buffer   ; void*
    , "UInt", BufferSize   ; DWORD
    , "UInt")   ; return: DWORD
●NetAlertRaise(AlertType, Buffer, BufferSize) = DLL("NETAPI32.dll", "dword NetAlertRaise(char*, void*, dword)")
# 呼び出し: NetAlertRaise(AlertType, Buffer, BufferSize)
# AlertType : LPCWSTR -> "char*"
# Buffer : void* -> "void*"
# BufferSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "netapi32" fn NetAlertRaise(
    AlertType: [*c]const u16, // LPCWSTR
    Buffer: ?*anyopaque, // void*
    BufferSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc NetAlertRaise(
    AlertType: WideCString,  # LPCWSTR
    Buffer: pointer,  # void*
    BufferSize: uint32  # DWORD
): uint32 {.importc: "NetAlertRaise", stdcall, dynlib: "NETAPI32.dll".}
pragma(lib, "netapi32");
extern(Windows)
uint NetAlertRaise(
    const(wchar)* AlertType,   // LPCWSTR
    void* Buffer,   // void*
    uint BufferSize   // DWORD
);
ccall((:NetAlertRaise, "NETAPI32.dll"), stdcall, UInt32,
      (Cwstring, Ptr{Cvoid}, UInt32),
      AlertType, Buffer, BufferSize)
# AlertType : LPCWSTR -> Cwstring
# Buffer : void* -> Ptr{Cvoid}
# BufferSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t NetAlertRaise(
    const uint16_t* AlertType,
    void* Buffer,
    uint32_t BufferSize);
]]
local netapi32 = ffi.load("netapi32")
-- netapi32.NetAlertRaise(AlertType, Buffer, BufferSize)
-- AlertType : LPCWSTR
-- Buffer : void*
-- BufferSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('NETAPI32.dll');
const NetAlertRaise = lib.func('__stdcall', 'NetAlertRaise', 'uint32_t', ['str16', 'void *', 'uint32_t']);
// NetAlertRaise(AlertType, Buffer, BufferSize)
// AlertType : LPCWSTR -> 'str16'
// Buffer : void* -> 'void *'
// BufferSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("NETAPI32.dll", {
  NetAlertRaise: { parameters: ["buffer", "pointer", "u32"], result: "u32" },
});
// lib.symbols.NetAlertRaise(AlertType, Buffer, BufferSize)
// AlertType : LPCWSTR -> "buffer"
// Buffer : void* -> "pointer"
// BufferSize : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t NetAlertRaise(
    const uint16_t* AlertType,
    void* Buffer,
    uint32_t BufferSize);
C, "NETAPI32.dll");
// $ffi->NetAlertRaise(AlertType, Buffer, BufferSize);
// AlertType : LPCWSTR
// Buffer : void*
// BufferSize : DWORD
// 構造体/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 Netapi32 extends StdCallLibrary {
    Netapi32 INSTANCE = Native.load("netapi32", Netapi32.class);
    int NetAlertRaise(
        WString AlertType,   // LPCWSTR
        Pointer Buffer,   // void*
        int BufferSize   // DWORD
    );
}
@[Link("netapi32")]
lib LibNETAPI32
  fun NetAlertRaise = NetAlertRaise(
    AlertType : UInt16*,   # LPCWSTR
    Buffer : Void*,   # void*
    BufferSize : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef NetAlertRaiseNative = Uint32 Function(Pointer<Utf16>, Pointer<Void>, Uint32);
typedef NetAlertRaiseDart = int Function(Pointer<Utf16>, Pointer<Void>, int);
final NetAlertRaise = DynamicLibrary.open('NETAPI32.dll')
    .lookupFunction<NetAlertRaiseNative, NetAlertRaiseDart>('NetAlertRaise');
// AlertType : LPCWSTR -> Pointer<Utf16>
// Buffer : void* -> Pointer<Void>
// BufferSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function NetAlertRaise(
  AlertType: PWideChar;   // LPCWSTR
  Buffer: Pointer;   // void*
  BufferSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'NETAPI32.dll' name 'NetAlertRaise';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "NetAlertRaise"
  c_NetAlertRaise :: CWString -> Ptr () -> Word32 -> IO Word32
-- AlertType : LPCWSTR -> CWString
-- Buffer : void* -> Ptr ()
-- BufferSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let netalertraise =
  foreign "NetAlertRaise"
    ((ptr uint16_t) @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* AlertType : LPCWSTR -> (ptr uint16_t) *)
(* Buffer : void* -> (ptr void) *)
(* BufferSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library netapi32 (t "NETAPI32.dll"))
(cffi:use-foreign-library netapi32)

(cffi:defcfun ("NetAlertRaise" net-alert-raise :convention :stdcall) :uint32
  (alert-type (:string :encoding :utf-16le))   ; LPCWSTR
  (buffer :pointer)   ; void*
  (buffer-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $NetAlertRaise = Win32::API::More->new('NETAPI32',
    'DWORD NetAlertRaise(LPCWSTR AlertType, LPVOID Buffer, DWORD BufferSize)');
# my $ret = $NetAlertRaise->Call($AlertType, $Buffer, $BufferSize);
# AlertType : LPCWSTR -> LPCWSTR
# Buffer : void* -> LPVOID
# BufferSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目