Thread: Registery with api?

  1. #1
    Registered User
    Join Date
    May 2002

    Question Registery with api?

    i know you could do it with api i done it with vb & c# but don't know how to do it in c++ i found this:

    The RegCreateKeyEx function creates the specified key. If the key already exists in the registry, the function opens it.

    LONG RegCreateKeyEx(

    HKEY hKey, // handle of an open key
    LPCTSTR lpSubKey, // address of subkey name
    DWORD Reserved, // reserved
    LPTSTR lpClass, // address of class string
    DWORD dwOptions, // special options flag
    REGSAM samDesired, // desired security access
    LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of key security structure
    PHKEY phkResult, // address of buffer for opened handle
    LPDWORD lpdwDisposition // address of disposition value buffer



    Identifies a currently open key or any of the following predefined reserved handle values:

    The key opened or created by the RegCreateKeyEx function is a subkey of the key identified by the hKey parameter.


    Points to a null-terminated string specifying the name of a subkey that this function opens or creates. The subkey specified must be a subkey of the key identified by the hKey parameter. This subkey must not begin with the backslash character ('\'). This parameter cannot be NULL.


    Reserved; must be zero.


    Points to a null-terminated string that specifies the class (object type) of this key. This parameter is ignored if the key already exists.


    Specifies special options for the key. This parameter can be one of the following values.

    Value Meaning
    REG_OPTION_NON_VOLATILE This key is not volatile; this is the default. The information is stored in a file and is preserved when the system is restarted. The RegSaveKey function saves keys that are not volatile.
    REG_OPTION_VOLATILE Windows NT: This key is volatile; the information is stored in memory and is not preserved when the system is restarted. The RegSaveKey function does not save volatile keys. This flag is ignored if the key already exists. Windows 95: This value is ignored in Windows 95. If REG_OPTION_VOLATILE is specified, the RegCreateKeyEx function creates a nonvolatile key and returns ERROR_SUCCESS.
    REG_OPTION_BACKUP_RESTORE Windows NT: If this flag is set, the function ignores the samDesired parameter and attempts to open the key with the access required to backup or restore the key. If the calling thread has the SE_BACKUP_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_READ access. If the calling thread has the SE_RESTORE_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_WRITE access. If both privileges are enabled, the key has the combined accesses for both privileges. Windows 95: This flag is ignored. Windows 95 does not support security in its registry.


    Specifies an access mask that specifies the desired security access for the new key. This parameter can be a combination of the following values:

    Value Meaning
    KEY_CREATE_LINK Permission to create a symbolic link.
    KEY_CREATE_SUB_KEY Permission to create subkeys.
    KEY_ENUMERATE_SUB_KEYS Permission to enumerate subkeys.
    KEY_EXECUTE Permission for read access.
    KEY_NOTIFY Permission for change notification.
    KEY_QUERY_VALUE Permission to query subkey data.
    KEY_SET_VALUE Permission to set subkey data.
    KEY_WRITE Combination of KEY_SET_VALUE and KEY_CREATE_SUB_KEY access.


    Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
    Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new key. If lpSecurityAttributes is NULL, the key gets a default security descriptor.
    Windows 95: The lpSecurityDescriptor member of the structure is ignored.


    Points to a variable that receives the handle of the opened or created key.


    Points to a variable that receives one of the following disposition values:

    Value Meaning
    REG_CREATED_NEW_KEY The key did not exist and was created.
    REG_OPENED_EXISTING_KEY The key existed and was simply opened without being changed.

    Return Values

    If the function succeeds, the return value is ERROR_SUCCESS.
    If the function fails, the return value is a nonzero error code defined in WINERROR.H. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.


    The key that the RegCreateKeyEx function creates has no values. An application can use the RegSetValue or RegSetValueEx function to set key values.
    The key identified by the hKey parameter must have been opened with KEY_CREATE_SUB_KEY access. To open the key, use the RegCreateKeyEx or RegOpenKeyEx function.
    An application cannot create a key under HKEY_USERS or HKEY_LOCAL_MACHINE.
    An application can use RegCreateKeyEx to temporarily lock a portion of the registry. When the locking process creates a new key, it receives the disposition value REG_CREATED_NEW_KEY, indicating that it "owns" the lock. Another process attempting to create the same key receives the disposition value REG_OPENED_EXISTING_KEY, indicating that another process already owns the lock.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    You can open the key using RegCreateKeyEx() and then use RegQueryValueEx() to get the data. Use RegCloseKey() to close the registry key. Here is a link that explains all the functions at MSDN.

    Here is a small example they give:
    // QueryKey - enumerates the subkeys of a given key and the associated 
    //    values and then copies the information about the keys and values 
    //    into a pair of edit controls and list boxes. 
    // hDlg - dialog box that contains the edit controls and list boxes 
    // hKey - key whose subkeys and values are to be enumerated 
    VOID QueryKey(HWND hDlg, HANDLE hKey) 
        CHAR     achKey[MAX_PATH]; 
        CHAR     achClass[MAX_PATH] = "";  // buffer for class name 
        DWORD    cchClassName = MAX_PATH;  // length of class string 
        DWORD    cSubKeys;                 // number of subkeys 
        DWORD    cbMaxSubKey;              // longest subkey size 
        DWORD    cchMaxClass;              // longest class string 
        DWORD    cValues;              // number of values for key 
        DWORD    cchMaxValue;          // longest value name 
        DWORD    cbMaxValueData;       // longest value data 
        DWORD    cbSecurityDescriptor; // size of security descriptor 
        FILETIME ftLastWriteTime;      // last write time 
        DWORD i, j; 
        DWORD retCode, retValue; 
        CHAR  achValue[MAX_VALUE_NAME]; 
        DWORD cchValue = MAX_VALUE_NAME; 
        CHAR  achBuff[80]; 
        // Get the class name and the value count. 
        RegQueryInfoKey(hKey,        // key handle 
            achClass,                // buffer for class name 
            &cchClassName,           // length of class string 
            NULL,                    // reserved 
            &cSubKeys,               // number of subkeys 
            &cbMaxSubKey,            // longest subkey size 
            &cchMaxClass,            // longest class string 
            &cValues,                // number of values for this key 
            &cchMaxValue,            // longest value name 
            &cbMaxValueData,         // longest value data 
            &cbSecurityDescriptor,   // security descriptor 
            &ftLastWriteTime);       // last write time 
        SetDlgItemText(hDlg, IDE_CLASS, achClass); 
        SetDlgItemInt(hDlg, IDE_CVALUES, cValues, FALSE); 
        SendMessage(GetDlgItem(hDlg, IDL_LISTBOX), 
            LB_ADDSTRING, 0, (LONG) ".."); 
        // Enumerate the child keys, until RegEnumKeyEx fails. Then 
        // get the name of each child key and copy it into the list box. 
        SetCursor(LoadCursor(NULL, IDC_WAIT)); 
        for (i = 0, retCode = ERROR_SUCCESS; 
                retCode == ERROR_SUCCESS; i++) 
            retCode = RegEnumKeyEx(hKey, 
            if (retCode == (DWORD)ERROR_SUCCESS) 
                SendMessage(GetDlgItem(hDlg, IDL_LISTBOX), 
                    LB_ADDSTRING, 0, (LONG) achKey); 
        SetCursor(LoadCursor (NULL, IDC_ARROW)); 
        // Enumerate the key values. 
        SetCursor(LoadCursor(NULL, IDC_WAIT)); 
        if (cValues) 
            for (j = 0, retValue = ERROR_SUCCESS; 
                    j < cValues; j++) 
                cchValue = MAX_VALUE_NAME; 
                achValue[0] = '\0'; 
                retValue = RegEnumValue(hKey, j, achValue, 
                    NULL,    // &dwType, 
                    NULL,    // &bData, 
                    NULL);   // &bcData 
                if (retValue != (DWORD) ERROR_SUCCESS && 
                        retValue != ERROR_INSUFFICIENT_BUFFER) 
                    wsprintf (achBuff, 
                        "Line:%d 0 based index = %d, retValue = %d, " 
                         "ValueLen = %d", 
                         __LINE__, j, retValue, cchValue); 
                    MessageBox (hDlg, achBuff, "Debug", MB_OK); 
                achBuff[0] = '\0'; 
                // Add each value to a list box. 
                if (!lstrlen(achValue)) 
                    lstrcpy(achValue, "<NO NAME>"); 
                wsprintf(achBuff, "%d) %s ", j, achValue); 
                    LB_ADDSTRING, 0, (LONG) achBuff); 
        SetCursor(LoadCursor(NULL, IDC_ARROW)); 
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform specific API or C standard API
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 01:32 AM
  2. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  3. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  4. Is it foolish to program with Win32 API ?
    By Kelvin in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2002, 02:03 PM
  5. pthread api vs win32 thread api
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 11-20-2001, 08:55 AM