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

GetThreadDescription

関数
スレッドに設定された説明名を取得する。
DLLKERNEL32.dll呼出規約winapi対応OSWindows 10 以降

シグネチャ

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

HRESULT GetThreadDescription(
    HANDLE hThread,
    LPWSTR* ppszThreadDescription
);

パラメーター

名前方向説明
hThreadHANDLEin説明を取得するスレッドへのハンドル。このハンドルには THREAD_QUERY_LIMITED_INFORMATION アクセス権が必要です。
ppszThreadDescriptionLPWSTR*outスレッドの説明を含む Unicode 文字列です。

戻り値の型: HRESULT

公式ドキュメント

SetThreadDescription を呼び出してスレッドに割り当てられた説明を取得します。

戻り値

関数が成功した場合、戻り値は処理の成功を示す HRESULT です。 関数が失敗した場合、戻り値はエラーを示す HRESULT です。

解説(Remarks)

Windows Server 2016Windows 10 LTSB 2016、および Windows 10 version 1607: GetThreadDescription は、KernelBase.dll 内の 実行時動的リンク によってのみ利用できます。

スレッドの説明はいつでも変更される可能性があります。たとえば、対象スレッドの説明を取得しようとしている間に、別のスレッドがその説明を変更することがあります。

スレッドの説明は一意である必要はありません。

スレッドの説明のメモリを解放するには、LocalFree メソッドを呼び出します。

次の例は、スレッドの説明を取得し、その説明を出力してから、説明のメモリを解放します。

HRESULT hr = GetThreadDescription(ThreadHandle, &data);
if (SUCCEEDED(hr))
{   
    wprintf(L"%ls\n", data);
    LocalFree(data);
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

GetThreadDescription = ctypes.windll.kernel32.GetThreadDescription
GetThreadDescription.restype = ctypes.c_int
GetThreadDescription.argtypes = [
    wintypes.HANDLE,  # hThread : HANDLE
    ctypes.c_void_p,  # ppszThreadDescription : LPWSTR* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
GetThreadDescription = Fiddle::Function.new(
  lib['GetThreadDescription'],
  [
    Fiddle::TYPE_VOIDP,  # hThread : HANDLE
    Fiddle::TYPE_VOIDP,  # ppszThreadDescription : LPWSTR* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn GetThreadDescription(
        hThread: *mut core::ffi::c_void,  // HANDLE
        ppszThreadDescription: *mut *mut u16  // LPWSTR* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll")]
public static extern int GetThreadDescription(IntPtr hThread, IntPtr ppszThreadDescription);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetThreadDescription' -Namespace Win32 -PassThru
# $api::GetThreadDescription(hThread, ppszThreadDescription)
#uselib "KERNEL32.dll"
#func global GetThreadDescription "GetThreadDescription" sptr, sptr
; GetThreadDescription hThread, varptr(ppszThreadDescription)   ; 戻り値は stat
; hThread : HANDLE -> "sptr"
; ppszThreadDescription : LPWSTR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global GetThreadDescription "GetThreadDescription" sptr, var
; res = GetThreadDescription(hThread, ppszThreadDescription)
; hThread : HANDLE -> "sptr"
; ppszThreadDescription : LPWSTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT GetThreadDescription(HANDLE hThread, LPWSTR* ppszThreadDescription)
#uselib "KERNEL32.dll"
#cfunc global GetThreadDescription "GetThreadDescription" intptr, var
; res = GetThreadDescription(hThread, ppszThreadDescription)
; hThread : HANDLE -> "intptr"
; ppszThreadDescription : LPWSTR* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetThreadDescription = kernel32.NewProc("GetThreadDescription")
)

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

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

typedef GetThreadDescriptionNative = Int32 Function(Pointer<Void>, Pointer<Pointer<Utf16>>);
typedef GetThreadDescriptionDart = int Function(Pointer<Void>, Pointer<Pointer<Utf16>>);
final GetThreadDescription = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetThreadDescriptionNative, GetThreadDescriptionDart>('GetThreadDescription');
// hThread : HANDLE -> Pointer<Void>
// ppszThreadDescription : LPWSTR* out -> Pointer<Pointer<Utf16>>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetThreadDescription(
  hThread: THandle;   // HANDLE
  ppszThreadDescription: PPWideChar   // LPWSTR* out
): Integer; stdcall;
  external 'KERNEL32.dll' name 'GetThreadDescription';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetThreadDescription"
  c_GetThreadDescription :: Ptr () -> Ptr CWString -> IO Int32
-- hThread : HANDLE -> Ptr ()
-- ppszThreadDescription : LPWSTR* out -> Ptr CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

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