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

ExitWindowsEx

関数
Windowsのログオフや再起動、シャットダウンを実行する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL ExitWindowsEx(
    EXIT_WINDOWS_FLAGS uFlags,
    SHUTDOWN_REASON dwReason
);

パラメーター

名前方向
uFlagsEXIT_WINDOWS_FLAGSin
dwReasonSHUTDOWN_REASONin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL ExitWindowsEx(
    EXIT_WINDOWS_FLAGS uFlags,
    SHUTDOWN_REASON dwReason
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool ExitWindowsEx(
    uint uFlags,   // EXIT_WINDOWS_FLAGS
    uint dwReason   // SHUTDOWN_REASON
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ExitWindowsEx(
    uFlags As UInteger,   ' EXIT_WINDOWS_FLAGS
    dwReason As UInteger   ' SHUTDOWN_REASON
) As Boolean
End Function
' uFlags : EXIT_WINDOWS_FLAGS
' dwReason : SHUTDOWN_REASON
Declare PtrSafe Function ExitWindowsEx Lib "user32" ( _
    ByVal uFlags As Long, _
    ByVal dwReason As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ExitWindowsEx = ctypes.windll.user32.ExitWindowsEx
ExitWindowsEx.restype = wintypes.BOOL
ExitWindowsEx.argtypes = [
    wintypes.DWORD,  # uFlags : EXIT_WINDOWS_FLAGS
    wintypes.DWORD,  # dwReason : SHUTDOWN_REASON
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
ExitWindowsEx = Fiddle::Function.new(
  lib['ExitWindowsEx'],
  [
    -Fiddle::TYPE_INT,  # uFlags : EXIT_WINDOWS_FLAGS
    -Fiddle::TYPE_INT,  # dwReason : SHUTDOWN_REASON
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn ExitWindowsEx(
        uFlags: u32,  // EXIT_WINDOWS_FLAGS
        dwReason: u32  // SHUTDOWN_REASON
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_ExitWindowsEx' -Namespace Win32 -PassThru
# $api::ExitWindowsEx(uFlags, dwReason)
#uselib "USER32.dll"
#func global ExitWindowsEx "ExitWindowsEx" sptr, sptr
; ExitWindowsEx uFlags, dwReason   ; 戻り値は stat
; uFlags : EXIT_WINDOWS_FLAGS -> "sptr"
; dwReason : SHUTDOWN_REASON -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global ExitWindowsEx "ExitWindowsEx" int, int
; res = ExitWindowsEx(uFlags, dwReason)
; uFlags : EXIT_WINDOWS_FLAGS -> "int"
; dwReason : SHUTDOWN_REASON -> "int"
; BOOL ExitWindowsEx(EXIT_WINDOWS_FLAGS uFlags, SHUTDOWN_REASON dwReason)
#uselib "USER32.dll"
#cfunc global ExitWindowsEx "ExitWindowsEx" int, int
; res = ExitWindowsEx(uFlags, dwReason)
; uFlags : EXIT_WINDOWS_FLAGS -> "int"
; dwReason : SHUTDOWN_REASON -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procExitWindowsEx = user32.NewProc("ExitWindowsEx")
)

// uFlags (EXIT_WINDOWS_FLAGS), dwReason (SHUTDOWN_REASON)
r1, _, err := procExitWindowsEx.Call(
	uintptr(uFlags),
	uintptr(dwReason),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function ExitWindowsEx(
  uFlags: DWORD;   // EXIT_WINDOWS_FLAGS
  dwReason: DWORD   // SHUTDOWN_REASON
): BOOL; stdcall;
  external 'USER32.dll' name 'ExitWindowsEx';
result := DllCall("USER32\ExitWindowsEx"
    , "UInt", uFlags   ; EXIT_WINDOWS_FLAGS
    , "UInt", dwReason   ; SHUTDOWN_REASON
    , "Int")   ; return: BOOL
●ExitWindowsEx(uFlags, dwReason) = DLL("USER32.dll", "bool ExitWindowsEx(dword, dword)")
# 呼び出し: ExitWindowsEx(uFlags, dwReason)
# uFlags : EXIT_WINDOWS_FLAGS -> "dword"
# dwReason : SHUTDOWN_REASON -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。