2010-09-21 18 views
1

J'ai un problème que je ne peux pas résoudre pendant un moment. Pour l'un de mes projets C++, je dois écrire une fonction pour changer le nom local de la radio Bluetooth. Il utilise la pile Microsoft Bluetooth. Vous pouvez trouver ce nom si vous ouvrez les propriétés du périphérique Bluetooth dongle et accédez à Propriétés avancées. J'ai besoin de cela sans bibliothèques tierces, seulement des fonctions WinApi. Besoin de le faire de la même manière que Windows Device Manager. Le problème est que je n'ai pas compris comment il le fait. BluetoothAPI et Setupapi semblent inutiles ici. Le paramètre SetupDiSetDeviceRegistryProperty avec SPDRP_FRIENDLYNAME n'est pas ce dont j'ai besoin. Changer le nom local dans le registre ne fait rien non plus. Peut-être que je devrais en quelque sorte redémarrer la pile bluetooth après ça. Je ne sais pas. Alors, comment puis-je modifier par programme les propriétés avancées du périphérique? Merci d'avance.Nom local du périphérique Bluetooth

Répondre

0

Je me suis trouvé la solution:

#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; 
} 
+0

Pourriez-vous poster comment vous avez résolu le problème? – Nightfirecat