问题
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.
回答1:
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.
回答2:
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.
回答3:
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).
来源:https://stackoverflow.com/questions/10017681/how-to-invoke-a-32bit-com-interop-dll-from-vs-2010-solution-which-is-targetting