ReactOS 0.4.16-dev-2498-g8632030
ThreadDescription.c File Reference
#include "k32_vista.h"
Include dependency graph for ThreadDescription.c:

Go to the source code of this file.

Functions

HRESULT WINAPI DECLSPEC_HOTPATCH GetThreadDescription (_In_ HANDLE hThread, _Outptr_result_z_ PWSTR *ppszThreadDescription)
 
HRESULT WINAPI DECLSPEC_HOTPATCH SetThreadDescription (_In_ HANDLE hThread, _In_ PCWSTR lpThreadDescription)
 

Function Documentation

◆ GetThreadDescription()

HRESULT WINAPI DECLSPEC_HOTPATCH GetThreadDescription ( _In_ HANDLE  hThread,
_Outptr_result_z_ PWSTR ppszThreadDescription 
)

Definition at line 13 of file ThreadDescription.c.

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}
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
HANDLE hThread
Definition: wizard.c:28
#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
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
UNICODE_STRING ThreadName
Definition: pstypes.h:1090
uint16_t * PWSTR
Definition: typedefs.h:56
#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
@ ThreadNameInformation
Definition: winternl.h:2319
#define HRESULT_FROM_NT(x)
Definition: winerror.h:216
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ SetThreadDescription()

HRESULT WINAPI DECLSPEC_HOTPATCH SetThreadDescription ( _In_ HANDLE  hThread,
_In_ PCWSTR  lpThreadDescription 
)

Definition at line 79 of file ThreadDescription.c.

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}
NTSTATUS NTAPI NtSetInformationThread(_In_ HANDLE ThreadHandle, _In_ THREADINFOCLASS ThreadInformationClass, _In_reads_bytes_(ThreadInformationLength) PVOID ThreadInformation, _In_ ULONG ThreadInformationLength)
Definition: query.c:2269
NTSYSAPI NTSTATUS WINAPI RtlInitUnicodeStringEx(PUNICODE_STRING, PCWSTR)

Referenced by rpcrt4_http_timer_thread(), RPCRT4_io_thread(), and RPCRT4_server_thread().