Search code examples
c#exenarrator

Not able to launch narrator from Visual Studio


I am not able to launch narrator program from Visual Studio using C#. I have tried using complete path and other similar hacks but no outcome ? The code is :

System.Diagnostics.Process.Start(@"C:\windows\system32\narrator.exe");

Similar code is able to execute notepad.exe present in the same folder. Can anyone help me in this regard ? The execption which i got was ::

"An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The system cannot find the file specified "

however the file exists in the specified path. Then i copied the entire system32 folder to my desktop and gave the new location .then the code passes through without any exception but no narrator application is launched .


Solution

  • You can disable the filesystem redirection using some system calls. Note that even with the redirection fixed, you still cannot launch Narrator without elevated privileges.

    const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.
    
    var oldValue = IntPtr.Zero;
    Process p = null;
    
    try
    {
        if (SafeNativeMethods.Wow64DisableWow64FsRedirection(ref oldValue))
        {
            var pinfo = new ProcessStartInfo(@"C:\Windows\System32\Narrator.exe")
            {
                CreateNoWindow = true,
                UseShellExecute = true,
                Verb = "runas"
            };
    
            p = Process.Start(pinfo);
        }
    
        // Do stuff.
    
        p.Close();
    
    }
    catch (Win32Exception ex)
    {
        // User canceled the UAC dialog.
        if (ex.NativeErrorCode != ERROR_CANCELLED)
            throw;
    }
    finally
    {
        SafeNativeMethods.Wow64RevertWow64FsRedirection(oldValue);
    }
    
    
    [System.Security.SuppressUnmanagedCodeSecurity]
    internal static class SafeNativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    
    }