July 12th, 2013, 07:56 PM
Problem accessing user directory in Win 7 Pro
I have been using the code below to extract the current user directory in order to read and write to certain files, presumeably independent of whether or not the user has administrative privileges. This has been necessitated in Win 7 because applications installed in the customary C:/Program Files(x86)/ are not allowed full access to files in the installed app root directory. This was working just fine until just this week when suddenly, for the first time, I found that the user directory retrieved was different when the app was run without administrative privileges in 'Start without debugging' configuration, both in Debug and Release configuration. Note that I have tested the identical app on Win2K and Vista and in neither case is it a problem on those OS machines. More mysterious is the fact that this ONLY happens on Win7 (VS2010 Ultimate) when run in the IDE. Run from the windows explorer double click, the app runs just fine in release and debug mode and monitoring the debug version using DbgView indicates that the expected 'Roaming' User directory is retrieved.
- without debugging (both Debug and Release configuration) the code below produces:
- with debugging (both Debug and Release configuration) the code below produces:
m_csUserDirectory =: C:\Users\MPLIAM\AppData\Local\PliaTech\UeberKrypt
Either this has been the situation from the outset and I just never noticed it (hard to imagine as I've been on this project for 6 months with 50+ builds), a recent Windows update has changed things, a bug, or I'm just plain stupid (not hard to imagine at all).
m_csUserDirectory =: C:\Users\MPLIAM\AppData\Roaming\PliaTech\UeberKrypt
From a practical standpoint, if this is the way things are to be, my installer will need to install necessary files in both user directories and the app will need to access both depending upon user privileges.
Any ideas greatly appreciated.
/// This routine retrieves the process's environment block
/// using GetEnvironmentStrings, parses that block pointed to
/// by the generic international text pointer LPTCH, and returns
/// the requested string lpszVarStr as a string.
/// Note that the block lpvEnv environment variable substrings
/// are separated by NULL byte, and the block is terminated by a NULL byte.
/// The header files <windows.h>, <tchar.h>, <stdio.h>, <string>, and
/// <iostream> need to be included.
/// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682009(v=vs.85).aspx //code fr example 3 here
CString CMyApp::GetEnvVar(wstring wsEnVar)
// Get a pointer to the environment block.
LPTCH lpvEnv = GetEnvironmentStrings();
LPTSTR lpszVarStr = (LPTSTR) lpvEnv;
// If the returned pointer is NULL, exit.
if (lpvEnv == NULL) return _T("GetEnvironmentStrings failed (%d)\n");
int n = -1;
wstring wstr = _T("");
CString cstr = _T("");
wstr = wstring(lpszVarStr);
n = wstr.find(wsEnVar);
if(n >= 0) break;
lpszVarStr += lstrlen(lpszVarStr) + 1;
n = wstr.find(_T("="));
wstr = wstr.substr(n+1, wstr.size()-n);
cstr = wstr.c_str();
}// GetEnvVar(string wsEnVar)
Last edited by Mike Pliam; July 12th, 2013 at 08:10 PM.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!