Microsoft KB Archive/243667

= How To Create ATL COM Wrapper for ATL COM Component to Be Used With ASP =

Article ID: 243667

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0, when used with:
 * Microsoft Internet Information Server 4.0
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



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
 Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard. Name the project Component1, and click OK. Select server type Dynamic link Library, click Finish, and click OK. From the Insert menu, select New ATL Object. Select Simple Object from the Object category, and click Next. Enter Obj1 for the short name field. From the ClassView tab, select and right-click IObj1 interface from the ObjectContext Classes and select Add Method from the menu.</li> Enter HelloWorld1 for the Method Name and [out, retval] BSTR *pval for parameters, and click OK.</li>  Implement the following code for the HelloWorld method: STDMETHODIMP CObj1::HelloWorld1(BSTR *pval) {   *pval =  SysAllocString(L"Hello World!"); return S_OK; }                   </li> From the Build menu select Build Component1.dll or press F7.</li> Close Visual C++. Now you are ready to create your wrapper component.</li> Open Visual C++ 6.0, create a new project, and select ATL COM AppWizard.</li> Name the project Component2 and click OK.</li> Select server type Dynamic link Library, click Finish, and click OK.</li> From the Insert menu select New ATL Object.</li> Select Simple Object from the Object category, and click Next.</li> Enter Obj2 for the short name field.</li> From the ClassView tab select and right-click IObj2 interface from the ObjectContext Classes and select Add Method from the menu.</li> Enter HelloWorld2 for the Method Name and [out, retval] BSTR *pval for parameters, and click OK.</li>  Implement the following code for the HelloWorld2 method:
 * 1) 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; }                   </li> From the Build menu, select Build Component2.dll or press F7.</li>  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 %>                   </li> <li>View the ASP file in a browser, and you'll see the following:

<pre class="fixed_text">Hello World!

</li></ol>

Keywords: kbhowto kbcodesnippet KB243667

-

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

© Microsoft Corporation. All rights reserved.