C++ 蓝牙设备本地名称

C++ 蓝牙设备本地名称,c++,winapi,properties,bluetooth,device,C++,Winapi,Properties,Bluetooth,Device,我有一个暂时解决不了的问题。 对于我的C++项目之一,我必须编写函数来更改蓝牙无线电本地名称。 它使用微软蓝牙协议栈。如果打开任何蓝牙加密狗设备属性并导航到高级属性,则可以找到此名称。 我需要这个没有任何第三方库,只有WinApi函数。需要以Windows设备管理器的方式执行此操作。问题是我不知道它是怎么做到的。BluetoothAPI和Setupapi在这里似乎毫无用处。我不需要带有SPDRP_FRIENDLYNAME参数的SetupDiSetDeviceRegistryProperty。更改

我有一个暂时解决不了的问题。 对于我的C++项目之一,我必须编写函数来更改蓝牙无线电本地名称。 它使用微软蓝牙协议栈。如果打开任何蓝牙加密狗设备属性并导航到高级属性,则可以找到此名称。
我需要这个没有任何第三方库,只有WinApi函数。需要以Windows设备管理器的方式执行此操作。问题是我不知道它是怎么做到的。BluetoothAPI和Setupapi在这里似乎毫无用处。我不需要带有SPDRP_FRIENDLYNAME参数的SetupDiSetDeviceRegistryProperty。更改注册表中的本地名称也不会起任何作用。也许我应该在那之后重启蓝牙协议栈。我不知道。那么,如何以编程方式更改设备高级属性?提前感谢。

我自己找到了解决方案:

#include <windows.h>
#include <Setupapi.h>
#include <stdio.h>
#include <tchar.h>
#include <cfgmgr32.h>
#include <devguid.h>
#include <string>
#pragma comment(lib,"setupapi.lib")
using namespace std;

#define WIN32_LEAN_AND_MEAN

typedef basic_string<TCHAR> tstring;
DEFINE_GUID(GUID_DEVCLASS_BLUETOOTH, {0xe0cbf06cL, 0xcd8b, 0x4647, {0xbb, 0x8a, 0x26, 0x3b, 0x43, 0xf0, 0xf9, 0x74}});


LPTSTR GetGenericBluetoothAdapterInstanceID(void)
{
        unsigned i;    
        CONFIGRET r;
        HDEVINFO hDevInfo;
        SP_DEVINFO_DATA DeviceInfoData;
        PTSTR deviceInstanceID = new TCHAR[MAX_DEVICE_ID_LEN];

        // Find all bluetooth radio modules
        hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, NULL, NULL, DIGCF_PRESENT);

        if (hDevInfo == INVALID_HANDLE_VALUE)
                return NULL;   

        // Get first Generic Bluetooth Adapter InstanceID
        for (i = 0; ; i++)  
        {              
                DeviceInfoData.cbSize = sizeof (DeviceInfoData);

                if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
                        break;

                r = CM_Get_Device_ID(DeviceInfoData.DevInst, deviceInstanceID , MAX_PATH, 0);

                if (r != CR_SUCCESS)
                        continue;

                if (_tcsncmp(_TEXT("USB"), deviceInstanceID, 3) == 0)
                {                      
                        return deviceInstanceID;
                }
        }

        return NULL;
}

void find_and_replace(LPTSTR source, LPCTSTR strFind, LPCTSTR strReplace)
{
        tstring s = tstring(source);
        tstring f = tstring(strFind);
        tstring r = tstring(strReplace);       
        size_t j;

        for ( ; (j = s.find( f )) != string::npos ; )
        {
                s.replace( j, f.length(), r );
        }

        _tcscpy(source, s.c_str());
}

