CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    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.

    For example:
    - without debugging (both Debug and Release configuration) the code below produces:
    m_csUserDirectory =: C:\Users\MPLIAM\AppData\Local\PliaTech\UeberKrypt
    - with debugging (both Debug and Release configuration) the code below produces:
    m_csUserDirectory =: C:\Users\MPLIAM\AppData\Roaming\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).

    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.

    Code:
    /// 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/ms683187(v=vs.85).aspx
    /// 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("");
        while (*lpszVarStr)
        {
    		wstr = wstring(lpszVarStr);
    		n = wstr.find(wsEnVar);
    		if(n >= 0) break;
            lpszVarStr += lstrlen(lpszVarStr) + 1;
        }
      
    	FreeEnvironmentStrings(lpvEnv);
    
    	n = wstr.find(_T("="));
    	wstr = wstr.substr(n+1, wstr.size()-n);
    
    	cstr = wstr.c_str();
    
    	return cstr;
    
    }// GetEnvVar(string wsEnVar)
    Last edited by Mike Pliam; July 12th, 2013 at 08:10 PM.
    mpliam

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem accessing user directory in Win 7 Pro

    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem accessing user directory in Win 7 Pro

    Managing Roaming User Data Deployment Guide

    Could you explain how the code is used in your app?
    Best regards,
    Igor

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    Re: Problem accessing user directory in Win 7 Pro

    The code is used in the Dialog based application App class to determine what the path of the User directory is on that user's machine. That path is set as a public static CString variable which makes it accessible to other classes in the application that need it.

    I suspect this is some setting in VS 2010, but it really makes little sense to me. What I have resorted to is to simply put the needed files in the 'Local' directory as well so that it all runs just fine in the IDE without debugging.
    mpliam

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem accessing user directory in Win 7 Pro

    Mike, the problem with using environment variables is that MS doesn't guarantee them to be consistant between OS releases, so using GetEnvironmentVariables really should only be a last resort for data that isn't exposed through api's.

    Remember that with environment variables you have user settings and system settings which get merged when a process is started. The funkyness that you are seeing may be a result of that. At any rate, you should use a better approach to determine where to locate these files such as what the previous two posters suggested.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem accessing user directory in Win 7 Pro

    Quote Originally Posted by Mike Pliam View Post
    The code is used in the Dialog based application App class to determine what the path of the User directory is on that user's machine. That path is set as a public static CString variable which makes it accessible to other classes in the application that need it.
    Mike, that iteration through all variables instead of getting a paricular one looks very suspicious to me. That's why I asked. And the asking was about exact logic of using.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured