Use GetModuleHandle() for kernel32.dll, since it's guaranteed to be loaded already

This commit is contained in:
Cameron Gutman 2021-05-25 19:40:14 -05:00
parent 4723f8ba7c
commit 46cea5011d

View File

@ -33,12 +33,10 @@ typedef struct tagTHREADNAME_INFO
typedef HRESULT (WINAPI *SetThreadDescription_t)(HANDLE, PCWSTR); typedef HRESULT (WINAPI *SetThreadDescription_t)(HANDLE, PCWSTR);
void setThreadNameWin32(const char* name) { void setThreadNameWin32(const char* name) {
HMODULE hKernel32;
SetThreadDescription_t setThreadDescriptionFunc; SetThreadDescription_t setThreadDescriptionFunc;
// This function is only supported on Windows 10 RS1 and later // This function is only supported on Windows 10 RS1 and later
hKernel32 = LoadLibraryA("kernel32.dll"); setThreadDescriptionFunc = (SetThreadDescription_t)GetProcAddress(GetModuleHandleA("kernel32.dll"), "SetThreadDescription");
setThreadDescriptionFunc = (SetThreadDescription_t)GetProcAddress(hKernel32, "SetThreadDescription");
if (setThreadDescriptionFunc != NULL) { if (setThreadDescriptionFunc != NULL) {
WCHAR nameW[16]; WCHAR nameW[16];
size_t chars; size_t chars;
@ -46,7 +44,6 @@ void setThreadNameWin32(const char* name) {
mbstowcs_s(&chars, nameW, ARRAYSIZE(nameW), name, _TRUNCATE); mbstowcs_s(&chars, nameW, ARRAYSIZE(nameW), name, _TRUNCATE);
setThreadDescriptionFunc(GetCurrentThread(), nameW); setThreadDescriptionFunc(GetCurrentThread(), nameW);
} }
FreeLibrary(hKernel32);
#ifdef _MSC_VER #ifdef _MSC_VER
// This method works on legacy OSes and older tools not updated to use SetThreadDescription yet, // This method works on legacy OSes and older tools not updated to use SetThreadDescription yet,