ExpandEnvironmentStrings fails on %APPDATA% when using 'Run As' command

950 views
Skip to first unread message

spoon...@gmail.com

unread,
Mar 2, 2009, 11:15:16 AM3/2/09
to
Using ExpandEnvironmentStrings to expand the %APPDATA% variable works
fine when the program runs as the logged on user. However, it fails
to find this variable when you use the 'Run as' command to run the
program as a different user. Example: when I run the included sample
code, I'm logged on as 'bob', the code works as expected. If I
navigate to my EXE in Explorer, right-click on the EXE and choose 'Run
as', choose the credentials of a local administrator, the program
fails to expand the %APPDATA% variable. I've done a ton of searching
these Google groups and only found one other person who had a similar
problem. Their solution was to remove %APPDATA% and replace it with
the hard-coded folder name. This is not an option for me. I tried
ExpandEnvironmentStringsForUser and that too didn't work. So, I
simplified it and just called GetEnvironmentVariable. This function
was also unable to find the APPDATA variable. However,
GetEnvironmentVariable was able to expand a bunch of other variables
like: ALLUSERSPROFILE and USERPROFILE. So, I'm confused why it can
expand some but not all variables.

Sample code:
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>

#define APPDATA "%APPDATA%"

int MyGetEnvVar( char * pVar )
{
char envvar[200];
DWORD RC;

memset( envvar, 0, sizeof(envvar) );
RC = GetEnvironmentVariable( pVar, envvar, sizeof(envvar) );
printf( "RC=%d, Variable <%s> = <%s>\n", RC, pVar, RC!=0 ?
envvar : "not found" );

return( 0 );
}

int main (int argc, char *argv[])
{
char szExpandedPath[MAX_PATH];
char username[40];
DWORD dwSize = sizeof(username);
DWORD RC;
RC = GetUserName( username, &dwSize );
if ( !RC )
{
printf( "Failed GetUserName, GetLastError=%d\n", GetLastError
() );
}
else
{
printf( "RC=%d, GetUserName=%s\n", RC, username );
}

RC = ExpandEnvironmentStrings( APPDATA, szExpandedPath, sizeof
(szExpandedPath) );
if ( !RC )
{
printf( "Failed to expand environment strings. GetLastError=%d
\n", GetLastError() );
}
else
{
printf( "RC=%d, Expanded string for <%s> = <%s>\n", RC,
APPDATA, szExpandedPath );
}

MyGetEnvVar( "APPDATA" );
MyGetEnvVar( "HOMEPATH" );
MyGetEnvVar( "ALLUSERSPROFILE" );
MyGetEnvVar( "USERPROFILE" );
MyGetEnvVar( "TEMP" );
MyGetEnvVar( "USERNAME" );

printf("Press Enter to exit.\n" );
int foo = getchar();

return( 0 );
}

Tim Roberts

unread,
Mar 3, 2009, 1:02:16 AM3/3/09
to
spoon...@gmail.com wrote:
>
>Using ExpandEnvironmentStrings to expand the %APPDATA% variable works
>fine when the program runs as the logged on user. However, it fails
>to find this variable when you use the 'Run as' command to run the
>program as a different user. Example: when I run the included sample
>code, I'm logged on as 'bob', the code works as expected. If I
>navigate to my EXE in Explorer, right-click on the EXE and choose 'Run
>as', choose the credentials of a local administrator, the program
>fails to expand the %APPDATA% variable. I've done a ton of searching
>these Google groups and only found one other person who had a similar
>problem. Their solution was to remove %APPDATA% and replace it with
>the hard-coded folder name. This is not an option for me. I tried
>ExpandEnvironmentStringsForUser and that too didn't work. So, I
>simplified it and just called GetEnvironmentVariable. This function
>was also unable to find the APPDATA variable. However,
>GetEnvironmentVariable was able to expand a bunch of other variables
>like: ALLUSERSPROFILE and USERPROFILE. So, I'm confused why it can
>expand some but not all variables.

Some of these variables are created during the profile logon process, and
RunAs doesn't actually run the entire logon process.

You can pull these strings from the registry.
HKEY_CURRENT_USER\Software\Microsoft\
Windows\CurrentVersion\Explorer\Shell Folders
The "AppData" value has the string you seek.

That assumes, of course, that HKEY_CURRENT_USER gets remapped in the "Run
As" process. If not, and you have the SID, you can build the HKEY_USERS
subkey from that. So, for example,
HKEY_USERS\S-1-5-18\Software\Microsoft\
Windows\CurrentVersion\Explorer\Shell Folders
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Serge Wautier

unread,
Mar 3, 2009, 3:13:48 AM3/3/09
to
What does SHGetFolderPath() say?

HTH,

Serge.
http://www.apptranslator.com - Localization tool for your Win32/MFC
applications


<spoon...@gmail.com> wrote in message
news:7646af8d-7302-4b58...@h20g2000yqn.googlegroups.com...

Greg

unread,
Mar 4, 2009, 2:41:03 PM3/4/09
to
On Mar 3, 3:13 am, "Serge Wautier" <se...@wautier.nospam.net> wrote:
> What does SHGetFolderPath() say?
>
> HTH,
>

SHGetFolderPath works like a charm. Thanks!

Reply all
Reply to author
Forward
0 new messages