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

标签 c# 64-bit 32-bit

下面的代码在我的 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 可能在 SystemsysWOW64 系统目录中为此保留了多个版本。

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

关于C# 适用于 32 位但不适用于 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229407/

相关文章:

c# - 将两个 PDF 页面合并在一起

windows - 64 位驱动程序无法在 64 位 Windows 7/8 中运行

windows-8 - IntPtr.ToInt32() 和 x64 系统

C#:SQLite 数据库始终处于锁定状态

c# - 如何将此 Linq 查询语法转换为方法语法?

C# 相当于 VB.Net AddressOf Operator

windows - 启动 Cassandra 服务器错误

c# - C# 中有长版本的 IntPtr 吗?

linux - 使用 NASM Assembly 在堆栈中打印数字

linux - 强制 gcc 在 64 位平台上编译 32 位程序