CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    9

    Typical issues with 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

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Typical issues with managed C++

    1. To convert String to char* in one line, use Marshal.StringToHGlobalAnsi Method.
    2. Yes. Both languages can use any .NET types.
    3. Depends on object type you need to create. Managed objects are created with gcnew and allocated on managed heap. Unmanaged objects and resources are created using native C++ ways (new, stack allocation, global variables etc.)

    I hope you are working with VS2005 or later, because managed C++ language syntax was completely changed from 2003 to 2005 version. This language is now called C++/CLI. If you are working with VS2003, I strongly recommend to move to a newest VS version before writing any managed C++ code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured