6

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

4

3 回答 3

10
  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);
    }
    
于 2009-04-06T01:33:09.937 回答
5

查看 P/Invoke。

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

如果它是 COM dll,那么您可以使用COM Interop

于 2009-04-06T01:31:19.473 回答
0

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

于 2010-06-03T14:52:43.790 回答