HOWTO: Create ATL COM Wrapper for ATL COM Component to be Used With ASP

ID: Q243667


The information in this article applies to:
  • Active Server Pages, included with:
    • Microsoft Internet Information Server version 4.0
  • Microsoft Visual C++, 32-bit Editions, version 6.0


SUMMARY

This article describes how to create an Active Template Library (ATL) Component Object Model (COM) wrapper component (client) for an existing ATL COM component that can be used with Active Server Pages (ASP).

To automate the ATL component you can use the #import directive. When the compiler encounters the #import directive, it produces wrapper classes for each interface described in the referenced component's type library.


MORE INFORMATION

  1. Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard.


  2. Name the project Component1, and click OK.


  3. Select server type Dynamic link Library, click Finish, and click OK.


  4. From the Insert menu, select New ATL Object.


  5. Select Simple Object from the Object category, and click Next.


  6. Enter Obj1 for the short name field.


  7. From the ClassView tab, select and right-click IObj1 interface from the ObjectContext Classes and select Add Method from the menu.


  8. Enter HelloWorld1 for the Method Name and [out, retval] BSTR *pval for parameters, and click OK.


  9. Implement the following code for the HelloWorld() method:


  10. 
    STDMETHODIMP CObj1::HelloWorld1(BSTR *pval)
    {
    	*pval =  SysAllocString(L"Hello World!"); 
    	return S_OK;
    } 
  11. From the Build menu select Build Component1.dll or press F7.


  12. Close Visual C++. Now you are ready to create your wrapper component.


  13. Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard.


  14. Name the project Component2 and click OK.


  15. Select server type Dynamic link Library, click Finish, and click OK.


  16. From the Insert menu select New ATL Object.


  17. Select Simple Object from the Object category, and click Next.


  18. Enter Obj2 for the short name field.


  19. From the ClassView tab select and right-click IObj2 interface from the ObjectContext Classes and select Add Method from the menu.


  20. Enter HelloWorld2 for the Method Name and [out, retval] BSTR *pval for parameters, and click OK.


  21. Implement the following code for the HelloWorld2() method:


  22. 
    #import "C:\component1.dll" no_namespace named_guids 'enter correct path to dll
    
    STDMETHODIMP CObj2::HelloWorld2(BSTR *pval)
    {
    	IObj1Ptr	pTemp; //Interface pointer
    	HRESULT hr = pTemp.CreateInstance(CLSID_Obj1); 
    
    	if (SUCCEEDED(hr))
    		*pval = pTemp->HelloWorld1(); 
    	return hr;
    } 
  23. From the Build menu, select Build Component2.dll or press F7.


  24. Copy and paste the following script into a new ASP file, and save the ASP file in a virtual directory:


  25. 
    <%
        Option Explicit
        Dim Obj
        Set Obj = Server.CreateObject("Component2.Obj2")
        Response.Write Obj.HelloWorld2
        Set Obj = Nothing
    %> 
  26. View the ASP file in a browser, and you'll see the following:
    
    Hello World! 


Additional query words:

Keywords : kbASP kbATL kbCOMt kbVC600
Version : winnt:4.0,6.0
Platform : winnt
Issue type : kbhowto


Last Reviewed: November 19, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.