WCF service library : BadImageFormatException with 32 bit dll - c#

My project Structure looks like this:
myApp
-->WCFSerLib (Any CPU Deafult application)
-->ClassLib1 (Any CPU)
-->ClassLib2 (x86)
When I try to execute my application Unfortuantely am getting this below error.
System.BadImageFormatException was unhandled by user code
Is there anyway such that I can achieve the above?
Thanks

If I guess right than you are trying to use an x86 dll on a x64 mashine. With AnyCPU the .net framework will use the currient architecture automatically.
So your application runs as a x64 Application which tries to load a x86 libary which failes. So you have two options:
Set the target architeture to x86
Add/create a x64 libary
If you have both verions (x86/x64) somewhere in you envireonment variable the operation system will choos the correct one automatically.

I suppose that you are running your application on a x64 system.
Mixing the Platform Type in that way will cause the BFE when the WCFService Library or the ClassLib1 call methods in ClassLib2 because the first two run as 64bit processes while the latter is forced to run in x86 mode.
You should set the same platform type on every project.
Of course, the WCFService library supports only AnyCPU, so it make sense to use, for every project, the AnyCPU platform type.
If your ClassLib2 requires the use of a 32bit library (winscard.dll) and there is no 64bit version of that library, then, I think you should use a WCF Service Application instead of a Service Library. See this question

Thank you guys,
problem was with winscard.dll it uses Handles which are Int32
.all I changed is replacing the Int32 with Int64.

Related

Why is LoadLibrary failing in some machines when calling a dll using P/Invoke?

I have a .NET application that calls a C++ API using P/Invoke. The C++ dll has a dependency from a third-party dll. That third party dll calls LoadLibrary to dynamically load libeay32.dll, an OpenSSL dependency.
After doing several tests I found out that the .NET application works correctly in Windows 10 but fails in some deployments using Windows 7. The cause of the failure is because the .NET process is not loading libeay32.dll. I tried placing libeay32.dll in the same directory as the process and in the System folder (having in mind the Dynamically load search order) but the failure persisted in Windows 7 32 bits only. Furthermore, if I called the API dll from a C++ console application it worked without issues in all platforms. To solve the problem I called LoadLibrary in the .NET application, before making the P/Invoke calls to the C++ API, and it worked in all target platforms (Windows 10/7 32 and 64 bits).
Why would LoadLibrary fail to load libeay32.dll in Windows 7 but not Windows 10 if called from the C++ dll dependency of the API called using P/Invoke?
Edit
Some notes from comments:
All the compiled assemblies are targeted for x86 architecture.
Before posting the question, I verified that no dependencies files
where missing, using dependency walker.
Likely because the dll is depending on some files that are missing. Use dependency walker and see what files it requires. Check on both systems. The tool will show you in red the missing files. Do not blindly copy the files.See what they are, what runtime or setup is required to deploy them, and deploy them properly.
.Net applications can be built with AnyCPU as their target and run in both 64 and 32 bit modes, but if you choose AnyCPU for your target the operating system will choose one based on it's preference. What this can mean if you have native dependencies is that the library that you're depending on can't be loaded because you're running in the wrong bitness. If you can constrain the application to the bitness you're expecting it might work better.

Running a 64bit program referencing a 32bit dll using visual studio - C#

I am currently facing a problem where I am referencing a 32bit dll. I tried changing the platform to 86bit which works fine but the requirements are to use a 64bit or 'Any CPU'. When I use the 'Any CPU' platform it somehow does not pick up the dll
Is there a way of using a 32bit dll in 64bit program?
I also read about a workaround of 'wrapping' which was not clear... can someone elaborate
Is there a way of using a 32bit dll in 64bit program?
In the same process: No.*
However, you could host the dll in a separate child process via something like the Add In Framework (which supports out of process workers).
* Note, I am assuming this dll is either natively compiled, or .NET but compiled explicitly for 32bit only.

BadImageFormatException occurring when loading a native DLL in .NET 4.0

