Original Post
Hi,
I want to use my c++ engine in c#. I've heard about the PInvoke, I've seen some tutorial, but it doesn't work, and I don't know where I am wrong.
c++ code:
This code has been complied to dll
the hDll isn't null, but the funcPtr is 0. Where am I wrong?
I hope, you could help me,
Thanks
I want to use my c++ engine in c#. I've heard about the PInvoke, I've seen some tutorial, but it doesn't work, and I don't know where I am wrong.
c++ code:
int __declspec(dllexport) Add(int a, int b)
{
return (a+b);
}This code has been complied to dll
using System;
using System.Runtime.InteropServices;
class main
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(int hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(int hModule);
delegate int Add(int a, int b);
public static void Main()
{
int hDll = LoadLibrary("DLLExporter.dll");
IntPtr funcPtr = GetProcAddress(hDll, "Add");
Add addFunc = (Add)Marshal.GetDelegateForFunctionPointer(funcPtr, typeof(Add));
int c = addFunc(5, 4);
Console.WriteLine(c);
FreeLibrary(hDll);
}
}the hDll isn't null, but the funcPtr is 0. Where am I wrong?
I hope, you could help me,
Thanks