How do I call unmanaged C/C++ code from a C# ASP.NET webpage

坚强是说给别人听的谎言 提交于 2019-11-26 21:24:41

问题


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?


回答1:


Check out P/Invoke.

Calling Win32 DLLs in C# with P/Invoke

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




回答2:


  1. create an unmanaged dll:

    extern "C" __declspec(dllexport) __cdecl int sum(int a,int b); ---->
    
  2. create a namespace/class to DllImport the above 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

    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.



来源:https://stackoverflow.com/questions/720004/how-do-i-call-unmanaged-c-c-code-from-a-c-sharp-asp-net-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!