这是我用来从 32 位应用程序以 64 位启动 regedit 的代码:
[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 是我要添加到注册表的注册表文件。