How To Implement Object With Events By Using ATL and eMbedded Visual Basic (300162)



The information in this article applies to:

  • Microsoft eMbedded Visual C++, Version: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.

MORE INFORMATION

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.
  1. Start the eMbedded Visual C++ integrated development environment.
  2. Create a new project by using the Windows CE ATL COM AppWizard.
  3. Name the project TestClass and click OK. Leave MFC support unselected and click Finish. Click OK in the New Project Information dialog box.
  4. From the Insert menu, click New ATL Object.
  5. In the ATL Object Wizard:
    1. From the list of Categories, click Objects.
    2. Under Objects, choose Simple Object.
    3. Click Next.
    4. 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.
    5. 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.
    6. Click OK.
    You should see the following in the ClassView of the testclass project:

    • _ISimpleEvents - an interface for event methods
    • CSimple - a class for the object interface
    • ISimple - an interface for the object
    • Globals - a project folder that shows global objects
  6. 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.
  7. 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.
  8. 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.
  9. 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.
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().
  1. Under the CSimple\ISimple branch of the project, double-click TestMethod(). This will open the source of the TestMethod() implementation.
  2. Add the code to call Fire_TestEvent(). It should look like this:
    STDMETHODIMP CSimple::TestMethod()
    {
    	Fire_TestEvent();
    	return S_OK;
    }
    					
  3. 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.
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.
  1. Start eMbedded Visual Basic and select Windows CE for the Pocket PC Project in the New Project dialog box. Click Open.
  2. Open the code window for the project and insert the following in the General Declarations section:
    Dim TestObject as Object
    					
  3. Open the load handler for the form object in the project code window to add the following:
    Set TestObject = CreateObjectWithEvents("testclass.simple", "T1_")
    					
  4. Create a handler subroutine for the event. As mentioned earlier, this must be done outside the .frm file in a .bas file.
    1. From the Project menu, click Add Module. On the New tab, select Module and click Open.
    2. Add the following subroutine:
      Sub T1_TestEvent()
          MsgBox ("Test Event Was Fired")
      End Sub
      						
    3. Save the .bas module with the file name Test.bas.
  5. Insert a command button on the form. Name the button cmdTest and type the caption Test the Event.
  6. 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()
    					
  7. Save the project. This will prompt you to save your form file and project file name.
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 "Test Event Was Fired".

Modification Type:MinorLast Reviewed:10/11/2004
Keywords:kbhowto KB300162