[英]C# works on 32bit but not 64bit
下面的代碼完全適用於我的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位進程,因此Windows 7 x64安裝上cscript.exe
的位置將是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
將指向正確的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.