如何根据外部资源有条件地 运行 我的程序以 64 位或 32 位模式运行?
How can I conditionally run my program in 64 bit or 32 bit mode based on external resources?
论坛上有一些关于 Microsoft.ACE.OLEDB.12.0
提供商未在本地计算机上注册的问题(和不需要的答案),例如 this one。问题的要点,我的理解是,应用程序将在与应用程序 运行 所在的平台相同的平台上寻找提供者。因此,如果您的计算机是 64 位的,而提供程序是 32 位的,那么如果您不将应用程序编译为 运行 32 位模式,则会出现不匹配。
大多数答案通过为当前平台安装适当的数据组件来有效地解决这个问题。其他建议是为数据组件可用的任何平台编译。
我正在使用 PC 运行ning Windows 7、8 和 10,全部 64 位开发应用程序,具体取决于我所在的位置,但有些使用旧版本的 Office,而其他使用更新版本.这导致我不得不根据我当前使用的 PC 更改我编译的平台。虽然这对我来说不是问题,但就我个人而言,我预见到这会给无法 运行 该程序的最终用户带来麻烦。
Trying to avoid asking users to install other components on their computers; is there a way I can tell the program to check the platform availability of the database provider and then run in that mode? Might it be possible to create 32 bit and 64 bit extensions of the database module and load the appropriate one regardless of the mode the main program is running in?
编辑:
我只是尝试在不同的平台上编译我的数据库扩展 无论哪个与应用程序平台不同的平台在加载时都会导致异常,说我正试图从不同的平台加载程序集。所以我想我的选项 2 不走运...
您可以使用 CorFlags 实用程序修改目标机器上的可执行文件,之后您将检测到它需要哪种模式 运行。
首先确保您的主 exe 是在未设置 Prefer 32 bit
标志的任何 cpu 下编译的。现在,当应用程序启动时,您需要检查我们是否处于 64 位进程中,并检查您的依赖项(我不会介绍 - 我不使用 OLEDB)。如果你发现不匹配(假设你在 64 位进程中 运行ning 但你的依赖项是 32 位) - 你需要 运行 外部进程来修改你的主要可执行文件然后重新启动它。最简单的方法是通过简单的 cmd 脚本,就像这样(在这个例子中我的主 exe 被称为 ConsoleApplication3.exe
):
:start
start /wait "" "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\CorFlags.exe" ConsoleApplication3.exe /32BIT+
if errorlevel 1 (
goto start
)
start "" ConsoleApplication3.exe
请注意,这只是示例,如果出现问题,将陷入死循环,请根据您的要求进行调整。这个脚本所做的只是在 32 位模式下使用 CorFlags 工具将您的 exe 更新为 运行,然后启动您的主 exe。
启动应用程序后,您可以立即进行以下检查:
static void Main() {
if (Environment.Is64BitProcess) {
// here you also check if you have dependency mismatch, and then:
Console.WriteLine("Running in 64-bit mode. Press any key to fix");
Console.ReadKey();
// run script (here we assume script is in the same directory as main executable, and is called fix-mode.cmd
var process = new Process() {
StartInfo = new ProcessStartInfo("cmd.exe", "/c call fix-mode.cmd")
};
process.Start();
// here you should exit your application immediatly, so that CorFlags can update it (since it cannot do that while it is running)
}
else {
// after restart by our script - we will get here
Console.WriteLine("Running in 32bit mode");
}
}
请注意,此工具 (CorFlags) 仅适用于 Visual Studio,因此您可能希望将其与您的应用程序打包在一起(可能在远程计算机上不可用)。
论坛上有一些关于 Microsoft.ACE.OLEDB.12.0
提供商未在本地计算机上注册的问题(和不需要的答案),例如 this one。问题的要点,我的理解是,应用程序将在与应用程序 运行 所在的平台相同的平台上寻找提供者。因此,如果您的计算机是 64 位的,而提供程序是 32 位的,那么如果您不将应用程序编译为 运行 32 位模式,则会出现不匹配。
大多数答案通过为当前平台安装适当的数据组件来有效地解决这个问题。其他建议是为数据组件可用的任何平台编译。
我正在使用 PC 运行ning Windows 7、8 和 10,全部 64 位开发应用程序,具体取决于我所在的位置,但有些使用旧版本的 Office,而其他使用更新版本.这导致我不得不根据我当前使用的 PC 更改我编译的平台。虽然这对我来说不是问题,但就我个人而言,我预见到这会给无法 运行 该程序的最终用户带来麻烦。
Trying to avoid asking users to install other components on their computers; is there a way I can tell the program to check the platform availability of the database provider and then run in that mode? Might it be possible to create 32 bit and 64 bit extensions of the database module and load the appropriate one regardless of the mode the main program is running in?
编辑:
我只是尝试在不同的平台上编译我的数据库扩展 无论哪个与应用程序平台不同的平台在加载时都会导致异常,说我正试图从不同的平台加载程序集。所以我想我的选项 2 不走运...
您可以使用 CorFlags 实用程序修改目标机器上的可执行文件,之后您将检测到它需要哪种模式 运行。
首先确保您的主 exe 是在未设置 Prefer 32 bit
标志的任何 cpu 下编译的。现在,当应用程序启动时,您需要检查我们是否处于 64 位进程中,并检查您的依赖项(我不会介绍 - 我不使用 OLEDB)。如果你发现不匹配(假设你在 64 位进程中 运行ning 但你的依赖项是 32 位) - 你需要 运行 外部进程来修改你的主要可执行文件然后重新启动它。最简单的方法是通过简单的 cmd 脚本,就像这样(在这个例子中我的主 exe 被称为 ConsoleApplication3.exe
):
:start
start /wait "" "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\CorFlags.exe" ConsoleApplication3.exe /32BIT+
if errorlevel 1 (
goto start
)
start "" ConsoleApplication3.exe
请注意,这只是示例,如果出现问题,将陷入死循环,请根据您的要求进行调整。这个脚本所做的只是在 32 位模式下使用 CorFlags 工具将您的 exe 更新为 运行,然后启动您的主 exe。
启动应用程序后,您可以立即进行以下检查:
static void Main() {
if (Environment.Is64BitProcess) {
// here you also check if you have dependency mismatch, and then:
Console.WriteLine("Running in 64-bit mode. Press any key to fix");
Console.ReadKey();
// run script (here we assume script is in the same directory as main executable, and is called fix-mode.cmd
var process = new Process() {
StartInfo = new ProcessStartInfo("cmd.exe", "/c call fix-mode.cmd")
};
process.Start();
// here you should exit your application immediatly, so that CorFlags can update it (since it cannot do that while it is running)
}
else {
// after restart by our script - we will get here
Console.WriteLine("Running in 32bit mode");
}
}
请注意,此工具 (CorFlags) 仅适用于 Visual Studio,因此您可能希望将其与您的应用程序打包在一起(可能在远程计算机上不可用)。