有没有办法在64位Windows环境中运行而不重新编译为x86时,在AnyCPU下编译的.NET应用程序作为32位应用程序?
我有一个Any CPU应用程序安装到Program Files(x86).在一台Windows 7 x64计算机上,它按预期运行x64,而在另一台Windows 7 x64计算机上运行为x86!
怎么会这样?我希望它在两台机器上以x64运行.可能是什么原因,我该怎样做才能始终以x64运行?
在我运行它的地方是否重要 - Program Files (x86)在我的情况下?
关于这个论坛有很多问题BadImageFormatException,但没有一个问题与我的问题相符.
我有一个包含几个项目的解决方案.当我在Debug或Release + Any CPU中运行时,一切正常.然而,当我切换到x64并运行我的ASP.NET MVC项目时,我得到了臭名昭着的BadImageFormatException
无法加载文件或程序集"AgileEFLib"或其依赖项之一.尝试加载格式不正确的程序.
AgileEFLib是一个内部实用程序库,因此我有足够的灵活性.但真正奇怪的是,当我运行一个也依赖于AgileEFLib它的控制台应用程序时,它运行得很好.它失败的唯一时间是我运行网站时.
我已经检查过Release | x64配置中的所有项目都设置为编译为x64.
我还能做些什么来解决这个问题?
编辑:另一个有用的信息是,这个问题只发生在我使用Visual Studio的IIS Express进行调试时.当我发布并部署到IIS时,它工作正常.
编辑:有关错误的更多详细信息:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Could not load file or assembly 'AgileEFLib' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated …Run Code Online (Sandbox Code Playgroud) 我正在搞乱一些相当低级别的东西,并试图确定为什么我使用CorFlags.exe实用程序得到不同的输出.作为参考,输出如下:
$ corflags test2.exe Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.0.30319.17929 Copyright (c) Microsoft Corporation. All rights reserved. Version : v4.0.30319 CLR Header: 2.5 PE : PE32 CorFlags : 0x1 ILONLY : 1 32BITREQ : 0 32BITPREF : 0 Signed : 0 $ corflags test.exe Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.0.30319.17929 Copyright (c) Microsoft Corporation. All rights reserved. Version : v4.0.30319 CLR Header: 2.5 PE : PE32 CorFlags : 0x20003 ILONLY : 1 32BITREQ : …
我需要我的程序来确定它是为32位窗口还是64位/任何CPU编译的.我需要根据编译选项选择不同的COM服务器标志.有任何想法吗?
我需要以编程方式确定程序集是x86,x64还是AnyCPU?有一个几乎相同的问题,但它提供的解决方案
Assembly assembly = Assembly.LoadFrom(fileName);
PortableExecutableKinds peKind;
ImageFileMachine imageFileMachine;
assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine);
Run Code Online (Sandbox Code Playgroud)
尝试从32位进程加载64位程序集时失败(反之亦然).
是否有一种以编程方式查找程序集的编译类型的万无一失的方法?
编辑:基于@BenVoigt建议,我创建了一个小命令行实用程序,用于检查DLL是否受管理以及是否为x86/x64/AnyCPU.我希望有人发现它有用.
如何测试现有的托管.NET .dll以查看其32位还是64位?
已知的死胡同:
我已经尝试将服务EXE文件编译为AnyCPU,x86和x64.它适用于我的Windows 7 x64盒子.我正在使用installutil安装.NET 4版本.
当我启动服务时,我收到错误.它没有说什么,但它确实指出了哪些模块被加载.那些有潜在兴趣的人:
LoadedModule [0] = D:\ yellowbook\grapevine_service\grapevinesystemservice.exe LoadedModule 3 = C:\ Windows\system32\KERNEL32.dll LoadedModule 4 = C:\ Windows\system32\KERNELBASE.dll LoadedModule [9] = C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\mscoreei.dll LoadedModule [17] = C:\ Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll LoadedModule [18] = C:\ Windows\WinSxS\amd64_microsoft .vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.4927_none_88dce9872fb18caf\MSVCR80.dll LoadedModule [22] = C:\ Windows\assembly\NativeImages_v2.0.50727_64\mscorlib\9a017aa8d51322f18a40f414fa35872d\mscorlib.ni.dll
我可以使用命令行开关将此服务作为控制台应用程序运行,从而产生以下结果:
未处理的异常:System.BadImageFormatException:无法加载文件或程序集"grapevinesystemservice.exe"或其依赖项之一.此程序集由比当前加载的运行时更新的运行时构建,无法加载.文件名称:'grapevinesystemservice.exe'
我试过注册表黑客强制使用最新的运行时.没有效果.我已经查看了已安装的EXE文件,以确保它没有被我的构建服务器获取,但它的目标是4.0.30319.我的本地,构建和测试服务器都有30319作为.NET 4版本.没有人有过beta/RC.
它们没有在加载模块中列出,但我也使用log4net(在.NET 4中从源代码重建),StructureMap,NoRM(MongoDB),AutoMapper,Newtonsoft.Json以及其他一些构建的DLL文件由我的团队.
我可以通过以编程方式在c#中加载dll来获得处理器体系结构吗?
有没有可以做到这一点的课程?
我需要弄脏dll是x86,x64,MSIL等.
我正在构建一个仅64位的Web应用程序(AssemblyA)并引用我已经构建的另一个应用程序,它也是64位(AssemblyB).
当我添加AssemblyB作为引用时AssemblyA,我在Visual Studio中得到以下编译错误:
ASPNETCOMPILER无法加载文件或程序集"AssemblyB"或其依赖项之一.尝试加载格式不正确的程序.
我的应用程序的平台目标设置都设置为x64,两个Target Framework设置都是.Net Framework 4.6和Prefer 32bit is unchecked.
我尝试引用AssemblyBin的每个依赖引用AssemblyA,确保所有依赖引用的版本相同.
我使用过这个问题:如何确定.NET程序集是为x86还是x64构建的?确认所有引用的程序集是MSIL或AMD64.
我使用了aspnet_compiler.exe命令行工具并errorstack启用了选项,并获得了以下堆栈跟踪:
[BadImageFormatException]:无法加载文件或程序集"AssemblyB"或其依赖项之一.尝试加载格式不正确的程序.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName,String codeBase,Evidence assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark&stackMark,IntPtr pPrivHostBinder,Boolean throwOnFileNotFound,Boolean forIntro spection,Boolean suppressSecurityChecks)
在System.Reflection.RuntimeAssembly.nLoad(的AssemblyName文件名,字符串的代码库,证据assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark&S tackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forIntros pection,布尔suppressSecurityChecks)
在System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(的AssemblyName assemblyRef,证据assemblySecurity,RuntimeAssembly reqAssembly,StackCrawlMark&stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forIntrospection,布尔suppressSecurityChecks)
在System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Evidence assemblySecurity,StackCrawlMark&stackMark,IntPtr pPrivHostBinder,Boolean forIntrospection)
在System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Evidence assemblySecurity,StackCrawlMark&stackMark,Boolean forIntrospection)
在System.Reflection.Assembly.Load(String assemblyString)
在System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName,Boolean starDirective)
[ConfigurationErrorsException]:无法加载文件或程序集"AssemblyB"或其依赖项之一.尝试加载格式不正确的程序.
在System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName,Boolean starDirective)
在System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomain …
我需要编写类似CorFlags的应用程序.如果我有一个汇编文件的路径,如何读取它的CorFlags?
我特别需要知道程序集是Any-CPU还是x86
我想避免使用反射加载程序集,因为我需要扫描许多文件.
我们的应用程序中有一个项目,它构建在32位或64位版本的dll中,具体取决于它所基于的机器上的处理器体系结构,以便于测试.
我可以预测将在给定机器上使用的dll,但是我们有几个开发人员,一些使用64位机器,一些使用32位机器,我希望能够检查他们放到实时服务器的构建是否已经上升使用32位版本的dll或64位版本的dll.
检查dll的属性不会提供此类信息.有没有办法得到它?
谢谢
我正在使用 C++ 创建 DLL 并使用 C# 导入它。我编译该 DLL 没有问题,并且从 .NET 4.7.1 调用它时工作正常。但是,当我尝试从 .NET 6.0 调用它时,出现错误EntryPointNotFoundException:
Unhandled exception. System.EntryPointNotFoundException:
Unable to find an entry point named 'E' in DLL 'test.dll'.
at Program.E(int a, int b)
at Program.Main(String[] args) in G:\C++\Test\Program.cs:line 6
Run Code Online (Sandbox Code Playgroud)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--<TargetFramework>net4.7.1</TargetFramework>-->
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<Configuration>Debug</Configuration>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
extern "C"__declspec(dllexport) int E(int a, int b)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
public class Program
{
[DllImport("test.dll")] public static extern int E(int a, int b); …Run Code Online (Sandbox Code Playgroud) .net ×11
c# ×7
64-bit ×3
compilation ×3
assemblies ×2
corflags ×2
dll ×2
.net-3.5 ×1
.net-4.0 ×1
.net-6.0 ×1
32bit-64bit ×1
anycpu ×1
architecture ×1
bit-fields ×1
c++ ×1
clr ×1
executable ×1
interop ×1
low-level ×1
processor ×1
windows ×1
x86-64 ×1