Simple VC registry operation

Source: Internet
Author: User
This article only implements simple open, create, read, write, and close operations on the registry. It is only an introduction to basic knowledge and is suitable for beginners who are not familiar with registry operations.
(I put this article here. In fact, the main purpose is to be afraid that I will forget it later. After all, I do not often use this knowledge, so I can easily understand it and save it as soon as possible, haha ^_^)

I. Description:
The Registry is an important part of windows. It records a large amount of information about computer software and hardware. The value in the registry is identified by its name. The Value Name consists of characters with the same key name. The value itself can be a string, binary data, or 32-bit unsigned value. Here we mainly use the following skills:

(1). predefined registry key. The registry contains several predefined keys:
1. HKEY_LOCAL_MACHINE contains entries describing the computer and its configuration. This includes information about the processor, system motherboard, memory, and installed software and hardware.
2. hkey_classes_root is the support key for document type and OLE/com-related information. This is the slave key of HKEY_LOCAL_MACHINE.
3. HKEY_USERS is used as the default user's preferred setting and also as the preferred setting for a single user.
4. hkey_classes_user is the information used for the current (LOGIN) user.
5. hkey_current_config contains information about the current system configuration.
6. There is another one, which generally does not appear. It will only be available after the configured LAN.

(2) Two registry operations in VC


In VC, there are two main methods to perform registry operations. One is to use the class cregkey encapsulated by MFC, which contains 14 member functions, they can implement simple operations on the registry (Basic
As mentioned in this article, "Open, create, read, write, and close"); second, use Windows
API functions. Of course, for me, I can use the underlying layer. Why not? After all, I amProgramYou can use underlying functions to port them to more development platforms.

1.
Required header file: atlbase. h
This method can be completed using the member functions in the cregkey class. First include the header file, and then declare a Class Object cregkey rkey; then you can call the member function to operate.
Member functions of basic functions:

Long Open (hkey hkeyparent, lpctstr lpszkeyname, regsam samdesired = key_all_access );
Long close ();
Long CREATE (hkey hkeyparent, lpctstr lpszkeyname,
Lptstr lpszclass = reg_none, DWORD dwoptions = reg_option_non_volatile,
Regsam samdesired = key_all_access,
Lpsecurity_attributes l1_cattr = NULL,
Lpdword lpdwdisposition = NULL );
Long setvalue (DWORD dwvalue, lpctstr lpszvaluename );
Long setvalue (maid = NULL );
Long queryvalue (DWORD & dwvalue, lpctstr lpszvaluename );
Long queryvalue (lptstr szvalue, lpctstr lpszvaluename, DWORD * pdwcount );

Others:

Long setkeyvalue (maid, maid );
Static long winapi setvalue (hkey hkeyparent, maid, maid );
Hkey detach ();
Void attach (hkey );
Long deletesubkey (lpctstr lpszsubkey );
Long recursedeletekey (lpctstr lpszkey );
Long deletevalue (lpctstr lpszvalue );


However, this article mainly introduces how to use Windows API functions to operate the Registry. Therefore, we will not detail the usage of these member functions here.

2.
The number of Windows API functions is relatively large, with a total of three or forty. Here we mainly introduce several common functions to implement the basic functions described in this article.

Common API functions

☆Function for opening a key: regopenkeyex
Function Syntax: Long regopenkeyex (hkey, // the handle of the opened key, or directly the preceding root keys
Lptstr lpsubkey, // address of the subkey name to be opened
DWORD uloptions, // reserved value, must be 0
Regsam samdesired, // open mode, such as read or write
Phkey phkresult // return the handle of the opened sub-Key
);
Note: If the specified sub-key to be opened does not exist, an error is returned. Otherwise, a successful error_success is returned;

☆Create a key function: regcreatekeyex
Function Definition: Long regcreatekeyex (hkey, // the handle of the opened key, or directly the preceding root keys
Lptstr lpsubkey, // address of the subkey name to be created
DWORD uloptions, // reserved value, must be 0
Regsam samdesired, // open mode, such as read or write
Phkey phkresult // return the created sub-key handle
);
Note: If the specified sub-key to be created already exists, open the sub-key. Otherwise, create the sub-key. If the function is successful, error_success is returned;
This sub-key can be used to create multiple layers of sub-keys at a time, in the format of "sub-key Layer 1 // sub-key Layer 2 // sub-key Layer 3 ..."

☆Query a key value: regqueryvalueex
Function Syntax: Long regqueryvalueex (hkey, // handle of the key to be queried
Lpctstr lpvaluename, // name of the key value to be queried
Lpdword lpreserved, // reserved value, must be 0
Lpdword lptype, // type of the data to be queried. If not, it can be null.
Lpbyte lpdata, // data to be returned for query
Lpdword lpcbdata // The length of the preset data, which can be null in theory, but I can't find it. It is best to set a value.
);

☆Set a key value regsetvalueex
Function Definition: Long regsetvalueex (hkey, // handle of the key to be set
Lpctstr lpvaluename, // name of the key value to be accessed
Lpdword lpreserved, // reserved value
DWORD dwtype, // data type to be set
Const byte * lpdata, // The Key To be set
DWORD cbdata // Data Length
);
Note: If the data to be written already exists, it only changes the value. If it does not exist, a new sub-key will coexist.

Ii. Example

I did some simple operations on the registry, mainly to open it. If it fails to open, it will be created, and then save the value;

hkey key;
cstring strname = "hello";
cstring strcompany = "world";
cstring strseries = "1111-1111 ";
bool g_bislicesed = true;

If (regopenkeyex (HKEY_CURRENT_USER, _ T ("software // my application // Settings"), 0, key_all_access, & hkey )! = Error_success)
{
Verify (! Regcreatekey (HKEY_CURRENT_USER, _ T ("software // my application // Settings"), & hkey ));
}

Verify (! Regsetvalueex (hkey, _ T ("licesedbool"), 0, REG_DWORD, (byte *) & g_bislicesed, 4 ));
Verify (! Regsetvalueex (hkey, _ T ("User Name"), 0, REG_SZ, (byte *) strname. getbuffer (strname. getlength (), 50 ));
Verify (! Regsetvalueex (hkey, _ T ("company name"), 0, REG_SZ, (byte *) strcompany. getbuffer (strcompany. getlength (), 25 ));
Verify (! Regsetvalueex (hkey, _ T ("license code"), 0, REG_SZ, (byte *) strseries. getbuffer (strseries. getlength (), 20 ));
Regclosekey (hkey );

In addition, I also read the registry.

Unsigned char chbuf [50];
Cstring strcompany = "";
Cstring strseries = "";
Hkey;
DWORD type (0 );
DWORD Len (50 );
Memset (chbuf, 0, sizeof (chbuf ));


If (regopenkeyex (HKEY_CURRENT_USER, _ T ("software // my
Application // Settings "), 0, key_all_access, & hkey) = error_success)
{
If (regqueryvalueex (hkey, _ T ("User Name"), 0, & type, chbuf, & Len) = error_success)
{
;
}
If (! Regqueryvalueex (hkey, _ T ("company name"), 0, & type, chbuf, & Len ))
{
Strcompany. Format ("% s", chbuf );
}
If (! Regqueryvalueex (hkey, _ T ("license code"), 0, & type, chbuf, & Len ))
{
Strseries. Format ("% s", chbuf );
}
If (! Regqueryvalueex (hkey, _ T ("licesedbool"), 0, & type, chbuf, & Len ))
{
G_bislicesed = chbuf [0];
}

Regclosekey (hkey );
}

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.