Microsoft KB Archive/300162

= How To Implement Object With Events By Using ATL and eMbedded Visual Basic =

Article ID: 300162

Article Last Modified on 10/11/2004

-

APPLIES TO


 * Microsoft eMbedded Visual C++ 4.0
 * Microsoft eMbedded Visual Basic 3.0

-



This article was previously published under Q300162



SUMMARY
In order to use an ActiveX object to implement an application, it is often necessary for the ActiveX object to use events to call back into the container application. This article shows how to implement a simple ActiveX object that exposes an event and use that object in an eMbedded Visual Basic application.



TestClass.Simple
This section provides the procedure for creating an ActiveX object that exposes events. To demonstrate this, there is a test method that will fire the event. In real-life scenarios, some other outside activity may cause the event, such as the object receiving notification.  Start the eMbedded Visual C++ integrated development environment. Create a new project by using the Windows CE ATL COM AppWizard. Name the project TestClass and click OK. Leave MFC support unselected and click Finish. Click OK in the New Project Information dialog box. From the Insert menu, click New ATL Object. In the ATL Object Wizard:  From the list of Categories, click Objects. Under Objects, choose Simple Object.</li> Click Next.</li> On the Names tab enter a short name: Simple. This fills in the names for the rest of the options. You can use the defaults.</li> On the Attributes tab, select Support Connection Points. This will include the events interface to allow for events to be fired and handled in the container.</li> Click OK.</li></ol>

You should see the following in the ClassView of the testclass project:

<ul> _ISimpleEvents - an interface for event methods</li> CSimple - a class for the object interface</li> ISimple - an interface for the object</li> Globals - a project folder that shows global objects</li></ul> </li> Add a method to the ISimple interface by right-clicking ISimple and selecting Add method from the context menu. Name the new method TestMethod. This will create the interface method and generate the code for the method.</li> Add a method to the _ISimpleEvents interface and name it TestEvent. This merely creates the event method in the interface. It does not generate the fire event code.</li> Build the control. It is necessary to build at this point so that the tools create a type library for the object to be used in the following steps.</li> Right-click the CSimple class and select Implement Connection Point from the context menu. Select _ISimpleEvents interface from the list of interfaces and click OK. This will add a CProxy_ISimpleEvents<class T> template to the project.</li></ol>

You should now see the following hierarchy in the ClassView of the testclass project:
 * _ISimpleEvents
 * TestEvent
 * CProxy_ISimpleEvents<class T>
 * Fire_TestEvent
 * CSimple
 * ISimple
 * TestMethod
 * CSimple
 * ISimple TestMethod

The following steps add code to enable you to test the firing of the events. In a real-life scenario some outside occurrence of an event would be detected in the ActiveX object and the implementation would call Fire_TestEvent. But for this example, TestMethod will call Fire_TestEvent.

<ol> Under the CSimple\ISimple branch of the project, double-click TestMethod. This will open the source of the TestMethod implementation.</li>  Add the code to call Fire_TestEvent. It should look like this: STDMETHODIMP CSimple::TestMethod {   Fire_TestEvent; return S_OK; }                   </li> That's all there is to this example. Build and download the testclass DLL object to your device. After eMbedded Visual C++ downloads the DLL, it will automatically register the ActiveX object.</li></ol>

You can use this procedure for Windows CE 2.11 devices or for Windows CE 3.0 devices. This procedure was created and tested on the Pocket PC.

The Visual Basic Test Container Application
This section demonstrates the testclass ActiveX object that is used by an eMbedded Visual Basic application. A confident eMbedded Visual Basic programmer may be inclined to skip this part but there are a couple things to remember. One is that you must instantiate the object by using CreateObjectWithEvents. The other is that your event handler code cannot be in an .frm file; it must be implemented in a separate .bas module. <ol> <li>Start eMbedded Visual Basic and select Windows CE for the Pocket PC Project in the New Project dialog box. Click Open.</li> <li> Open the code window for the project and insert the following in the General Declarations section: Dim TestObject as Object </li> <li> Open the load handler for the form object in the project code window to add the following: Set TestObject = CreateObjectWithEvents(&quot;testclass.simple&quot;, &quot;T1_&quot;) </li> <li>Create a handler subroutine for the event. As mentioned earlier, this must be done outside the .frm file in a .bas file. <ol style="list-style-type: lower-alpha;"> <li>From the Project menu, click Add Module. On the New tab, select Module and click Open.</li> <li> Add the following subroutine: Sub T1_TestEvent MsgBox (&quot;Test Event Was Fired&quot;) End Sub </li> <li>Save the .bas module with the file name Test.bas .</li></ol> </li> <li>Insert a command button on the form. Name the button cmdTest and type the caption Test the Event .</li> <li> Double-click the command button on the form to insert a click handler in the form code. In the cmd_TestClick subroutine, add the following: Dim rVal rVal = TestObject.TestMethod </li> <li>Save the project. This will prompt you to save your form file and project file name.</li></ol>

If your host development computer is connected to the device, you can try the test eMbedded Visual Basic application that uses the testclass ActiveX object.
 * 1) By default your eMbedded Visual Basic project may be set up to target the emulator. To change this, from the Project menu select your project's Properties. In this dialog box set the Run on Target to Pocket PC (default device). Click OK.
 * 2) From the Run menu, click Execute.
 * 3) When the eMbedded Visual Basic test application is downloaded to the device and runs, click the Test the Event button. A message box should appear that says &quot;Test Event Was Fired&quot;.

Additional query words: eVB eVC ATL event method

Keywords: kbhowto KB300162

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.