VC registry functions

Source: Internet
Author: User
Recently, when I was working on a project, I used some functions that operate the Registry. Now I want to record these functions for future use.
1. Open the registry key long regopenkeyex (hkey, // handle to open key primary key lpctstr lpsubkey, // subkey name subkey DWORD uloptions, // reserved. Must be 0 regsam samdesired, // security access mask read/write identity phkey phkresult // handle to open key returned hkey type pointer. Later, read/write, close using this pointer );
 
For example: // open the SOFTWARE \ cleaner subkey hkey under the HKEY_LOCAL_MACHINE primary key; hkey hkeyroot = HKEY_LOCAL_MACHINE; long ret0 = (: regopenkeyex (hkeyroot, "Software \ cleaner", 0, key_read, & hkey); If (ret0! = Error_success) // If the hkey cannot be opened, stop the program execution {afxmessagebox ("error: Unable to open the relevant hkey"); return ;}
2. read LONG RegQueryValueEx (HKEY hKey, // handle to key open the Registry pointer LPCTSTR lpValueName, // value name the key name to be read LPDWORD lpReserved, // reserved must be NULL. must be null lpdword lpType, // type buffer, key type. I usually use REG_SZ, REG_DWORD LPBYTE lpData, // data buffer. The buffer LPDWORD lpcbData // size of data buffer that saves the query results. Buffer size); for example, // hKEY is the pointer obtained when it is opened above. LPBYTE getValue = new BYTE [80]; // The obtained key value DWORD keyType = REG_SZ; // defines the data type DWORD DataLen = 80; // define the Data Length CString strUser = _ T ("Version"); // The key name to be queried long ret1 =: RegQueryValueEx (hKEY, strUser, NULL, & keyType, getValue, & DataLen); if (ret1! = ERROR_SUCCESS) {AfxMessageBox ("error: Unable to query the relevant registry information"); return ;}
 
3. Write the Registry LONG RegSetValueEx (HKEY hKey, // handle to key. Open the Registry pointer LPCTSTR lpValueName, // value name to write the key DWORD Reserved, // reserved must be 0 DWORD dwType, // value type write value type const byte * lpData, // value data: DWORD cbData // size of value data. Data SIZE); for example: // write the registry. Change Version to 1.0.12 // write data of the CString type CString strVersion = _ T ("Version"); // The Name Of The Key to be written: LPCTSTR strVersionValue = "1.0.12"; long ret =:: RegSetValueEx (hKEY, strVersion, 0, REG_SZ, (const BYTE *) strVersionValue, strlen (strVersionValue) + 1); if (ret! = ERROR_SUCCESS) {AfxMessageBox ("error: Unable to query the relevant registry information"); return;} [/code]
 
4. Create a new key LONG RegCreateKeyEx (HKEY hKey, // handle to open key. Open the Registry pointer maid, // subkey name. The subkey name is DWORD Reserved, // reserved. It must be 0 LPTSTR lpClass, // class string. It is used when it already exists. Generally, it is null dword dwOptions, // special options // default value: REG_OPTION_VOLATILE, Which is saved in the Registry and will still exist at next boot. // REG_OPTION_VOLATILE, stored in the memory // REG_OPTION_BACKUP_RESTORE REGSAM samDesired, // desired security access. Operation permission. Generally, KEY_ALL_ACCESS, unless you have special requirements, please refer to MSDN LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance. Inheritance. It is generally null phkey phkResult, // key handle. Return the town of this key value. LPDWORD lpdwDisposition // disposition value buffer // 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.
);
 
5. delete a key LONG RegDeleteKey (HKEY hKey, // handle to open key LPCTSTR lpSubKey // subkey name );
 
6. delete a key value LONG RegDeleteValue (HKEY hKey, // handle to key LPCTSTR lpValueName/value name. Value Name, not the opened pointer, but the queried pointer. If it is null, the value created by RegSetValueEx will be deleted );
 
7. Refresh the Registry LONG RegFlushKey (HKEY hKey // handle to key to write. Write all values in the given pointer );
// This function writes the changed data directly to the hard disk. Do not use this function frequently, which may affect the performance.
 
8. Import a registry file to LONG RegLoadKey (HKEY hKey, // handle to open key LPCTSTR lpSubKey, // subkey name LPCTSTR lpFile // registry file name) under the specified key );
// Useless
 
9. close the opened registry LONG RegCloseKey (HKEY hKey // handle to key to close );
// Close and release the pointer to the opened registry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.