Lab 9: Creating the STUpload Database Access Component

In this lab you will return to the STUpload project. You will create an in-process COM server called STLoadData.dll, which hosts the UploadStockData component. This component exposes the IUploadStockData interface, which contains three methods: ConnectToDatabase(), Disconnect(), and UploadRecord().

Start by creating the STLoadData project within the STUpload workspace.

  • To create the STLoadData project
    1. Open the STUpload workspace. From the File menu, select New.
    2. In the New dialog box, click the Projects tab. Select the ATL COM AppWizard project category.
    3. Type STLoadData in the Project name box. Select Add to current workspace and click OK.
    4. In Step 1 of the ATL COM AppWizard click Finish, and then click OK to create the new project.
    5. The STLoadData project appears in ClassView. The project name appears in bold text, signifying that STLoadData is the active project. Make sure that you have the full Build toolbar displayed rather than the Build minibar. This enables you to switch between projects easily by using the project drop-down list box.
    6. Right-click the STLoadData project in ClassView. On the shortcut menu, select New ATL Object.
    7. In the ATL Object Wizard dialog box, select the Objects item in the Category list. In the Objects box, select the Simple Object icon, and then click Next.
    8. In the ATL Object Wizard Properties dialog box select the Names tab. In the Short Name box, type UploadStockData.
    9. On the Attributes page, set the following attributes:
      • Under Threading Model, select Apartment.
      • Under Interface, select Dual.
      • Under Aggregation, select No.

    10. Ensure that all check boxes are cleared, and then click OK to add the UploadStockData object.

  • To add the IUploadStockData methods
    1. In ClassView, expand the STLoadData classes item. Right-click the IUploadStockData interface.
    2. On the shortcut menu, click Add Method.
    3. In the Return Type box, select HRESULT.
    4. In the Method Name box, type UploadRecord.
    5. In the Parameters box, enter the following code:
    6. [in] BSTR fund, [in] DATE date, [in] double price, [in] BSTR uplBy,  [in] DATE uplDate

    7. Repeat the procedure to add methods with the following signatures:
    8. HRESULT ConnectToDatabase()

      and

      HRESULT Disconnect()

    You will implement these methods in Lab 11 because they themselves are clients of other COM components (the ADO library).

    Take a look at the STLoadData.idl file. Note particularly the interface definition:

    [      object,      uuid(241A7771-6888-11D3-934F-0080C7FA0C3E),      dual,      helpstring("IUploadStockData Interface"),      pointer_default(unique) ] interface IUploadStockData : Idispatch {      [id(1), helpstring("method UploadRecord")]       HRESULT UploadRecord([in] BSTR fund, [in] DATE date,            [in] double price, [in] BSTR uplBy,            [in] DATE uplDate);      [id(2), helpstring("method ConnectToDatabase")]       HRESULT ConnectToDatabase();      [id(3), helpstring("method Disconnect")]       HRESULT Disconnect(); };

    Note how the IUploadStockData interface inherits from the IDispatch interface. This is how ATL implements a dual interface. Scripting clients will be able to use the UploadStockData component through the dispatch interface. Visual C++ and Visual Basic clients will be able to connect directly to the component's vtable.



    Microsoft Press - Desktop Applications with Microsoft Visual C++ 6. 0. MCSD Training Kit
    Desktop Applications with Microsoft Visual C++ 6.0 MCSD Training Kit
    ISBN: 0735607958
    EAN: 2147483647
    Year: 1999
    Pages: 95

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net