2
3 BOOL APIENTRY DllMain( HANDLE hModule,
4 DWORD ul_reason_for_call,
5 LPVOID lpReserved)
6 {
7 return TRUE;
8 }
9
10 // THEPOINT Callback prototype.
11 typedef bool (CALLBACK *POINTCALLBACKFUNCTION)( THEPOINT* i );
12
13 // Simple Callback prototype.
14 typedef bool (CALLBACK *SIMPLECALLBACKFUNCTION)();
15
16 extern "C" MYCUSTOMCALLBACKDLL_API void ChangePOINTAndReportBack(POINTCALLBACKFUNCTION pf, THEPOINT* thePoint)
17 {
18 MessageBox(NULL, "Received THEPOINT and am about to change it...",
19 "Unmanaged DLL", MB_OK);
20
21 // Take the incoming THEPOINT and change it.
22 thePoint->x = 10000;
23 thePoint->y = 20000;
24
25 // Call the managed function.
26 bool res = (*pf)(thePoint);
27
28 // Get result from callback.
29 if( res )
30 MessageBox(NULL, "Callback says TRUE",
31 "Unmanaged DLL", MB_OK);
32 else
33 MessageBox(NULL, "Callback says FALSE",
34 "Unmanaged DLL", MB_OK);
35 }
36
37 extern "C" MYCUSTOMCALLBACKDLL_API void VerifyAndReportBack(SIMPLECALLBACKFUNCTION pf)
38 {
39 MessageBox(NULL, "You called me...about to call you!",
40 "Unmanaged DLL", MB_OK);
41
42 // Call the managed function.
43 bool res = (*pf)();
44
45 // Get result from callback.
46 if( res )
47 MessageBox(NULL, "Callback says TRUE",
48 "Unmanaged DLL", MB_OK);
49 else
50 MessageBox(NULL, "Callback says FALSE",
51 "Unmanaged DLL", MB_OK);
52 }
2 using System.Runtime.InteropServices;
3
4 namespace CustomCallbackClient
5 {
6 // A simple struct to pass to the DLL.
7 [StructLayout(LayoutKind.Sequential)]
8 public class THEPOINT
9 {
10 public int x;
11 public int y;
12 }
13
14 class ManagedCallBackApp
15 {
16 public delegate bool SendTHEPOINTHere(THEPOINT pt);
17 public delegate bool ReportBackHere();
18
19 [DllImport("MyCustomCallbackDLL.dll")]
20 public static extern void VerifyAndReportBack(ReportBackHere x);
21
22 [DllImport("MyCustomCallbackDLL.dll")]
23 public static extern void ChangePOINTAndReportBack(SendTHEPOINTHere x, THEPOINT pt);
24
25 [STAThread]
26 public static void Main()
27 {
28 ReportBackHere simpleCallback = new ReportBackHere(ManagedCallBackApp.Report);
29 VerifyAndReportBack(simpleCallback);
30
31 SendTHEPOINTHere theCallBack = new SendTHEPOINTHere(ManagedCallBackApp.GiveMeThePoint);
32 THEPOINT pt = new THEPOINT();
33 pt.x = 10;
34 pt.y = 10;
35 Console.WriteLine("Point is:");
36 Console.WriteLine("X = {0}\nY = {1}", pt.x, pt.y);
37 ChangePOINTAndReportBack(theCallBack, pt);
38 }
39
40 public static bool GiveMeThePoint(THEPOINT pt)
41 {
42 Console.WriteLine("New Point is:");
43 Console.WriteLine("X = {0}\nY = {1}", pt.x, pt.y);
44 return true;
45 }
46
47 public static bool Report()
48 {
49 Console.WriteLine("I was called by the DLL!");
50 return false;
51 }
52
53 }
54 }