int _tmain(int argc, TCHAR *argv[])
{
        if ( argc != 2 )
        {
                _tprintf( TEXT("Invalid parameters number. "
                        TEXT("Usage: Bthrmmodifier.exe <radio module local name>\n")));
                return 1;
        }

        LPTSTR instanceID = GetGenericBluetoothAdapterInstanceID();

        if (instanceID == NULL)
        {
                _tprintf(_TEXT("Failed to get Generic Bluetooth Adapter InstanceID\n"));
                return 1;
        }

        LPTSTR instanceIDModified = new TCHAR[_tcslen(instanceID)];
        _tcscpy(instanceIDModified, instanceID);
        find_and_replace(instanceIDModified, _TEXT("\\"), _TEXT("#"));

        HANDLE hDevice;
        TCHAR fileName[1024] = { 0 };  
        _tcscpy(fileName, _TEXT("\\\\.\\"));   
        _tcscat(fileName, instanceIDModified);
        _tcscat(fileName, _TEXT("#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"));

        hDevice = CreateFile(fileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

        if (hDevice == INVALID_HANDLE_VALUE)
        {
                _tprintf(_TEXT("Failed to open device. Error code: %d\n"), GetLastError());            
                return EXIT_FAILURE;
        }

        //Change radio module local name in registry
        LPTSTR RMLocalName = argv[1];
        CHAR bufRMLocalName[256];

        int nLength = WideCharToMultiByte(CP_UTF8, 0, RMLocalName,
                -1, NULL, 0, NULL, NULL);
        WideCharToMultiByte(CP_UTF8, 0, RMLocalName,
                -1, bufRMLocalName, nLength, NULL, NULL);

        HKEY hKey;
        TCHAR rmLocalNameKey[1024] = { 0 };
        LSTATUS ret;
        _tcscpy(rmLocalNameKey, _TEXT("SYSTEM\\ControlSet001\\Enum\\"));       
        _tcscat(rmLocalNameKey, instanceID);
        _tcscat(rmLocalNameKey, _TEXT("\\Device Parameters"));

        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rmLocalNameKey,
                0L, KEY_SET_VALUE , &hKey);

        if(ret != ERROR_SUCCESS)
        {
                _tprintf(TEXT("Failed to open registry key. Error code: %d\n"), ret);
                return EXIT_FAILURE;
        }

        ret = RegSetValueEx(hKey, _TEXT("Local Name"), 0, REG_BINARY,
                (LPBYTE)bufRMLocalName, nLength);

        if (ret != ERROR_SUCCESS)
        {
                _tprintf(TEXT("Failed to set registry key. Error code: %d\n"), ret);
                return EXIT_FAILURE;
        }

        RegCloseKey(hKey);

        // This check decides what IO control code to use based on if we're in XP or Vista (and later).
        OSVERSIONINFO osver;
        osver.dwOSVersionInfoSize = sizeof(osver);
        GetVersionEx(&osver);

        UINT ctlCode = (UINT)(6 > osver.dwMajorVersion ? 0x220fd4 : 0x411008);
        long reload = 4;  // tells the control function to reset or reload or similar...
        DWORD bytes = 0; // merely a placeholder

        // Send radio module driver command to update device information
        if (!DeviceIoControl(hDevice, ctlCode, &reload, 4, NULL, 0, &bytes, NULL))
        {
                _tprintf(TEXT("Failed to update radio module local name. Error code: %d\n"), GetLastError());
                return EXIT_FAILURE;
        }

        _tprintf(TEXT("Done\n"));

        return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“setupapi.lib”)
