如何从C#ASP.NET网页调用非托管C / C ++代码

我有一个使用C#的ASP.NET网站,我想从非托管的C / C ++ DLL调用函数。 我该怎么做?

查看P / Invoke。

使用P / Invoke在C#中调用Win32 DLL

如果它是一个COM DLL,那么你可以使用COM Interop

  1. 创建一个非托管DLL:

    extern "C" __declspec(dllexport) __cdecl int sum(int a,int b); ----> 
  2. 创建一个名称空间/类到DllImport上面的DLL

     using System.Runtime.InteropServices; namespace ImportDLL { public class importdll { public importdll() { } DllImport("mysum.dll", EntryPoint="sum", ExactSpelling=false, CallingConvention = CallingConvention.Cdecl)] public extern int myfun(int a, int b); } } 
  3. 在后面创建一个aspx代码

     using ImportDLL; namespace TEST { public int my_result; protected importdll imp = new importdll(); my_result = imp.myfun(1,1); } 

只需添加pinvoke.net就可以满足您的Win32需求。