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 );
}
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.
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...
SHGetFolderPath works like a charm. Thanks!