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

SetThreadDescription

関数
スレッドに説明用の名前を設定する。
DLLKERNEL32.dll呼出規約winapi対応OSWindows 10 以降

シグネチャ

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

HRESULT SetThreadDescription(
    HANDLE hThread,
    LPCWSTR lpThreadDescription
);

パラメーター

名前方向
hThreadHANDLEin
lpThreadDescriptionLPCWSTRin

戻り値の型: HRESULT

各言語での呼び出し定義

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

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

SetThreadDescription = ctypes.windll.kernel32.SetThreadDescription
SetThreadDescription.restype = ctypes.c_int
SetThreadDescription.argtypes = [
    wintypes.HANDLE,  # hThread : HANDLE
    wintypes.LPCWSTR,  # lpThreadDescription : LPCWSTR
]
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetThreadDescription = kernel32.NewProc("SetThreadDescription")
)

// hThread (HANDLE), lpThreadDescription (LPCWSTR)
r1, _, err := procSetThreadDescription.Call(
	uintptr(hThread),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpThreadDescription))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function SetThreadDescription(
  hThread: THandle;   // HANDLE
  lpThreadDescription: PWideChar   // LPCWSTR
): Integer; stdcall;
  external 'KERNEL32.dll' name 'SetThreadDescription';
result := DllCall("KERNEL32\SetThreadDescription"
    , "Ptr", hThread   ; HANDLE
    , "WStr", lpThreadDescription   ; LPCWSTR
    , "Int")   ; return: HRESULT
●SetThreadDescription(hThread, lpThreadDescription) = DLL("KERNEL32.dll", "int SetThreadDescription(void*, char*)")
# 呼び出し: SetThreadDescription(hThread, lpThreadDescription)
# hThread : HANDLE -> "void*"
# lpThreadDescription : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。