GetPrivateProfileString in MFC


I'm a VB programmer learning C++. I have an application I am writing which I want to read the values in an ini file on init...the code I am using is as follows:

// CWtpautoApp initialization
BOOL CWtpautoApp::InitInstance()
char lpbuffer[256];

;WritePrivateProfileString("Colors", "color2", lpbuffer, "Myini.ini");

///End example

#Myini.ini looks like this:

I'd like the app to read the ini file and then write to it.
I have tried everything the last couple of days and just cannot get this thing to work. If anybody could show me how to write this correctly, I'd appreciate it very much. I am using MS Visual C++ 4.0 Pro...and I am using statically linked libraries in MFC.

Thanks in advance...I'm going to get some sleep...:-o

Who is Participating?
Tommy HuiEngineerCommented:
You did you say where you put the myini.ini. Make sure it is located in the Windows directory. By the way, your code looks okay. I don't see anything wrong with it right away. So I suspect that the problem is the location of the file itself. One way to confirm this is to actually write something to the file first and then try reading it back. Then you can see where the file is supposed to be located.

If you need an example, let me know, but I suspect that the problem is location of your ini file.
First of all you can use CWinApp class GetProfileString, WriteProfileString, GetProfileInt and WriteProfileString function instaed of global functions. You can define INI file using CWinApp m_pszProfileName member variable. By default it has the same name as executable. Say you want to get "color1" entry value under "colors" section so you have to write
AfxGetApp()->GetProfileString(_T("Colors"), _T("color1")).This function retuns variable of type CString which is entry if found or default string(last third parameter in GetProfileString, default - NULL).
AltecAuthor Commented:
Thanks for your answer, but I don't want to use the WIN.INI file. Could you please read my question again.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

AltecAuthor Commented:
Adjusted points to 125
Code I wrote you allws to read from private INI file not from WIN.INI but by slightly modifying it you can read from ANY file(not .INI) CWinApp m_pszProfileName variable contains file name you read from.By default it is application INI file having the same name as executable.If you want to change file you can do this by changing the value of m_pszProfileName.
AltecAuthor Commented:
OK, but I still don't understand where the string is??? How do I write to the INI file? If I am using:

; AfxGetApp()->GetProfileString(_T("Colors"), _T("color1"))

Where is the variable that is holding "color1" ? How would I use that variable to write it to the same ini file...or put it in a messagebox so I could see it return true?

;AfxMessageBox(WHAT_IS_VARIABLE_HERE_FOR_color1, MB_OK);

Forgive me if I don't understand, but this is very different for me. In VB I could just use it as a String$.
I wrote you that CWinApp returns variable of type CString, class which wraps regular string in MFC. There two overloaded AfxMessageBox functions, one which takes resourec ID and another which takes LPCTSTR. There is CString operator (LPCTSTR) which converts from CString to LPCSTR so you can pass CString which  you obtained from call to CWinApp GetProfileString, as parameter to AfxMessageBox.
AltecAuthor Commented:
Why can't you just show me an example like I asked? You obviously possess the know what I am asking...why can't you just show me? I am asking for an example of how this all works. The simple fact is I'm not understanding...please just show me an example. That's how I learn...once I understand the process of getting the string, assigning a variable and passing it (or writing it) somewhere...I can move on to other things. I appreciate your time and realize how simple this must be to you, but please...just work with me a bit more.


AltecAuthor Commented:

That's unreal. I feel really red-faced! My code has been working this whole time. The myini.ini file was in the Windows directory...and all my test run entries were right there inside...every one of them. I kept checking the myini.ini file that was in the same directory as my exe Visual Basic the default is the exe directory or specified with app.path:

result1% = GetPrivateProfileString("Colors", "color1", "Default", Rturn$, "100", App.Path & "\MYINI.INI")

Rturn$ is the value returned as a string (declared earlier).

T - H - A - N - K - S !!!

I went ahead and bumped this up to 200 points...can you tell me one more thing? When I throw out a messagebox with lpbuffer just for checking my work:

AfxMessageBox(lpbuffer, MB_OK);

It does return the correct value as outlined in my original code do I add the string to a message? ie: in VB, I would write it like this:

MsgBox ("The color chosen was: " + lpbuffer)

This would return the messagebox:
"The color chosen was: Blue"

Blue being what was returned by GetPrivateProfileString. How would I add it in C++?

Thanks in advance!!


Tommy HuiEngineerCommented:
You would create a CString instance and use the Format method to create the string you want (similar to printf):

  CString foo;
  foo.Format("This is the string I got back %s ", lpBuffer);
  AfxMessageBox(foo, MB_OK);

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.