Windows API one-day training (63) regopenkeyex and regcreatekeyex Functions

Source: Internet
Author: User
Windows API one-day training (63) regopenkeyex and regcreatekeyex Functions

Because the computer is often shut down, or applications are often closed, but some parameters need to be saved frequently. For example, when you open a program and set the window size, you want to set the window size to the size of the previous window. In this way, you need to save the size of the window. What about the window size parameter? In fact, the most convenient way in Windows is to save it to the Registry. For example, when logging on to a game, you always want to save the last logon user. You also need to save the user to the Registry. In fact, the Registry is a database for Windows to save system configurations, such as different language settings, different time zone settings, different user logon, and different permissions. Next we will learn how to use the regopenkeyex function to open the key in the registry and use the regcreatekeyex function to create a new key.

The regopenkeyex and regcreatekeyex functions are declared as follows:

Winadvapi
Long
Apientry
Regopenkeyexa (
_ In hkey,
_ In_opt lpcstr lpsubkey,
_ Reserved DWORD uloptions,
_ In regsam samdesired,
_ Out phkey phkresult
);
Winadvapi
Long
Apientry
Regopenkeyexw (
_ In hkey,
_ In_opt lpcwstr lpsubkey,
_ Reserved DWORD uloptions,
_ In regsam samdesired,
_ Out phkey phkresult
);
# Ifdef Unicode
# Define regopenkeyex regopenkeyexw
# Else
# Define regopenkeyex regopenkeyexa
# Endif //! Unicode

Winadvapi
Long
Apientry
Regcreatekeyexa (
_ In hkey,
_ In lpcstr lpsubkey,
_ Reserved DWORD reserved,
_ In_opt lpstr lpclass,
_ In DWORD dwoptions,
_ In regsam samdesired,
_ In_opt lpsecurity_attributes lpsecurityattributes,
_ Out phkey phkresult,
_ Out_opt lpdword lpdwdisposition
);
Winadvapi
Long
Apientry
Regcreatekeyexw (
_ In hkey,
_ In lpcwstr lpsubkey,
_ Reserved DWORD reserved,
_ In_opt lpwstr lpclass,
_ In DWORD dwoptions,
_ In regsam samdesired,
_ In_opt lpsecurity_attributes lpsecurityattributes,
_ Out phkey phkresult,
_ Out_opt lpdword lpdwdisposition
);
# Ifdef Unicode
# Define regcreatekeyex regcreatekeyexw
# Else
# Define regcreatekeyex regcreatekeyexa
# Endif //! Unicode

Hkey is the primary key.
Lpsubkey is a subkey.
Uloptions is an option.
Samdesired is a key operation.
Phkresult is the key to be opened.
The lpclass is the new key value.

An example of calling a function is as follows:
#001 // enter the registry. HKEY_CURRENT_USER/"software"/"wincpp"/"testreg"
#002 // Cai junsheng 2007/11/02 QQ: 9073204 Shenzhen
#003 hkey getappregistrykey (void)
#004 {
#005 hkey happkey = NULL;
#006 hkey hsoftkey = NULL;
#007 hkey hcompanykey = NULL;
#008
#009 // open HKEY_CURRENT_USER/"software ".
#010 if (regopenkeyex (HKEY_CURRENT_USER, _ T ("software"), 0, key_write | key_read,
#011 & hsoftkey) = error_success)
#012 {
#013 DWORD dw;
#014 // create and enable HKEY_CURRENT_USER/"software"/"wincpp"
#015 if (regcreatekeyex (hsoftkey, _ T ("wincpp"), 0, reg_none,
#016 reg_option_non_volatile, key_write | key_read, null,
#017 & hcompanykey, & DW) = error_success)
#018 {
#019 // create and enable HKEY_CURRENT_USER/"software"/"wincpp"/"testreg"
#020 regcreatekeyex (hcompanykey, _ T ("testreg"), 0, reg_none,
#021 reg_option_non_volatile, key_write | key_read, null,
#022 & happkey, & DW );
#023}
#024}
#025
#026 // close the enabled key value.
#027 if (hsoftkey! = NULL)
#028 {
#029 regclosekey (hsoftkey );
#030}
#031
#032 if (hcompanykey! = NULL)
#033 {
#034 regclosekey (hcompanykey );
#035}
#036
#037 return happkey;
#038}

Related Article

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.