
|  | 
| 技术资料  > ASP技术 > 组件开发 : 在VC++ 编写的组件中使用ASP对象 |  | 在VC++ 编写的组件中使用ASP对象March 25,2004
 |  | 简介 本文用一个简单的sample演示如何在VC++ ATL编写的组件中调用我们熟悉的ASP对象(Request,Response,Session等等)。你会发现在 Visual C++中使用 ATL Object Wizard就可以达到我们的目的。但使用OnStartPage,OnEndPage事件并不是最佳方法。
 在IIS3.0中,组件能通过IscriptingContext 接口存取ASP内建对象,但这是通过页面级方法来存取内建对象。现在有了一种更好的实现方法,就是利用ObjectContext对象直接存取。ObjectContext使IIS应用有更好的扩展性,提供更完善的事务处理功能。强烈建议你把原有的应用转换到这种方式,但要求你的应用支持事务处理。
 代码
 首先需要包含一些必要的库文件。我们需要mtx.h来定义一些常量,通过mtxas.dll得到IobjectContext接口,通过asp.dll得到ASP对象。
 #include <mtx.h>
 #import "c:Winntsystem32mtsmtxas.dll"
 #import "c:Winntsystem32inetsrvasp.dll"
 
 然后,我们调入IobjectContext接口。
 MTxAS::ObjectContextPtr pObjectContext;
 HRESULT hr = GetObjectContext((IObjectContext**)
 &pObjectContext);
 
 下一步,通过context 对象得到我们需要的东西。这里举两个例子:session和response。
 //Session Object
 CComVariant v;
 CComBSTR bstr(L"Session");
 CComQIPtr<IGetContextProperties, &__uuidof
 (IGetContextProperties)> pProps(pObjectContext);
 hr = pProps->GetProperty(bstr, &v);
 CComPtr<IDispatch> pDisp;
 pDisp = V_DISPATCH(&v);
 CComQIPtr<ASPTypeLibrary::ISessionObject, &__uuidof
 (ASPTypeLibrary::ISessionObject)> pSession(pDisp);
 
 
 //Response Object
 CComVariant v;
 CComBSTR bstr(L"Response");
 CComQIPtr<IGetContextProperties, &__uuidof
 (IGetContextProperties)> pProps(pObjectContext);
 hr = pProps->GetProperty(bstr, &v);
 CComPtr<IDispatch> pDisp;
 pDisp = V_DISPATCH(&v);
 CComQIPtr<ASPTypeLibrary::IResponse, &__uuidof
 (ASPTypeLibrary::IResponse)> pResponse(pDisp);
 
 最后来一个使用这个对象得简单例子。
 //Retrieve a value from the Session Object.
 CComBSTR bstrVarName(L"TestSessionVar");
 VARIANT* pValue;
 pSession->get_Value(bstrVarName, pValue);
 
 //Write that value out to the browser.
 pResponse->Write(pValue);
 
 总结
 虽然这只是一个很简单的在VC++编写的组件中调用ASP 内建对象的例子,你可以按这个原理做更多的事情。Good luck。
 
 |  | 
  
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved