Codechange: [Windows] Use SetThreadDescription over magic exception

This commit is contained in:
Rubidium
2026-07-13 17:13:04 +02:00
committed by rubidium42
parent d1ec9f4897
commit 808ae7f270
+5 -31
View File
@@ -16,6 +16,7 @@
#include <windows.h>
#include <fcntl.h>
#include <mmsystem.h>
#include <processthreadsapi.h>
#include <regstr.h>
#define NO_SHOBJIDL_SORTDIRECTION /**< Avoid multiple definition of SORT_ASCENDING. */
#include <shlobj.h> /* SHGetFolderPath */
@@ -533,36 +534,9 @@ int Win32StringContains(std::string_view str, std::string_view value, bool case_
return -1; // Failure indication.
}
#ifdef _MSC_VER
/* Based on code from MSDN: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx */
const DWORD MS_VC_EXCEPTION = 0x406D1388;
PACK_N(struct THREADNAME_INFO {
DWORD dwType; ///< Must be 0x1000.
LPCSTR szName; ///< Pointer to name (in user addr space).
DWORD dwThreadID; ///< Thread ID (-1=caller thread).
DWORD dwFlags; ///< Reserved for future use, must be zero.
}, 8);
/**
* Signal thread name to any attached debuggers.
*/
void SetCurrentThreadName(const std::string &thread_name)
void SetCurrentThreadName(const std::string &name)
{
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = thread_name.c_str();
info.dwThreadID = -1;
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
#pragma warning(pop)
using SetThreadDescriptionProc = HRESULT(WINAPI *)(HANDLE, PCWSTR);
static const SetThreadDescriptionProc std_proc = GetKernel32Function("SetThreadDescription");
if (std_proc != nullptr) std_proc(GetCurrentThread(), OTTD2FS(name).c_str());
}
#else
void SetCurrentThreadName(const std::string &) {}
#endif