问题描述
蓝牙 API 的 Microsoft 文档,例如 BluetoothGetDeviceInfo
提供使用静态或动态导入调用这些函数的说明.
The Microsoft documentation for Bluetooth APIs such as BluetoothGetDeviceInfo
provide instructions for calling these functions using either static or dynamic imports.
与 bthprops.lib
链接的静态导入工作正常.
The static import, linking with bthprops.lib
, works fine.
#include <windows.h>
#include <BluetoothAPIs.h>
#include <iostream>
int main(int argc, char** argv)
{
BLUETOOTH_DEVICE_INFO binfo = {};
binfo.dwSize = sizeof binfo;
binfo.Address.ullLong = 0xBAADDEADF00Dull;
auto result = ::BluetoothGetDeviceInfo(nullptr, &binfo);
std::wcout << L"BluetoothGetDeviceInfo returned " << result
<< L"\nand the name is \"" << binfo.szName << "\"\n";
return 0;
}
但这在超便携代码中并不理想,因为文档说它们在 Windows XP SP2 之前不受支持.所以应该使用动态链接并从丢失的功能中恢复.但是,按照 MSDN 文档的指示动态加载 bthprops.dll
失败:
But this isn't ideal in ultra-portable code, because the documentation says they are not supported prior to Windows XP SP2. So one should use dynamic linking and recover from missing functions. However, dynamic loading bthprops.dll
as instructed by the MSDN docs fails:
decltype(::BluetoothGetDeviceInfo)* pfnBluetoothGetDeviceInfo;
bool LoadBthprops( void )
{
auto dll = ::LoadLibraryW(L"bthprops.dll");
if (!dll) return false;
pfnBluetoothGetDeviceInfo = reinterpret_cast<decltype(pfnBluetoothGetDeviceInfo)>(::GetProcAddress(dll, "BluetoothGetDeviceInfo"));
return pfnBluetoothGetDeviceInfo != nullptr;
}
应该如何动态链接到这些函数?
How should one dynamically link to these functions?
推荐答案
显然 Google 非常了解这一事实,但 MSDN 却不知道.如果你想动态加载这些函数,使用 LoadLibrary("bthprops.
cpl
")
这是正确的DLL 名称,与函数文档中的 nice 表相反.
Apparently this fact is pretty well known to Google but not to MSDN. If you want to dynamically load these functions, use LoadLibrary("bthprops.
cpl
")
which is the correct DLL name, contrary to the nice table in the function documentation.
这有效:
decltype(::BluetoothGetDeviceInfo)* pfnBluetoothGetDeviceInfo;
bool LoadBthprops( void )
{
auto dll = ::LoadLibraryW(L"bthprops.cpl");
if (!dll) return false;
pfnBluetoothGetDeviceInfo = reinterpret_cast<decltype(pfnBluetoothGetDeviceInfo)>(::GetProcAddress(dll, "BluetoothGetDeviceInfo"));
return pfnBluetoothGetDeviceInfo != nullptr;
}
这篇关于LoadLibrary("bthprops.dll") 失败,找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!