Windows API one-day training (64) regsetvalueex and regdeletevalue Functions

Source: Internet
Author: User
Windows API one-day training (64) regsetvalueex and regdeletevalue Functions

The last time I talked about how to create the registry key, the next question was how to save the data to the Registry. The Registry uses a tree-like method to manage data, so its extension and access are flexible. However, the Registry is an important information library of the system. When a Windows system is loaded, it is first read from the hard disk to know all the hardware configuration information of each computer, and then load different drivers. Therefore, as an important file of the system, the Registry should not write data larger than 2 K to it, which can speed up the system. The following describes how to save the key value of a string. It needs to use the regsetvalueex function to set the key value and use the regdeletevalue function to delete the original key value.

The regsetvalueex and regdeletevalue functions are declared as follows:

Winadvapi
Long
Apientry
Regsetvalueexa (
_ In hkey,
_ In_opt lpcstr lpvaluename,
_ Reserved DWORD reserved,
_ In DWORD dwtype,
_ In_bcount_opt (cbdata) const byte * lpdata,
_ In DWORD cbdata
);
Winadvapi
Long
Apientry
Regsetvalueexw (
_ In hkey,
_ In_opt lpcwstr lpvaluename,
_ Reserved DWORD reserved,
_ In DWORD dwtype,
_ In_bcount_opt (cbdata) const byte * lpdata,
_ In DWORD cbdata
);
# Ifdef Unicode
# Define regsetvalueex regsetvalueexw
# Else
# Define regsetvalueex regsetvalueexa
# Endif //! Unicode

Winadvapi
Long
Apientry
Regdeletevaluea (
_ In hkey,
_ In_opt lpcstr lpvaluename
);
Winadvapi
Long
Apientry
Regdeletevaluew (
_ In hkey,
_ In_opt lpcwstr lpvaluename
);
# Ifdef Unicode
# Define regdeletevalue regdeletevaluew
# Else
# Define regdeletevalue regdeletevaluea
# Endif //! Unicode

Hkey is the primary key.
Lpvaluename is the key name.
Dwtype is the key-value type.
Lpdata is the key data.
Cbdata is the size of the key value.

An example of calling a function is as follows:
#001 // enter the registry. HKEY_CURRENT_USER/"software"/"wincpp"/"testreg"
#002 // "Windows" // "winsize" = "800*600"
#003 // Cai junsheng 2007/11/04 QQ: 9073204 Shenzhen
#004 bool writeprofilestring (maid, maid,
#005 lpctstr lpszvalue)
#006 {
#007 //
#008 long lresult;
#009 if (lpszentry = NULL) // Delete the entire key.
#010 {
#011 hkey happkey = getappregistrykey ();
#012 if (happkey = NULL)
#013 {
#014 return false;
#015}
#016
#017 lresult =: regdeletekey (happkey, lpszsection );
#018 regclosekey (happkey );
#019}
#020 else if (lpszvalue = NULL)
#021 {
#022 // delete a key value.
#023 hkey happkey = getappregistrykey ();
#024 if (happkey = NULL)
#025 {
#026 return false;
#027}
#028
#029 hkey hseckey = NULL;
#030 DWORD dw;
#031 regcreatekeyex (happkey, lpszsection, 0, reg_none,
#032 reg_option_non_volatile, key_write | key_read, null,
#033 & hseckey, & DW );
#034 regclosekey (happkey );
#035
#036 if (hseckey = NULL)
#037 {
#038 return false;
#039}
#040
#041 //
#042 lresult =: regdeletevalue (hseckey, (lptstr) lpszentry );
#043 regclosekey (hseckey );
#044}
#045 else
#046 {
#047 // set the key value.
#048 hkey happkey = getappregistrykey ();
#049 if (happkey = NULL)
#050 {
#051 return false;
#052}
#053
#054 hkey hseckey = NULL;
#055 DWORD dw;
#056 // create a subkey.
#057 regcreatekeyex (happkey, lpszsection, 0, reg_none,
#058 reg_option_non_volatile, key_write | key_read, null,
#059 & hseckey, & DW );
#060 regclosekey (happkey );
#061
#062 if (hseckey = NULL)
#063 {
#064 return false;
#065}
#066
#067 // set the item value in the subkey.
#068 lresult = regsetvalueex (hseckey, lpszentry, null, REG_SZ,
#069 (lpbyte) lpszvalue, (lstrlen (lpszvalue) + 1) * sizeof (tchar ));
#070 regclosekey (hseckey );
#071}
#072 return lresult = error_success;
#073
#074}

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.