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

标签 c++ multithreading winapi dll windows-server-2016

如果我只是从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/

相关文章:

c++ - 运行 C++ 应用程序时摆脱黑色控制台窗口

c++ - Win32 处理来自 Rich Edit 控件的 WM_NOTIFY 消息

c++ - Bison/Flex 以相反的顺序处理 token

c++ - gcc -O0 与 -Og 编译时间

c++ - 在 C++ 中创建一个列表来保存对象

python - 在线程中使用 Popen 会阻塞每个传入的 Flask-SocketIO 请求

multithreading - 是否可以多次打开串口?

android - 将 Handler 用作计时器但无法停止

c++ - 使用 cv 限定符和 ref 限定符实现 std::is_function

c++ - CBT Hook 仅接收一些事件