Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Spilled on July 09, 2006, 12:19 AM

Title: GetPrivateProfileString() Help?
Post by: Spilled on July 09, 2006, 12:19 AM
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

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


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)
Title: Re: GetPrivateProfileString() Help?
Post by: UserLoser on July 09, 2006, 01:24 AM
Have to supply a path to config.ini.  Try ".\\config.ini" instead.  GetStuff? lol
Title: Re: GetPrivateProfileString() Help?
Post by: Spilled on July 09, 2006, 01:41 AM
lol u like that? haha but yea UL that works thanks alot :-D
Title: Re: GetPrivateProfileString() Help?
Post by: Kp on July 09, 2006, 11:28 AM
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:


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


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).
Title: Re: GetPrivateProfileString() Help?
Post by: Spilled on July 09, 2006, 04:00 PM
Quote from: Kp on July 09, 2006, 11:28 AM
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:


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


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


Thanks kp