ReactOS 0.4.16-dev-2498-g8632030
ThreadDescription.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: NT10+ Thread Description helpers
5 * COPYRIGHT: Copyright 2025-2026 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
6 */
7
8#include "k32_vista.h"
9
15 _Outptr_result_z_ PWSTR* ppszThreadDescription)
16{
18 PVOID ptr;
21
22 *ppszThreadDescription = NULL;
23
24 /* Since there may be concurrent SetThreadDescription() invocations being
25 * executed, we need to loop over the buffer allocation size until we can
26 * successfully capture the thread name. */
27 Length = 0;
30 return HRESULT_FROM_NT(Status);
31
32 /* Loop again only if we get buffer-too-small types of errors;
33 * otherwise, break the loop: any error will be handled below. */
36 {
37 /* (Re-)allocate the information buffer */
38 ptr = (NameInfo ? LocalReAlloc(NameInfo, Length, LMEM_MOVEABLE)
40 if (!ptr)
41 {
43 break;
44 }
45 NameInfo = ptr;
47 NameInfo, Length, &Length);
48 }
49 if (!NT_SUCCESS(Status))
50 {
51 LocalFree(NameInfo);
52 return HRESULT_FROM_NT(Status);
53 }
54
55 /*
56 * We already have a suitable memory buffer, containing a UNICODE_STRING
57 * followed by the actual string data. Just move the string back to the
58 * beginning of the buffer and resize it down. Since sizeof(UNICODE_STRING)
59 * is larger than sizeof(WCHAR), we also have space for the NUL-terminator.
60 */
61 *ppszThreadDescription = (PWSTR)NameInfo;
62 Length = NameInfo->ThreadName.Length;
63 if (Length)
64 RtlMoveMemory(*ppszThreadDescription, NameInfo->ThreadName.Buffer, Length);
65 (*ppszThreadDescription)[Length / sizeof(WCHAR)] = UNICODE_NULL;
66
67 /* Resize down the buffer. If the call fails, the old buffer
68 * is still valid, but is larger than what is necessary. */
69 ptr = LocalReAlloc(*ppszThreadDescription, Length + sizeof(WCHAR), LMEM_FIXED);
70 if (ptr)
71 *ppszThreadDescription = ptr;
72
73 return HRESULT_FROM_NT(Status);
74}
75
81 _In_ PCWSTR lpThreadDescription)
82{
85
86 Status = RtlInitUnicodeStringEx(&NameInfo.ThreadName, lpThreadDescription);
87 if (NT_SUCCESS(Status))
88 {
90 &NameInfo, sizeof(NameInfo));
91 }
92 return HRESULT_FROM_NT(Status);
93}
HRESULT WINAPI DECLSPEC_HOTPATCH GetThreadDescription(_In_ HANDLE hThread, _Outptr_result_z_ PWSTR *ppszThreadDescription)
HRESULT WINAPI DECLSPEC_HOTPATCH SetThreadDescription(_In_ HANDLE hThread, _In_ PCWSTR lpThreadDescription)
#define DECLSPEC_HOTPATCH
Definition: _mingw.h:240
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
Status
Definition: gdiplustypes.h:25
HLOCAL NTAPI LocalReAlloc(HLOCAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:1625
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define LMEM_MOVEABLE
Definition: minwinbase.h:82
#define LMEM_FIXED
Definition: minwinbase.h:81
static PVOID ptr
Definition: dispmode.c:27
#define _Outptr_result_z_
Definition: ms_sal.h:434
HANDLE hThread
Definition: wizard.c:28
#define _In_
Definition: no_sal2.h:158
#define UNICODE_NULL
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
NTSTATUS NTAPI NtQueryInformationThread(_In_ HANDLE ThreadHandle, _In_ THREADINFOCLASS ThreadInformationClass, _Out_writes_bytes_to_opt_(ThreadInformationLength, *ReturnLength) PVOID ThreadInformation, _In_ ULONG ThreadInformationLength, _Out_opt_ PULONG ReturnLength)
Definition: query.c:2985
NTSTATUS NTAPI NtSetInformationThread(_In_ HANDLE ThreadHandle, _In_ THREADINFOCLASS ThreadInformationClass, _In_reads_bytes_(ThreadInformationLength) PVOID ThreadInformation, _In_ ULONG ThreadInformationLength)
Definition: query.c:2269
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
UNICODE_STRING ThreadName
Definition: pstypes.h:1090
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define WINAPI
Definition: msvc.h:6
NTSYSAPI NTSTATUS WINAPI RtlInitUnicodeStringEx(PUNICODE_STRING, PCWSTR)
@ ThreadNameInformation
Definition: winternl.h:2319
#define HRESULT_FROM_NT(x)
Definition: winerror.h:216
__wchar_t WCHAR
Definition: xmlstorage.h:180