Tzu ng
Tzu ng

Reputation: 9244

Save file settings in ini instead of registry

I'm new to MFC, once I create my first app, in myApp::InitInstance() . I have

SetRegistryKey(_T("Local AppWizard-Generated Applications"));

Can I delete this and save settings to my own ini construct ?

Upvotes: 2

Views: 5263

Answers (4)

Nate
Nate

Reputation: 19030

Edit: After further testing, the solution below does not work if your app class is derived from CWinAppEx ! It does work if your app is directly derived from CWinApp.


To store values in an .ini file instead of the registry:

  1. Omit the call to SetRegistryKey.
  2. In your app class, set m_pszProfileName to the full path of your .ini file. The filename string must be allocated using malloc, because the framework will call free on it when your app shuts down. First free the existing value, then assign your new string:

    free((void*)m_pszProfileName);
    m_pszProfileName = ::_tcsdup(_T("C:\\somedir\\myini.ini"));

  3. Call CWinApp::GetProfileInt, CWinApp::WriteProfileInt and similar functions as usual.

I strongly recommend using a path under APPDATA for storing your .ini file.

Upvotes: 7

Raghuram Reddy N
Raghuram Reddy N

Reputation: 55

Use win32 APIs WriteProfileString (write to INI file) and GetProfileString (read from INI file) For more help ms-help://MS.MSDNQTR.v90.en/sysinfo/base/writeprofilestring.htm

Upvotes: 1

Roel
Roel

Reputation: 19642

Yes you can. CWinApp::SetProfileXXX() does this for you, actually - but I wouldn't use these methods anymore in 2010, they were OK when ppl moved from .ini to the registry.

Upvotes: 2

Sunscreen
Sunscreen

Reputation: 3564

I am not sure if this is possible as a .ini file has only strings for your program. You can create an operating system script (.bat for windows, .sh for unix etc) and call it using system() call.

Upvotes: 1

Related Questions