Developer
Developer

Reputation: 18649

Interop issue in .NET

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?

Upvotes: 2

Views: 593

Answers (5)

Anzurio
Anzurio

Reputation: 17014

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

Upvotes: 0

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

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.

Upvotes: 2

JaredPar
JaredPar

Reputation: 754545

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.

Upvotes: 2

Igal Serban
Igal Serban

Reputation: 10684

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.

Upvotes: 1

Tom Anderson
Tom Anderson

Reputation: 10827

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.

Upvotes: 0

Related Questions