I am learning COM through C++. From MSDN:
Applications are required to use CoInitializeEx before they make any other COM library calls except for memory allocation functions.
The memory allocation functions is CoTaskMemAlloc
and CoTaskMemFree
in my opinion.
But I see, my "Hello World" works fine with and without the CoInitializeEx
and CoUninitialize
functions calling. In my code I use the StringFromCLSID
function which is declared in the combaseapi.h
header. So, it is a COM function in my opinion. My code:
/* entry_point.cpp */
#include "Tools.h"
#include <objbase.h>
int main(){
HRESULT hr = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr)){
trace("Can't initialize COM for using in the current thread.");
keep_window_opened();
return 1;
}
// {D434CF7D-2CDD-457A-A4EF-5822D629CE83}
static const CLSID clsid =
{ 0xd434cf7d, 0x2cdd, 0x457a, {
0xa4, 0xef, 0x58, 0x22, 0xd6, 0x29, 0xce, 0x83 } };
const size_t SIZE = 39;
wchar_t* wch = nullptr;
hr = ::StringFromCLSID(clsid, &wch);
if (FAILED(hr)){
trace("Can't convert CLSID to wchar_t array.");
}
else{
trace("CLSID converted to wchar_t array.");
char mch[SIZE];
size_t count = 0;
int result = ::wcstombs_s(&count, mch, wch, SIZE);
if (result){
trace("Can't convert wchar_t array to char array.");
}
else{
trace(mch);
}
::CoTaskMemFree(wch);
}
::CoUninitialize();
keep_window_opened();
return 0;
}
If I remove the calls of CoInitializeEx
and CoUninitialize
functions, then my code works still. I expected it will not work...
Why StringFromCLSID
work even without the calling of CoInitializeEx
before?
Thank you.