how to use the return value when using C# dlls in a C++ project?

V

Vivienne

hi,

I am using a C# library in my C++ project. I want to make use of the
classes defined in the library to do some job, and use the return
value to go on other works in my project. But it seem difficult to
convert a return value of C# datatype into C++ complaint datatype,
like convert System::String in C# into std::string in C++.

like in my test code:

ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library
System::String result = a-> getResult();

std::string str; // here I want to copy result to str, then I can use
the string freely. But I don't know how to do it.

Is there any way ?

Thanks!
 
V

Victor Bazarov

Vivienne said:
I am using a C# library in my C++ project. I want to make use of the
classes defined in the library to do some job, and use the return
value to go on other works in my project. But it seem difficult to
convert a return value of C# datatype into C++ complaint datatype,
like convert System::String in C# into std::string in C++.

like in my test code:

ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library
System::String result = a-> getResult();

std::string str; // here I want to copy result to str, then I can use
the string freely. But I don't know how to do it.

Is there any way ?

You need to ask in a C# forum. See 'microsoft.pulbic.dotnet.*'
hierarchy of newsgroups.

V
 
T

tragomaskhalos

hi,

I am using a C# library in my C++ project. I want to make use of the
classes defined in the library to do some job, and use the return
value to go on other works in my project. But it seem difficult to
convert a return value of C# datatype into C++ complaint datatype,
like convert System::String in C# into std::string in C++.

like in my test code:

ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library
System::String result = a-> getResult();

std::string str; // here I want to copy result to str, then I can use
the string freely. But I don't know how to do it.

Is there any way ?

Thanks!

AFAIK you have to grub about getting the internal representation of
the System::String and cons a std::string from that. It's awkward but
a solved problem - MSDN will tell you how.
 
M

Marcus Kwok

Vivienne said:
I am using a C# library in my C++ project. I want to make use of the
classes defined in the library to do some job, and use the return
value to go on other works in my project. But it seem difficult to
convert a return value of C# datatype into C++ complaint datatype,
like convert System::String in C# into std::string in C++.

like in my test code:

ClassA ^a = gcnew ClassA(); // ClassA is a class in the C# library
System::String result = a-> getResult();

std::string str; // here I want to copy result to str, then I can use
the string freely. But I don't know how to do it.

Is there any way ?

What you need to do is "marshal" the string. Here are two articles that
give sample code:

http://msdn2.microsoft.com/en-us/library/1b4az623(vs.80).aspx
http://blogs.msdn.com/slippman/archive/2004/06/02/147090.aspx

For any further questions, please consult a C# newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top