I have a 32 bit managed assembly that access a 32 bit COM component. When i compile the assembly using the 64 bit flag, i get an error when i try to access the 32 bit COM component from it.
Is there a way around this problem?
I have a 32 bit managed assembly that access a 32 bit COM component. When i compile the assembly using the 64 bit flag, i get an error when i try to access the 32 bit COM component from it.
Is there a way around this problem?
You can either try to use corflags (may/may not work), or set your app to compile in 32bit only.
corflags <assemblyname> /32bit-
that removes the 32bit restriction on .Net assemblies.
You should find and install the 64 bit version of this com component. If it is not available, You will have to target your .net application to run as 32 bit application. The com component is running in your process. And you can't run both 32bit and 64bit code together in the same process.
When you use the 64 bit flag and run on a 64bit OS, the assembly will load into a 64 bit process. The vast majority of COM objects are created as "In Proc Servers."
The first step in creating an "in proc server" is to load the DLL containing the COM object into the process doing the creation. The DLL is 32 bit and cannot be loaded into the process.
You're unfortunately stuck with 2 options
In addition to the already given answers:
In Windows x64 a process may be started as 32-bit or 64-bit process. A 64-bit process can only load 64-bit dlls and a 32bit process only 32-bit dlls.
If your platform target (e.g. specified in the project properties) of your .Net application is set to "Any CPU", the intermediate code will be compiled to 32bit or 64bit code depending on the target platform, i.e. on a x64 system 64bit code will be generated.
Therefore the code can no longer load a 32-bit dll on a 64-bit system.
If your code loads unmanaged assemblies you should always specify the target platform explicitly.
When a process is loaded as x64, you can't load x86 binaries/assemblies/anything.