Solved

GetPrivateProfileString in MFC

Posted on 1997-12-09
10
3,436 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
ID: 1311858
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
ID: 1311859
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
ID: 1311860
Adjusted points to 125
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 7

Expert Comment

by:galkin
ID: 1311861
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
ID: 1311862
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
 
LVL 7

Expert Comment

by:galkin
ID: 1311863
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
ID: 1311864
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
ID: 1311865
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
ID: 1311866
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
ID: 1311867
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 72
Saving the SQL Login Credentials for Excel 2010 Microsoft Queries 1 142
fizzArray2 challenge 1 81
How do ASP.NET and MVC work together? 4 48
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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…

831 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