Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:45:19+00:00 2026-06-10T06:45:19+00:00

I am working on a C++/CLI wrapper for a native c++ class. The C++/CLI

  • 0

I am working on a C++/CLI wrapper for a native c++ class. The C++/CLI wrapper is being used in a WPF application. I have encountered a weird problem when trying to marshal strings.

The WPF application passes a System::String object to my wrapper. The wrapper then converts the System::String to a std::string that the native class expects. This all goes fine, but once I pass the string to the native object, it is empty.

Here is some relevant code

WPF Event Handler (C#)

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

Property In 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;
}

So when I break in the WPF app and trace using the debugger, the String objects look fine, the std::string val marshaling looks good, but the parameter template_file in the setConfigFile function is an empty string. When I step out of the native function, I can see that the std::string val variable still looks fine.

I have tried using Marshal::StringToHGlobalAnsi function, it produces the same result. I have tried changing the native function to take copy of the string instead of a reference, this produces an exception about an invalid block of memory (if requested I will post the exact message). I have tried allocating the string on the heap, no luck.

Now for the kicker: The native code was compiled with Microsoft Visual Studio 2008, and the wrapper + wpf code was compiled with 2010. I hope this is not the problem because it will not be easy for us to migrate either codebase to the other version.

Any ideas?

UPDATE

I was able to switch the native code over to visual studio 2010 and this did fix the problem. (why must microsoft make my life so difficult?) While I did make the system build, the lead on the project is giving me MAJOR hassles about this solution (he is concerned that it may not run correctly or that we will have to switch over dependent libraries).

SO is there a solution to this problem that doesn’t force me to switch visual studio version?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:45:21+00:00Added an answer on June 10, 2026 at 6:45 am

    Passing C++ objects across DLL boundaries is a bad idea. std::string is not the same type, layout, or implementation, which is causing your problem.

    If you pass either a BSTR (SysStringAlloc et al) or raw char* across the DLL boundary, and convert to std::string inside the native code instead of in the wrapper, your problem will go away.

    This problem is not specific to .NET or C++/CLI. Any attempt to pass std::string instances to your native DLL from any other compiler version will fail.

    Example fix:

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project where a c++/cli library is being used primarily from
I'm working on a mixed managed/native application using c++/CLI. I know that the CLR
I'm working on a CLI application that deals with uploading of files, and would
I've been trying to get started with unit-testing while working on a little cli
It's a pretty simple problem. I need to get my heroku CLI app working
So I have a native 3rd party C++ code base I am working with
I'm working with some C++/CLI code (new syntax) and am trying to declare a
I am working on a phpwebsocket application. I have to store the instance of
I'm trying to get a live progress indicator working on my php CLI app.
I'm working on updating the GUI of a native (MFC) application using .NET and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.