tags:

views:

229

answers:

5

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?

A: 

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.

Tom Anderson
How would that solve the problem? Shouldn't this be /32bit+?
0xA3
no, you are removing the 32bit restriction on the interop, it may or may not work, but you can shoot for it, sometimes they don't have issues.
Tom Anderson
But how can an interop running as 64bit process load a 32bit COM component? The whole process should be started as a 32bit process.
0xA3
I agree with divo, I think it's the other way around
Mauricio Scheffer
+1  A: 

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.

Igal Serban
+2  A: 

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

  1. Explicitly compile the .Net EXE for 32 bit
  2. Write the component so that it can be hosted in it's own process. Even then I'm not sure how easy it will be to get the 32bit and 64bit process to talk with each other.
JaredPar
+2  A: 

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.

0xA3
+1  A: 

When a process is loaded as x64, you can't load x86 binaries/assemblies/anything.

Anzurio