passing objects from VB as VARIANT to COM server object failing 
Author Message
 passing objects from VB as VARIANT to COM server object failing

hi,
the scenario is:

MyComObject1 is developed in VC ATL COM and implements dual interface.
It contains private member variables, the values of which can be
initialized by calling setter methods.

MyComObject2 is also developed in VC ATL COM and implements dual
interface. I need to add a method "func" to this which accepts an
optional parameter. If passed in, the parameter will be an instance of
MyComObject1 and the method will use the getter methods in
MyComObject1 to get the values of the member variables in
MyComObject1.

VB 6.0 is the client. It instantiates MyComObject1 and initializes the
member variables by calling on the setter methods. It then
instantiates MyComObject2 and calls the method "func" passing the
instance of MyComObject1.

the problem is:

If I use VARIANT, then I am not able to pass the instance of
MyComObject1 correctly from VB. What happens is that all the member
variables get reset to junk values.

If I use IDispatch* or IUnknown*, then I am able to correctly receive
the instance of MyComObject1 from VB (ie, all the member variables
retain their values). But now, the optional argument is not effective
anymore (ie, if I dont pass the instance of MyComObject1, then VB
throws a "type mismatch" error).

the Code:

[id(2), helpstring("method AnotherHello")] HRESULT
AnotherHello([optional, in]VARIANT vtarg);

STDMETHODIMP CMyComObject2::AnotherHello(VARIANT vtarg)
{
        if (vtarg.vt!=VT_ERROR){
                if ((vtarg.vt==VT_DISPATCH)|(vtarg.vt==VT_DISPATCH||VT_BYREF)){
                        CMyComObject1 *pobj=(CMyComObject1*)vtarg.pdispVal;
                        pobj->MyHello();
                }
        }
        return S_OK;

Quote:
}

Am I missing something? Is there any workaround?

Please help me.

thanks,
Sujit Vasudev



Sat, 05 Jun 2004 15:14:38 GMT  
 passing objects from VB as VARIANT to COM server object failing
You are handling VT_DISPATCH | VT_BYREF incorrectly - in this case
IDispatch* is accessed via

IDispatch *pDisp = *vtarg.ppDispVal;

And by the way, you have | (single pipe) and || (double pipe) operators
mixed up. | is a bitwise OR and should be used as in VT_DISPATCH |
VT_BYREF. || is a logical OR, and should be used as in

if (condition1 || condition2) {}

The easiest way to deal with probability of varying types in VARIANT is
probably

IDispatch *pDisp = 0;
VARIANT varCoerced;
VariantInit(&varCoerced)
HRESULT hr = VariantChangeType(&varCoerced, &vtarg, 0, VT_DISPATCH);
if (SUCCEEDED(hr))
    pDisp = V_DISPATCH(&varCoerced);

Check for VT_ERROR first.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> hi,
> the scenario is:

> MyComObject1 is developed in VC ATL COM and implements dual interface.
> It contains private member variables, the values of which can be
> initialized by calling setter methods.

> MyComObject2 is also developed in VC ATL COM and implements dual
> interface. I need to add a method "func" to this which accepts an
> optional parameter. If passed in, the parameter will be an instance of
> MyComObject1 and the method will use the getter methods in
> MyComObject1 to get the values of the member variables in
> MyComObject1.

> VB 6.0 is the client. It instantiates MyComObject1 and initializes the
> member variables by calling on the setter methods. It then
> instantiates MyComObject2 and calls the method "func" passing the
> instance of MyComObject1.

> the problem is:

> If I use VARIANT, then I am not able to pass the instance of
> MyComObject1 correctly from VB. What happens is that all the member
> variables get reset to junk values.

> If I use IDispatch* or IUnknown*, then I am able to correctly receive
> the instance of MyComObject1 from VB (ie, all the member variables
> retain their values). But now, the optional argument is not effective
> anymore (ie, if I dont pass the instance of MyComObject1, then VB
> throws a "type mismatch" error).

> the Code:

> [id(2), helpstring("method AnotherHello")] HRESULT
> AnotherHello([optional, in]VARIANT vtarg);

> STDMETHODIMP CMyComObject2::AnotherHello(VARIANT vtarg)
> {
> if (vtarg.vt!=VT_ERROR){
> if ((vtarg.vt==VT_DISPATCH)|(vtarg.vt==VT_DISPATCH||VT_BYREF)){
> CMyComObject1 *pobj=(CMyComObject1*)vtarg.pdispVal;
> pobj->MyHello();
> }
> }
> return S_OK;
> }

> Am I missing something? Is there any workaround?

> Please help me.

> thanks,
> Sujit Vasudev



Mon, 07 Jun 2004 06:27:13 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. passing objects from VB as VARIANT to COM server object failing

2. How to pass failed HRESULT from a VB COM object

3. passing COM object pointers to another COM object in ATL

4. Passing COM objects as parameters to other COM objects

5. Passing an Excel COM object to a C++ COM Server

6. Problem with scripting context when using VB com object inside VC com object

7. Passing COM objects as parameters to other COM objects

8. Passing a VARIANT by ref to a COM object

9. Testing a Variant Output argument in ATL Com object using VB client

10. Convert variant to object (VB-COM used in VC)

11. Passing COM object pointers through VARIANT from ActiveX component

12. Passing Arrays Between VB and ATL COM object

 

 
Powered by phpBB® Forum Software