I'm attempting to use a CAN device over USB that comes with a native DLL that needs to be wrapped by a .NET C# class (source code provided by the vendor) that gets included in one's project. Their sample applications target .NET 2.0 where my application targets .NET 4.0. I'm able to use the code in their sample apps and debug everything just fine, however, when I try to debug my application, I get a BadImageFormatException:
System.TypeInitializationException: The type initializer for 'TotalPhase.KomodoApi' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
The only differences between their code and mine appears to be that their code is an application built for .NET 2.0 and (currently) my code is running as an MSTest unit test in .NET 4.0. Both solutions target AnyCPU. I'm running on a Windows 7 Ultimate 64-bit install. Even changing from AnyCPU to x86 didn't make any difference. How can I get this native DLL to load in an AnyCPU project?
If you get a BadImageFormatException when interfacing with a native DLL, it almost always means that you are trying to interface with a 32-bit DLL while running in the 64-bit CLR, or vice versa.
When you run the sample applications, do the processes have *32 in the "Image Name" column of Task Manager's "Processes" tab? That indicates the applications are running in the 32-bit CLR. Check your own application as well. It is possible that the machine you are testing on only has a 32-bit .NET 2.0 runtime, but both 32-bit and 64-bit .NET 4.0 runtimes, or the other way around.
If you are distributing a native DLL with your .NET application, then you should set your startup project to target x86 or x64 (as opposed to AnyCPU), depending on whether the native libraries are 32-bit or 64-bit. You can always ship both 32-bit and 64-bit versions, and let the installer choose which binaries to install based on the client architecture.
Alternatively, you can ship both 32-bit and 64-bit DLLs with different file names, define separate P/Invoke stubs for each version, and decide which one to call at runtime. The easiest way to do this would probably be to wrap your native calls in an interface (e.g., INativeMethods) and choose which implementation to instantiate at runtime based on IntPtr.Size. With this method, you could still target AnyCPU.

Need 64-bit SQLite DLL for managed C# application

I'm trying to embed SQLite into my portable C# .NET 2.0 application rather than having the DLL files included in the distribution folder. However, to embed SQLite, I cannot use the Mixed-Mode libraries provided by PHXSoftware. Instead, I am using their 'Managed Only' version.
This works fine on 32-bit computers, but when it's running on a 64-bit machine, it throws a format exception. As I found out from here: http://sqlite.phxsoftware.com/forums/p/2564/9939.aspx I need to load the unmanaged sqlite3.dll manually in the required architecture format first before I use the managed libraries.
That's where I fall short. I cannot find a 64-bit version of SQLite to include along with the 32-bit one. Can anyone help? Dare I say, does anyone have any better ideas?
I'd recommend you build the source yourself. It's very straight-forward to do. Especially considering Sqlite offers amalgamation source.
Here are the compiler pre-processor defines I use for a 64-bit release build:
WIN64 NDEBUG
_WINDOWS
_USRDLL
NO_TCL
_CRT_SECURE_NO_DEPRECATE
THREADSAFE=1
TEMP_STORE=1
SQLITE_MAX_EXPR_DEPTH=0
Here are the compiler pre-processor defines I use for a 32-bit release build:
WIN32
NDEBUG
_WINDOWS
_USRDLL
NO_TCL
_CRT_SECURE_NO_DEPRECATE
THREADSAFE=1
TEMP_STORE=1
SQLITE_MAX_EXPR_DEPTH=0
The System.Data.SQLite fork has x86/x64 binaries for .Net 2, 3.5, and 4. Downloads are here.
Update:
Another possible solution is to target your application for x86 platform and just use the x86 SQLite libraries. If your application doesn't require x64 features targeting the x86 platform will greatly reduce the complexity of your deployment.

Unable to load assembly on XP 64bit

I have compiled a .NET application using Any CPU option. This .NET application uses an unmanaged dll (managed wrapper) that encapsulated C++ calls. This .NET wrapper resides in GAC.
When I run the .NET application it runs fine on XP 32 bit. But when I run it on XP 64 bit, it fails and gives the following exception:
Could not load file or assembly "Dll name, version, Culture=neutral, PublicKeyToken" or one of its dependencies.
How do I resolve this?
You need to use the x86 CPU option. If you know that one of your unmanaged dependencies is 32bit then you need to build your solution with the x86 option for the CPU. This ensures that even on a 64bit operating system your application will be run in a 32 bit process.
This is required because its not possible to load 32 bit compiled code into a 64 bit process.
You need to make sure your unmanaged dll is also 64bit capable and within the search path.
You can find some explanation on Microsoft Connect website

Categories