在VC++ 編寫的組件中使用ASP對象

來源:互聯網
上載者:User
簡介
本文用一個簡單的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。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.