views:

667

answers:

2

Is there any function that I can call to in a 32-bit app that would convert the paths that it thinks it's using to and from the paths that it's actually using? (For instance, call it to convert the path for a folder in Program Files to a path in Program Files (x86) or vice versa when running on a 64-bit system.) I need to do this so that, when the user is presented with a directory in the GUI (as when browsing for files or directories), he sees the real directory, rather than what the app itself thinks it sees.

We need to support both 32- and 64-bit Windows, so this should work in both environments.

Edit The need to deal with WOW64 lies in the fact that my app is the bootstrap application of an installer. By default, the product is installed under Program Files on a 32-bit system, while on a 64-bit system, it's installed under Program Files (x86). The dialogue that lets the user choose where to install the product (including on a different hard drive altogether if that's what he wants or needs); and it must work properly in both environments. Currently, it's always displaying (and browsing in) Program Files, even on 64-bit systems.

+1  A: 

Is browsing through Program Files and Windows\System32 a use-case for your app? I encourage you to abandon solving this problem, as it gets really ugly. Either stay all in the 32-bit world, or recompile for x64. Living in the "in-between" world of WOW64 will make your code much more complex.

If you really want to do this, http://msdn.microsoft.com/en-us/library/aa365743(VS.85).aspx will do the trick.

Paul Betts
I know about that function. It doesn't give me the path conversion facility that I need.
RobH
A: 

Here I have a working sample of disablig wow64 redirection:

BOOL CCopyUtils::CopyServiceFiles(CAtlString szSourceDir, CAtlString szTargetDir)
{
 BOOL bResult = TRUE;
 TCHAR szSource[MAX_PATH];
 TCHAR szDest[MAX_PATH];
 TCHAR szSourceFile[MAX_PATH];
 TCHAR szDestFile[MAX_PATH];

 HINSTANCE hDll;
 PVOID OldValue;
 Wow64DisableWow64FsRedirection pfWow64DisableWow64FsRedirection=NULL;
 Wow64RevertWow64FsRedirection pfWow64RevertWow64FsRedirection=NULL;

 hDll=LoadLibrary("Kernel32.dll");
 ATLASSERT(hDll!=NULL);

 pfWow64DisableWow64FsRedirection=(Wow64DisableWow64FsRedirection)GetProcAddress(
  hDll, "Wow64DisableWow64FsRedirection");
 // http://msdn.microsoft.com/en-us/library/aa365745(VS.85).aspx
 pfWow64RevertWow64FsRedirection=(Wow64RevertWow64FsRedirection)GetProcAddress(
  hDll, "Wow64RevertWow64FsRedirection");

 if(pfWow64DisableWow64FsRedirection==NULL)
  ATLTRACE(_T("Function 'Wow64DisableWow64FsRedirection' not loaded."));
 else if((pfWow64DisableWow64FsRedirection)(&OldValue)==TRUE)
  ATLTRACE(_T("Wow64 filesystem redirecton disabled."));
 else 
  ATLTRACE(_T("Wow64 filesystem redirecton not disabled."));

 if (_taccess(szTargetDir, 0) != 0)
  _tmkdir(szTargetDir);

 DeleteOldFiles(szTargetDir);

 int  i = 0;
 _tcscpy(szSource, szSourceDir);
 if (szSource[_tcslen (szSource) -1] != '\\')
  _tcscat (szSource,"\\");    

 _tcscpy(szDest, szTargetDir);
 if (szDest[_tcslen (szDest) -1] != '\\')
  _tcscat (szDest,"\\");    

 while (m_ServiceFiles[i] != NULL)
 {
  _tcscpy(szSourceFile, szSource);
  _tcscat(szSourceFile, m_ServiceFiles[i]);

  _tcscpy(szDestFile, szDest);
  _tcscat(szDestFile, m_ServiceFiles[i]);

  if (!CopyFile(szSourceFile, szDestFile, FALSE))  
  {
   bResult = FALSE;
   ATLTRACE(_T("Error coping %s to %s\n"), szSourceFile, szDestFile);
   ATLASSERT(FALSE);     
   break;
  }
  i++;
 }

 if(pfWow64RevertWow64FsRedirection==NULL)
  ATLTRACE(_T("Function 'pfWow64RevertWow64FsRedirection' not loaded."));
 else if((pfWow64RevertWow64FsRedirection)(OldValue)==TRUE)
  ATLTRACE(_T("Wow64 filesystem redirecton restored."));
 else 
  ATLTRACE(_T("Wow64 filesystem redirecton not restored."));
 FreeLibrary(hDll);

 return bResult;
}

Best regards

xgoan
The question was not 'how do I disable WOW64 redirection?' I already know how to do that. What I need is a way to determine, given a path, what the redirected path is so that I can display that path in the GUI. Conversely, given a redirected path, I need the reverse conversion. (Only if the app is running on a 64-bit system. If the app is running on a 32-bit system, the conversions should do nothing to the path.)
RobH