如何在 Windows 2016 Server 版本 1607 中访问 SetThreadDescription()

How to access SetThreadDescription() in Windows 2016 Server, Version 1607

本文关键字:访问 SetThreadDescription 1607 版本 Windows 2016 Server      更新时间:2023-10-16

如果我只是从WinAPI调用SetThreadDescription(),它可以在Windows 10版本2004上运行。但是,在 Windows 2016 Server 1607 上,它会生成以下消息框:

过程入口点 SetThreadDescription 无法在动态链接库中找到

我的可执行程序的路径遵循消息。

根据这篇文章:

SetThreadDescription 仅通过 Run Time Dynamic Links 在 上可用 视窗服务器 2016, 1607.

所以我尝试了如下动态链接:

typedef HRESULT (WINAPI *TSetThreadDescription)(HANDLE, PCWSTR);
namespace {
TSetThreadDescription gpSetThreadDescription = nullptr;
}
void Initialize() {
HMODULE hKernel32 = GetModuleHandleA("Kernel32.dll");
if (hKernel32 == nullptr) {
cerr << "FATAL: failed to get kernel32.dll module handle, error: " << GetLastError() << endl;
quick_exit(5);
}
gpSetThreadDescription = reinterpret_cast<TSetThreadDescription>(
GetProcAddress(hKernel32, "SetThreadDescription"));
if (gpSetThreadDescription == nullptr) {
cerr << "FATAL: failed to get SetThreadDescription() address, error: " << GetLastError() << endl;
quick_exit(6);
}
}

此代码也适用于 Windows 10。但是,我在Windows Server 127上收到错误2016("找不到指定的过程"(。

我在运行时动态链接方面做错了什么?

显然,尽管MSDN说"DLL:Kernel32.dll",但该函数实际上是KernelBase.DLL,所以我在更改为

HMODULE hKernelBase = GetModuleHandleA("KernelBase.dll");