Añadir al registro

Iniciado por ANTRAX, Mayo 22, 2011, 09:57:14 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Mayo 22, 2011, 09:57:14 PM Ultima modificación: Febrero 08, 2014, 05:49:17 PM por Expermicid
Enumerating the values under a Registry Key:
   There are three major steps involved in Enumerating values from registry.
   1. Open the Registry key
    2. Loop and print using RegEnumKey till the return value is not equal to ERROR_SUCCESS.
    3. Close the Registry key

Código: c
#include <windows.h>
#include <stdio.h>
void main()
{
    char lszValue[100];
    LONG lRet, lEnumRet;
    HKEY hKey;
    DWORD dwLength=100;
    int i=0;
    lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SOFTWARE\\ADOBE", 0L, KEY_READ , &hKey);
    if(lRet == ERROR_SUCCESS)
    {
         lEnumRet = RegEnumKey (hKey, i,lszValue,dwLength);
         while(lEnumRet == ERROR_SUCCESS)
         {
              i++;
              printf ("%s\n",lszValue);
              lEnumRet = RegEnumKey (hKey, i,lszValue,dwLength);
           }
       }

     RegCloseKey(hKey);
}


Accessing a REG_DWORD value from Registry:
   Accessing a DWORD from registry is simple. Open the Registry key and read it using RegQueryValueEx.

#include <windows.h>
#include <stdio.h>
void main()
{
    DWORD dwVersion;
    HKEY hKey;
    LONG returnStatus;
DWORD dwType=REG_DWORD;
   DWORD dwSize=sizeof(DWORD);
    returnStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Path", 0L, KEY_ALL_ACCESS, &hKey);

    if (returnStatus  = = ERROR_SUCCESS)
    {
        returnStatus = RegQueryValueEx(hKey, "Value", NULL, &dwType,(LPBYTE)&dwVersion, &dwSize);
        if (returnStatus == ERROR_SUCCESS)
        {
             printf("REG_DWORD value is %d\n", dwVersion);
        }
     }

    RegCloseKey(hKey);
}

Note: The only error that might crop up during the usage of this function is due to assigning a wrong value for the dwSize. If the dwSize is smaller than the queried value, then the RegQueryEx will fail. This happens especially during repeated mixed usage of the same variables for reading DWORD as well as character arrays. This is very much untraceable and might also get missed during testing. So it is better to check this problem during coding itself.
Accessing a REG_SZ from Registry:


#include <windows.h>
#include <stdio.h>
void main()
{
     char lszValue[255];
     HKEY hKey;
     LONG returnStatus;
     DWORD dwType=REG_SZ;
     DWORD dwSize=255;
     returnStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MICROSOFT\\INETMGR\\PARAMETERS", 0L,  KEY_ALL_ACCESS, &hKey);
     if (returnStatus == ERROR_SUCCESS)
     {
          returnStatus = RegQueryValueEx(hKey, "HelpLocation", NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
          if (returnStatus == ERROR_SUCCESS)
          {
               printf("Value Read is %s\n", lszValue);
          }
       }
     RegCloseKey(hKey);
}