469,645 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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

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);
}
--

Paul - Consultant: C++ Application Design and Development
May 1 '07 #1
3 2473
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);
}

--
Consultant: C++ Application Design and Development
"spd3001" wrote:
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);
}
--

Paul - Consultant: C++ Application Design and Development
May 1 '07 #2
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
---------

"spd3001" <sp*****@discussions.microsoft.comwrote in message
news:FA**********************************@microsof t.com...
>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);
}
--

Paul - Consultant: C++ Application Design and Development

May 2 '07 #3
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" wrote:
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
---------

"spd3001" <sp*****@discussions.microsoft.comwrote in message
news:FA**********************************@microsof t.com...
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);
}
--

Paul - Consultant: C++ Application Design and Development


May 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nadav | last post: by
22 posts views Thread by glenn | last post: by
3 posts views Thread by Jeffery Franzen | last post: by
10 posts views Thread by Peter Afonin | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
7 posts views Thread by R Reyes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.