How to invoke a 32bit COM interop dll from VS 2010 Solution which is targetting 64 bit machines

Go To StackoverFlow.com

2

I am working on Windows client application using c#, have two projects in my VS 2010 solution, the start-up project and a class library which uses third party COM interop DLL.My desktop is 64 bit and all the client machines are 64 bit as well.

If I keep the start-up project as any cpu in (build settings) and x86 for the second project which invokes the COM DLL, I am getting the following error:

Could not load file or assembly ... or one of its dependencies. An attempt was made load a program with an incorrect format.

If I keep both the projects as Any cpu, I am getting the following error:

Retrieving the COM class factory for component with CLSID .. failed due to the following error:

Any idea how to resolve the issue without converting the solution (all projects) to x86.

2012-04-04 19:09
by skjagini
You are going to have to talk to the COM component vendor and see what they can do to provide you with a 64-bit version of their product. Or just set the Platform target setting of your main EXE project to x86 - Hans Passant 2012-04-04 19:32
not possible in general, see related question hereyms 2012-04-04 19:38
Is there a good reason why you don't just target x86 - David Heffernan 2012-04-04 20:19


3

Well, I suggest you to look at DLL surrogates. This trick will definitely solve your problem, but only as long as you may change the registration info of the DLL inrpoc server in the registry. If the class is configured to use a surrogate, it will run in a separate process. COM allows one to use an out-of-proc 32bit COM server from a 64bit application.

All you need to change is shown below:

Look at the following entry in the registry:

HKCR\CLSID\{CLSID_CoClass}

And look if it has the AppID field. If it does, just visit this registry key:

HKLM\Software\Classes\AppID\{APPID}

and add the DllSurrogate string key with no value.

If the class doesn't have an assosiated AppID, you may create it yourself. Then you may use your 32 -bit inproc library from the 64bit targeted .net application.

2012-04-04 20:16
by Igor Chornous
Here some more information: http://www.gfi.com/blog/32bit-object-64bit-environment/ - But the question is are there any magical tools that will do this for all objects from my dll and add keys for all classes in register - jotbek 2013-01-03 11:34


1

Probably the easiest thing to do is just set your main executable to be x86. Its the application that will determine how the rest of the assemblies are jitted.

2012-04-04 20:34
by Andy


-1

None of the above worked for me. BUT what you can do is when you click 'Add Reference' select the relevant reference via the COM tab rather than browsing to the DLL. This makes it version specific, so if you select 32 bit version, runtime will go with that. Also, be sure you have selected relevant .NET framework version for application pool and you may want to set 'Enable 32 Bit Application' to true (advanced application pool settings).

2014-04-17 13:56
by DAvvy Boy
The question is about WinForms, not Asp.Net so your App pool setting isn't relevant - Andy 2015-04-28 14:53
Ads