BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Get %APPDATA%

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
victor
Guest





PostPosted: Tue Jun 14, 2005 4:51 pm    Post subject: Get %APPDATA% Reply with quote



How ca I retrieve %APPDATA% variable?
Back to top
Jonathan Benedicto
Guest





PostPosted: Tue Jun 14, 2005 9:29 pm    Post subject: Re: Get %APPDATA% Reply with quote



"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





PostPosted: Tue Jun 14, 2005 10:14 pm    Post subject: Re: Get %APPDATA% Reply with quote




"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





PostPosted: Tue Jun 14, 2005 11:07 pm    Post subject: Re: Get %APPDATA% Reply with quote

"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





PostPosted: Wed Jun 15, 2005 9:30 am    Post subject: Re: Get %APPDATA% Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.