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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
cyrusbm
  • 41
  • 1
  • 5
  • i cant understand in c++ to c# direction, c++ foo function return void? how can i give this string in c#? – cyrusbm Sep 01 '15 at 04:58
  • What are you talking about? What `foo()`? Other than that, whats the problem with your code above? It looks like it should work –  Sep 01 '15 at 05:00
  • can you explain it with sample "hello world" and use it in textbox in c#? – cyrusbm Sep 01 '15 at 05:00
  • Is the problem that you don't _understand_ the above code rather than _that there may be a bug in it_? –  Sep 01 '15 at 05:01
  • https://msdn.microsoft.com/en-us/library/68td296t.aspx –  Sep 01 '15 at 05:02
  • @X-TECH I don't think that duplicate applies because the above example is **C++/CLI** –  Sep 01 '15 at 05:03
  • http://stackoverflow.com/questions/20752001/passing-strings-from-c-sharp-to-c-dll-and-back-minimal-example i cant understand what should i use this code – cyrusbm Sep 01 '15 at 05:05
  • Ah I see. Thanks. `foo()` is from other page –  Sep 01 '15 at 05:05
  • @user3778594 _"i cant understand what should i use this code"_ - I think ignore that page. It is not relevant to your code above as you are using c++/CLI. Please refer to the MSDN link I posted –  Sep 01 '15 at 05:48
  • Really passing a string from a c++ dll to c# has not a simple way? – cyrusbm Sep 01 '15 at 06:43
  • @nvoigt - the duplicate you chose is particularly bad and doesn't apply to this question. OP uses C++/CLI, not plain C++. – Lucas Trzesniewski Sep 01 '15 at 10:01
  • @LucasTrzesniewski oh, indeed. He managed to hide it quite well :) – nvoigt Sep 01 '15 at 10:03

2 Answers2

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);
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
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());
}
shaile
  • 1