Solved

GetPrivateProfileString in MFC

Posted on 1997-12-09
10
3,380 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:Altec
  • 5
  • 3
  • 2
10 Comments
 
LVL 7

Expert Comment

by:galkin
Comment Utility
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).
0
 

Author Comment

by:Altec
Comment Utility
Thanks for your answer, but I don't want to use the WIN.INI file. Could you please read my question again.

-Al
0
 

Author Comment

by:Altec
Comment Utility
Adjusted points to 125
0
 
LVL 7

Expert Comment

by:galkin
Comment Utility
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.
0
 

Author Comment

by:Altec
Comment Utility
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$.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:galkin
Comment Utility
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.
0
 

Author Comment

by:Altec
Comment Utility
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

0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 200 total points
Comment Utility
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.
0
 

Author Comment

by:Altec
Comment Utility
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

0
 
LVL 15

Expert Comment

by:Tommy Hui
Comment Utility
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);


0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now