2012-09-02 175 views
1

我想使用ShellExecute從我的32位應用程序打開64位註冊表編輯器。從32位應用程序打開64位註冊表

我注意到在Process Explorer中,如果我通常打開Regedit,它說圖像是64位的,但是如果我從ShellExecute的32位應用程序打開C:\Windows\Regedit.exe,Process Explorer說圖像是32位。

(和它在Windows目錄中打開註冊表編輯,而不是在SysWOW64中)

我發現,如果我調用ShellExecute的前使用Wow64DisableWow64FsRedirection功能,它會打開它的64位圖像。但我的應用程序不能在32位XP上運行。

不管我用哪種方式打開註冊表,它們都駐留在C:\Windows中,而且它們都是相同的可執行文件。同一個可執行文件如何具有2種不同的圖像類型?如何在不使用Wow64DisableWow64FsRedirection的情況下打開64位?

+1

似乎有一些神奇的事情與regedit.exe進行。這可能是一個無證的特例。正如Scott指出的那樣,最好的解決方法可能是啓動regedt32.exe。 (但請注意,如果任一版本的regedit實例已在運行,則不會啓動新實例。) –

回答

3

您需要檢測您是否在使用Is64BitProcess的64位進程中,如果是,請訪問%windir%\Sysnative,因爲它指向32位應用程序需要訪問64位System32文件夾的「Real」System32文件夾。

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32"); 
if(Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) 
{ 
    // For 32-bit processes on 64-bit systems, %windir%\system32 folder 
    // can only be accessed by specifying %windir%\sysnative folder. 
    system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative"); 

} 
+0

是的,但Regedit.exe位於'C:\ Windows',我試圖打開' C:\ Windows \ SysNative \ .. \ Regedit.exe「,但似乎沒有工作,圖像仍然是32位。 – Josh

+2

您是否嘗試過'C:\ Windows \ sysnative \ regedt32.exe' –

0

這是我使用來自32位應用程序在64位,以啓動註冊表編輯器的代碼:

[DllImport("kernel32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); 

    internal int ExecuteCommand64(string Command, string Parameters) 
    { 

     IntPtr ptr = new IntPtr(); 
     bool isWow64FsRedirectionDisabled = Wow64DisableWow64FsRedirection(ref ptr); 
     if (isWow64FsRedirectionDisabled) 
     { 

      //Set up a ProcessStartInfo using your path to the executable (Command) and the command line arguments (Parameters). 
      ProcessStartInfo ProcessInfo = new ProcessStartInfo(Command, Parameters); 
      ProcessInfo.CreateNoWindow = true; 
      ProcessInfo.UseShellExecute = false; 
      ProcessInfo.RedirectStandardOutput = true; 

      //Invoke the process. 
      Process Process = Process.Start(ProcessInfo); 
      Process.WaitForExit(); 

      //Finish. 
      // this.Context.LogMessage(Process.StandardOutput.ReadToEnd()); 
      int ExitCode = Process.ExitCode; 
      Process.Close(); 
      bool isWow64FsRedirectionOK = Wow64RevertWow64FsRedirection(ptr); 
      if (!isWow64FsRedirectionOK) 
      { 
       throw new Exception("Le retour en 32 bits a échoué."); 
      } 
      return ExitCode; 
     } 

     else 
     { 
      throw new Exception("Impossible de passer en 64 bits"); 
     } 

    } 

,我和下面的行調用它:

ExecuteCommand64(@"c:\windows\regedit", string.Format("\"{0}\"", regFileName)); 

凡regFilename是我想要添加到註冊表中的註冊表文件。

相關問題