从BSTR到字符串的转换 [英] conversion from BSTR to string

查看:133
本文介绍了从BSTR到字符串的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个.NET组件,它接受一个字符串对象.我有一个调用此函数的VC ++应用程序.

因此,当我将BSTR对象传递给接受字符串的.NET组件函数时,该字符串在组件内部为空.

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?

推荐答案

Hello,

C ++方面的示例:
接口声明
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;
	}
};


具有指定封送处理类型的C#端示例:
这是指定的类型,因此.NET将在RCW中进行封送处理.


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


没有封送处理的C#端示例:
在这里,您可以直接执行封送处理类型.


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



问候,
Maxim.



Regards,
Maxim.


您好!我假设您的组件中有put_StringData(BSTR str),因此对于std :: string,您可以使用以下代码:
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());



或用于CString te following



or for CString te following

CString cstr_string = "Some string data";
_bstr_t some_string(cstr_string);

mycomponent->put_StringData(some_string.GetBSTR());


这篇关于从BSTR到字符串的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