0

I'm trying to start .lnk shortcuts from my application. However, I'm struggling with the infamous automatic filesystem redirection for 32/64-bit processes.

I'm searching for a way to simply start an application from the shortcut and I don't care, what happens to that process later. Effectively I'd like to start the shortcut the same way as if user doubleclicked it in the Explorer.

Currently I'm using the following method, but it still doesn't work (ie. I'm unable to start Word this way):


[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

private static void Start(string shortcut)
{
    IntPtr temp = IntPtr.Zero;

    try
    {
        Wow64DisableWow64FsRedirection(ref temp);
        var error = Marshal.GetLastWin32Error();

        ProcessStartInfo info = new ProcessStartInfo(shortcut);
        info.UseShellExecute = true;

        Process.Start(info);
    }
    finally
    {
        Wow64RevertWow64FsRedirection(temp);
    }            
}

How can I reliably start an application, knowing its direct location on the drive in C#?


Edit:

I used SysInternals' ProcMon to check, what is done behind scenes. It looks like the proper path is searched, but then system for some reason still falls back to the forced 32-bit one.

ProcMon result

Spook
  • 25,318
  • 18
  • 90
  • 167
  • Why are you wanting to use `.lnk` files via `ShellInvoke`? Why not do it normally? – Dai Mar 05 '21 at 11:05
  • `UseShellInvoke` _should_ mean you wont be affected by SysWow64 redirection because it's `Explorer.exe` that handles it. – Dai Mar 05 '21 at 11:06
  • Does this answer your question? https://stackoverflow.com/questions/6141821/run-application-via-shortcut-using-process-start-c-sharp – Nikita Chayka Mar 05 '21 at 11:06
  • You **need** to check the return-value from `Wow64DisableWow64FsRedirection` btw. – Dai Mar 05 '21 at 11:07
  • `the same way as if user doubleclicked it in the Explorer` - that would be https://devblogs.microsoft.com/oldnewthing/20131118-00/?p=2643. – GSerg Mar 05 '21 at 11:22
  • Wow64Disable... is too dangerous in a .NET process, it likes loading DLLs on-demand. And doesn't work here since it only disables it for your process, not for Explorer. – Hans Passant Mar 05 '21 at 15:41

0 Answers0