简体   繁体   中英

Calling C++ DLLs from ASP.NET 4

I'm trying to call a C++ DLL from ASP.NET 4.0 web application but keep getting a DLL Import error - unable to load library 'Library name' .

I've read a lot of blogs where developers experience the same probem and what seems to be working for most people is adding the path to where the unmanaged DLLs are to the PATH Environment Variable

I've done the same on the server and double, tripple checked that I spalled the path correctly and that the C++ DLLs are in that folder but when I view the application in the browser I'm still getting the same error.I've placed logging in the code and it's definitely coming from the portion of code which calls the unmanaged DLL.

Here is some information on my environment:

  1. Windows Server 2003
  2. IIS 6.0
  3. .NET Framework 4.0
  4. ASP.NET MVC 4.0
  5. C#

On the dev machine it works if I copy the unmamanaged DLLs to C:\\Program Files\\IIS Express

Tried copying the C++ DLLs to the bin folder but as far as I understand ASP.NET copies the DLLs from the bin folder in inetpub\\wwwroot\\sitename\\bin to a dynamic location when it executes the code but this process excludes the unmanaged DLLs.Any advice on what else I can try?

EDIT:

In terms of the code it's exactly the same as any other interop code,I have a wrapper class where I use DllImport to define all th required function calls and in my code I create a new instance of the wrapper and call the required function.As I mentioned this works fine on the dev machine as long as the C++ DLLs are i the IISExpress folder but when I deploy to the server it throws the above mentioned error:

[DllImport("My3rdparty.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int myfunction(int handle, int* response, int timer);

So after weeks of struggling I came up with a work around.The integration that I was implementing was for credit card processing. Along with the C++ DLLs they also have a compiled configuration file called system.dna and my suspicion is that when you add the folder containing the interop DLLs to the PATH Environment Variables it does not pick up the system.dna file.

As a workaround I created a WCF service which is installed as a Windows Service on the server and I've placed the logic for doing payments into this service and the web application simply talks to the service.I hope this helps other developers who are facing the same issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM