Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#
Tip/Trick

Using 64-bit Shortcuts from a 32-bit Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
9 Jun 2015Apache2 min read 10.5K   251   7  
A C# library that resolves 64-bit shortcuts from a 32-bit application

Image 1

Introduction

Shell links (shortcuts) are files with a .lnk extension that store information to allow Windows Explorer to access files, folders and other objects. Applications can manipulate shortcuts using the IShellLink COM interface.

One of the problems with 32-bit applications using shortcuts on 64-bit Windows is that the target location of shortcuts which point to items in the 64-bit Program Files and System32 directories may be redirected to the 32-bit version of those directories.

This library fixes the redirection of 64-bit shortcuts for 32-bit applications. It was originally developed to allow my 8bf filter host to use shortcuts to plugins which install both the 32-bit and 64-bit versions in a subdirectory of the 64-bit Program Files. Portions of this tip were adapted from my previous article on the TechNet Wiki.

Background

The WoW64 subsystem on 64-bit Windows emulates the environment of a 32-bit operating system when executing 32-bit applications.

This includes redirecting many 64-bit environment variables (such as %ProgramFiles%) to their 32-bit equivalents. For example, a shortcut to the 64-bit version of Internet Explorer would launch the 32-bit version when run from a 32-bit application.

The Wow64DisableWow64FsRedirection function can be used by 32-bit applications to temporarily disable file system redirection for the System32 directory, it does not affect redirection of Program Files.

Using the Code

The following example resolves the target of the Internet Explorer shortcut in the start menu, when run on a 64-bit OS, the target will be resolved to the 64-bit version of Internet Explorer for both 32-bit and 64-bit applications.

C#
private string GetInternetExplorerPath()
{
    string startMenu = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
    string link = Path.Combine(startMenu, "Programs\\Internet Explorer.lnk");
 
    return ShortcutResolver.Resolve(link);
}

The Resolve method loads the shortcut and fixes the redirection of shortcut targets in the %CommonProgramFiles%, %ProgramFiles% and System32 directories when run from a 32-bit application, if the 64-bit target does not exist, it returns the 32-bit path.

Points of Interest

On Windows 7 and later, the 64-bit Program Files location is exposed to 32-bit applications by the %ProgramW6432% environment variable.

On Windows Vista and earlier, the 64-bit Program Files folder is resolved by removing the (x86) identifier from the 32-bit Program Files folder path.

References

History

  • 06-08-2015: Original tip

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --