kutruta
asked on
CoInitializeEx problem
I have a simple COM object dll
skeletal description ...
class ATL_NO_VTABLE Cxxx :
public CComObjectRootEx<CComMulti ThreadMode l>,
public CComCoClass<Cxxx,
&CLSID_Cxxx>,
public Ixxx
{
public:
DECLARE_REGISTRY_RESOURCEI D(IDR_xxx)
DECLARE_NOT_AGGREGATABLE(C xxx)
DECLARE_PROTECT_FINAL_CONS TRUCT()
BEGIN_COM_MAP(Cxxx)
COM_INTERFACE_ENTRY(Ixxx)
END_COM_MAP()
STDMETHOD(AddNumbers)(/*[i n]*/ long Num1, /*[in]*/ long Num2, /*[out]*/ long *ReturnVal);
};
with this simple IDL interface ..
interface Ixxx : IUnknown
{
[helpstring("method AddNumbers")]
HRESULT AddNumbers([in] long Num1, [in] long Num2, [out] long *ReturnVal);
};
I can SUCCESSFULLY use the dll from a C++ snippet, by using CoInitializeEx or CoInitialize, like this
.
.
#define _WIN32_DCOM
#import "..\xxx.dll" no_namespace
#include "..\xxx_i.c"
.
.
HRESULT hr = CoInitializeEx(NULL,
COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE |
COINIT_SPEED_OVER_MEMORY);
// OR . . .HRESULT hr = CoInitialize(NULL);
if(SUCCEEDED(hr))
{
Ixxx* ixxx;
long x;
char temp[20];
hr = CoCreateInstance(CLSID_xxx ,
NULL,
CLSCTX_ALL,
IID_Ixxx, (void**) &ixxx);
if(SUCCEEDED(hr))
{
hr = ixxx->AddNumbers(5, 6, &x);
hr = ixxx->Release();
::MessageBox(NULL, ltoa(x, temp, 10), "", MB_OK);
}
}
CoUnInitialize();
.
.
.
But when I use CoInitializeEx in the way I wish to, (multithreaded) like this:
HRESULT hr = CoInitializeEx(NULL,
COINIT_MULTITHREADED |
COINIT_DISABLE_OLE1DDE |
COINIT_SPEED_OVER_MEMORY);
the CoCreateInstance fails.
Why is COINIT_MULTITHREADED causing this?
best regards .....
skeletal description ...
class ATL_NO_VTABLE Cxxx :
public CComObjectRootEx<CComMulti
public CComCoClass<Cxxx,
&CLSID_Cxxx>,
public Ixxx
{
public:
DECLARE_REGISTRY_RESOURCEI
DECLARE_NOT_AGGREGATABLE(C
DECLARE_PROTECT_FINAL_CONS
BEGIN_COM_MAP(Cxxx)
COM_INTERFACE_ENTRY(Ixxx)
END_COM_MAP()
STDMETHOD(AddNumbers)(/*[i
};
with this simple IDL interface ..
interface Ixxx : IUnknown
{
[helpstring("method AddNumbers")]
HRESULT AddNumbers([in] long Num1, [in] long Num2, [out] long *ReturnVal);
};
I can SUCCESSFULLY use the dll from a C++ snippet, by using CoInitializeEx or CoInitialize, like this
.
.
#define _WIN32_DCOM
#import "..\xxx.dll" no_namespace
#include "..\xxx_i.c"
.
.
HRESULT hr = CoInitializeEx(NULL,
COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE |
COINIT_SPEED_OVER_MEMORY);
// OR . . .HRESULT hr = CoInitialize(NULL);
if(SUCCEEDED(hr))
{
Ixxx* ixxx;
long x;
char temp[20];
hr = CoCreateInstance(CLSID_xxx
NULL,
CLSCTX_ALL,
IID_Ixxx, (void**) &ixxx);
if(SUCCEEDED(hr))
{
hr = ixxx->AddNumbers(5, 6, &x);
hr = ixxx->Release();
::MessageBox(NULL, ltoa(x, temp, 10), "", MB_OK);
}
}
CoUnInitialize();
.
.
.
But when I use CoInitializeEx in the way I wish to, (multithreaded) like this:
HRESULT hr = CoInitializeEx(NULL,
COINIT_MULTITHREADED |
COINIT_DISABLE_OLE1DDE |
COINIT_SPEED_OVER_MEMORY);
the CoCreateInstance fails.
Why is COINIT_MULTITHREADED causing this?
best regards .....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If that's so, you're stuck with the apartment you are in.
Joe