Click here to Skip to main content
15,910,083 members
Home / Discussions / COM
   

COM

 
GeneralRe: Forward declaration of interfaces Pin
arun140529-Oct-03 2:18
arun140529-Oct-03 2:18 
GeneralRe: Forward declaration of interfaces Pin
Rowan Seymour29-Oct-03 2:58
Rowan Seymour29-Oct-03 2:58 
GeneralIE Automation Pin
nikhilkhedkar8-Sep-03 21:08
nikhilkhedkar8-Sep-03 21:08 
GeneralRe: IE Automation Pin
nikhilkhedkar9-Sep-03 2:18
nikhilkhedkar9-Sep-03 2:18 
GeneralStack messed up upon function returned Pin
DionChen8-Sep-03 8:04
DionChen8-Sep-03 8:04 
QuestionHow to pass VBA Collection to COM DLL from .NET C#? Pin
ajkumar4-Sep-03 15:21
ajkumar4-Sep-03 15:21 
Generalpassing a pointer into C++ from VB Pin
rajdawg3-Sep-03 16:31
rajdawg3-Sep-03 16:31 
GeneralRe: passing a pointer into C++ from VB Pin
Lim Bio Liong18-Sep-03 7:51
Lim Bio Liong18-Sep-03 7:51 
Hello rajdawg,

The problem you are facing here is most likely caused by multi-inheritance. Note well that a pointer to a class (e.g. _clsAccount) is NOT necessarily the same as a pointer to an interface that the same class implements (e.g. IclsAccount). More on this later.

I successfully re-created a crash using my own code. I created my own "GetAccount()" function and my own "ObjPtr()" function (both in C++). However, the value returned from my ObjPtr() and the value of the "pAcct" parameter were the same. I believe this has got to do with the way my "ObjPtr()" function is implemented (code is provided below).

long __stdcall ObjPtr(LPDISPATCH lpDispatch)
{
IClsAccount* pIClsAccount = NULL;
long lRet = 0;

lpDispatch -> QueryInterface(IID_IClsAccount, (void**)&pIClsAccount);

lRet = (long)pIClsAccount;

pIClsAccount -> Release();
pIClsAccount = NULL;

return lRet;
}


My crash occurred at the exact same place (i.e. when put_AccountName() was called). The reason for MY crash is due to the face that when the "GetAccount()" function is invoked, and the "pAcct" parameter is given its value, "pAcct" points to a _clsAccount object (and NOT a IclsAccount interface).

Now, as I mentioned right at the top, a _clsAccount pointer is NOT equivalent to a IclsAccount interface pointer. On some occasion, their values are indeed the same. But on other occasions, especially when your class implements several interfaces via multiple-inheritance, they are not the same. For more details, refer to "Inside COM" by Dale Rogerson (Microsoft Press).

Hence, there are two ways to overcome your problem, depending on what ObjPtr() returns :

void __stdcall GetAccount(_clsAccount* pAcct)
{
IClsAccount* pIClsAccount = NULL;
HRESULT hr = CoInitialize(0); //initializes COM library

if (SUCCEEDED(hr))
{
pAcct -> QueryInterface(IID_IClsAccount, (void**)&pIClsAccount);

_bstr_t bstr1(_T("TestInfo")); //use constructor to enter string

if (pIClsAccount)
{
pIClsAccount -> put_AccountName(bstr1);
pIClsAccount -> Release();
pIClsAccount = NULL;
}

CoUninitialize();
}
}

The above method is suitable when ObjPtr() returns a pointer to a _clsAccount class object.

This method takes the _clsAccount pointer and calls the QueryInterface() method to obtain a proper pointer to a IClsAccount interface. With a valid pointer to a IClsAccount interface, calling its put_AccountName() method goes fine.

Note that calling the QueryInterface() method from a _clsAccount pointer will succeed due to the fact that it is a common IUnknown method and will therefore be valid even from a _clsAccount pointer (see "Inside COM" for more details on this).

A second method is described below :

void __stdcall GetAccount(_clsAccount* pAcct)
{
HRESULT hr = CoInitialize(0); //initializes COM library

if (SUCCEEDED(hr))
{
_clsAccount* p_clsAccount= NULL;
_bstr_t bstr1(_T("TestInfo")); //use constructor to enter string

p_clsAccount = dynamic_cast<_clsAccount*>(pAcct);

p_clsAccount -> put_AccountName(bstr1);

CoUninitialize();
}
}

The above method is suitable if "pAcct" actually points to a IclsAccount interface.

This method takes "pAcct" (an IclsAccount interface pointer) and dynamically casts it to a pointer to a _clsAccount object. In my code, the casted pointer ("p_clsAccount") becomes a valid pointer to a _clsAccount object and with this pointer, any of class methods can be called, including methods of any interface that the class implements.

Hence alot depends on how ObjPtr() processes and returns its value.

Hope the above info will b helpful to you.

Best Regards,
Bio.
GeneralThreading issue Pin
kkfromus2-Sep-03 11:16
kkfromus2-Sep-03 11:16 
GeneralRe: Threading issue Pin
valikac3-Sep-03 6:22
valikac3-Sep-03 6:22 
GeneralRe: Threading issue Pin
kkfromus3-Sep-03 11:35
kkfromus3-Sep-03 11:35 
GeneralRe: Threading issue Pin
valikac4-Sep-03 9:07
valikac4-Sep-03 9:07 
GeneralRe: Threading issue Pin
Anonymous12-Sep-03 12:16
Anonymous12-Sep-03 12:16 
GeneralRe: Threading issue Pin
kkfromus12-Sep-03 14:40
kkfromus12-Sep-03 14:40 
GeneralThread Id of client app Pin
In-At1-Sep-03 18:30
In-At1-Sep-03 18:30 
GeneralRe: Thread Id of client app Pin
igor19603-Sep-03 10:33
igor19603-Sep-03 10:33 
Generalwhy CoCreateInstance( ... IID_IShellLink, ..) failed Pin
nofeel1-Sep-03 16:41
nofeel1-Sep-03 16:41 
GeneralRe: why CoCreateInstance( ... IID_IShellLink, ..) failed Pin
PengFeidu7-Sep-03 20:08
PengFeidu7-Sep-03 20:08 
GeneralRe: why CoCreateInstance( ... IID_IShellLink, ..) failed Pin
Lim Bio Liong18-Sep-03 8:06
Lim Bio Liong18-Sep-03 8:06 
GeneralRe: why CoCreateInstance( ... IID_IShellLink, ..) failed Pin
Lim Bio Liong19-Sep-03 0:53
Lim Bio Liong19-Sep-03 0:53 
GeneralKeeping COM server active Pin
Gilrock1-Sep-03 14:00
Gilrock1-Sep-03 14:00 
GeneralRe: Keeping COM server active Pin
igor19603-Sep-03 10:43
igor19603-Sep-03 10:43 
GeneralRe: Keeping COM server active Pin
Gilrock3-Sep-03 11:20
Gilrock3-Sep-03 11:20 
GeneralRe: Keeping COM server active Pin
igor19603-Sep-03 11:36
igor19603-Sep-03 11:36 
GeneralRe: Keeping COM server active Pin
Gilrock3-Sep-03 11:50
Gilrock3-Sep-03 11:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.