Read and Write the Registry in VC

Source: Internet
Author: User
The registry of Windows 95 and NT is an important component of the system. There is a set of Reg functions in Win32 API to handle these problems. The General read/write process is as follows:
 
1. Use the regopenkeyex or regcreatekeyex function to open or create a key;
2. If the previous step is successful, use regqueryvalueex to read the sub-key value, use regsetvalueex to set the sub-key value, use regenumkey to obtain all the sub-keys, and use regdeletekey to delete a key;
3. Use regclosekey to close the key after completing the operation.
In the following program, open the HKEY_CURRENT_USER \ Software \ zeal softstudio \ askpro FTP \ lasttime key, and then read the value of the wol subkey.

Hkey;
Char SZ [256];
Dwords dwtype, SL = 256;

Regopenkeyex (HKEY_CURRENT_USER,
"Software \ zeal softstudio \ askpro FTP \ lasttime ",
Null, key_all_access, & hkey );
Regqueryvalueex (hkey, "Wol", null, & dwtype, (lpbyte) SZ, & SL );
Regclosekey (hkey );
The MFC program can use the cregkey class to read and write the registry. For more information about how to call APIs in VB, see qa000226 "how to access the Windows Registry ".

Open registration key
Long regopenkeyex (hkey, // handle to open key

Lptstr lpsubkey, // address of name of subkey to open
DWORD uloptions, // reserved = 0
Regsam samdesired, // security access mask
Phkey phkresult // address of handle to open key
);

Example:
Hkey HD;
Hd = HKEY_LOCAL_MACHINE;
Char * regkeyname = "SOFTWARE \ xy123 \ poker \\";
Long A = regopenkeyex (HD, regkeyname, 0, key_read, & HD); // error_success is returned successfully; otherwise, error code is returned.
 

