简体   繁体   中英

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.

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. 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. 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

Source: 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.

public static bool Is64BitOperatingSystem { get; }

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. IntPtr.Size is dependant on the process rather than machine.

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.

You are better of using the .NET 4.0 methods.

You could also just use this:

public static bool Is64BitProcess { get; }

This way you know which cscript.exe to actually launch. If your process is 64-bit you should only communicate with a 64-bit process. If its 32-bit then launch only the 32-bit process.

I do believe Windows 7 x64 keeps multiple versions for this exact perhaps in the System and sysWOW64 system directories.

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. Looking into it [ why am I forced to research this instead of you? ] Environment.SpecialFolder.SystemX86 will point to the correct location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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