Hi, I have an exe com. I have two questions. 1. After I create an instance: IXserverPtr _pServer; _pServer.CreateInstance(CLSID_Xserver); Do I need to release like this _pServer.Release() ? 2. Do I need CoInitialize(NULL) before CreateInstance? Why it still works if I don't have CoInitilzie? Thanks, Jean
ZhenCai wrote: > I have an exe com. I have two questions. > > 1. After I create an instance: > > IXserverPtr _pServer; > _pServer.CreateInstance(CLSID_Xserver); > > Do I need to release like this _pServer.Release() ? You can, but you don't have to. IXserverPtr is a smart pointer class, it will automatically release the underlying interface pointer in its destructor when _pServer goes out of scope. > 2. Do I need CoInitialize(NULL) before CreateInstance? Yes. > Why it still > works if I don't have CoInitilzie? Are you sure your program does not call CoInitialize anywhere? Depending on the framework you use, and the wizard you used to originally generate the project (if any), chances are high there is a CoInitialize call somewhere. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Thanks for reply. For #2, I searched all code, i could not fine CoInitialize
Proposal for experiment: You can call CoInitializeEx() in two versions of your program with (a) APARTMENT_THREADED and (b) MULTITHREADED before you create instance. If both calls retrurn with S_OK, no CoInitialize has been called before. In that case the framework that implements the smartpointer calls probably somehow CoInitializeEx. But I expect, that one calls returns S_FALSE, so any object that you create is derived from a class that calls CoInitializeEx. It's not necessary to find the call in your source code. /Helge