C ++ / CLI string interop

I am working on a C ++ / CLI shell for a native C ++ class. The C ++ / CLI wrapper is used in a WPF application. I ran into some strange problem while trying to marshal strings.

The WPF application passes the object System::Stringto my shell. The shell then converts System::Stringto std::stringwhich the native class expects. This is all fine, but as soon as I pass the string to my own object, it is empty.

Here is the code

WPF Event Handler (C #)

private void tbInputConfig_TextChanged(object sender, TextChangedEventArgs e)
{
    _OrionBasicApp.ConfigTemplateFile = tbInputConfig.Text;
}

Property in the Wrapper class (C ++ / CLI)

void BasicApp::ConfigTemplateFile::set(String ^value)
{
    std::string val = marshal_as<std::string>(value);
    _NativeApp->setConfigTemplateFile(val);
}

Native code (C ++)

void Basic_App::setConfigTemplateFile(const std::string& template_file)
{
   m_gParams.configTemplateFile = template_file;
}

, WPF , String , std::string val , template_file setConfigFile - . , , std::string val - .

Marshal::StringToHGlobalAnsi, . , , - ( ). , .

: Microsoft Visual Studio 2008, + wpf 2010 . , , .

?

UPDATE

visual studio 2010, . ( Microsoft ?) , ( , ).

SO , ?

+5
1

++ DLL - . std::string - , , .

BSTR (SysStringAlloc et al) raw char* DLL std::string , , .

.NET ++/CLI. std::string DLL .

:

void BasicApp::ConfigTemplateFile::set(String ^value)
{
    std::string val = marshal_as<std::string>(value);
    _NativeApp->setConfigTemplateFile(val.c_str(), val.size());
}

void Basic_App::setConfigTemplateFile(const char* template_file_content, size_t template_file_length)
{
   m_gParams.configTemplateFile = std::string(template_file_content, template_file_length);
}
+6

All Articles