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

前端 未结 3 1950
挽巷
挽巷 2020-12-06 08:08

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?

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 08:42

    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);
      }
      

提交回复
热议问题