tags:

views:

63

answers:

4

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 ?

+1  A: 

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.

Sunscreen
+1  A: 

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.

Roel
I see people really love ini file the program will be portable, registry is messy sometimes . Thanks
nXqd
+4  A: 

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.

Nate
Thanks for your answer :)
nXqd
I discovered that this solution doesn’t work if your app class is a subclass of `CWinAppEx`. It still works if your app is a direct subclass of `CWinApp`. I tested it with a dialog-based app, which is based on `CWinApp`, which is why I didn’t catch the error.
Nate
+1  A: 

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

Raghuram Reddy N