Marshal native string to managed string for wrapper DLL
-
I have the following native function call:
wchar_t* SXNative::ReturnString()
{
wchar_t retString[128];
wchar_t Test[] = L"This is a dummy";wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}The return value works fine from the native function. In a C++/CLI wrapper class I have a managed type that marshalls the return type:
String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
return gcnew String(test);
}The return type from the manaaged function returns garbage. Any idea what is wrong with this? Thanks in advance.
Jer 29:11
-
I have the following native function call:
wchar_t* SXNative::ReturnString()
{
wchar_t retString[128];
wchar_t Test[] = L"This is a dummy";wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}The return value works fine from the native function. In a C++/CLI wrapper class I have a managed type that marshalls the return type:
String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
return gcnew String(test);
}The return type from the manaaged function returns garbage. Any idea what is wrong with this? Thanks in advance.
Jer 29:11
Al_S wrote:
wchar_t* SXNative::ReturnString() { wchar_t retString[128]; return retString; }
IMO this isn't valid C or C++ code, it returns a pointer to a stack-based array, which is going to diappear in thin air as soon as the function returns. It may look like a good pointer on immediate inspection, but it will not work reliably. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
Al_S wrote:
wchar_t* SXNative::ReturnString() { wchar_t retString[128]; return retString; }
IMO this isn't valid C or C++ code, it returns a pointer to a stack-based array, which is going to diappear in thin air as soon as the function returns. It may look like a good pointer on immediate inspection, but it will not work reliably. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
OK. It goes out of scope after it returns. How do I mend that?
Jer 29:11
modified on Tuesday, October 20, 2009 9:18 PM
by making sure it does not sit on the stack (the alternatives being static allocation and heap allocation). In C, it would be a global array, or a malloc'ed buffer. in C++ it could also be (a member of) an object, anything that gets gcnew'ed or is static. And for anything dynamic, you would eventually have to deallocate it (e.g. call free on the thing that was malloc'ed). :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
by making sure it does not sit on the stack (the alternatives being static allocation and heap allocation). In C, it would be a global array, or a malloc'ed buffer. in C++ it could also be (a member of) an object, anything that gets gcnew'ed or is static. And for anything dynamic, you would eventually have to deallocate it (e.g. call free on the thing that was malloc'ed). :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
I don't know what it is you want to achieve. I guess this would be the simplest C++ code that should do it:
wchar_t* SXNative::ReturnString() {
return L"This is a dummy";
}as now the string sits statically in memory. If you need more C++ advice, better someone else kicks in. I answered the OP because it started as a P/Invoke issue, which it wasn't after all. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
I don't know what it is you want to achieve. I guess this would be the simplest C++ code that should do it:
wchar_t* SXNative::ReturnString() {
return L"This is a dummy";
}as now the string sits statically in memory. If you need more C++ advice, better someone else kicks in. I answered the OP because it started as a P/Invoke issue, which it wasn't after all. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:
Jer 29:11
-
Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:
Jer 29:11
"Marshal" in the subject line got my attention... :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
-
"Marshal" in the subject line got my attention... :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
Look at his thread ... maybe it can help, you its going from System::String (utf8 charset) do std::string link
-
Thanks. Not sure where the post was intended as P/Invoke. I was sure I was pursuing an interop question. What you posted is a portion of what I had initially. The native code worked as posted. It just goes out of scope when called my the managed function. As stated in the OP, I want to return a native string to a managed function that marshalls it to a System::String. Illustrated in the top of the thread :confused:
Jer 29:11
As Luc mentioned, one way to do this is to allocate the native string and free it when you're done with it:
wchar_t* SXNative::ReturnString()
{
wchar_t* retString = new wchar_t[128];
wchar_t Test[] = L"This is a dummy";wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
String^ retstr = gcnew String(test);
delete test;
return retstr;
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
As Luc mentioned, one way to do this is to allocate the native string and free it when you're done with it:
wchar_t* SXNative::ReturnString()
{
wchar_t* retString = new wchar_t[128];
wchar_t Test[] = L"This is a dummy";wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test);
return retString;
}String^ ManagedWrapper::Managed_ReturnString()
{
wchar_t* test = nat_ptr->ReturnString();
String^ retstr = gcnew String(test);
delete test;
return retstr;
}Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks Mark, An illustration goes a long way than a theoretical discussion. I thought about the return type from the native code, what's been discussed and realized that rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function. That's one of the bigger bonuses of C++/CLI right? So I decided to do the following: <pre> String^ SXNative::ReturnString() { wchar_t Test[] = L"This is a dummy"; msclr::auto_gcroot<String^> ret; wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test); ret = marshal_as<String^>(retString); return ret->ToString(); } </pre> <pre> String^ ManagedWrapper::Managed_ReturnString() { return nat_ptr->ReturnString(); } </pre> I want to say thanks again for posting the example because that may help someone else like it did for me. I also want to post my solution as well as I feel that it shows an alternative solution. Thanks again Mark :-D Al
Jer 29:11
-
Thanks Mark, An illustration goes a long way than a theoretical discussion. I thought about the return type from the native code, what's been discussed and realized that rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function. That's one of the bigger bonuses of C++/CLI right? So I decided to do the following: <pre> String^ SXNative::ReturnString() { wchar_t Test[] = L"This is a dummy"; msclr::auto_gcroot<String^> ret; wcscpy_s(retString, sizeof(Test) / sizeof(wchar_t), Test); ret = marshal_as<String^>(retString); return ret->ToString(); } </pre> <pre> String^ ManagedWrapper::Managed_ReturnString() { return nat_ptr->ReturnString(); } </pre> I want to say thanks again for posting the example because that may help someone else like it did for me. I also want to post my solution as well as I feel that it shows an alternative solution. Thanks again Mark :-D Al
Jer 29:11
Al_S wrote:
rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function
I don't know what others read into your OP, but I personally figured the native function needed to be pure native. Had I known it could be mixed then it would have been much simpler :) Cheers, MArk
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Al_S wrote:
rather than marshalling or converting it in the managed function, why not have the managed string object returned in native function
I don't know what others read into your OP, but I personally figured the native function needed to be pure native. Had I known it could be mixed then it would have been much simpler :) Cheers, MArk
Mark Salsbery Microsoft MVP - Visual C++ :java:
Just the same, I would appreciate your opinion on what I posted. Both version work but I would value your input as to what I showed. The real native code is left out for proprietary reasons, but what was illustrated in context was not necessarily written in stone as to having to be completely native code. I should have stressed that the wrapper was a CLR DLL and that mixed mode was viable.
Jer 29:11
-
Just the same, I would appreciate your opinion on what I posted. Both version work but I would value your input as to what I showed. The real native code is left out for proprietary reasons, but what was illustrated in context was not necessarily written in stone as to having to be completely native code. I should have stressed that the wrapper was a CLR DLL and that mixed mode was viable.
Jer 29:11
Al_S wrote:
I would appreciate your opinion on what I posted
It won't compile as shown? :) I suppose it could be simplified...
String^ SXNative::ReturnString()
{
wchar_t Test[] = L"This is a dummy";return gcnew String(Test);
}or
String^ SXNative::ReturnString()
{
return gcnew String(L"This is a dummy");
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Al_S wrote:
I would appreciate your opinion on what I posted
It won't compile as shown? :) I suppose it could be simplified...
String^ SXNative::ReturnString()
{
wchar_t Test[] = L"This is a dummy";return gcnew String(Test);
}or
String^ SXNative::ReturnString()
{
return gcnew String(L"This is a dummy");
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Mark Salsbery wrote: It won't compile as shown? Smile I suppose it could be simplified... What YOU posted DOES compile and work for me. Perhaps I don't understand what you mean.
Jer 29:11
Al_S wrote:
Perhaps I don't understand what you mean.
That was my opinion (joking) on the code you posted, because you asked. There's so many ways to code - I try hard not to give opinions (I'm hardly qualified to decide what code is good or bad), but instead I prefer to provide alternatives. Someone else may come along and provide an even better alternative...etc.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Al_S wrote:
Perhaps I don't understand what you mean.
That was my opinion (joking) on the code you posted, because you asked. There's so many ways to code - I try hard not to give opinions (I'm hardly qualified to decide what code is good or bad), but instead I prefer to provide alternatives. Someone else may come along and provide an even better alternative...etc.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Look at his thread ... maybe it can help, you its going from System::String (utf8 charset) do std::string link
-
Mark Salsbery wrote: Someone else may come along and provide an even better alternative...etc. Fair enough! My intent was just to put helpful information to add to what I received as help.
Jer 29:11
Al_S wrote:
My intent was just to put helpful information to add to what I received as help
I know. But you also asked my opinion - I quoted you on that ;P (ok, you didn't "ask", but you stated you'd appreciate it heh)
Mark Salsbery Microsoft MVP - Visual C++ :java: