小弟发现:用atl向导生成的类,都无法直接new创建,要么就用CoCreateInstance。
在atl中如何生成可以new创建对象的类?(该类又实现了某个接口)
生成这种类对象的目的在于,该类对象并不需要client程序通过CoCreateInstance创建,而是在com server内部分配好, 把其实现的接口作为某个方法的返回值,返回给用户。所以希望能够用new 创建对象。
谢谢
8 个解决方案
#1
在atl中如何生成可以new创建对象的类?(该类又实现了某个接口)
梦里面 :)
你想想com原理呀,让你用new创建有接口的类!怎么可能?
梦里面 :)
你想想com原理呀,让你用new创建有接口的类!怎么可能?
#2
typedef CComObject<此处是ATL生成的类> CXXXX;
然后直接new
然后直接new
#3
to:realhill
不行啊,还是不能编译
不行啊,还是不能编译
#4
是这个样子的吗;
// .H文件
class ATL_NO_VTABLE CMailSummary :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CMailSummary, &CLSID_MailSummary>,
public IDispatchImpl<IMailSummary, &IID_IMailSummary, &LIBID_WEBMAILLib>
{
public:
CMailSummary()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MAILSUMMARY)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMailSummary)
COM_INTERFACE_ENTRY(IMailSummary)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
public:
typedef CEmailEx::XEMAILEX Recipient;
// IMailSummary
public:
STDMETHOD(get_Status)(/*[out, retval]*/ BSTR *pVal);
STDMETHOD(put_Status)(/*[in]*/ BSTR newVal);
};
typedef CComObject<CMailSummary> CMailSummaryEx;
//.CPP文件使用
CMailSummary*pSummary = new CMailSummaryEx();
// .H文件
class ATL_NO_VTABLE CMailSummary :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CMailSummary, &CLSID_MailSummary>,
public IDispatchImpl<IMailSummary, &IID_IMailSummary, &LIBID_WEBMAILLib>
{
public:
CMailSummary()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MAILSUMMARY)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMailSummary)
COM_INTERFACE_ENTRY(IMailSummary)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
public:
typedef CEmailEx::XEMAILEX Recipient;
// IMailSummary
public:
STDMETHOD(get_Status)(/*[out, retval]*/ BSTR *pVal);
STDMETHOD(put_Status)(/*[in]*/ BSTR newVal);
};
typedef CComObject<CMailSummary> CMailSummaryEx;
//.CPP文件使用
CMailSummary*pSummary = new CMailSummaryEx();
#5
谢谢,给分!
#6
紧急求教:
我用了上述方法创建对象后,返回出来,结果出错,程序异常退出
是在vb里使用的,
dim obj1 as new XXXX
dim obj2 as YYYY
set obj2 = obj1.Method1 <---执行这句就报错
请问这个问题怎么解决?
谢谢
我用了上述方法创建对象后,返回出来,结果出错,程序异常退出
是在vb里使用的,
dim obj1 as new XXXX
dim obj2 as YYYY
set obj2 = obj1.Method1 <---执行这句就报错
请问这个问题怎么解决?
谢谢
#7
使用方法如下:
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = NULL;
HRESULT hr = CMailSummaryEx::CreateInstance(&pex);
if(SUCCEEDED(hr))
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = NULL;
HRESULT hr = CMailSummaryEx::CreateInstance(&pex);
if(SUCCEEDED(hr))
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
#8
也可:
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = new CMailSummaryEx();
if(pex)
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = new CMailSummaryEx();
if(pex)
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
#1
在atl中如何生成可以new创建对象的类?(该类又实现了某个接口)
梦里面 :)
你想想com原理呀,让你用new创建有接口的类!怎么可能?
梦里面 :)
你想想com原理呀,让你用new创建有接口的类!怎么可能?
#2
typedef CComObject<此处是ATL生成的类> CXXXX;
然后直接new
然后直接new
#3
to:realhill
不行啊,还是不能编译
不行啊,还是不能编译
#4
是这个样子的吗;
// .H文件
class ATL_NO_VTABLE CMailSummary :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CMailSummary, &CLSID_MailSummary>,
public IDispatchImpl<IMailSummary, &IID_IMailSummary, &LIBID_WEBMAILLib>
{
public:
CMailSummary()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MAILSUMMARY)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMailSummary)
COM_INTERFACE_ENTRY(IMailSummary)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
public:
typedef CEmailEx::XEMAILEX Recipient;
// IMailSummary
public:
STDMETHOD(get_Status)(/*[out, retval]*/ BSTR *pVal);
STDMETHOD(put_Status)(/*[in]*/ BSTR newVal);
};
typedef CComObject<CMailSummary> CMailSummaryEx;
//.CPP文件使用
CMailSummary*pSummary = new CMailSummaryEx();
// .H文件
class ATL_NO_VTABLE CMailSummary :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CMailSummary, &CLSID_MailSummary>,
public IDispatchImpl<IMailSummary, &IID_IMailSummary, &LIBID_WEBMAILLib>
{
public:
CMailSummary()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_MAILSUMMARY)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CMailSummary)
COM_INTERFACE_ENTRY(IMailSummary)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
public:
typedef CEmailEx::XEMAILEX Recipient;
// IMailSummary
public:
STDMETHOD(get_Status)(/*[out, retval]*/ BSTR *pVal);
STDMETHOD(put_Status)(/*[in]*/ BSTR newVal);
};
typedef CComObject<CMailSummary> CMailSummaryEx;
//.CPP文件使用
CMailSummary*pSummary = new CMailSummaryEx();
#5
谢谢,给分!
#6
紧急求教:
我用了上述方法创建对象后,返回出来,结果出错,程序异常退出
是在vb里使用的,
dim obj1 as new XXXX
dim obj2 as YYYY
set obj2 = obj1.Method1 <---执行这句就报错
请问这个问题怎么解决?
谢谢
我用了上述方法创建对象后,返回出来,结果出错,程序异常退出
是在vb里使用的,
dim obj1 as new XXXX
dim obj2 as YYYY
set obj2 = obj1.Method1 <---执行这句就报错
请问这个问题怎么解决?
谢谢
#7
使用方法如下:
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = NULL;
HRESULT hr = CMailSummaryEx::CreateInstance(&pex);
if(SUCCEEDED(hr))
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = NULL;
HRESULT hr = CMailSummaryEx::CreateInstance(&pex);
if(SUCCEEDED(hr))
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
#8
也可:
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = new CMailSummaryEx();
if(pex)
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}
STDMETHODIMP CMail::GetSummary(IMailSummary **ppObj)
{
*ppObj = NULL;
CMailSummaryEx*pex = new CMailSummaryEx();
if(pex)
{
pex->AddRef();
pex->m_MailSummary = m_Mail.m_Summary;
pex->QueryInterface(IID_IMailSummary,(void**)ppObj);
pex->Release();
}
return S_OK;
}