求用ATL编写的单实例(DLL)服务器的原代码的例子?

Dev 2000-05-19 01:59:00
加精
本人是新手,刚学会点击鼠标,请高手能否发一个用ATL编写的单实例(DLL)服务器的原代码的例子(最好是能编译的工程)给我研究一下,当然能给些指点最好了?
...全文
607 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuating03 2000-08-14
  • 打赏
  • 举报
回复
自己找本ATL摸板库参考手册,上面很多列子。
michaell 2000-05-30
  • 打赏
  • 举报
回复
hi,

would you like to give me a COM object example.


janna_cn@yahoo.com
mic
michaell 2000-05-30
  • 打赏
  • 举报
回复
hi,

would you like to give me a COM object example.

mic
royluo 2000-05-25
  • 打赏
  • 举报
回复
Softdoctor,我实验过了你所说的方法,很有效

另外,我看到直接使用CComClassFactorySingleton
也可以达到相同的效果。
用法非常简单,是直接
加一个宏DECLARE_CLASSFACTORY_SINGLETON就可以了
当然,实际上,它的原理和你的思路是一样的

class CMyClass : ..., public CComCoClass< ... >
{
public:
DECLARE_CLASSFACTORY_SINGLETON(CMyClass)

...
};

另外在MSDN中
有一篇“HOWTO: Alternative Implementation of ATL Singleton”
提供了另外一种替代的方法
This article shows an implementation of singletons that differs from the default ATL implementation in the following ways:



The lifetime of an ATL singleton is tied to the class factory; the singleton gets destroyed only when the class factory is destroyed, which is when the EXE/DLL unloads [CComModule::RevokeClassObject() and CComModule::Term()]. The singleton implementation in this article maintains a refcount and deletes itself when it goes to zero.


Implementing singletons in a DLL can be problematic. One scenario is if your singleton object marked "Apartment", is in a DLL. The client creates two STA threads, each one creates your singleton object. Both STA threads have the same raw pointer to your singleton, allowing them to simultaneously call into the singleton. You are probably not synchronizing access to instance data in your singleton object, because according to COM rules, you don't need to synchronize access to instance data for objects marked "Apartment". It's only a matter of time before data is corrupted.

The singleton implementation in this article works around this problem by returning a marshaled pointer in IClassFactory::CreateInstance(). But marshaling the pointer introduces another potential problem. All calls are now marshaled to the first Apartment that calls IClassFactory::CreateInstance. If the first Apartment goes away, calls to the singleton from other Apartments will fail. If you use the singleton implementation in this article in a DLL marked "Apartment", you must ensure that the first Apartment that uses the component stays alive. Because of all these potential problems, you should only use singletons in a DLL when absolutely necessary. If you just want to share data in the DLL within the same process, an alternative is to create global/static variables to hold the data. You just need to synchronize access to this data.



Dev 2000-05-22
  • 打赏
  • 举报
回复
softdoctor,我觉的你讲的很在行,如能给我EMAIL( zt.my@netease.com )个简单工程,或指出那有例子下载,分即全数送出,我学COM也就十来天,又要急于写程序。
softdoctor 2000-05-19
  • 打赏
  • 举报
回复
要想单实例,必须控制IClassFactory接口。
在你的对象类声明里
加入
DECLARE_CLASSFACTORY_EX(CMyClassFactory)
CMyClassFactory是你从CComClassFactory继承来的。
重载CreateInstance函数。
CreateInstance函数如下
HRESULT CMyClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
REFIID riid, void** ppvObj)
{
_ASSERTE(m_pfnCreateInstance != NULL); HRESULT hRes = E_POINTER;
static void * pObj = NULL; // 用于保存实例指针
// 如果实例已存在直接返回
if(pObj)
{
*ppvObj = pObj;
((LPUNKNOWN)pObj)->AddRef();
hRes = S_OK;
}
else // 不存在创建新的
{
hRes = m_pfnCreateInstance(pUnkOuter, riid, &pObj);
*ppvObj = pObj;
}
return hRes;
}

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