I want to use my dll at runtime by code.(Not putting the dll at Plugin asset)
So I write this code below:
Assembly dll = Assembly.LoadFrom("D:\HaoranZhu\workspace\UnityProjects\ViveDRMSDKTest\Assets\Plugins\x86_64\vita_api.dll");
But it shows the error here in Unity console:
BadImageFormatException: Could not load file or assembly 'D:\HaoranZhu\workspace\UnityProjects\ViveDRMSDKTest\Assets\Plugins\x86_64\vita_api.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
System.Reflection.Assembly.LoadFrom (System.String assemblyFile) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Assembly.cs:520)
loadExternalDll.Start () (at Assets/TestScripts/loadExternalDll.cs:16)
what's the problem here?
You are getting this error because (according to https://msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx):
----BadImageFormatException----
assemblyFile is not a valid assembly; for example, a 32-bit assembly in a 64-bit process. See the exception topic for more information.
-or-
Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.
---
This means that either a you are using a 32-bit assembly when the process is 64 bit, (just get a 64-bit version).
Or if you downloaded a library for a later version of the common language runtime.
Related
I am trying to include some reference files in Wix# managed project using DefaultRefAssemblies.Add method:
ManagedProject project = new ManagedProject();
project.DefaultRefAssemblies.Add("FontAwesome.Sharp.dll");
project.DefaultRefAssemblies.Add("protobuf-net.dll");
project.DefaultRefAssemblies.Add("Newtonsoft.Json.dll");
project.DefaultRefAssemblies.Add("ManagedOpenSsl.dll");
project.DefaultRefAssemblies.Add("ssleay32.dll");
When I try to build a MSI I get an error. The problem happens to be in loading of ssleay32 assembly (part of OpenSSL). When I exclude this file, the build succeeds. Can you please help me understand the exception? The target framework of the Wix# project is .NET Framework 4.8.
Unhandled Exception: System.BadImageFormatException: Could not load file or assembly '361984 bytes loaded from WixSharp, Version=1.15.0.0, Culture=neutral, PublicKeyToken=3775edd25acc43c2' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Bad IL format.
Wixsharp supports .net assemblies compiled for:
Platform x86
Managed assemblies (Not native)
Target framework 3.5
So, make sure "ssleay32.dll" has 3 requirements above. Every ssleay32 assembly i have found was native (unmanaged).
Workaround in case of no luck with:
You can try to save ssleay32.dll to embedding resource file and before "ManagedOpenSsl.dll" types usage you should load dll in the memory. Load unmanaged assembly
Good luck!
I'm using the library SharpShell to develop a simple shell extension (a property sheet) to display some information for .NET assemblies, see:
This shell extension and SharpShell itself is not relevant to this question, but to explain my scenario so you can understand better my question.
My project is compiled as Any CPU mode, then, to manage the loading of x86, x64 an Any CPU assemblies in my program I'm using the overload of the function Assembly.Load() that takes the bytes of a raw assembly as unique argument:
Assembly asm = Assembly.Load( File.ReadAllBytes(filepath) );
I tested it with dlls and executables of both x86 and x64 architectures, and Any CPU, it works fine... except for the problem I'll explain.
The problem is that for some reason that I ignore it is not working for some x86 assemblies, for example when I try to load the x86 assembly: System.Web.dll ( System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ), the Assembly.Load() function throws an exception. It is NOT a BadImageException, its a FileLoadException with this error message:
Could not load file or assembly 'System.Web, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
dependencies. The given assembly name or codebase was invalid.
(Exception from HRESULT: 0x80131047)
( note that Assembly.ReflectionOnlyLoad() will also throw the same exception. )
However, I can load the x64 version of the same assembly with success. Please note that as I said, I can load most of the 32-bit assemblies in my program.
If I compile my project as x86, then I can load this assembly with sucess. But my project must compile as Any CPU (to be able handle the loading of both x86 and x64 assemblies ...since I'm not aware of a better approach).
I would like to know why I can't load that specific assembly, and a viable approach to solve this issue.
Here you can download the dlls if you want to test it:
http://www.mediafire.com/file/8h9256w02b2j3dj/System.Web.dll.zip/file
It is not possible to load Assemblies for a different processor architecture than the current process using Assembly.Load().
Use Assembly.ReflectionOnlyLoadFrom() instead.
In a simple demo application that is run as 64 bit, I see the following behaviour:
var lAss = Assembly.Load(File.ReadAllBytes(#"C:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll"));
MessageBox.Show(lAss.Location);
This throws the exception as described in the question.
var lAss = Assembly.ReflectionOnlyLoadFrom((#"C:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll"));
MessageBox.Show(lAss.Location);
This throws no exception and shows the message box.
I have a language compiler that I've just updated to target .NET 4.0 and the metadata generator for framework assemblies now fails on System.Data.dll, while all others work fine (mscorlib, System, etc.)
The metadata generator simply loads assemblies, reflects over all the types and memberinfo and generates a stub class for the language.
I'm using Assembly.Load() and/or Assembly.ReflectionOnlyLoad()
Could not load file or assembly 'file:///c:\windows\Microsoft.NET\framework\v4.0.30319\System.Data.dll'
or one of its dependencies. An attempt was made to load a program with
an incorrect format.
It is built for "AnyCPU". I'm running Windows 7/x64 with .NET 4.5 update installed.
If you use corflags tool to analyze the assemblies, you should see that ILONLY: 0 for System.Data.dll while the others (mscorlib, System) ILONLY: 1. More information regarding ILONLY can be found at here.
Thus, the assemblies were built differently and you should not expect reflection to work for all. System.Data.dll seems to be a mixed-mode assembly, which is always bitness dependent.
If you cannot run your app as 32 bit so as to load the 32 bit assemblies, you might consider Mono.Cecil as #xmojmr said, as it supports reading mixed-mode assemblies.
I'm having little problem running my C# application after switching to Windows 8.1 from 8.
The problem is that I get this exception from title and there's not much help online. I tried to pinpoint the problem and it seems like my x64 application is trying to call x86 CSharp library. The reference in project leads to a DLL file, that upon calling x64 dumpbin program with /headers parameter outputs:
Dump of file Microsoft.CSharp.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
14C machine (x86)
3 number of sections
4FFA5C64 time date stamp Mon Jul 09 06:21:56 2012
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
2022 characteristics
Executable
Application can handle large (>2GB) addresses
DLL
Notice the machine is x86, while the application is running in x64 (checked with Environment.Is64BitProcess). This might be the problem I'm facing, however I can't find way to solve it - there seems to be no x64 .Net libraries installed. The only ones I have found are at: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework. Or maybe I'm completely off and there's some other problem. Anyway the exception occurs at startup in one of constructors and full detail is:
Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
"Could not load file or assembly 'Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The module was expected to contain an assembly manifest."
Edit:
While still panicking, I solved this by downloading CSharp.dll and replacing the dummy 0-byte file in path shown in answer by it.
The module was expected to contain an assembly manifest
You've been looking at a reference assembly, it is not the one that's actually loaded when you run your program. Microsoft.CSharp.dll is stored in the GAC, put there by the .NET installer. You can see the file by navigating to c:\windows\microsoft.net\assembly\gac_msil\microsoft.csharp. Keep clicking until you get to the file.
The exception message is a very unhealthy one, it doesn't recognize the DLL as a .NET assembly. There are few decent explanations for that, other than the file being corrupted. File corruption is always bad news, a strong hint that your hard-disk is failing. You'll need to get it fixed, follow-up if necessary at superuser.com
I have a C# application which is built with platform target as ANY CPU. It references a managed C++ application which is built with the platform target as Win32. However the loading of the C++ dll fails at runtime with the following error. "Could not load file or assembly 'abc_Debug, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Attempt to access invalid address. (Exception from HRESULT: 0x800701E7)"
Can anyone give me some pointers as to what could be wrong?
If you are running the application at a x64 architecture then your app is starting as x64 and then it can't load the c++ reference. You cannot build an application as AnyCpu if you don't have all the dependences available for AnyCpu, otherwise set x86 as the target.
in general for any loading problem of managed dlls, it's better to use fuslogvw it can gives you more details.