如果我只是从WinAPI调用SetThreadDescription()
,它将在Windows 10版本2004上运行。但是,在Windows 2016 Server 1607上,它将生成以下消息框:
The procedure entry point SetThreadDescription could not be located in the dynamic link library
消息中将显示我的可执行程序的路径。
根据this article:
SetThreadDescription is only available by Run Time Dynamic Linking on Windows Server 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 2016上出现错误127(“找不到指定的过程”)。
我在运行时动态链接上做错了什么?
最佳答案
显然,尽管MSDN说“DLL:Kernel32.dll”,但该函数实际上在KernelBase.DLL
中,因此在更改为以下内容后,我已解决了该问题:
HMODULE hKernelBase = GetModuleHandleA("KernelBase.dll");
关于c++ - 如何在Windows 2016 Server版本1607中访问SetThreadDescription(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243162/