Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How does Calvin's code work?
Message
From
25/02/2004 16:09:45
 
 
To
25/02/2004 16:07:50
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00880866
Message ID:
00880867
Views:
76
We all know Calvin can do magic. This is just another example *G*.


>Calvin Hsia posted a code sample on the MS site, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfoxgen7/html/vfpandcom.asp , regarding "VFP Callback Design" (at the bottom). I've run the code in VFP7 SP1 and it works fine. However, I don't understand *how* it works. Specifically, in the definition of the cCallback class there's the line
implements iDemoEvents in idemo.dll
I can't see where the iDemoEvents interface is defined. And, in the sample IDEMO.PRG, the cCallBack class is instantiated before the iDemo COM server.
>
>For reference I've pasted the code sample below:
CLEAR ALL
>IF PROGRAM() != "IDEMO"
>   ?"this file MUST BE NAMED 'idemo.prg'"
>   RETURN
>ENDIF
>IF .t.
>   IF FILE("idemo.dll")
>      DECLARE integer DllUnregisterServer IN idemo.dll
>      DllUnregisterServer()
>      CLEAR DLLS
>   ENDIF
>   BUILD PROJECT idemo FROM idemo
>   BUILD DLL idemo from idemo recomp
>endif
>clear
>
>oCallback = NEWOBJECT("cCallback")    && the event callback
>ostock=newOBJECT("idemo.idemo")       && the business COM obj
>ostock.setcallback(oCallBack)         && like BindEvents
>?ostock.BuyStock("MSFT",10000 )       && invoke a method
>ostock.setcallback(.null.)            && like UnBindEvents
>?ostock.BuyStock("MSFT",20000 )       && this one doesn't fire events
>
>*This is the actual implementation of the event interface
>DEFINE CLASS cCallback as session
>   implements iDemoEvents in idemo.dll
>   procedure iDemoEvents_BeforeBuyStock(cStock as String, qty AS Number)
>      ?program(),cstock,qty
>   procedure iDemoEvents_AfterBuyStock(cStock as String, qty AS Number)
>      ?program(),cstock,qty
>
>enddefine
>*the rest of this file is used in the COM server
>DEFINE CLASS idemo as session olepublic
>   oc = .null.
>   PROCEDURE init
>      this.SetCallBack(.null.)    && set callback to default
>   PROCEDURE setcallback(oC as Variant)
>      IF ISNULL(oc)
>         && dummy instance that does nothing: virtual function
>         this.oc = NEWOBJECT("DemoEvents")
>      else
>         IF VARTYPE(oc) != 'O'
>            COMRETURNERROR(PROGRAM(),"callback must be obj")
>         ENDIF
>         this.oc = GETINTERFACE(oC,"iDemoEvents","idemo.idemo")
>      endif
>   procedure MyDoCmd(cCmd as String) as Variant
>      &cCmd
>   procedure MyEval(cExpr as String) as Variant
>      return &cExpr
>   procedure BuyStock(cStock as String, qty AS Number) as Boolean
>      this.oc.BeforeBuyStock(cStock, qty)
>      *here we buy the stock
>      this.oc.AfterBuyStock(cStock, qty)
>   FUNCTION Error(nError, cMethod, nLine)
>      COMreturnerror(cMethod+'  err#='+str(nError,5)+'  line='+;
>         str(nline,6)+' '+message(),_VFP.ServerName)
>      && this line is never executed
>
>enddefine
>
>*Just an interface definition that should be implemented by outside callers
>DEFINE CLASS DemoEvents as session olepublic
>   procedure BeforeBuyStock(cStock as String, qty AS Number)
>   procedure AfterBuyStock(cStock as String, qty AS Number)
>enddefine
>
>
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform