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

时间:2022-09-01 15:44:51

I have an ASP.NET website that uses C# and I'd like to call functions from an unmanaged C/C++ DLL. How do I do it?

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

3 个解决方案

#1


Check out P/Invoke.

查看P / Invoke。

Calling Win32 DLLs in C# with P/Invoke

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

If it's a COM dll, then you can use COM Interop

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

#2


  1. create an unmanaged dll:

    创建一个非托管DLL:

    extern "C" __declspec(dllexport) __cdecl int sum(int a,int b); ---->
    
  2. create a namespace/class to DllImport the above DLL

    创建一个名称空间/类到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. create a aspx code behind

    在后面创建一个aspx代码

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

#3


Just adding that pinvoke.net is a great wiki/resource for your Win32 needs.

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

#1


Check out P/Invoke.

查看P / Invoke。

Calling Win32 DLLs in C# with P/Invoke

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

If it's a COM dll, then you can use COM Interop

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

#2


  1. create an unmanaged dll:

    创建一个非托管DLL:

    extern "C" __declspec(dllexport) __cdecl int sum(int a,int b); ---->
    
  2. create a namespace/class to DllImport the above DLL

    创建一个名称空间/类到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. create a aspx code behind

    在后面创建一个aspx代码

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

#3


Just adding that pinvoke.net is a great wiki/resource for your Win32 needs.

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