extern "C" { void (*foo1)(int)= NULL; __declspec(dllexport) void DisplayHelloFromDLL() { //foo = &my_int_func; foo1(2); printf ("Hello from DLL !\n"); } __declspec(dllexport) void InitializeFun(void (*foo)(int)) { foo1=foo; } }
public class NativeMethods { private const string KERNEL32_DLL = "Kernel32.dll"; [DllImport(KERNEL32_DLL)] static internal extern IntPtr LoadLibrary(string szFileName); [DllImport(KERNEL32_DLL)] static internal extern IntPtr GetProcAddress(IntPtr hLibrary, string szProcName); [DllImport(KERNEL32_DLL)] static internal extern int FreeLibrary(IntPtr hLibrary); } class Program { [DllImport("TestLib.dll")] public static extern void DisplayHelloFromDLL(); [DllImport("TestLib.dll")] public static extern void InitializeFun(VoidDelegate MyFunc); public delegate void VoidDelegate(); static void Main(string[] args) { Console.WriteLine("This is C# program"); IntPtr hUser32 = NativeMethods.LoadLibrary("TestLib.dll"); IntPtr pMethod = NativeMethods.GetProcAddress(hUser32, "InitializeFun"); VoidDelegate method2 = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(pMethod, typeof(VoidDelegate)); InitializeFun(method2); DisplayHelloFromDLL(); string str = Console.ReadLine(); } public static void MyFunc() { Console.WriteLine("I was called by c dll ..."); } }
VoidDelegate method2 = (VoidDelegate)Marshal.GetDelegateForFunctionPointer(pMethod, typeof(VoidDelegate));
VoidDelegate method2 = MyFunc;
InitializeFun(MyFunc)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)