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

SuspendThread

関数
指定スレッドの実行を一時停止する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD SuspendThread(
    HANDLE hThread
);

パラメーター

名前方向説明
hThreadHANDLEin

中断するスレッドのハンドル。

このハンドルには THREAD_SUSPEND_RESUME アクセス権が必要です。詳細については、 スレッドのセキュリティとアクセス権 を参照してください。

戻り値の型: DWORD

公式ドキュメント

指定したスレッドを中断します。

戻り値

関数が成功した場合、戻り値はスレッドの直前の中断カウントです。失敗した場合は (DWORD) -1 です。拡張エラー情報を取得するには、 GetLastError 関数を使用します。

解説(Remarks)

関数が成功すると、指定したスレッドの実行が中断され、スレッドの中断カウントがインクリメントされます。スレッドを中断すると、そのスレッドはユーザーモード(アプリケーション)コードの実行を停止します。

この関数は主にデバッガーで使用することを目的として設計されています。スレッドの同期に使用することは意図されていません。ミューテックスやクリティカルセクションなどの同期オブジェクトを所有するスレッドに対して SuspendThread を呼び出すと、呼び出し側スレッドが中断されたスレッドの所有する同期オブジェクトを取得しようとした場合にデッドロックが発生する可能性があります。この状況を回避するには、デバッガーではないアプリケーション内のスレッドは、対象のスレッドに対して自身を中断するようシグナルを送る必要があります。対象のスレッドは、このシグナルを監視し適切に応答するように設計されている必要があります。

各スレッドは中断カウント(最大値は MAXIMUM_SUSPEND_COUNT)を持ちます。中断カウントが 0 より大きい場合、スレッドは中断されています。それ以外の場合、スレッドは中断されておらず、実行の対象となります。 SuspendThread を呼び出すと、対象スレッドの中断カウントがインクリメントされます。最大中断カウントを超えてインクリメントしようとすると、カウントはインクリメントされずにエラーが発生します。

ResumeThread 関数は、中断されたスレッドの中断カウントをデクリメントします。

Windows Phone 8.1: この関数は、Windows Phone 8.1 以降の Windows Phone ストアアプリでサポートされています。

Windows 8.1 および Windows Server 2012 R2: この関数は、Windows 8.1、Windows Server 2012 R2 以降の Windows ストアアプリでサポートされています。

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

各言語での呼び出し定義

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

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

SuspendThread = ctypes.windll.kernel32.SuspendThread
SuspendThread.restype = wintypes.DWORD
SuspendThread.argtypes = [
    wintypes.HANDLE,  # hThread : HANDLE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SuspendThread = Fiddle::Function.new(
  lib['SuspendThread'],
  [
    Fiddle::TYPE_VOIDP,  # hThread : HANDLE
  ],
  -Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SuspendThread(
        hThread: *mut core::ffi::c_void  // HANDLE
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern uint SuspendThread(IntPtr hThread);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SuspendThread' -Namespace Win32 -PassThru
# $api::SuspendThread(hThread)
#uselib "KERNEL32.dll"
#func global SuspendThread "SuspendThread" sptr
; SuspendThread hThread   ; 戻り値は stat
; hThread : HANDLE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global SuspendThread "SuspendThread" sptr
; res = SuspendThread(hThread)
; hThread : HANDLE -> "sptr"
; DWORD SuspendThread(HANDLE hThread)
#uselib "KERNEL32.dll"
#cfunc global SuspendThread "SuspendThread" intptr
; res = SuspendThread(hThread)
; hThread : HANDLE -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSuspendThread = kernel32.NewProc("SuspendThread")
)

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

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

typedef SuspendThreadNative = Uint32 Function(Pointer<Void>);
typedef SuspendThreadDart = int Function(Pointer<Void>);
final SuspendThread = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<SuspendThreadNative, SuspendThreadDart>('SuspendThread');
// hThread : HANDLE -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SuspendThread(
  hThread: THandle   // HANDLE
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'SuspendThread';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

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

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

関連項目

公式の関連項目