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

GetCurrentThreadStackLimits

関数
現在のスレッドのスタック境界を取得する。
DLLKERNEL32.dll呼出規約winapi対応OSwindows8.0

シグネチャ

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

void GetCurrentThreadStackLimits(
    UINT_PTR* LowLimit,
    UINT_PTR* HighLimit
);

パラメーター

名前方向説明
LowLimitUINT_PTR*out現在のスレッドスタックの下限を受け取るポインター変数。
HighLimitUINT_PTR*out現在のスレッドスタックの上限を受け取るポインター変数。

戻り値の型: void

公式ドキュメント

現在のスレッドに対してシステムが割り当てたスタックの境界を取得します。

解説(Remarks)

ユーザーモードのコードが、スレッド作成時にシステムが割り当てた領域の外側のスタックメモリで実行される場合があります。呼び出し元は GetCurrentThreadStackLimits 関数を使用して、現在のスタックポインターが返された境界内にあることを確認できます。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT >= 0x0602 を設定します。詳細については、Windows ヘッダーの使用を参照してください。

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

各言語での呼び出し定義

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

void GetCurrentThreadStackLimits(
    UINT_PTR* LowLimit,
    UINT_PTR* HighLimit
);
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern void GetCurrentThreadStackLimits(
    out UIntPtr LowLimit,   // UINT_PTR* out
    out UIntPtr HighLimit   // UINT_PTR* out
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Sub GetCurrentThreadStackLimits(
    <Out> ByRef LowLimit As UIntPtr,   ' UINT_PTR* out
    <Out> ByRef HighLimit As UIntPtr   ' UINT_PTR* out
)
End Sub
' LowLimit : UINT_PTR* out
' HighLimit : UINT_PTR* out
Declare PtrSafe Sub GetCurrentThreadStackLimits Lib "kernel32" ( _
    ByRef LowLimit As LongPtr, _
    ByRef HighLimit As LongPtr)
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetCurrentThreadStackLimits = ctypes.windll.kernel32.GetCurrentThreadStackLimits
GetCurrentThreadStackLimits.restype = None
GetCurrentThreadStackLimits.argtypes = [
    ctypes.POINTER(ctypes.c_size_t),  # LowLimit : UINT_PTR* out
    ctypes.POINTER(ctypes.c_size_t),  # HighLimit : UINT_PTR* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetCurrentThreadStackLimits = Fiddle::Function.new(
  lib['GetCurrentThreadStackLimits'],
  [
    Fiddle::TYPE_VOIDP,  # LowLimit : UINT_PTR* out
    Fiddle::TYPE_VOIDP,  # HighLimit : UINT_PTR* out
  ],
  Fiddle::TYPE_VOID)
#[link(name = "kernel32")]
extern "system" {
    fn GetCurrentThreadStackLimits(
        LowLimit: *mut usize,  // UINT_PTR* out
        HighLimit: *mut usize  // UINT_PTR* out
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern void GetCurrentThreadStackLimits(out UIntPtr LowLimit, out UIntPtr HighLimit);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetCurrentThreadStackLimits' -Namespace Win32 -PassThru
# $api::GetCurrentThreadStackLimits(LowLimit, HighLimit)
#uselib "KERNEL32.dll"
#func global GetCurrentThreadStackLimits "GetCurrentThreadStackLimits" sptr, sptr
; GetCurrentThreadStackLimits varptr(LowLimit), varptr(HighLimit)
; LowLimit : UINT_PTR* out -> "sptr"
; HighLimit : UINT_PTR* out -> "sptr"
出力引数:
#uselib "KERNEL32.dll"
#func global GetCurrentThreadStackLimits "GetCurrentThreadStackLimits" var, var
; GetCurrentThreadStackLimits LowLimit, HighLimit
; LowLimit : UINT_PTR* out -> "var"
; HighLimit : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; void GetCurrentThreadStackLimits(UINT_PTR* LowLimit, UINT_PTR* HighLimit)
#uselib "KERNEL32.dll"
#func global GetCurrentThreadStackLimits "GetCurrentThreadStackLimits" var, var
; GetCurrentThreadStackLimits LowLimit, HighLimit
; LowLimit : UINT_PTR* out -> "var"
; HighLimit : UINT_PTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetCurrentThreadStackLimits = kernel32.NewProc("GetCurrentThreadStackLimits")
)

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

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

typedef GetCurrentThreadStackLimitsNative = Void Function(Pointer<UintPtr>, Pointer<UintPtr>);
typedef GetCurrentThreadStackLimitsDart = void Function(Pointer<UintPtr>, Pointer<UintPtr>);
final GetCurrentThreadStackLimits = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetCurrentThreadStackLimitsNative, GetCurrentThreadStackLimitsDart>('GetCurrentThreadStackLimits');
// LowLimit : UINT_PTR* out -> Pointer<UintPtr>
// HighLimit : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure GetCurrentThreadStackLimits(
  LowLimit: Pointer;   // UINT_PTR* out
  HighLimit: Pointer   // UINT_PTR* out
); stdcall;
  external 'KERNEL32.dll' name 'GetCurrentThreadStackLimits';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetCurrentThreadStackLimits"
  c_GetCurrentThreadStackLimits :: Ptr CUIntPtr -> Ptr CUIntPtr -> IO ()
-- LowLimit : UINT_PTR* out -> Ptr CUIntPtr
-- HighLimit : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getcurrentthreadstacklimits =
  foreign "GetCurrentThreadStackLimits"
    ((ptr size_t) @-> (ptr size_t) @-> returning void)
(* LowLimit : UINT_PTR* out -> (ptr size_t) *)
(* HighLimit : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("GetCurrentThreadStackLimits" get-current-thread-stack-limits :convention :stdcall) :void
  (low-limit :pointer)   ; UINT_PTR* out
  (high-limit :pointer))   ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetCurrentThreadStackLimits = Win32::API::More->new('KERNEL32',
    'void GetCurrentThreadStackLimits(LPVOID LowLimit, LPVOID HighLimit)');
# my $ret = $GetCurrentThreadStackLimits->Call($LowLimit, $HighLimit);
# LowLimit : UINT_PTR* out -> LPVOID
# HighLimit : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。