This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

main.cpp

By: a guest on Jul 18th, 2011  |  syntax: C++  |  size: 4.21 KB  |  views: 655  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <windows.h>
  2. #include <Setupapi.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <cfgmgr32.h>
  6. #include <devguid.h>
  7. #include <string>
  8. #pragma comment(lib,"setupapi.lib")
  9. using namespace std;
  10.  
  11. #define WIN32_LEAN_AND_MEAN
  12.  
  13. typedef basic_string<TCHAR> tstring;
  14. DEFINE_GUID(GUID_DEVCLASS_BLUETOOTH, {0xe0cbf06cL, 0xcd8b, 0x4647, {0xbb, 0x8a, 0x26, 0x3b, 0x43, 0xf0, 0xf9, 0x74}});
  15.  
  16.  
  17. LPTSTR GetGenericBluetoothAdapterInstanceID(void)
  18. {
  19.         unsigned i;    
  20.         CONFIGRET r;
  21.         HDEVINFO hDevInfo;
  22.         SP_DEVINFO_DATA DeviceInfoData;
  23.         PTSTR deviceInstanceID = new TCHAR[MAX_DEVICE_ID_LEN];
  24.  
  25.         // Find all bluetooth radio modules
  26.         hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, NULL, NULL, DIGCF_PRESENT);
  27.  
  28.         if (hDevInfo == INVALID_HANDLE_VALUE)
  29.                 return NULL;   
  30.        
  31.         // Get first Generic Bluetooth Adapter InstanceID
  32.         for (i = 0; ; i++)  
  33.         {              
  34.                 DeviceInfoData.cbSize = sizeof (DeviceInfoData);
  35.  
  36.                 if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
  37.                         break;
  38.  
  39.                 r = CM_Get_Device_ID(DeviceInfoData.DevInst, deviceInstanceID , MAX_PATH, 0);
  40.                                                
  41.                 if (r != CR_SUCCESS)
  42.                         continue;
  43.  
  44.                 if (_tcsncmp(_TEXT("USB"), deviceInstanceID, 3) == 0)
  45.                 {                      
  46.                         return deviceInstanceID;
  47.                 }
  48.         }
  49.  
  50.         return NULL;
  51. }
  52.  
  53. void find_and_replace(LPTSTR source, LPCTSTR strFind, LPCTSTR strReplace)
  54. {
  55.         tstring s = tstring(source);
  56.         tstring f = tstring(strFind);
  57.         tstring r = tstring(strReplace);       
  58.         size_t j;
  59.  
  60.         for ( ; (j = s.find( f )) != string::npos ; )
  61.         {
  62.                 s.replace( j, f.length(), r );
  63.         }
  64.  
  65.         _tcscpy(source, s.c_str());
  66. }
  67.  
  68. int _tmain(int argc, TCHAR *argv[])
  69. {
  70.         if ( argc != 2 )
  71.         {
  72.                 _tprintf( TEXT("Invalid parameters number. "
  73.                         TEXT("Usage: Bthrmmodifier.exe <radio module local name>\n")));
  74.                 return 1;
  75.         }
  76.  
  77.         LPTSTR instanceID = GetGenericBluetoothAdapterInstanceID();
  78.  
  79.         if (instanceID == NULL)
  80.         {
  81.                 _tprintf(_TEXT("Failed to get Generic Bluetooth Adapter InstanceID\n"));
  82.                 return 1;
  83.         }
  84.  
  85.         LPTSTR instanceIDModified = new TCHAR[_tcslen(instanceID)];
  86.         _tcscpy(instanceIDModified, instanceID);
  87.         find_and_replace(instanceIDModified, _TEXT("\\"), _TEXT("#"));
  88.  
  89.         HANDLE hDevice;
  90.         TCHAR fileName[1024] = { 0 };  
  91.         _tcscpy(fileName, _TEXT("\\\\.\\"));   
  92.         _tcscat(fileName, instanceIDModified);
  93.         _tcscat(fileName, _TEXT("#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"));
  94.  
  95.         hDevice = CreateFile(fileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  96.  
  97.         if (hDevice == INVALID_HANDLE_VALUE)
  98.         {
  99.                 _tprintf(_TEXT("Failed to open device. Error code: %d\n"), GetLastError());            
  100.                 return EXIT_FAILURE;
  101.         }
  102.  
  103.         //Change radio module local name in registry
  104.         LPTSTR RMLocalName = argv[1];
  105.         CHAR bufRMLocalName[256];
  106.  
  107.         int nLength = WideCharToMultiByte(CP_UTF8, 0, RMLocalName,
  108.                 -1, NULL, 0, NULL, NULL);
  109.         WideCharToMultiByte(CP_UTF8, 0, RMLocalName,
  110.                 -1, bufRMLocalName, nLength, NULL, NULL);
  111.        
  112.         HKEY hKey;
  113.         TCHAR rmLocalNameKey[1024] = { 0 };
  114.         LSTATUS ret;
  115.         _tcscpy(rmLocalNameKey, _TEXT("SYSTEM\\ControlSet001\\Enum\\"));       
  116.         _tcscat(rmLocalNameKey, instanceID);
  117.         _tcscat(rmLocalNameKey, _TEXT("\\Device Parameters"));
  118.        
  119.         ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rmLocalNameKey,
  120.                 0L, KEY_SET_VALUE , &hKey);
  121.  
  122.         if(ret != ERROR_SUCCESS)
  123.         {
  124.                 _tprintf(TEXT("Failed to open registry key. Error code: %d\n"), ret);
  125.                 return EXIT_FAILURE;
  126.         }
  127.  
  128.         ret = RegSetValueEx(hKey, _TEXT("Local Name"), 0, REG_BINARY,
  129.                 (LPBYTE)bufRMLocalName, nLength);
  130.  
  131.         if (ret != ERROR_SUCCESS)
  132.         {
  133.                 _tprintf(TEXT("Failed to set registry key. Error code: %d\n"), ret);
  134.                 return EXIT_FAILURE;
  135.         }
  136.  
  137.         RegCloseKey(hKey);
  138.  
  139.         // This check decides what IO control code to use based on if we're in XP or Vista (and later).
  140.         OSVERSIONINFO osver;
  141.         osver.dwOSVersionInfoSize = sizeof(osver);
  142.         GetVersionEx(&osver);
  143.  
  144.         UINT ctlCode = (UINT)(6 > osver.dwMajorVersion ? 0x220fd4 : 0x411008);
  145.         long reload = 4;  // tells the control function to reset or reload or similar...
  146.         DWORD bytes = 0; // merely a placeholder
  147.  
  148.         // Send radio module driver command to update device information
  149.         if (!DeviceIoControl(hDevice, ctlCode, &reload, 4, NULL, 0, &bytes, NULL))
  150.         {
  151.                 _tprintf(TEXT("Failed to update radio module local name. Error code: %d\n"), GetLastError());
  152.                 return EXIT_FAILURE;
  153.         }
  154.  
  155.         _tprintf(TEXT("Done\n"));
  156.  
  157.         return EXIT_SUCCESS;
  158. }
clone this paste RAW Paste Data