Jump to content

Help using GetPrivateProfileString()


MasteR
 Share

Recommended Posts

Does anyone have any experience using the WritePrivateProfile and GetPrivateProfile functions in windows.h?

 

I decided to write an .INI parser and through my research learned of the above functions, I can't get them to work however. I'm not adverse to simply writing the read and write functions from scratch, I thought that's what I'd be doing, but these functions simplify things nicely.

 

WritePrivateProfileString((LPCWSTR)"Section",  (LPCWSTR)"Key", "1", (LPCWSTR)"C:\Test.ini");
int result=GetPrivateProfileInt((LPCWSTR)"Section",  (LPCWSTR)"Key", 0, (LPCWSTR)"C:\Test.ini");

 

The above code works to a point, the INI file is located and successfully opened in both cases, but the information in the INI file is never read or written.

 

Example INI file: Test.ini

[section]
Key=2

 

...So does anyone have any experience using the above function set?

AMD Athlon 64 X2 Dual Core 6000+ (3.0 GHz)

4 GB DDR2 RAM

2 x NVIDIA GeForce 9500 GT 512 MB (SLI 1.0 GB)

Windows XP Pro

Link to comment
Share on other sites

I don't have any experience in using those, since I use std::fstream for files (std::ifstream for reading, std::ofstream for writing). Open the files in binary mode, and then use the .read() and .write() operations. That has worked fine for my own projects, from all the way back from last November when I built that functionality for myself.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

I guess your problem is character encoding.

You convert a pointer to an ASCII string to a wstr:

 

WritePrivateProfileString((LPCWSTR)"Section",  (LPCWSTR)"Key", "1", (LPCWSTR)"C:\Test.ini");
int result=GetPrivateProfileInt((LPCWSTR)"Section",  (LPCWSTR)"Key", 0, (LPCWSTR)"C:\Test.ini");

 

So it won't understand where you want the file to be created because the bytes from "C:\Test.ini" interpreted as a wide char string are just garbage. For sure not an existing directory's name xD. In your case, you should have preceded all literals with L, like (L"Section", L"Key", L"1", L"C:\Test.ini"), then the casting wouldn't even be necessary.

 

But is better to always provide string literals enclosed in the _T() macro (which adds the L"" when compiling unicode). Include "<tchar.h>" to use this.

Also, never use LPCWSTR, LPCSTR, CHAR (= char), WCHAR explicitly when dealing whit text, but implicitly through LPCTSTR and TCHAR.

And since this is probably for le, switch your Character Set (project options) to Multi-Byte (= ASCII). This will make windows.h to define WritePrivateProfileString as WritePrivateProfileStringA instead of WritePrivateProfileStringW as it is now.

 

Your code should then look like this:

WritePrivateProfileString(_T("Section"),  _T("Key"), "1", _T("C:\Test.ini"));
int result=GetPrivateProfileInt(_T("Section"),  _T("Key"), 0, _T("C:\Test.ini"));

 

PS: Simply casting pointer types is usually a bad idea most of times when the compiler complains about wrong argument types.

This is a good read about character encoding with C/C++ and win api: http://msdn.microsoft.com/en-us/library/06b9yaeb.aspx

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...