使用名称空间std;
#定义WIN32_精益_和_平均值
typedef基本字符串t字符串;
定义GUID(GUID类蓝牙,{0xe0cbf06cL,0xcd8b,0x4647,{0xbb,0x8a,0x26,0x3b,0x43,0xf0,0xf9,0x74});
LPTSTR GetGenericBluetoothAdapterInstanceID(无效)
{
未签名的i;
配置器;
HDEVINFO HDEVINFO;
SP_设备信息_数据设备信息数据;
PTSTR deviceInstanceID=新TCHAR[最大设备ID长度];
//查找所有蓝牙无线电模块
hDevInfo=SetupDiGetClassDevs(&GUID\u DEVCLASS\u蓝牙,NULL,NULL,DIGCF\u存在);
if(hDevInfo==无效的句柄值)
返回NULL;
//获取第一个通用蓝牙适配器InstanceID
对于(i=0;i++)
{              
DeviceInfo.cbSize=sizeof(DeviceInfo数据);
if(!SetupDiEnumDeviceInfo(hdeInfo、i和DeviceInfo数据))
打破
r=CM_Get_Device_ID(deviceinfo.DevInst,deviceInstanceID,MAX_PATH,0);
如果(r!=CR_成功)
继续;
如果(_tcsncmp(_TEXT(“USB”),deviceInstanceID,3)=0)
{                      
返回deviceInstanceID;
}
}
返回NULL;
}
无效查找和替换(LPTSTR源、LPCTSTR strFind、LPCTSTR strReplace)
{
t字符串s=t字符串(源);
t字符串f=t字符串(strFind);
t管柱r=t管柱(strReplace);
尺寸j;
对于(;(j=s.find(f))!=string::npos;)
{
s、 更换(j,f.长度(),r);
}
_tcscpy(来源,s.c_str());
}
int_tmain(int argc,TCHAR*argv[])
{
如果(argc!=2)
{
_tprintf(文本(“无效参数编号”)
文本(“用法:Bthrmmodifier.exe\n”);
返回1;
}
LPTSTR instanceID=GetGenericBluetoothAdapterInstanceID();
if(instanceID==NULL)
{
_tprintf(_TEXT(“获取通用蓝牙适配器实例ID失败”);
返回1;
}
LPTSTR instanceIDModified=新的TCHAR[_tcslen(instanceID)];
_tcscpy(instanceIDModified,instanceID);
查找和替换(InstancedModified、\u TEXT(“\\”)、\u TEXT(“\\”);
处理设备;
TCHAR文件名[1024]={0};
_tcscpy(文件名,\\\.\\\”);
_tcscat(文件名,InstancedModified);
_tcscat(文件名,文本({a5dcbf10-6530-11d2-901f-00c04fb951ed});
hDevice=CreateFile(文件名,一般写入,0,NULL,打开,0,NULL);
if(hDevice==无效的句柄值)
{
_tprintf(_TEXT(“无法打开设备。错误代码:%d\n”)、GetLastError();
返回退出失败;
}
//更改注册表中的无线电模块本地名称
LPTSTR RMLocalName=argv[1];
CHAR bufRMLocalName[256];
int nLength=WideCharToMultiByte(CP_UTF8,0,RMLocalName,
-1,空,0,空,空);
宽图表多字节(CP_UTF8,0,RMLocalName,
-1,bufRMLocalName,nLength,NULL,NULL);
HKEY HKEY;
TCHAR rmLocalNameKey[1024]={0};
LSTATUS-ret;
_tcscpy(rmLocalNameKey,_TEXT(“SYSTEM\\ControlSet001\\Enum\”);
_tcscat(rmLocalNameKey,instanceID);
_tcscat(rmLocalNameKey,_TEXT(\\设备参数”);
ret=RegOpenKeyEx(HKEY_LOCAL_机器,rmLocalNameKey,
0L,键设置值和hKey);
if(ret!=错误\u成功)
{
_tprintf(文本(“无法打开注册表项。错误代码:%d\n”),ret);
返回退出失败;
}
ret=RegSetValueEx(hKey,文本(“本地名称”),0,REG二进制,
(LPBYTE)bufRMLocalName,nLength);
if(ret!=错误\u成功)
{
_tprintf(文本(“设置注册表项失败。错误代码:%d\n”),ret);
返回退出失败;
}
雷克洛斯基(香港中学),;
//如果我们在XP或Vista(及更高版本)中,此检查决定使用什么IO控制代码。
OSVERSIONINFO osver;
osver.dwosVersionInfo=sizeof(osver);
GetVersionEx(&osver);
UINT ctlCode=(UINT)(6>osver.dwMajorVersion?0x220fd4:0x411008);
long reload=4;//通知控制函数重置或重新加载或类似操作。。。
DWORD字节=0;//仅为占位符
//发送无线电模块驱动程序命令以更新设备信息
if(!DeviceIoControl(hDevice、ctlCode和reload、4、NULL、0和bytes、NULL))
{
_tprintf(文本(“未能更新无线电模块本地名称。错误代码:%d\n”)、GetLastError();
返回退出失败;
}
_tprintf(文本(“完成”);
返回退出成功;
}