ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
VBScript Classes: Part 1 of N
Migrating to ASP.NET
Getting Scripts to Run on a Schedule

Use Descriptive Naming Of Files And Directories
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Writing Objects Returning Other Objects
By Peter Persits
Copyright (c) 1999 Persits Software, Inc.
This articles demonstrates how to write an object which returns another object. Our component (which we will call ParentChild) will host two objects, Parent and Child. The Parent object will be createable via the Server.CreateObject statement. The Child object, on the other hand, will not have a ProgID of its own. It will be created by calling Parent's method CreateChild, as follows:
Set Parent = Server.CreateObject("ParentChild.Parent")
Set Child = Parent.CreateChild

Fire up your Visual C++, go to File/New, select ATL COM AppWizard, on the Projects tab, enter ParentChild under Project name, and specify a location for your poject under Location. On the next screen, accept default settings and click Finish.
Once the project has been created, go to Insert/Insert New ATL object. Select Simple Object and click Next. In the Short Name box, type in Parent and accept all the default values by clicking OK. Repeat this procedure. This time enter Child in the Short Name box. Your component now has two classes, CParent and CChild.
Let's strip the CChild object of everything that allows it to be created via  Server.CreateObject.
1. On the ResourceView tab, delete the IDR_CHILD registry entry under the "REGISTRY" category.
2. On the FileView tab, delete the Child.rgs entry under Resource Files.
3. Open the file Child.h. Delete the following lines:
public CComCoClass<CCHILD, &CLSID_Child>,

4. Open the file ParentChild.cpp. Delete the line
 5. Open the file ParentChild.idl. Delete the lines
  helpstring("Child Class")
coclass Child
  [default] interface IChild;

In the same file, add the following line right after the #import statements at the top:
interface IChild;
You can now compile the file to make sure all the changes were performed correctly.


On the ClassView tab, right-click on an IParent node and select Add Method. Under Method name, type CreateChild. Under Parameters, type
[out, retval]IChild ** ppVal
Click OK to add the method to the Parent class.
Open the file Parent.cpp. Add the line
#include "Child.h"
underneath other #include statements.
In the body of the CreateChild methods, add the following lines:
CComObject<CChild> * pChild;
CComObject<CChild>::CreateInstance( &pChild );
return pChild->QueryInterface( IID_IChild, reinterpret_cast<void **>(ppVal) );



We need to add a property or method to the Child class to make sure the Child object does indeed get created. Right-click on an IChild node in ClassView and select "Add Property". We will add the propery Name of the type BSTR. Uncheck the "Put Function" checkbox to make the property read-only, then click OK.
Open the file Child.cpp. In the body of CChild::get_Name, insert the line
* pVal = ::SysAllocString( L"Child");

right before return S_OK; Compile the project. We are now ready to test our component with the following simple script:
Set Parent = Server.CreateObject("ParentChild.Parent")
Set Child = Parent.CreateChild
Response.Write Child.Name

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers