How To Create ATL COM Wrapper for ATL COM Component to Be Used With ASP (243667)



The information in this article applies to:

  • Microsoft Active Server Pages, when used with:
    • Microsoft Internet Information Server 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q243667

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:
    STDMETHODIMP CObj1::HelloWorld1(BSTR *pval)
    {
    	*pval =  SysAllocString(L"Hello World!"); 
    	return S_OK;
    }
    					
  10. From the Build menu select Build Component1.dll or press F7.
  11. Close Visual C++. Now you are ready to create your wrapper component.
  12. Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard.
  13. Name the project Component2 and click OK.
  14. Select server type Dynamic link Library, click Finish, and click OK.
  15. From the Insert menu select New ATL Object.
  16. Select Simple Object from the Object category, and click Next.
  17. Enter Obj2 for the short name field.
  18. From the ClassView tab select and right-click IObj2 interface from the ObjectContext Classes and select Add Method from the menu.
  19. Enter HelloWorld2 for the Method Name and [out, retval] BSTR *pval for parameters, and click OK.
  20. Implement the following code for the HelloWorld2() method:
    #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;
    }
    					
  21. From the Build menu, select Build Component2.dll or press F7.
  22. Copy and paste the following script into a new ASP file, and save the ASP file in a virtual directory:
    <%
        Option Explicit
        Dim Obj
        Set Obj = Server.CreateObject("Component2.Obj2")
        Response.Write Obj.HelloWorld2
        Set Obj = Nothing
    %>
    					
  23. View the ASP file in a browser, and you'll see the following:

    Hello World!
    							


Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbCodeSnippet kbhowto KB243667