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?
Execution of Unmanaged Code In C# the unmanaged code is directly executed by the operating system. Generally, the executable files of unmanaged or unsafe code are in the form of binary images which are loaded into the memory.
C++/CLI can call any C# function as if it were a "regular" C++ function. Add your references, /clr and It Just Works.
create an unmanaged dll:
extern "C" __declspec(dllexport) __cdecl int sum(int a,int b); ---->
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);
}
}
create a aspx code behind
using ImportDLL;
namespace TEST
{
public int my_result;
protected importdll imp = new importdll();
my_result = imp.myfun(1,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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With