Passing by reference to an ATL COM component 
Author Message
 Passing by reference to an ATL COM component

I'm having trouble making changes to parameters passed to
my COM object and then having the changes appear in the
calling VBScript.

DESIRE:
I would like to pass a variable from VBScript to my ATL
COM object and have the ATL COM object modify the value.

PROBLEM:
My ATL COM object is getting called from VBS as expected,
but if I change the parameters value, the value us
unchanged when we return to VBScript.

Here is the VBScript:

Function test(xx)
  ' my COM object
  Set UOTCtrl = CreateObject("UOTCtrl.Application")
  ' the session handle I want to fill in
  ' (We set it to 12 just so we can check it in the called
routine)
  hSession = 12
  ' This will call my COM object to fill in the session
handle
  ' I check it in my COM routine and it is indeed(12)
  UOTCtrl.Connect(hSession)
  ' At this point however, the session is still 12!!
  UOTCtrl.Disconnect(hSession)
End Function

Here is my Method Implementation:

STDMETHODIMP CApplication::Connect(unsigned long* hSession)
{
        HANDLE res = (HANDLE)13;
        *hSession = (unsigned long)res;
        return S_OK;

Quote:
}

STDMETHODIMP CApplication::Disconnect(unsigned long
hSession)
{
        bool res = (hSession == 13);
        return res ? S_OK : S_FALSE;

Quote:
}

Here is my IDL:
[id(13), helpstring("method Connect")] HRESULT Connect
(unsigned long* hSession);
[id(14), helpstring("method Disconnect")] HRESULT
Disconnect(unsigned long hSession);

And my Class interface:
// IApplication
public:
STDMETHOD(Disconnect)(unsigned long hSession);
STDMETHOD(Connect)(unsigned long* hSession);

So, what have I done wrong? Connect takes a unsigned
long*, so my code that assigns to it:
*hSession = (unsigned long)res;
Should change it, eh?



Tue, 28 Jun 2005 09:21:34 GMT  
 Passing by reference to an ATL COM component

Quote:

> I'm having trouble making changes to parameters passed to
> my COM object and then having the changes appear in the
> calling VBScript.

> DESIRE:
> I would like to pass a variable from VBScript to my ATL
> COM object and have the ATL COM object modify the value.

> PROBLEM:
> My ATL COM object is getting called from VBS as expected,
> but if I change the parameters value, the value us
> unchanged when we return to VBScript.

Q197957 - PRB: Passing Parameters By Reference to a VC COM Object
http://support.microsoft.com/default.aspx?kbid=197957

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Tue, 28 Jun 2005 10:16:45 GMT  
 Passing by reference to an ATL COM component
Well the article you pointed me to seems to be addressing
my problem, but it still doesn't work :(

Here is the error I get now:
Error: Wrong number of arguments or invalid property
assignment: 'UOTCtrl.Connect', ; in line 5

I tried changing the IDL from [out,retval] to [out] and
that seemed to eliminate the error message, but then we're
back to having the change made in 'Connect' not make it
back up to to VBScript.

Here's my VBScript function:

Function test(xx)
 Set UOTCtrl = CreateObject("UOTCtrl.Application")
 hSession=123
 UOTCtrl.Connect(hSession)
 test = hSession
End Function

The problem is that the function always returns 123, even
when I change the VARIANT as described in the article.

Any further help would be greatly appriciated.

For the record, here are the changes I made per the
article:

STDMETHODIMP CApplication::Connect(/*[out, retval]
*/VARIANT* vVal)
{
 HANDLE res = (HANDLE)Start();
 vVal->vt = VT_UINT;
 vVal->uintVal = (UINT)res;
 return res ? S_OK : S_FALSE;

Quote:
}

// IDL
[id(13), helpstring("method Connect")] HRESULT Connect
([out/*, retval*/] VARIANT* vVal);

Quote:
>-----Original Message-----

>> I'm having trouble making changes to parameters passed
to
>> my COM object and then having the changes appear in the
>> calling VBScript.

>> DESIRE:
>> I would like to pass a variable from VBScript to my ATL
>> COM object and have the ATL COM object modify the value.

>> PROBLEM:
>> My ATL COM object is getting called from VBS as
expected,
>> but if I change the parameters value, the value us
>> unchanged when we return to VBScript.

>Q197957 - PRB: Passing Parameters By Reference to a VC
COM Object
>http://support.microsoft.com/default.aspx?kbid=197957

>--
>Michael Harris
>Microsoft.MVP.Scripting
>Seattle WA US

>.



Wed, 29 Jun 2005 10:30:24 GMT  
 Passing by reference to an ATL COM component

Quote:
> Any further help would be greatly appriciated.

Sorry...

If I actually knew anything about coding in VC++/ATL (other than how to
spell it ;-), I could probably help more.

