Valhalla Legends Forums Archive | C/C++ Programming | GetPrivateProfileString() Help?

AuthorMessageTime
Spilled[DW]
Im running into trouble with using this API and i cant find the problem. The API keeps returning the default value that i set to "default"

heres the function
[code]
char* getStuff(char* appname, char* key)
{
      char* answer = new char[120];
      GetPrivateProfileString("Settings","User","Default",answer,120,"Config.ini");
     
      return answer;                             
}
[/code]

Heres the INI i read from:
[Settings]
User=Spilled
Pass=
Server=uswest.battle.net
CDKey=
Home=Op )(
Client=PXES
Trigger=$

When i call getStuff it just returns "Default" Any ideas or alternative ways u suggest? Thanks guys
(Config.INI is in the same folder as the exe)
July 9, 2006, 5:19 AM
UserLoser
Have to supply a path to config.ini.  Try ".\\config.ini" instead.  GetStuff? lol
July 9, 2006, 6:24 AM
Spilled[DW]
lol u like that? haha but yea UL that works thanks alot :-D
July 9, 2006, 6:41 AM
Kp
Setting aside that GetPrivateProfileString is deprecated, that's horrible code!  It's not const-correct, it uses magic numbers for size allocation, and it relies on the caller to free the memory.  Consider doing this:

[code]
void getStuff(vector<char>& result, const char *appname, const char *key)
{
result.resize(120);
GetPrivateProfileString("Settings", "User", "Default", &result[0], result.size(), ".\\config.ini");
}[/code]

It's still horribly ugly and could use a lot of improvement, but at least this one will not leak memory.  It's also a bit more resistant to memory corruption since I removed one occurence of the magic number (120).
July 9, 2006, 4:28 PM
Spilled[DW]
[quote author=Kp link=topic=15363.msg155533#msg155533 date=1152462503]
Setting aside that GetPrivateProfileString is deprecated, that's horrible code!  It's not const-correct, it uses magic numbers for size allocation, and it relies on the caller to free the memory.  Consider doing this:

[code]
void getStuff(vector<char>& result, const char *appname, const char *key)
{
result.resize(120);
GetPrivateProfileString("Settings", "User", "Default", &result[0], result.size(), ".\\config.ini");
}[/code]

It's still horribly ugly and could use a lot of improvement, but at least this one will not leak memory.  It's also a bit more resistant to memory corruption since I removed one occurence of the magic number (120).

[/quote]

Thanks kp
July 9, 2006, 9:00 PM

Search