Advertisement
Guest User

Untitled

a guest
Nov 27th, 2011
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. bool CodeProjectUtils::UpdateRegistry(CString strNetCfgInstanceId, LPCTSTR lpszMacID /*= NULL*/)
  2. {
  3.     bool bRet = false;
  4.     HKEY hKey = NULL;          
  5.     if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  6.         _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"),
  7.         0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
  8.     {
  9.         DWORD dwIndex = 0;
  10.         TCHAR Name[1024];
  11.         DWORD cName = 1024;
  12.         while( RegEnumKeyEx(hKey, dwIndex, Name, &cName,
  13.             NULL, NULL, NULL, NULL) == ERROR_SUCCESS )
  14.         {
  15.             HKEY hSubKey = NULL;
  16.             if(RegOpenKeyEx(hKey,Name,0,KEY_ALL_ACCESS,&hSubKey) == ERROR_SUCCESS)
  17.             {
  18.                 BYTE Data[1204];
  19.                 DWORD cbData = 1024;
  20.                 if(RegQueryValueEx(hSubKey,_T("NetCfgInstanceId"),NULL,NULL,Data,&cbData) == ERROR_SUCCESS)
  21.                 {
  22.                     if(_tcscmp((TCHAR*)Data,strNetCfgInstanceId) == 0)
  23.                     {
  24.                         if(lpszMacID == NULL)
  25.                         {
  26.                             //Delete the NetCfgInstanceId entry
  27.                             LONG nErr = RegDeleteValue(hSubKey, _T("NetworkAddress"));
  28.                             if( (nErr == ERROR_SUCCESS) || (nErr == ERROR_FILE_NOT_FOUND) )
  29.                             {
  30.                                 bRet = true;
  31.                             }                          
  32.                         }
  33.                         else
  34.                         {
  35.                             //Add the NetCfgInstanceId entry
  36.                             if(RegSetValueEx(hSubKey,_T("NetworkAddress"),0,REG_SZ,(const BYTE*)lpszMacID,sizeof(TCHAR) * ((DWORD)_tcslen(lpszMacID) + 1)) == ERROR_SUCCESS)
  37.                             {
  38.                                 bRet = true;
  39.                             }                          
  40.                         }                                              
  41.                     }
  42.                 }
  43.                 RegCloseKey(hSubKey);
  44.             }                  
  45.             cName = 1024;
  46.             dwIndex++;
  47.         }
  48.         RegCloseKey(hKey);
  49.     }
  50.     return bRet;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement