Typical issues in managed c++
-
Hi all we have unmanaged C++ third party library which needs to be used in C# Environment. hence I am writing managed C++ wrapper on top to use it in C# environment I came across some of the situation put me into trouble.Can any one resolve my doubts 1) I was using the pin pointers to convert from managed string (String^) to unmanaged string(char * buffer) is it safe? are there any performance issues with this ? If then what is the best logic to do? please see the code snippet below i used for converting from C# string to char * buffer(unmanaged string) bool bRet = false; pin_ptr<const wchar_t> w_string = PtrToStringChars(s); size_t converted_chars = 0; size_t size_bytes = ((s->Length + 1) * 2); if ((size_bytes / 2) <= output_len) { memset(output, '\0', output_len); if (!wcstombs_s(&converted_chars, output, output_len, w_string, size_bytes)) bRet = true; } return bRet; 2)Can i use System::Generic::Collections such as Dictionary and List for sending data from C# to managedC++ and viceversa? I need to send collection of datasets from managed C++ to C# code. Can i use the List to do this. if not are there any best practices to achieve this? 3)Is it necessary to use gc when i am creating any memory in managed C++ Waiting for ur reply Thanks in advance Sukumar
-
Hi all we have unmanaged C++ third party library which needs to be used in C# Environment. hence I am writing managed C++ wrapper on top to use it in C# environment I came across some of the situation put me into trouble.Can any one resolve my doubts 1) I was using the pin pointers to convert from managed string (String^) to unmanaged string(char * buffer) is it safe? are there any performance issues with this ? If then what is the best logic to do? please see the code snippet below i used for converting from C# string to char * buffer(unmanaged string) bool bRet = false; pin_ptr<const wchar_t> w_string = PtrToStringChars(s); size_t converted_chars = 0; size_t size_bytes = ((s->Length + 1) * 2); if ((size_bytes / 2) <= output_len) { memset(output, '\0', output_len); if (!wcstombs_s(&converted_chars, output, output_len, w_string, size_bytes)) bRet = true; } return bRet; 2)Can i use System::Generic::Collections such as Dictionary and List for sending data from C# to managedC++ and viceversa? I need to send collection of datasets from managed C++ to C# code. Can i use the List to do this. if not are there any best practices to achieve this? 3)Is it necessary to use gc when i am creating any memory in managed C++ Waiting for ur reply Thanks in advance Sukumar
Hi, 1) I tend to use the msclr::interop::marshal_as templates for string conversions rather than pinning memory as they work in a nice tidy C++ RAII way, clearing up any temporary/locked memory when going out of scope. They handle stl, COM and straight const char * type conversions. See http://msdn.microsoft.com/en-us/library/bb384859.aspx[^] 2) Yes. Works just the same, except with C++/CLI syntax. You will need to do some translation (e.g. to copy into an stl data type) to send to purely native code though. 3) The general rule is if you are creating a managed object, use gcnew, otherwise use straight c++ new. The garbage collector will do the rest. You can still delete sooner if you want to. Hope that helps.
-
Hi, 1) I tend to use the msclr::interop::marshal_as templates for string conversions rather than pinning memory as they work in a nice tidy C++ RAII way, clearing up any temporary/locked memory when going out of scope. They handle stl, COM and straight const char * type conversions. See http://msdn.microsoft.com/en-us/library/bb384859.aspx[^] 2) Yes. Works just the same, except with C++/CLI syntax. You will need to do some translation (e.g. to copy into an stl data type) to send to purely native code though. 3) The general rule is if you are creating a managed object, use gcnew, otherwise use straight c++ new. The garbage collector will do the rest. You can still delete sooner if you want to. Hope that helps.
Hi Is it necessary to use delete when i am using gcnew to create an object i got this code snippet from msdn deletes the context that was created by gcnew is it correct? marshal_context ^ context = gcnew marshal_context(); const char* str4 = context->marshal_as<const char*>(str); puts(str4); delete context;
-
Hi Is it necessary to use delete when i am using gcnew to create an object i got this code snippet from msdn deletes the context that was created by gcnew is it correct? marshal_context ^ context = gcnew marshal_context(); const char* str4 = context->marshal_as<const char*>(str); puts(str4); delete context;
The delete is not necessary as this is a references and the GC will collect that context object when there are no more outstanding references. The delete is probably just attempting to make it explicit and hope to clean up sooner from what I can tell.