Saturday, 3 March 2012

How to read from registry and display a value.

The Windows Registry is an integral part of the Windows Operating System.  Many of the configurations are not explicitly provided to users.  But folks from Microsoft decide to pin it down only in the registry.  So if you are into Windows programming eventually there will be a need for reading from and writing to registry.  The following examples do just that.  I am newbie to Windows Programming though have a substantial experience and interest in programming.

Code Sample:

The following code sample provides a simple functionality.  This part of code reads the registry key responsible for supressing the java-script error dialog when we browse web pages using Internet Explorer.

Registry Key : HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main

Key Type      : REG_SZ  // Null Terminated string value

void readEntriesFromRegistry()
{
     wchar_t disableScriptDebugger[128]=L"Software\\Microsoft\\Internet Explorer\\Main";
     wchar_t keyValue[3]; // I know the value might be either 'yes' or 'no'
     int keySize=sizeof(wchar_t)*3; // Size in bytes.
     int          errorCode=-1;  // Initialize errorCode
     HKEY hKey=0;             // Initialize key
    // Open Registry key.
 errorCode=RegOpenKeyEx(HKEY_CURRENT_USER,disableScriptDebugger,0,KEY_READ,&hKey);

     if(errorCode==0)
     {
          // Read the value for the registry key.  The value is stored in keyValue
   errorCode=RegQueryValueEx(hKey,L"Disable Script Debugger",0,&keyType,(LPBYTE)keyValue,&keySize);
     }
     CString str(keyValue);
     ATLTRACE(L"The value is : %s \n",str);
    RegCloseKey(hKey);
}  

Note:
If you use the above code snippet repeatedly allocate memory for keyValue and size for keySize everytime, since you may get errorCode as 234 instead of 0.















}

No comments:

Post a Comment