Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# COM
Hi,
 
I have a .NET component which accepts a string object. I have a VC++ application which calls this function.
 
So when I pass a BSTR object to .NET component function which accepts a string, the string is empty inside the component.
 
How should we do this conversion i.e passing a BSTR object to .NET string object?
Posted 16-Oct-12 20:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello,
 
Example from C++ side:
interface declaration
interface ISampleInterface:public IUnknown
{
    STDMETHOD(put_Value)(BSTR _value) = 0;
    STDMETHOD(get_Value)(BSTR * _value) = 0;
};
Class example which provide that functionality:
class CTest : public ISampleInterface
{
	// skipped IUnknown
private:
	WCHAR m_szString[1024];
public:
	CTest() {wcscpy_s(m_szString,L"");}
public:
	STDMETHODIMP put_Value(BSTR _value);
	{
		if (!_value || !wcslen(_value)) return E_INVALIDARG;
		wcscpy_s(m_szString,_value);
		return NOERROR;
	}
        STDMETHODIMP get_Value(BSTR * _value)
	{
		if (!_value) return E_INVALIDARG;
		*_value = SysAllocString(m_szString);
		return NOERROR;
	}
};
Example on C# side with specify marshaling type:
Here is type specified so .NET will do marshaling stuff in RCW.
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISampleInterface
{
    [PreserveSig]
    int put_Value([In, MarshalAs(UnmanagedType.BStr)] string _value);
 
    [PreserveSig]
    int get_Value([Out, MarshalAs(UnmanagedType.BStr)] out string _value);
}
Example on C# side without marshaling:
Here you directly perform marshaling type.
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISampleInterface
{
    [PreserveSig]
    int put_Value(IntPtr _value);
 
    [PreserveSig]
    int get_Value(out IntPtr _value);
}
For such declaration you should have code:
// initialized somewhere
ISampleInterface _sample;
// Put value
{
    IntPtr _ptr = Marshal.StringToBSTR("some string");
    _sample.put_Value(_ptr);
    Marshal.FreeBSTR(_ptr);
}
// Get value
{
    IntPtr _ptr;
    _sample.get_Value(out _ptr);
    string _result = Marshal.PtrToStringBSTR(_ptr);
    Marshal.FreeBSTR(_ptr);
}
 
Regards,
Maxim.
  Permalink  
v2
Comments
skydger at 18-Oct-12 3:18am
   
Nice solution. It's not just convertion strings answer, but a complete guide to work with COM xD. My 5.
Maxim Kartavenkov at 18-Oct-12 5:35am
   
Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello! I presume, that you have put_StringData(BSTR str ) in your component, so for std::string you could use this code:
std::string std_string = "Some string data";
_bstr_t some_string = std_string.c_str();
 
mycomponent->put_StringData(some_string.GetBSTR());
 
or for CString te following
CString cstr_string = "Some string data";
_bstr_t some_string(cstr_string);
 
mycomponent->put_StringData(some_string.GetBSTR());
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 OriginalGriff 248
2 ChintanShukla 185
3 Richard Deeming 175
4 Bhavik_Patel 170
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100