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

ExitProcess

関数
現在のプロセスを指定終了コードで終了する。
DLLKERNEL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

void ExitProcess(
    DWORD uExitCode
);

パラメーター

名前方向説明
uExitCodeDWORDinプロセスおよびすべてのスレッドの終了コード。

戻り値の型: void

公式ドキュメント

呼び出し元プロセスとそのすべてのスレッドを終了します。

解説(Remarks)

プロセスの終了値を取得するには、 GetExitCodeProcess 関数を使用します。スレッドの終了値を取得するには、 GetExitCodeThread 関数を使用します。

プロセスを終了すると、次の処理が行われます。

  1. 呼び出し元スレッドを除く、プロセス内のすべてのスレッドは、DLL_THREAD_DETACH 通知を受け取ることなく実行を終了します。
  2. 手順 1 で終了したすべてのスレッドの状態がシグナル状態になります。
  3. 読み込まれているすべてのダイナミックリンクライブラリ (DLL) のエントリポイント関数が DLL_PROCESS_DETACH で呼び出されます。
  4. アタッチされているすべての DLL がプロセス終了コードを実行した後、ExitProcess 関数は、呼び出し元スレッドを含む現在のプロセスを終了します。
  5. 呼び出し元スレッドの状態がシグナル状態になります。
  6. プロセスによって開かれたすべてのオブジェクトハンドルが閉じられます。
  7. プロセスの終了ステータスが STILL_ACTIVE からプロセスの終了値に変わります。
  8. プロセスオブジェクトの状態がシグナル状態になり、そのプロセスの終了を待機していたすべてのスレッドが満たされます。
プロセス内で終了したスレッドの 1 つがロックを保持しており、読み込まれている DLL のいずれかの DLL デタッチコードが同じロックを取得しようとした場合、ExitProcess を呼び出すとデッドロックが発生します。これに対し、プロセスが TerminateProcess を呼び出して終了する場合、そのプロセスがアタッチされている DLL にはプロセス終了が通知されません。したがって、プロセス内のすべてのスレッドの状態が不明な場合は、ExitProcess よりも TerminateProcess を呼び出す方が安全です。アプリケーションの main 関数から戻ると、ExitProcess の呼び出しが行われることに注意してください。

DLL 内で ExitProcess を呼び出すと、予期しないアプリケーションエラーやシステムエラーを引き起こす可能性があります。DLL 内から ExitProcess を呼び出すのは、その DLL を読み込むアプリケーションまたはシステムコンポーネントを把握しており、このコンテキストで ExitProcess を呼び出しても安全であるとわかっている場合のみにしてください。

プロセスを終了しても、子プロセスは終了しません。

プロセスを終了しても、必ずしもオペレーティングシステムからプロセスオブジェクトが削除されるわけではありません。プロセスオブジェクトは、そのプロセスへの最後のハンドルが閉じられたときに削除されます。

例については、 入力と出力をリダイレクトした子プロセスの作成を参照してください。

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

各言語での呼び出し定義

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

void ExitProcess(
    DWORD uExitCode
);
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern void ExitProcess(
    uint uExitCode   // DWORD
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Sub ExitProcess(
    uExitCode As UInteger   ' DWORD
)
End Sub
' uExitCode : DWORD
Declare PtrSafe Sub ExitProcess Lib "kernel32" ( _
    ByVal uExitCode As Long)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ExitProcess = ctypes.windll.kernel32.ExitProcess
ExitProcess.restype = None
ExitProcess.argtypes = [
    wintypes.DWORD,  # uExitCode : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
ExitProcess = Fiddle::Function.new(
  lib['ExitProcess'],
  [
    -Fiddle::TYPE_INT,  # uExitCode : DWORD
  ],
  Fiddle::TYPE_VOID)
#[link(name = "kernel32")]
extern "system" {
    fn ExitProcess(
        uExitCode: u32  // DWORD
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern void ExitProcess(uint uExitCode);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_ExitProcess' -Namespace Win32 -PassThru
# $api::ExitProcess(uExitCode)
#uselib "KERNEL32.dll"
#func global ExitProcess "ExitProcess" sptr
; ExitProcess uExitCode
; uExitCode : DWORD -> "sptr"
#uselib "KERNEL32.dll"
#func global ExitProcess "ExitProcess" int
; ExitProcess uExitCode
; uExitCode : DWORD -> "int"
; void ExitProcess(DWORD uExitCode)
#uselib "KERNEL32.dll"
#func global ExitProcess "ExitProcess" int
; ExitProcess uExitCode
; uExitCode : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procExitProcess = kernel32.NewProc("ExitProcess")
)

// uExitCode (DWORD)
r1, _, err := procExitProcess.Call(
	uintptr(uExitCode),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void
procedure ExitProcess(
  uExitCode: DWORD   // DWORD
); stdcall;
  external 'KERNEL32.dll' name 'ExitProcess';
result := DllCall("KERNEL32\ExitProcess"
    , "UInt", uExitCode   ; DWORD
    , "Int")   ; return: void
●ExitProcess(uExitCode) = DLL("KERNEL32.dll", "int ExitProcess(dword)")
# 呼び出し: ExitProcess(uExitCode)
# uExitCode : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef ExitProcessNative = Void Function(Uint32);
typedef ExitProcessDart = void Function(int);
final ExitProcess = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<ExitProcessNative, ExitProcessDart>('ExitProcess');
// uExitCode : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure ExitProcess(
  uExitCode: DWORD   // DWORD
); stdcall;
  external 'KERNEL32.dll' name 'ExitProcess';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ExitProcess"
  c_ExitProcess :: Word32 -> IO ()
-- uExitCode : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let exitprocess =
  foreign "ExitProcess"
    (uint32_t @-> returning void)
(* uExitCode : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("ExitProcess" exit-process :convention :stdcall) :void
  (u-exit-code :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ExitProcess = Win32::API::More->new('KERNEL32',
    'void ExitProcess(DWORD uExitCode)');
# my $ret = $ExitProcess->Call($uExitCode);
# uExitCode : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目