C++ Interop - Getting String value back from ATL COM Server

G

Guest

I have a C++/CLI app that uses a logging control in an ATL COM Server. C++
Interop seems to work just fine to pass data from the C++/CLI app to the ATL
Control but I can't seem to figure out how to get a string value back from
the ATL Control. When debugging the code seen below, I see that the VARIANT
pointed to by 'data' is updated but the String 'str' passed from the C++/CLI
app doesn't change. So the String 'str' seems to be passed by value rather
than reference but I can't seem to get anything else to work without throwing
out C++ interop. Any suggestions?


System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
System::String^ str("temp");

this->axLogCtl1->GetLogText(str);
richTextBox1->Text = str;
}


HRESULT LogCtl::GetLogText(VARIANT* data)
{
CComVariant v(m_strData);

/////////////////////////////////////////////////////////////////
// Pass the current log data to the caller.
/////////////////////////////////////////////////////////////////
if (VT_BSTR == data.vt)
v.CopyTo(&(data.bstrVal));

return (S_OK);
}
 
G

Guest

Oops... Example should be as follows:

HRESULT LogCtl::GetLogText(VARIANT* data)
{
CComVariant v(m_strData);

/////////////////////////////////////////////////////////////////
// Pass the current log data to the caller.
/////////////////////////////////////////////////////////////////
if (VT_BSTR == data->vt)
v.CopyTo(&(data->bstrVal));

return (S_OK);
}
 
G

G Himangi

How is the axLogCtl1->GetLogText() method defined in C++/CLI?

If you want the value back, you should define the parameter as an out or ref

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 
G

Guest

Finally got it to work as follows:

System::Void toolStripButton1_Click(System::Object^ sender,
System::EventArgs^ e)
{
IntPtr ip = Marshal::AllocHGlobal(0x00007FFF);
void * p = ip.ToPointer();
BYTE * pStr = (BYTE *) p;

this->axLogCtl1->GetLogText(*pStr);
richTextBox1->Text = Marshal::ptrToStringAuto(ip);

Marshal::FreeHGlobal(ip);

}

HRESULT LogCtl::GetLogText(BYTE * data)
{
_tcscpy_s((TCHAR *) data,
0x00007FFF/sizeof(TCHAR),
(const TCHAR *)m_strData.GetBuffer());

return (S_OK);
}

--
Consultant: C++ Application Design and Development


G Himangi said:
How is the axLogCtl1->GetLogText() method defined in C++/CLI?

If you want the value back, you should define the parameter as an out or ref

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 

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