Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


GetPrivateProfileString in MFC

Posted on 1997-12-09
Medium Priority
Last Modified: 2013-11-25

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

Question by:Altec
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2

Expert Comment

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

Author Comment

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


Author Comment

ID: 1311860
Adjusted points to 125
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Expert Comment

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.

Author Comment

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$.

Expert Comment

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.

Author Comment

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.


LVL 15

Accepted Solution

Tommy Hui earned 800 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.

Author Comment

ID: 1311866

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:

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!!


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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

618 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