But I don't, so I can't :-(

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Wed, 29 Jun 2005 12:24:30 GMT  
 Passing by reference to an ATL COM component

Quote:

> Well the article you pointed me to seems to be addressing
> my problem, but it still doesn't work :(

> Here is the error I get now:
> Error: Wrong number of arguments or invalid property
> assignment: 'UOTCtrl.Connect', ; in line 5

> I tried changing the IDL from [out,retval] to [out] and
> that seemed to eliminate the error message, but then we're
> back to having the change made in 'Connect' not make it
> back up to to VBScript.

That makes sense since the COM method modifies the passed argument rather
than returning a value.  You should be aware that while VBScript supports
pass by reference, JScript does not, so you exclude any JScript clients.

Quote:
> Here's my VBScript function:

> Function test(xx)
>  Set UOTCtrl = CreateObject("UOTCtrl.Application")
>  hSession=123
>  UOTCtrl.Connect(hSession)

This should be:

   UOTCtrl.Connect hSession 'preferred syntax

or

  Call UOTCtrl.Connect(hSession) 'OK but unnecessary

When calling a COM method that does not return a value (in VBScript
terminology, a Sub as opposed to a Function that does return a value), ()s
are not used to enclose the argument list unless you explicitly use the Call
statement.

In the case of calling a method that takes only a single argument, using the
otherwise unnecessary ()s around the single argument turns the argument into
an expression, the value of which is passed to the method.  The method
expects a byref argument but ends up modifying the temporary memory holding
the value of the expression rather than the memory representing the client
variable within the expression.

If your COM method had expected 2 arguments, then

  UOTCtrl.Connect(hSession, arg2)

would have thrown a runtime error of "cannot use parentheses when calling a
Sub" and the problem would have been more obvious...

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 30 Jun 2005 03:54:26 GMT  
 Passing by reference to an ATL COM component

Quote:

>>That makes sense since the COM method modifies the

passed argument rather
Quote:
>>than returning a value.  You should be aware that while
VBScript supports
>>pass by reference, JScript does not, so you exclude any

JScript clients.

I'm adding VBScript to my application (not web page) via
the Scripting Control (VBX?) so there is no concern of
JScript.

So the question remains...
How do I pass a parameter to a COM control and have the
modified parameter passed back up to the calling script?
This *must* be possible!

Quote:
>-----Original Message-----

>> Well the article you pointed me to seems to be
addressing
>> my problem, but it still doesn't work :(

>> Here is the error I get now:
>> Error: Wrong number of arguments or invalid property
>> assignment: 'UOTCtrl.Connect', ; in line 5

>> I tried changing the IDL from [out,retval] to [out] and
>> that seemed to eliminate the error message, but then
we're
>> back to having the change made in 'Connect' not make it
>> back up to to VBScript.

>That makes sense since the COM method modifies the passed
argument rather
>than returning a value.  You should be aware that while
VBScript supports
>pass by reference, JScript does not, so you exclude any
JScript clients.

>> Here's my VBScript function:

>> Function test(xx)
>>  Set UOTCtrl = CreateObject("UOTCtrl.Application")
>>  hSession=123
>>  UOTCtrl.Connect(hSession)

>This should be:

>   UOTCtrl.Connect hSession 'preferred syntax

>or

>  Call UOTCtrl.Connect(hSession) 'OK but unnecessary

>When calling a COM method that does not return a value
(in VBScript
>terminology, a Sub as opposed to a Function that does

return a value), ()s
Quote:
>are not used to enclose the argument list unless you

explicitly use the Call
Quote:
>statement.

>In the case of calling a method that takes only a single
argument, using the
>otherwise unnecessary ()s around the single argument

turns the argument into
Quote:
>an expression, the value of which is passed to the
method.  The method
>expects a byref argument but ends up modifying the

temporary memory holding
Quote:
>the value of the expression rather than the memory

representing the client
Quote:
>variable within the expression.

>If your COM method had expected 2 arguments, then

>  UOTCtrl.Connect(hSession, arg2)

>would have thrown a runtime error of "cannot use

parentheses when calling a

- Show quoted text -

Quote:
>Sub" and the problem would have been more obvious...

>--
>Michael Harris
>Microsoft.MVP.Scripting
>Seattle WA US

>.



Sat, 02 Jul 2005 03:08:19 GMT  
 Passing by reference to an ATL COM component

Quote:
> I'm adding VBScript to my application (not web page) via
> the Scripting Control (VBX?) so there is no concern of
> JScript.

> So the question remains...
> How do I pass a parameter to a COM control and have the
> modified parameter passed back up to the calling script?
> This *must* be possible!

I think you didn't read all of my reply which was inline in 2 parts ;-)...

--
Michael Harris
Microsoft.MVP.Scripting



Sat, 02 Jul 2005 04:54:57 GMT  
 Passing by reference to an ATL COM component
Hey Michael thanks! That worked like a charm.
(I also *understand* what was happening.)

I'm a bit surprised that using parentheses for function
arguments would change the program semantics so radically.
Oh well.
Heh, I've been programming in C (and the like) for 20
years... not putting parentheses after a function call is
a hard habit to break ;P

Anyway, thanks again.
Luke

Quote:
>-----Original Message-----
>> I'm adding VBScript to my application (not web page) via
>> the Scripting Control (VBX?) so there is no concern of
>> JScript.

>> So the question remains...
>> How do I pass a parameter to a COM control and have the
>> modified parameter passed back up to the calling script?
>> This *must* be possible!

>I think you didn't read all of my reply which was inline
in 2 parts ;-)...

>--
>Michael Harris
>Microsoft.MVP.Scripting

>.



Sat, 02 Jul 2005 05:50:40 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. pass a COM object reference to a WSC component

2. Connecting VBScript application to ATL/COM/DLL server component

3. ATL COM Component firing events into VBScript event sink

4. Passing array of strings from ASP (JScript) to ATL COM

5. Accessing eVC ATL COM Component in eVB

6. Passing by reference through ATL?

7. passing com/atl object in vb

8. CAlling C++ COM Component with reference paramater

9. Passing optional object arguments from COM components

10. Passing JScript variables by reference to an ActiveX component

11. Passing Arrays to COM Components

12. Passing array to COM component

 

 
Powered by phpBB® Forum Software