Understanding Platform Invocation Services(Chapter 1 of COM and .NET Interoperability) part3

ExpandedBlockStart.gif C++
 1  #include  " MyCustomCallbackDLL.h "
 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 -> =   10000 ;
23      thePoint -> =   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  }

 

ExpandedBlockStart.gif C#
 1  using  System;
 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  }

 

 

 

 

转载于:https://www.cnblogs.com/zhtf2014/archive/2010/03/14/1685464.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值