![]() | * 3 Months FREE * CLICK HERE! | |
| CoverYourASP --> ATL COM under ASP --> Part 2 | ||
| 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.
[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 // Get Request Object Pointer 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 // Get the Response Object spContext->GetProperty("Response",\ IResponse* pResponse; vInterface.pdispVal->QueryInterface(\ ConclusionsUnless 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. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||