I assume that under the hood NEWCOM does "CoInitialize" and "CoCreateInstance" to create an instance of the COM class or object. So how long is the life of the instance? Does it depend on the life of the varb pointing to the instance, i. e. local or global?
Announcement
Collapse
No announcement yet.
Life of NEWCOM
Collapse
X
-
> I assume that under the hood NEWCOM does "CoInitialize" and "CoCreateInstance" to create an instance of the COM class or object.
Wrong assumption. NEWCOM only does CoCreateInstance. The compiler does CoInitialize when the program starts and CoUninitialize when it ends.
> So how long is the life of the instance? Does it depend on the life of the varb pointing to the instance, i. e. local or global?
If it is global or static, until the program ends; if it is local, until the variable goes out of scope. But you can "kill" it at any time setting it to NOTHING.
-
Thank you, Mr. Roca.
The reason I asked is I have seen C/C++ code that uses coinitialize(), and cocreateinstance to work with ISHELLLINK and IPERSISTFILE.
At the end of the code it does:
iPersistFile->Release();
iShellLink->Release();
couninitalize()
However, I have not been able to find release methods for IPERSISTFILE and ISHELLLINK.
Walt Decker
Comment
-
The reason I asked is I have seen C/C++ code that uses coinitialize(), and cocreateinstance to work with ISHELLLINK and IPERSISTFILE.
At the end of the code it does:
iPersistFile->Release();
iShellLink->Release();
couninitalize()
However, I have not been able to find release methods for IPERSISTFILE and ISHELLLINK.
iPersistFile = NOTHING
iShellLink = NOTHING
But if the program is going to end or iPersistFile and iShellLink are local, you can omit it.
PB has always tried to imitate VB6.
Comment
-
I believe PB also calls CoInitialize on THREAD CREATE . (Must be called on each thread using COM objects).
Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
That sounds reasonable....
From the Microsoft API doc for CoInitializeEx() ...
RemarksCoInitializeEx must be called at least once, and is usually called only once, for each thread that uses the COM library. Multiple calls to CoInitializeEx by the same thread are allowed as long as they pass the same concurrency flag, but subsequent valid calls return S_FALSE. To close the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize.
Note You must include the #define _WIN32_DCOM preprocessor directive at the beginning of your code to be able to use CoInitializeEx.
A thread must call CoInitializeEx or CoInitialize before calling any other COM library function except the CoGetMalloc function and other memory allocation calls (CoTaskMemAlloc, CoTaskMemFree, CoTaskMemReAlloc, and the IMalloc methods on the task allocation supplied by CoGetMalloc).
.....Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
Comment