"Udi" <Ud**********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Thanks Willy!
I really appreciate it!
What if I need an out string parameter?
I mean PtrToStringChars returns a const char, what is the right way to
initialize it inside
MyClass and return it back from the wrapper?
int DoSomthing(out System::String * buf)
{
// convert String to char*
__const_Char_ptr wch = PtrToStringChars( buf );
return mc->DoSomthing( wch ); --> need it out here!
}
Difficult to answer, it depends who allocated the buffer holding the string,
what allocator has been used and what string type it is (ansi, unicode).
Following sample that assumes the callee (native code) allocates and
returns an ansi string.
// managed wrapper
int SomeWrapper::RetString(System::String** buffer)
{
// allocate native memory to hold the pointer to the char* returned by
callee
System::IntPtr pointerTobufPointer =
Marshal::AllocHGlobal(sizeof(void*));
int ret = ac->RetString(static_cast<char**>(pointerTobufPoint er
..ToPointer()));
if (buf != 0)
{
*buffer =
Marshal::PtrToStringAnsi(Marshal::ReadIntPtr(point erTobufPointer ));
}
return ret;
}
// native C++
int SomeClass::RetString(char** buffer)
{
s = "Hello Ansi world";
*buffer = const_cast<char*>(s.c_str());
return s.length();
}
...
std::string s;
// In C#
SomeWrapper sw = new SomeWrapper();
int ret = sw.RetString(ref s);
...
Note that - I suggets you read all possible documents and white papers found
on
http://msdn.microsoft.com/visualc/, and as your questions relate to C++
you might better post to the vc NG microsoft.public.dotnet.languages.vc
Willy.