Wednesday, May 05, 2010

Loading an unmanaged C++ dll into a managed c# class (.Net 3.5)

I am developing a .NET 3.5 C# Webservice which calls methods from within an unmanaged C++ dll. Aside from the basic issues of wrapping the structs and method calls for use by the C# class, I ran into a couple of hurdles deploying.

ISSUES:
(1)
Server was unable to process request. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

(2)
Server was unable to process request. ---> Unable to load DLL 'SPInterface.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

SOLUTIONS:
(1)
Be sure that your app_pool managing your webservice/site is allowing 32bit applications if running on a 64bit platform (IIS7)
OR
Be sure that your application is built as an x86 project
goto Configuration Manager and set the project to build as x86
(If Configuration Manager is not available, goto Tool>Options>Projects and Solutions>General>Show advanced build configurations)

(2)
Be sure that your unmanaged dll appears in your site bin folder.
Be sure that all resources (dll, lib, etc.) referenced by your dll are also in your bin.