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

C++ 如何在Windows 2016 Server版本1607中访问SetThreadDescription(),c++,multithreading,winapi,dll,windows-server-2016,C++,Multithreading,Winapi,Dll,Windows Server 2016,如果我只是从WinAPI调用SetThreadDescription(),它可以在Windows 10 2004版上运行。但是,在Windows 2016 Server 1607上,它会生成以下消息框: 在动态链接库中找不到过程入口点SetThreadDescription 我的可执行程序的路径如下所示 根据: SetThreadDescription仅通过上的运行时动态链接可用 Windows Server 2016,1607 因此,我尝试了如下动态链接: typedef HRESULT (W

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

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

我的可执行程序的路径如下所示

根据:

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);
  }
}
typedef HRESULT(WINAPI*tsetthreadscription)(句柄,PCWSTR);
名称空间{
tsetthreadscription gpsethreadscription=nullptr;
}
void Initialize(){
HMODULE hKernel32=GetModuleHandleA(“Kernel32.dll”);
if(hKernel32==nullptr){

cerr显然,尽管MSDN上写着“DLL:Kernel32.DLL”,但函数实际上在
KernelBase.DLL
中,因此我在更改为之后解决了这个问题:

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

您正在调用
GetLastError
,此时它将不再返回有意义的值。在您修复该问题之前,您向
std::cerr
写入的任何内容都是无用的。也可以使用
NtSetInformationThread/NtQueryInformationThread
,在此处具有未记录和未声明的优点-您不仅可以设置字符串,还可以设置ny二进制数据大小高达64kb,您可以使用静态链接与ntdll.dll-NtSetInformationThread/NtQueryInformationThread无处不在,只是在win10之前它返回
状态\u未实现
@rbm您不能静态链接dll。您可以使用或。@IInspectable-好的,我指的是加载时动态链接。我们从一开始就知道了我的意思是,嗨,@Serge,你也可以。