MFC Registry: How to store the application settings in registry?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: MFC Registry: How to store the application settings in registry?

  1. #1
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    MFC Registry: How to store the application settings in registry?

    Q: How to store and retrieve the MFC application settings in/from registry?

    A: By using the following CWinApp member functions:
    • 'CWinApp::GetProfileString()'
    • 'CWinApp::GetProfileInt()'
    • CWinApp::WriteProfileString()'
    • CWinApp::WriteProfileInt()'

    Before calling any of above functions, you have to call 'CWinApp::SetRegistryKey()'. This causes application settings to be stored in the registry instead of regualr INI files.

    Further, 'CWinApp' registry functions will read/write under 'HKEY_CURRENT_USER\Software\<'lpszRegistryKey' argument of 'SetRegistryKey()'>\<'lpszSection' argument of 'CWinApp' registry function>'.

    Here is an example of an application that stores last "User Name" for login (e.g. to a database) to be used in subsequent application instances:

    Code:
    class CMyApp : public CWinApp
    {
      // ...
      // Attributes
    protected:
      CString m_strUserName;
      LPCTSTR const m_pszLoginSection;
      LPCTSTR const m_pszUserNameEntry;
      // ... 
    
      // Operations
    public:
      void GetUserName(CString& strUserName) const
      {
        strUserName = m_strUserName;
      }
    
      void SetUserName(LPCTSTR pszNewValue)
      {
        m_strUserName = pszNewValue;
      }
    
      //...
    };
    
    CMyApp::CMyApp() : m_pszLoginSection(_T("Login")),
                       m_pszUserNameEntry(_T("User Name"))
    {
    }
    
    BOOL CMyApp::InitInstance()
    {
      // ...
      // Change the registry key under which our settings are stored.
      // Usually it's the company name.
      SetRegistryKey(_T("Zerolei Software"));
    
      // Read "User Name" registry entry (value) from 
      // HKEY_CURRENT_USER\Software\Zerolei Software\MyApp\Login key
      m_strUserName = GetProfileString(m_pszLoginSection, m_pszUserNameEntry);
    
      // ...
      return TRUE;
    }
    
    int CMyApp::ExitInstance() 
    {
      // write 'm_strUserName' in "User Name" registry entry (value) under
      // HKEY_CURRENT_USER\Software\Zerolei Software\MyApp\Login key
      WriteProfileString(m_pszLoginSection, m_pszUserNameEntry, m_strUserName);
    
      // ...
      return CWinApp::ExitInstance();
    }
    
    void CMainFrame::OnLogin() 
    {
      CMyApp* pApp = (CMyApp*)AfxGetApp();
      CLoginDialog dlgLogin;
      pApp->GetUserName(dlgLogin.m_strUserName);
    
      // ...
    
      if(IDOK == dlgLogin.DoModal())
      {
        pApp->SetUserName(dlgLogin.m_strUserName);
        //...
      }
    }

    Last edited by Andreas Masur; June 17th, 2006 at 04:48 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center