How to save application configuration information to the Registry in VC ++ MFC (1), vcmfc

Source: Internet
Author: User

How to save application configuration information to the Registry in VC ++ MFC (1), vcmfc

Registry is an important database in Microsoft Windows. It is used to store system and application settings. For example, we open a notepad, adjust its window size, and close it after use. The size of the next notepad is the same as that of the last one on the screen. The location and size information is saved to the registry when notepad is closed. When it is opened next time, we extract the data from the registry and display the size and location information of the Notepad program according to the data.

The following describes how to use the GetProfileInt, WriteProfileInt, GetProfileString, WriteProfileString, and SetRegistryKey functions to save the application information you want to save to the Registry and obtain the information from the registry.

UINT WINAPI GetProfileInt( LPCTSTR lpAppName,  LPCTSTR lpKeyName, int nDefault);BOOL WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);BOOL WINAPI WriteProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString);BOOL WriteProfileInt(LPCTSTR lpszSection,LPCTSTR lpszEntry,int nValue );

The above four functions provide a set of interfaces for reading and writing application configurations in the CWinApp class, which can be conveniently used to read and write application configurations. For specific function descriptions and parameter explanations, refer to MSDN.

Next we will teach you how to save the information you want to save to the Registry.

1.After creating a dialog box-based MFC project using the VC ++ wizard, add the following statement to the CClassApp: InitInstance () function definition:

SetRegistryKey (_ T ("Local Application generated by the Application Wizard "));

This function will create a working environment for the methods mentioned above. If WriteProfileInt is used to write data, it will be written to the following registry location:

HKEY_CURRENT_USER \ Software \ Local Application \ Application name generated by the Application Wizard \

You can open the Registry to check whether the corresponding key-value item exists.

If SetRegistryKey is not executed in InitInstance, when data is written using WriteProfileInt, it is written to % windir % \ Application name. ini.

2.Write the data to be saved to the Registry

A.If SetRegistryKey ("Local Application generated by the Application Wizard") is executed in InitInstance,:

WriteProfileInt("section","val1",10);

Write Data in the following path in the registry:
[HKEY_CURRENT_USER \ Software \ Local Application \ Application name \ section generated by the Application Wizard] "val1" = dword: 0000000a

B.If SetRegistryKey is not executed in InitInstance:

WriteProfileInt("section","val1",10);

Will be written in "% windir % \ test application. ini:
[Section]

Val1 = 10

3. Read the required value from the registry.

GetProfileInt("section","val1",10);

The last parameter is the default value. If no value is obtained from the registry, this default value is used.

The GetProfileString and WriteProfileString interfaces are used to obtain the value of the string type.

The following is a test routine. You can test it in the project!

Add the SetRegistryKey function in CTestClassApp: InitInstance ().

BOOL CTestClassApp: InitInstance () {SetRegistryKey (_ T ("Test Regedit Solutions"); AfxEnableControlContainer ();............. // The code here is omitted ............. return FALSE ;}

You can add the following test code to the response function of the OK button in the dialog box.

void CTestClassDlg::OnOK() {        AfxGetApp()->WriteProfileInt("Settings", "Int", 21);    AfxGetApp()->WriteProfileString("Settings", "String", _T("Welcome you come here!"));     int nGetInt = AfxGetApp()->GetProfileInt("Settings", "Int", 21);    CString strGetString = AfxGetApp()->GetProfileString("Settings", "String", _T("Welcome you come here!"));     TCHAR szBuffer[256];    wsprintf(szBuffer,_T("int:%d  string:%s"),nGetInt,strGetString);    AfxMessageBox(szBuffer);    //CDialog::OnOK();}

Run the program and click OK to bring up the dialog box

The obtained value is the same as the value written in our program.

Enter regedit in the search program to open the registry, and click the path above to locate the response. view the key value and corresponding data, as shown in.

The saved value is also correct.

With this, we can save some simple program configuration data that we need to save in the registry.

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.