Disable registration key
Long regclosekey (hkey // handle to key to close );
Example:
Regclosekey (HKEY_LOCAL_MACHINE );
Or: regclosekey (HD );
Create a registration key
Long regcreatekeyex (hkey, // handle to an open key
Lptstr lpsubkey, // address of subkey name
DWORD reserved, // reserved = 0
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 );
Example:
Char * sclass = ""; // The class name is null.
Dword nbf = 0; // accept the return value, indicating whether to create a new key or open an existing key. (reg_opened_existing_key is always returned after the test.
Long II = regcreatekeyex (HD, regkeyname, 0, sclass, reg_option_non_volatile,
Key_read | key_write, null, & HD, & NBF );

// Reg_option_non_volatile indicates that the key is permanently retained. If the security structure is null, a default value is automatically obtained.
// Error_success is returned successfully; otherwise, error code is returned.
Enumeration key value
Long regenumvalue (hkey, // handle to key to query
DWORD dwindex, // index of value to query
Lptstr lpvaluename, // address of buffer for value string
Lpdword lpcbvaluename, // address for size of value Buffer
Lpdword lpreserved, // reserved = NULL
Lpdword lptype, // address of buffer for Type Code

Lpbyte lpdata, // address of buffer for value data
Lpdword lpcbdata // address for size of data buffer );
Example:
DWORD dinx = 0;
Char valuename [70]; // allocate the Value Name Buffer
Strcpy (valuename, "comment pattern"); // specifies the name of a key value.
DWORD nsize = 69; // value name buffer size
Dword k = REG_SZ; // specify the Data Type
Unsigned char vari [70]; // allocate a numerical Buffer
DWORD ncbvari = 69; // numeric buffer size
Dinx = 0; // starts from 0

While (II = regenumvalue (HD, dinx, valuename, & nsize, null, & K, vari, & ncbvari ))
! = Error_no_more_items)
{
Dinx ++; // index + 1, ready to take the next value
Nsize = 69; // restore the original size
Ncbvari = 69;
}
The return value is 0. the return value of each variable is as follows:
Valuename = Value Name, ending with 0; for example, using color
Nsize = Value Name Length, 9
K = REG_SZ deskcolor is of the REG_SZ type.
Vari = key value, 32768 bytes color = "32768 ",
Ncbvari = key value length REG_SZ includes end 0, = 6,
Read key value
Long regqueryvalueex (hkey, // handle to key to query

Lptstr lpvaluename, // address of name of value to query
Lpdword lpreserved, // Reserved
Lpdword lptype, // address of buffer for Value Type
Lpbyte lpdata, // address of Data Buffer
Lpdword lpcbdata // address of data buffer size );
Example:
Regqueryvalueex (HD, valuename, null, & K, vari, & ncbvari );
After the variable is defined and successfully defined, the value of each variable is the same as that of regenumvalueex.
Write key value
Long regsetvalueex (hkey, // handle to key to set value
Lpctstr lpvaluename, // name of the value to set

DWORD reserved, // Reserved
DWORD dwtype, // flag for Value Type
Const byte * lpdata, // address of value data
DWORD cbdata // size of value data );
Example:
Strcpy (valuename, "hello ");
Unsigned char vari [10];
Dword k = REG_SZ;
Strcpy (char *) vari, "1234567 ")
Regsetvalueex (HD, valuename, 0, K, vari, 7 );
Add a key value "Hello: REG_SZ: 1234567" to the poker.
Write integer variable:
Int HI = 8;
Regsetvalueex (PJ, valuename, 0, REG_BINARY, (unsigned char *) & hi, sizeof (INT ));

Add a key value hello2: REG_BINARY: 08 00 00 under poker.

Void addeventsource ()
{
Hkey HK;
DWORD dwdata;
Uchar szbuf [80];

// Add your source name as a subkey under the Application
// Key in the EventLog registry key.

If (regcreatekey (HKEY_LOCAL_MACHINE,
"System \ CurrentControlSet \ Services \
\ Eventlog \ Application \ samplapp ", & HK ))
Errorexit ("cocould not create the registry key .");

// Set the name of the message file.

Strcpy (szbuf, "% SystemRoot % \ System \ samplapp. dll ");

// Add the name to the eventmessagefile subkey.

If (regsetvalueex (HK, // subkey handle
"Eventmessagefile", // Value Name
0, // must be zero
REG_EXPAND_SZ, // Value Type
(Lpbyte) szbuf, // pointer to value data
Strlen (szbuf) + 1) // length of value data

Errorexit ("cocould not set the event message file .");

// Set the supported event types in the typessupported subkey.

Dwdata = eventlog_error_type | eventlog_warning_type |
Eventlog_information_type;

If (regsetvalueex (HK, // subkey handle
"Typessupported", // Value Name
0, // must be zero
REG_DWORD, // Value Type
(Lpbyte) & dwdata, // pointer to value data

Sizeof (DWORD) // length of value data
Errorexit ("cocould not set the supported types .");

Regclosekey (HK );
}

Run the following code to rewrite the shell key of the Registry to C: \ DK1 \ ATM \ harp \ exatm shell.exe:

Hkey;
Long res;
DWORD datatype = REG_SZ;
Unsigned char szvalue [_ max_path];
Strcpy (char *) szvalue, "C: \ DK1 \ ATM \ harp \ exatm shell.exe ");

Res =: regopenkeyex (HKEY_LOCAL_MACHINE,
"SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ WinLogon \", 0,
Key_write | key_read, & hkey );

If (res! = Error_success)
{
Afxmessagebox ("AAA ");
Return;
}
Res =: regsetvalueex (hkey, "shell", 0, datatype, szvalue, strlen (lpcstr (szvalue )));

Regclosekey (hkey );
If (RES = error_success)
: Afxmessagebox ("You have successfully set the Registry self-starting shell key value to C: \ DK1 \ ATM \ harp \ exatm shell.exe ");
Else
: Afxmessagebox ("setting failed: the target location does not have such a key! ");

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.