CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  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

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