我正在使用系统重定向器从32位C#应用程序调用64位Powershell,效果很好:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe";
proc.StartInfo.UseShellExecute = false;
proc.Start();
一旦我添加了一个明确的用户名/密码(甚至成功运行了上面的用户名/密码),我就会收到file not found
错误,开始该过程:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.WorkingDirectory = "somedir";
proc.StartInfo.UserName = "username";
proc.StartInfo.PasswordInClearText = "somepassword";
proc.StartInfo.Domain = "somedomain";
proc.Start();
在这种情况下,sysnative
重定向器是否存在某些限制?
答案 0 :(得分:0)
设置UseShellExecute = false;
时,Process.Start()
将调用kernel32!CreateProcess
而不是shell32!ShellExecute
,并且只有后者似乎可以正确解析sysnative
引用。
要解决此问题,请disable SysWOW64 redirection暂时用于当前线程:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64DisableWow64FsRedirection(ref IntPtr oldValue);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64RevertWow64FsRedirection(IntPtr oldValue);
// Disable redirector
IntPtr oldValue = IntPtr.Zero;
Wow64DisableWow64FsRedirection(ref oldValue);
try
{
// Start your process while fs redirector is disabled
process.Start()
}
catch (Exception ex)
{
// Handle ex if necessary, error logging, otherwise re-throw
throw;
}
finally
{
// Make sure you revert redirection settings again
Wow64RevertWow64FsRedirection(oldValue);
}