2

I wrote a class library in VS 2010 C++/CLI and created a dll.

// testclass.h
#pragma once

#include <string>

namespace test
{
    public ref class testclass
    {
      public:
         std::string getstringfromcpp()
         {
            return "Hello World";   
         }
    };
}

And I want to use it in C# program, add this dll to reference then:

using test;
... 
testclass obj = new testclass();
textbox1.text = obj.getstringfromcpp();
...

What should I do with this issue?

12
  • i cant understand in c++ to c# direction, c++ foo function return void? how can i give this string in c#? Commented Sep 1, 2015 at 4:58
  • What are you talking about? What foo()? Other than that, whats the problem with your code above? It looks like it should work Commented Sep 1, 2015 at 5:00
  • can you explain it with sample "hello world" and use it in textbox in c#? Commented Sep 1, 2015 at 5:00
  • Is the problem that you don't understand the above code rather than that there may be a bug in it? Commented Sep 1, 2015 at 5:01
  • msdn.microsoft.com/en-us/library/68td296t.aspx Commented Sep 1, 2015 at 5:02

2 Answers 2

4

For an interop scenario, you need to return a string object you'll be able to read from .NET code.

Don't return a std::string (there is no such thing in C#) or a const char * (readable from C# but you'd have to manage memory deallocation) or things like that. Return a System::String^ instead. This is the standard string type in .NET code.

This will work:

public: System::String^ getStringFromCpp()
{
    return "Hello World";   
}

But if you actually have a const char * or std::string object you'll have to use the marshal_as template:

#include <msclr/marshal.h>
public: System::String^ getStringFromCpp()
{
    const char *str = "Hello World";
    return msclr::interop::marshal_as<System::String^>(str);
}

Read Overview of Marshaling in C++ for more details.


To convert a System::String^ to std::string you can also use the marshal_as template, as explained in the above link. You just need to include a different header:

#include <msclr/marshal_cppstd.h>
System::String^ cliStr = "Hello, World!";
std::string stdStr = msclr::interop::marshal_as<std::string>(cliStr);
Sign up to request clarification or add additional context in comments.

3 Comments

wow, it works fine at last. very very very thank you dear Lucas. Would you kindly explain about other direction? send a string from c# and use it as std:string in c++?
@user3778594 I added that to the answer
I involved with this issue for 2 days, I appreciate you.
0

In my program somehow it refuses to convert the std::string directly to System::String^ but takes the char* casting ==> std::string.c_str()

public: System::String^ getStringFromCpp()
{
    std::string str = "Hello World";
    return msclr::interop::marshal_as<System::String^>(str.c_str());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.