Passing parameters by reference to a VC COM Object

  • Thread starter Marja Ribbers-de Vroed
  • Start date
M

Marja Ribbers-de Vroed

I need to change a C++ COM Object so that it will correctly accept parameters by reference from a classic ASP/VBscript webapplication.
I'm working from the article found here: http://support.microsoft.com/kb/197957/EN-US/, as this article descibes my problem with the COM Object exactly.

The article gives the following original example method:

STDMETHODIMP CByRefObj::ByRefMethod( BSTR* bstrVal )
{
CComBSTR bstrRtnVal = L"This variable is passed by Reference";
*bstrVal = bstrRtnVal.Detach();
return S_OK;
}

And then at the bottom of the article, they say that the method needs to change so that it will accept a variant instead of a string as the output parameter. And as an example they suggest the following change:

// Where m_bstr is a BSTR member of SomeComObject.
vVal->vt = VT_BSTR;
vVal->bstrVal = m_bstr.Copy();

But since I don't know much C++ (I didn't write the COM object, but I do have its project sources) I don't understand where to put this in the example method.
I think it should look like this:

STDMETHODIMP CByRefObj::ByRefMethod( VARIANT* bstrVal )
{
CComBSTR bstrRtnVal = L"This variable is passed by Reference";
vVal->vt = VT_BSTR;
vVal->bstrVal = bstrRtnVal.Detach();
return S_OK;
}

Is this correct?

Regards, Marja
 
M

Marja Ribbers-de Vroed

Is this correct?

I get compilation errors when I do it like that:

error C2065: 'vVal' : undeclared identifier
error C2227: left of '->vt' must point to class/struct/union/generic type

What's wrong?
 
C

Carl Daniel [VC++ MVP]

Change it to:

STDMETHODIMP CByRefObj::ByRefMethod( VARIANT* vVal)
{
CComBSTR bstrRtnVal = L"This variable is passed by Reference";
vVal->vt = VT_BSTR;
vVal->bstrVal = bstrRtnVal.Detach();
return S_OK;
}

-cd

Marja Ribbers-de Vroed said:

I get compilation errors when I do it like that:

error C2065: 'vVal' : undeclared identifier
error C2227: left of '->vt' must point to class/struct/union/generic type

What's wrong?
 
M

Marja Ribbers-de Vroed

Change it to:
STDMETHODIMP CByRefObj::ByRefMethod( VARIANT* vVal)
{
CComBSTR bstrRtnVal = L"This variable is passed by Reference";
vVal->vt = VT_BSTR;
vVal->bstrVal = bstrRtnVal.Detach();
return S_OK;
}

In this example, where does bstrVal come from in vVal->bstrVal = bstrRtnVal.Detach(); ?
What does it do?

Regards, Marja
 
B

Brian Muth

Marja Ribbers-de Vroed said:
Change it to:

STDMETHODIMP CByRefObj::ByRefMethod( VARIANT* vVal)
{
CComBSTR bstrRtnVal = L"This variable is passed by Reference";
vVal->vt = VT_BSTR;
vVal->bstrVal = bstrRtnVal.Detach();
return S_OK;
}
In this example, where does bstrVal come from in vVal->bstrVal =
bstrRtnVal.Detach(); ?
What does it do?

It populates the bstrVal member variable with the BSTR pointer. Calling
Detach() indicates to the CComBSTR smart pointer that ownership of the BSTR
has been transferred to the assigned variable.

In the end, you have defined vVal to be a VARIANT containing the BSTR that
was created for you by the CComBSTR smart pointer.

Regards, Marja
 
M

Marja Ribbers-de Vroed

In this example, where does bstrVal come from in vVal->bstrVal =
It populates the bstrVal member variable with the BSTR pointer. Calling
Detach() indicates to the CComBSTR smart pointer that ownership of the BSTR
has been transferred to the assigned variable.

In the end, you have defined vVal to be a VARIANT containing the BSTR that
was created for you by the CComBSTR smart pointer.

Thank you very much for this explanation and your help.

Regards, Marja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top