Simple tutorial on registry API

Source: Internet
Author: User

 

Reprinted: http://www.xuyibo.org/article/71.htm

  1. Glossary
  2. Common APIs for registry operations
API Description
Regcreatekey Create a key and return the corresponding hkey
Regopenkey Open the registry and obtain an hkey, which is used as the first parameter of the following functions.
Regopenkeyex Similar to regopenkey, it is rarely used. an access control type parameter is added.
Regsetvalue Set the default value of an hkey
Regsetvalueex Set an hkey value other than the default value
Regqueryvalue Obtains the default value of an hkey.
Regqueryvalueex Obtains the value of an hkey other than the default value.
Regdeletekey Deletes a key that cannot contain a subkey.
Shdeletekey Delete a key and all sub-keys
Regdeletevalue Delete values in key
Regclosekey Disable Registry

  1. Registry data type
Type Description
REG_DWORD 32-digit
REG_SZ A string ending with null. It can be a unicode or ANSI string, depending on whether Unicode or ANSI functions are used.

  1. Function usage
  2. Regcreatekey
    LONG RegCreateKey(  HKEY hKey,        // handle to an open key  LPCTSTR lpSubKey, // subkey name  PHKEY phkResult   // buffer for key handle);

    If we want to save many camera parameters of the demo program to: HKEY_LOCAL_MACHINE \ SOFTWARE \ daheng_directx, use this function to create the specified key and obtain the hkey for further operations.

    Hkey; If (regcreatekey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {// here, you can use hkey to operate the value in the key daheng_directx .} Regclosekey (hkey );

    Note: Generally, the Program maintains the following data locations: HKEY_LOCAL_MACHINE \ software and HKEY_CURRENT_USER \ Software. The difference between the two is that the data maintained by the former is, all accounts in the operating system can access the service. For example, you have two accounts on your machine, Xu Yibo and Kang. If you save the Registry in HKEY_LOCAL_MACHINE \ Software, after the system logs in with Xu Yibo's account, it runs the demo and enters Kang to run the demo. The obtained initial values are the same .), While HKEY_CURRENT_USER \ softwar is for the current account. The system logs in with different accounts, and the values under this key are different.

  • Regopenkey
    LONG RegOpenKey(  HKEY hKey,        // handle to open key  LPCTSTR lpSubKey, // name of subkey to open  PHKEY phkResult   // handle to open key);

    This function is different from regcreatekey in that if this key does not exist, this function fails to be executed (while regcreatekey: returns an existing hkey; does not exist, create one and return its hkey ). If we want to save many camera parameters of the demo program to: HKEY_LOCAL_MACHINE \ SOFTWARE \ daheng_directx, use this function to open the specified key and obtain the hkey for further operations.

    Hkey; If (regopenkey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {// here, you can use hkey to operate the value in the key daheng_directx .} Regclosekey (hkey );
  • Regsetvalueex
    LONG RegSetValueEx(  HKEY hKey,           // handle to key  LPCTSTR lpValueName, // value name  DWORD Reserved,      // reserved  DWORD dwType,        // value type  CONST BYTE *lpData,  // value data  DWORD cbData         // size of value data);

    Suppose we want to keep the camera exposure data to HKEY_LOCAL_MACHINE \ SOFTWARE \ daheng_directx. The data name is AEC and the value is 1:

    Hkey; hkey hsubkey; DWORD dwvalue = 1; char buffer [] = "raw2rgb. dll"; // use regcreatekey to ensure that if SOFTWARE \ daheng_directx does not exist, create one. If (regcreatekey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {//// here, you can use hkey to operate the value in the key daheng_directx. // If (regsetvalueex (hkey, "AEC", 0, REG_DWORD, (const byte *) & dwvalue, sizeof (DWORD) = error_success) {printf ("regsetvalueex: AEC = % d \ n ", dwvalue) ;}/// if you want to create a plugins key in Software \ daheng_directx, you can no longer use hkey, you need to // obtain the hkey of this node again. // If (regcreatekey (hkey, "plugins", & hsubkey) = error_success) {If (regsetvalueex (hsubkey, "color correction plugin", 0, REG_SZ, (const byte *) buffer, strlen (buffer) + 1) = error_success) {printf ("regsetvalueex: color correction plugin = % s \ n", buffer );} regclosekey (hsubkey) ;}} regclosekey (hkey );
  • Regqueryvalueex
    LONG RegQueryValueEx(  HKEY hKey,            // handle to key  LPCTSTR lpValueName,  // value name  LPDWORD lpReserved,   // reserved  LPDWORD lpType,       // type buffer  LPBYTE lpData,        // data buffer  LPDWORD lpcbData      // size of data buffer);

    Suppose we want to read the value set in regsetvalueex above:

    Hkey; hkey hsubkey; DWORD dwtype; DWORD dwvalue; DWORD dwsize; // use regcreatekey to ensure that if SOFTWARE \ daheng_directx does not exist, create one. If (regopenkey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {//// here, you can use hkey to operate the value in the key daheng_directx. // Dwtype = REG_DWORD; dwsize = sizeof (DWORD); If (regqueryvalueex (hkey, "AEC", 0, & dwtype, & dwvalue, & dwsize) = error_success) {printf ("regqueryvalueex AEC = % d \ n", dwvalue);} else {printf ("some error occurred! \ N ") ;}//// if you want to create a plugins key in Software \ daheng_directx, you can no longer use hkey. You Need To // obtain the hkey of this node. // If (regopenkey (hkey, "plugins", & hsubkey) = error_success) {char buffer [256]; dwtype = REG_SZ; dwsize = sizeof (buffer ); if (regqueryvalueex (hsubkey, "color correction plugin", 0, & dwtype, (lpbyte) buffer, & dwsize) = error_success) {printf ("regqueryvalueex color correction plug-in = % s \ n", buffer);} else {printf ("some error occurred! \ N ") ;}regclosekey (hsubkey) ;}} regclosekey (hkey );
  • Regdeletekey
    LONG RegDeleteKey(  HKEY hKey,         // handle to open key  LPCTSTR lpSubKey   // subkey name);

    Suppose we want to delete the key set by regsetvalueex:

     RegDeleteKey (HKEY_LOCAL_MACHINE, "Software\\daheng_directx");
  • Shdeletekey
    LONG SHDeleteKey(  HKEY hKey,         // handle to open key  LPCTSTR lpSubKey   // subkey name);

    Suppose we want to delete the key set by regsetvalueex and all sub-keys:

     SHDeleteKey (HKEY_LOCAL_MACHINE, "Software\\daheng_directx");
  • Regdeletevalue
    LONG RegDeleteValue(  HKEY hKey,            // handle to key  LPCTSTR lpValueName   // value name);

    Suppose we want to delete the value set in regsetvalueex above:

    Hkey; hkey hsubkey; DWORD dwtype; DWORD dwvalue; DWORD dwsize; // use regcreatekey to ensure that if SOFTWARE \ daheng_directx does not exist, create one. If (regopenkey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {dwtype = REG_DWORD; dwsize = sizeof (DWORD); If (regdeletevalue (hkey, "AEC") = error_success) {printf ("regdeletevalue AEC = % d \ n", dwvalue);} else {printf ("some error occurred! \ N ") ;}} regclosekey (hkey );
  • Regclosekey
    LONG RegCloseKey(  HKEY hKey   // handle to key to close);

    HKEY hKey;if (RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\daheng_directx", &hKey) == ERROR_SUCCESS) {      // …}RegCloseKey(hKey);

    This function is relatively simple. Parameter 1 is the hkey returned by the regcreatekey, regopenkey, regcreatekeyex, and regopenkeyex functions.

  1. Instance
/* ++ Copyright (c) 2007 http://www.xuyibo.orgModule name: Reg. cabstract: Small registry demo for my good friend liumin;) Author: xuyibo (xuyibo) 2007-05-15revision history: -- */# include <stdio. h> # include <windows. h> # pragma comment (Lib, "advapi32.lib") void setregistryvalue () {hkey; hkey hsubkey; DWORD dwvalue = 1; char buffer [] = "raw2rgb. DLL "; // use regcreatekey to ensure that if SOFTWARE \ daheng_directx does not exist Create one. If (regcreatekey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {//// here, you can use hkey to operate the value in the key daheng_directx. // If (regsetvalueex (hkey, "AEC", 0, REG_DWORD, (const byte *) & dwvalue, sizeof (DWORD) = error_success) {printf ("regsetvalueex: AEC = % d \ n ", dwvalue) ;}/// if you want to create a plugins key in Software \ daheng_directx, you can no longer use hkey, you need to // obtain the hkey of this node again. // If (regcreatekey (hkey, "plugins", & hsubkey) = error_success) {If (regsetvalueex (hsubkey, "color correction plugin", 0, REG_SZ, (const byte *) buffer, strlen (buffer) + 1) = error_success) {printf ("regsetvalueex: color correction plugin = % s \ n", buffer );} regclosekey (hsubkey) ;}} regclosekey (hkey) ;}void getregistryvalue () {hkey; hkey hsubkey; DWORD dwtype; DWORD dwvalue; DWORD dwsize; // use regcreatekey to ensure that if SOFTWARE \ daheng _ If DirectX does not exist, create one. If (regopenkey (HKEY_LOCAL_MACHINE, "Software \ daheng_directx", & hkey) = error_success) {//// here, you can use hkey to operate the value in the key daheng_directx. // Dwtype = REG_DWORD; dwsize = sizeof (DWORD); If (regqueryvalueex (hkey, "AEC", 0, & dwtype, & dwvalue, & dwsize) = error_success) {printf ("regqueryvalueex AEC = % d \ n", dwvalue);} else {printf ("some error occurred! \ N ") ;}//// if you want to create a plugins key in Software \ daheng_directx, you can no longer use hkey. You Need To // obtain the hkey of this node. // If (regopenkey (hkey, "plugins", & hsubkey) = error_success) {char buffer [256]; dwtype = REG_SZ; dwsize = sizeof (buffer ); if (regqueryvalueex (hsubkey, "color correction plugin", 0, & dwtype, (lpbyte) buffer, & dwsize) = error_success) {printf ("regqueryvalueex color correction plug-in = % s \ n", buffer);} else {printf ("some error occurred! \ N ") ;}regclosekey (hsubkey) ;}} regclosekey (hkey);} int main (INT argc, char * argv []) {setregistryvalue (); getregistryvalue (); getchar (); Return 0 ;}

Running result:


  1. Related Articles

Program exception capture library-crashrpt
Mapi mapisendmail
Lightweight browser control htmllite
Use the Link Control in XP
My computer software configuration
Allow your program to be detached
Transparent background button

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.