GetPrivateProfileString in MFC

Hi...

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];
GetPrivateProfileString("Colors","color1","Default",lpbuffer,sizeof(lpbuffer),"Myini.ini")

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

///End example

#Myini.ini looks like this:
[COLORS]
color1=Blue
color2=

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

-Al
AltecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

galkinCommented:
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.

-Al
AltecAuthor Commented:
Adjusted points to 125
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

galkinCommented:
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$.
galkinCommented:
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 knowledge...you 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.

Thanks...
-Al

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AltecAuthor Commented:
Thui...

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 file...hahahahahaha...in Visual Basic the default is the exe directory or specified with app.path:

(vb432bit):
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 above...how 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!!

-Al

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);


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.