top
Loading...
在VC++編寫的組件中使用ASP對象
簡介
本文用一個簡單的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。

北斗有巢氏 有巢氏北斗