HRESULT hr=E_FAIL;
CComPtr<IObjectContext>pObjContext;
hr=::GetObjectContext(&pObjContext);
    CComPtr<IRequest>pRequest=NULL;
CComPtr<ISessionObject>pSession=NULL;
if(SUCCEEDED(hr))
{
CComPtr<IGetContextProperties>pProps;
hr=pObjContext->QueryInterface(IID_IGetContextProperties,(void**)&pProps);
if(SUCCEEDED(hr))
{
CComBSTR bstrASPObj="Session";
CComVariant vt;
if(SUCCEEDED(pProps->GetProperty(bstrASPObj,&vt)))
{
if(V_VT(&vt)==VT_DISPATCH)
{
IDispatch *pDispatch=V_DISPATCH(&vt);
if(pDispatch)
{
hr=pDispatch->QueryInterface(IID_ISession,(void **)&pSession);
}
}
}
以后的代码不用写啦吧

解决方案 »

  1.   

    上面倒数第5行的IIS_ISession应为IID_ISession;
    下面是我的一个使用session对象的简单例子
    看看在参照MSDN应该可以解决问题的
    STDMETHODIMP CASPComp::GetSessionValue(BSTR bstrValue, VARIANT *pvar)
    {
    // TODO: Add your implementation code here
     HRESULT hr=E_FAIL;
    CComPtr<IObjectContext>pObjContext;
    hr=::GetObjectContext(&pObjContext);
       // CComPtr<IRequest>pRequest=NULL;
    CComPtr<ISessionObject>pSession=NULL;
    if(SUCCEEDED(hr))
    {
    CComPtr<IGetContextProperties>pProps;
    hr=pObjContext->QueryInterface(IID_IGetContextProperties,(void**)&pProps);
    if(SUCCEEDED(hr))
    {
    CComBSTR bstrASPObj="Session";
    CComVariant vt;
    if(SUCCEEDED(pProps->GetProperty(bstrASPObj,&vt)))
    {
    if(V_VT(&vt)==VT_DISPATCH)
    {
    IDispatch *pDispatch=V_DISPATCH(&vt);
    if(pDispatch)
    {
    hr=pDispatch->QueryInterface(IID_ISessionObject,(void **)&pSession);
    if(pSession)
    {

    pSession->get_Value(bstrValue,pvar);
    }
    }
                         
    }
    }
    }
    } return S_OK;
    }
    使用啦ISessionObject的get_value方法
    下面是接口的定义
    public interface asp.ISessionObject 
        extends com.ms.com.IUnknown
    {
               //Methods
        public abstract void Abandon();
        public abstract com.ms.com.Variant getValue(java.lang.String);
        public abstract void putValue(java.lang.String, com.ms.com.Variant);
        public abstract int getCodePage();
        public abstract void putCodePage(int);
        public abstract asp.IVariantDictionary getContents()
        public abstract int getLCID();
        public abstract asp.IVariantDictionary getStaticObjects();
        public abstract java.lang.String getSessionID();
        public abstract int getTimeout();
        public abstract void putTimeout(int);
        public abstract void putLCID(int);
    }