Board index » cppbuilder » Error while using getenv()

Error while using getenv()


2003-11-29 06:30:21 PM
cppbuilder57
My program creates a folder in the folder
Documents and Settings\user01\Application Data
I'm getting the name of the user by calling the function getenv().
AnsiString userPath;
userPath = AnsiString(getenv("USERPROFILE"));
CreateDir(userPath + "\\Application Data\\Folder1");
If the value of the environment variable USERPROFILE contains a character
with a
ascii value between 128 and 255, getenv() replaces this character with
another
character and CreateDir() function doesn't work because there is no such
folder.
Maybe, is there a better method than using getenv()?
Thanks...
 
 

Re:Error while using getenv()

"Umut Ozden" < XXXX@XXXXX.COM >wrote in message
Quote
My program creates a folder in the folder
Documents and Settings\user01\Application Data
I'm getting the name of the user by calling the function getenv().
You should be using GetUserName() instead of getenv().
Or better yet, you should really be using SHGetSpecialFolderLocation() with
the CSIDL_APPDATA flag instead, it already determines the appropriate folder
name automatically for you. SHGetSpecialFolderLocation() is the official
function to use for determining various system-defined paths:
SHGetSpecialFolderLocation Function
msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shgetspecialfolderlocation.asp
Quote
AnsiString userPath;
userPath = AnsiString(getenv("USERPROFILE"));
CreateDir(userPath + "\\Application Data\\Folder1");
That should be more like this instead:
LPITEMIDLIST pidl = NULL;
char path[MAX_PATH+1] = {0};
if( SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl)) )
{
if( SHGetPathFromIDList(pidl, path) );
CreateDir(IncludeTrailingBackslash(path) + "Folder1");
CoTaskMemFree(pidl);
}
Gambit
 

Re:Error while using getenv()

Quote
That should be more like this instead:

LPITEMIDLIST pidl = NULL;
char path[MAX_PATH+1] = {0};

if( SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA,
&pidl)) )
{
if( SHGetPathFromIDList(pidl, path) );
CreateDir(IncludeTrailingBackslash(path) + "Folder1");

CoTaskMemFree(pidl);
}
Which header file should I use to include lines above? The compiler gives
error messages. "Undefined symbol LPITEMIDLIST...". Furthermore, the help
for SHGetSpecialFolderLocation() in Win32 Programmer's Reference does not
contain CSIDL_APPDATA as second parameter. Anything wrong?
 

{smallsort}

Re:Error while using getenv()

"Umut Ozden" < XXXX@XXXXX.COM >wrote in message
Quote
Which header file should I use to include lines above?
SHGetSpecialFolderLocation() and SHGetPathFromIDList() are both declared in
shlobj.h. If you are using BCB5 or BCB6, you will also need to add
NO_WIN32_LEAN_AND_MEAN to your project's Conditionals list in order to
compile that header correctly.
Quote
Furthermore, the help for SHGetSpecialFolderLocation() in
Win32 Programmer's Reference does not contain
CSIDL_APPDATA as second parameter. Anything wrong?
The Win32 help files that come with BCB are outdated. If you follow the URL
I gave you, you will see the latest documentation at MSDN (which is always
up to date), which lists all of the supported values including
CSIDL_APPDATA.
Gambit