Click here to Skip to main content
15,888,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 6 check boxes and I wanted to store the value's in the registry while closing application and set back while starting the application. how do I do that...?

What I have tried:

	//These are the check boxes on the child dialog Settings
BOOL m_chkAddStudentVal;
	BOOL m_chkUpdateStudentVal;
	BOOL m_chkDeleteStudentVal;
	BOOL m_chkAddTeacherVal;
	BOOL m_chkUpdateTeacherVal;
	BOOL m_chkDeleteTeacherVal;


typedef struct {
		void Set(int* _chkBoxes[],CWnd* settingwindow, int nFlag=1)
		{
//Here I want to restore saved data from registry
			settingwindow->UpdateData();
			for(int i=0; i<6; i++)
			{
				chkBoxes[i]=_chkBoxes[i];
				*chkBoxes[i]=nFlag;
			}
			settingwindow->UpdateData(FALSE);
		}
		int* chkBoxes[6];
	}CHKSETTINGS;
	CHKSETTINGS m_chkSettings;


void CDialogControlDlg::OnExit()
	{
// Here My main application is going to close so here I want to save status of the check boxes to the registry
		m_setWindows.DeleteWindows();
		Shell_NotifyIcon(NIM_DELETE,&niData);
		exit(0);
	}
Posted
Updated 26-Feb-17 21:39pm

When you create a new MFC project there will be a call to SetRegistryKey() placed at InitInstance. Change the string to your company name. This will use the registry instead of INI files for profile settings. The used registry path will be HKCU\Software\<company_name>\<application_name>.

Then just use the CWinApp Get and Set profile functions. To call them from other classes than your CWinApp derived class use AfxGetApp():
C++
// Save
AfxGetApp()->WriteProfileInt(_T("Settings"), _T("AddStudent"), m_chkAddStudentVal);

// Restore
m_chkAddStudentVal = AfxGetApp()->GetProfileInt(_T("Settings"), _T("AddStudent"), 0) ? 1 : 0;

Choose appropriate section and key names. I have used "Settings" as section name because it is common to store recent settings at program termination.
 
Share this answer
 
v2
Here at CodeProject you may find a lof of articles on reading/writing values from/to the registry[^].

You might also use the WriteProfileString function (Windows)[^] for such purpose.
 
Share this answer
 
Use of the registry is now generally discouraged. There are better ways to save application information if you are not using a database. See Where should I store my data?[^] by OriginalGriff.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900