简体   繁体   English

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

[英]C# works on 32bit but not 64bit

The code below works perfectly on my 32bit machine but I have tested the code now on my 64bit machine, I expected it to work as I was calling the 64bit version of the cscript.exe. 下面的代码完全适用于我的32位机器,但我现在已经在我的64位机器上测试了代码,我希望它可以正常工作,因为我正在调用64位版本的cscript.exe。

Instead the code gets to the point where it runs the script and then waits for exactly 30seconds then exits the script and continues the rest of the program. 相反,代码到达运行脚本的程度,然后等待30秒,然后退出脚本并继续执行程序的其余部分。 The script however appears not to run, (it works fine if I run it manually). 然而,该脚本似乎不会运行(如果我手动运行它可以正常工作)。

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

You should not be trying to access the sysWOW64 folder that is the location of 32-bit windows assemblies. 您不应该尝试访问作为32位Windows程序集位置的sysWOW64文件夹。 Since you indicated that cscript.exe is a 64-bit process the location of cscript.exe on Windows 7 x64 installation would be the System directory 由于您指示cscript.exe是64位进程,因此Windows 7 x64安装上cscript.exe的位置将是System目录

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

You should also use the following to determine if the operating system is 64-bit or not. 您还应该使用以下内容来确定操作系统是否为64位。

public static bool Is64BitOperatingSystem { get; }

http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx

I should point out that your current method failing because its trying to [based on the lack information this is only a guess] start the 32-bit process. 我应该指出你当前的方法失败,因为它试图[基于缺乏信息这只是一个猜测]启动32位进程。 IntPtr.Size is dependant on the process rather than machine. IntPtr.Size依赖于进程而不是机器。

If you want to use your method your limited to using the following code to do so. 如果您想使用您的方法,您只能使用以下代码来执行此操作。

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

You could use 你可以用

System.Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITECTURE" )

Except that it will return x86 if the process is a 32-bit process. 如果进程是32位进程,它将返回x86。

You are better of using the .NET 4.0 methods. 您最好使用.NET 4.0方法。

You could also just use this: 你也可以使用这个:

public static bool Is64BitProcess { get; }

This way you know which cscript.exe to actually launch. 这样你就知道实际启动了哪个cscript.exe If your process is 64-bit you should only communicate with a 64-bit process. 如果您的进程是64位,则只应与64位进程通信。 If its 32-bit then launch only the 32-bit process. 如果它的32位然后只启动32位进程。

I do believe Windows 7 x64 keeps multiple versions for this exact perhaps in the System and sysWOW64 system directories. 我相信Windows 7 x64可能会在SystemsysWOW64系统目录中保留多个版本。

If the process is not actually a 64-bit process then it won't be located at c:\\windows\\system32 on a 64-bit installation. 如果该进程实际上不是64位进程,那么它将不会位于64位安装的c:\\windows\\system32上。 Looking into it [ why am I forced to research this instead of you? 调查它[为什么我被迫研究这个而不是你? ] Environment.SpecialFolder.SystemX86 will point to the correct location. ] Environment.SpecialFolder.SystemX86将指向正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM