下面的代码在我的 32 位机器上完美运行,但我现在已经在我的 64 位机器上测试了代码,我希望它能够像我调用 64 位版本的 cscript.exe 一样运行。
相反,代码会到达运行脚本的位置,然后等待 30 秒,然后退出脚本并继续程序的其余部分。然而,该脚本似乎没有运行(如果我手动运行它,它工作正常)。
using (var ServerProcess = new System.Diagnostics.Process())
{
var fileInformation = new FileInfo(VBScriptToRun);
string processFileName = IntPtr.Size == 8 ? @"c:\windows\sysWOW64\cscript.exe " : @"c:\windows\system32\cscript.exe ";
string processWorkDir = IntPtr.Size == 8 ? @"c:\windows\sysWOW64\" : @"c:\windows\system32\";
string processArguments = fileInformation.FullName;
ServerProcess.StartInfo.FileName = processFileName;
ServerProcess.StartInfo.WorkingDirectory = processWorkDir;
ServerProcess.StartInfo.Arguments = processArguments;
ServerProcess.StartInfo.CreateNoWindow = false;
ServerProcess.StartInfo.UseShellExecute = false;
ServerProcess.StartInfo.RedirectStandardOutput = true;
ServerProcess.StartInfo.LoadUserProfile = true;
EventLogger.Instance.WriteInformation("Total Integration Service Processing File: Starting to launch the specified program");
try
{
ServerProcess.Start();
ServerProcess.WaitForExit();
}catch(Exception e)
{
EventLogger.Instance.WriteInforamtion("Error running script: " + e)
}
Run Code Online (Sandbox Code Playgroud)
// Sample for the Environment.GetFolderPath method
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.System));
}
}
/*
This example produces the following results:
GetFolderPath: C:\WINNT\System32
*/
Run Code Online (Sandbox Code Playgroud)
您不应尝试访问sysWOW6432 位 Windows 程序集所在的文件夹。由于您指出这cscript.exe是一个 64 位进程,因此cscript.exeWindows 7 x64 安装的位置将是System目录
来源:http://msdn.microsoft.com/en-us/library/system.environment.specialfolder
您还应该使用以下内容来确定操作系统是否是 64 位。
public static bool Is64BitOperatingSystem { get; }
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx
我应该指出,您当前的方法失败了,因为它尝试[基于缺乏信息,这只是一个猜测]启动 32 位进程。 IntPtr.Size取决于过程而不是机器。
如果您想使用您的方法,则只能使用以下代码来执行此操作。
[DllImport("kernel32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
Run Code Online (Sandbox Code Playgroud)
你可以使用
System.Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITECTURE" )
Run Code Online (Sandbox Code Playgroud)
但如果进程是 32 位进程,它将返回 x86。
您最好使用 .NET 4.0 方法。
你也可以使用这个:
public static bool Is64BitProcess { get; }
Run Code Online (Sandbox Code Playgroud)
这样您就知道cscript.exe实际要启动哪个。如果您的进程是 64 位,您应该只与 64 位进程通信。如果是 32 位,则仅启动 32 位进程。
我确实相信 Windows 7 x64 可能在System和sysWOW64系统目录中保留了多个版本。
如果该进程实际上不是 64 位进程,那么它不会位于c:\windows\system3264 位安装上。调查一下[为什么我被迫研究这个而不是你?]Environment.SpecialFolder.SystemX86将指向正确的位置。
| 归档时间: |
|
| 查看次数: |
1792 次 |
| 最近记录: |