Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 CoverYourASP --> ATL COM under ASP --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
11 active users
1052 visitors today
1070 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2012 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

If you've read Part 1, you'll have seen how easy it was to use the ATL AppWizard to write a C++ COM component.

But, of course, I didn't tell the whole story. As always, the code produced by wizards does the job, and allows you to rapidly prototype, but you still need to "make the code your own" - remember that it's still your code.

One of the first things I noticed when I started reading this book was that the AppWizard's use of the ScriptingContext object had already been deprecated by Microsoft!.

[By the way, it's an excellent book. It covers building COM components with VB and C++ (both ends of the spectrum!), and has a lot of technical information in it. Every programmer needs to know COM.]

The ScriptingContext object.

To understand the problem we first have to understand what the ScriptingContext object is.

The ScriptingContext object is the global object that contains the "ASP object model" as you know it. Rather than typing..

ScriptingContext.Response.Write('Hi');

..in an ASP script, we are used to using..

Response.Write('Hi');

In our components we still may need access to the Response and Server objects, but as we can now see, we first need a ScriptingContext. Luckily, from IIS3 onwards, when ASP encounters a Server.CreateObject on your page it asks the component for a special method called OnStartPage.

(if you were paying attention in Part 1, step 6, the ATL AppWizard asked you if you wanted to create this method, and only gave you the option of using the ASP objects if you chose Yes)

If the component has an OnStartPage method it is called and passed a pointer to an IUnknown interface that can be queried for a ScriptingContext object. This is still true today with IIS5, even though the ScriptingContext object has been deprecated by Microsoft since IIS4.

The AppWizard generated code then uses ScriptingContext methods such as get_Request to get access to the intrinsic ASP objects:

(Note that for clarity I have omitted any error handling code - not something you should do in the real world!)

CComPtr<IScriptingContext> spContext;

// Get the IScriptingContext Interface
pUnk->QueryInterface(\
   IID_IScriptingContext,\
   reinterpret_cast<void **>\
   (&spContext));

// Get Request Object Pointer
CComPtr<IRequest> piRequest;

spContext->get_Request(&piRequest);

The ObjectContext object.

Since IIS4 merged with Microsoft's Transaction Server (MTS) the ObjectContext object has been the preferred method of gaining access to the ASP intrinsic objects such as Response and Request.

The problem is that the ATL AppWizard doesn't support the ObjectContext object. The answer is to call the global function GetContextObject() which will return a pointer to an IObjectContext interface:

CComPtr<IObjectContext> spContext;

// Get the IObjectContext Interface
GetObjectContext(&spObjectContext);

// Get the Response Object
CComVariant vInterface;

spContext->GetProperty("Response",\
   &vInterface);

IResponse* pResponse;

vInterface.pdispVal->QueryInterface(\
   __uuidof(IResponse),\
   reinterpret_cast<void**>\
   (&pResponse));

Conclusions

Unless you're running on IIS3, which surely is unlikely, you'll need to do some "real" programming after using the ATL AppWizard!

Buying this book will make it easier.

Featured sponsor
My favorite resources

Tiki Statues - Tiki Masks - Tiki Totems


CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!