 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
victor Guest
|
Posted: Tue Jun 14, 2005 4:51 pm Post subject: Get %APPDATA% |
|
|
How ca I retrieve %APPDATA% variable?
|
|
| Back to top |
|
 |
Jonathan Benedicto Guest
|
Posted: Tue Jun 14, 2005 9:29 pm Post subject: Re: Get %APPDATA% |
|
|
"victor" <envysoft (AT) gmx (DOT) net> wrote
| Quote: | How ca I retrieve %APPDATA% variable?
|
I made this function for one of my apps. It allows you to get special
folder locations very easily.
This is untested because I had used a boost::scoped_array for the char
array.
AnsiString __fastcall GetSpecialPath(int Folder)
{
char *Path = new char[MAX_PATH];
AnsiString StrPath;
SHGetSpecialFolderPath(NULL, Path, Folder, 0);
StrPath = Path;
delete[] Path;
return StrPath;
}
For app data, call it like this :
GetSpecialPath(CSIDL_APPDATA)
HTH
Jonathan
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Jun 14, 2005 10:14 pm Post subject: Re: Get %APPDATA% |
|
|
"Jonathan Benedicto" <incorrect (AT) no (DOT) server> wrote
| Quote: | char *Path = new char[MAX_PATH];
|
You don't need to allocate the buffer on the heap. Use the stack instead.
This is also partly in case the assignment to the AnsiString throws an
exception. You are not currently protecting the buffer from exceptions, so
if one ever occurs then your buffer will be leaked.
| Quote: | SHGetSpecialFolderPath(NULL, Path, Folder, 0);
|
You are not checking that SHGetSpecialFolderPath() is actually succeeding
before returning the buffer value.
Try this instead:
AnsiString __fastcall GetSpecialPath(int Folder)
{
char Path[MAX_PATH+1] = {0};
if( SHGetSpecialFolderPath(NULL, Path, Folder, 0) )
return AnsiString(Path);
return "";
}
Gambit
|
|
| Back to top |
|
 |
Jonathan Benedicto Guest
|
Posted: Tue Jun 14, 2005 11:07 pm Post subject: Re: Get %APPDATA% |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: | You don't need to allocate the buffer on the heap. Use the stack
instead.
This is also partly in case the assignment to the AnsiString throws
an
exception. You are not currently protecting the buffer from
exceptions, so
if one ever occurs then your buffer will be leaked.
|
[snip]
| Quote: | You are not checking that SHGetSpecialFolderPath() is actually
succeeding
before returning the buffer value.
Try this instead:
|
Thank you very much. I'll use this instead.
Jonathan
|
|
| Back to top |
|
 |
victor Guest
|
Posted: Wed Jun 15, 2005 9:30 am Post subject: Re: Get %APPDATA% |
|
|
Thanks to all!!!
Jonathan Benedicto ha scritto:
| Quote: | "Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:42af583b$1 (AT) newsgroups (DOT) borland.com...
You don't need to allocate the buffer on the heap. Use the stack
instead.
This is also partly in case the assignment to the AnsiString throws
an
exception. You are not currently protecting the buffer from
exceptions, so
if one ever occurs then your buffer will be leaked.
[snip]
You are not checking that SHGetSpecialFolderPath() is actually
succeeding
before returning the buffer value.
Try this instead:
Thank you very much. I'll use this instead.
Jonathan
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|