Skip to main content
 首页 » 编程设计

C# 适用于 32 位但不适用于 64 位

2024年02月20日11mayingbao

下面的代码在我的 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) 
            } 

请您参考如下方法:

// 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 
*/ 

您不应尝试访问 32 位 Windows 程序集所在的 sysWOW64 文件夹。由于您指出 cscript.exe 是一个 64 位进程,因此 cscript.exe 在 Windows 7 x64 安装上的位置将是 System 目录

来源:http://msdn.microsoft.com/en-us/library/system.environment.specialfolder

您还应该使用以下内容来确定操作系统是否为 64 位。

public static bool Is64BitOperatingSystem { get; } 

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); 

你可以使用

System.Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITECTURE" ) 

除了如果进程是 32 位进程它将返回 x86。

最好使用 .NET 4.0 方法。

你也可以只使用这个:

public static bool Is64BitProcess { get; } 

这样您就知道要实际启动哪个 cscript.exe。如果您的进程是 64 位的,您应该只与 64 位进程通信。如果它是 32 位的,则仅启动 32 位进程。

我确实相信 Windows 7 x64 可能在 System 和 sysWOW64 系统目录中为此保留了多个版本。

如果进程实际上不是 64 位进程,那么在 64 位安装中它不会位于 c:\windows\system32。调查一下【为什么非要我研究这个而不是你? ] Environment.SpecialFolder.SystemX86 将指向正确的位置。