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

问题描述 投票:0回答:1

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

过程入口点SetThreadDescription不能在动态链接库中找到

以及消息中包含我的可执行程序的路径。

根据this article

SetThreadDescription仅在运行时动态链接上可用 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(“找不到指定的过程”)。

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

c++ multithreading winapi dll windows-server-2016
1个回答
2
投票
显然,尽管MSDN说“ DLL:Kernel32.dll”,但该函数实际上在KernelBase.DLL中,因此,在更改为:之后,我已解决了该问题:

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

© www.soinside.com 2019 - 2024. All rights reserved.