Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Typical issues in managed c++

Typical issues in managed c++

Scheduled Pinned Locked Moved Managed C++/CLI
performancequestioncsharpc++workspace
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sukumarvg
    wrote on last edited by
    #1

    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

    I 1 Reply Last reply
    0
    • S sukumarvg

      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

      I Offline
      I Offline
      ian__lindsay 0
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • I ian__lindsay 0

        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.

        S Offline
        S Offline
        sukumarvg
        wrote on last edited by
        #3

        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;

        J 1 Reply Last reply
        0
        • S sukumarvg

          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;

          J Offline
          J Offline
          John Schroedl
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups