Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:15:52+00:00 2026-05-23T19:15:52+00:00

I am wrapping some native C++ code in a C++/CLI .dll for use in

  • 0

I am wrapping some native C++ code in a C++/CLI .dll for use in .NET projects – mainly C#. The number of calls will be large so I am looking to do this in an effective way. The function I am wrapping takes the following arguments:

int SomeFun(
       const char* input_text,
       int* output_array,
       bool* output_value);

I know how to do an efficient System::String to const char* cast thanks to this answer. My questions are these:

  1. The function expects pointers to output_array and output_value which need to be created/cleaned-up and returned to the managed component all inside the wrapper. How do I do that?
  2. How do I return multiple values to the managed environment from the wrapper .dll – by using a struct as a return value?
  3. For now I am trying to do all the pointer handling and managed/unmanaged interaction inside the wrapper .dll but this article (Solution 3) suggests that using an “unsafe” environment inside C# is the fastest. Is this likely to be a better option for what I am trying to do? I guess it would make the wrapper less complex but also require more elaborate treatment in C#.

Thanks,

/David

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:15:53+00:00Added an answer on May 23, 2026 at 7:15 pm

    You can pin arrays the same way you pin strings, e.g. for byte[]:

    pin_ptr<Byte> ptrBytes = &myArray[myArray->GetLowerBound(0)];
    

    ptrBytes can now be used as an unsigned char*

    You can pin single variables that come from C#, too, for example with a parameter that is a ref to an int called int% ival:

    pin_ptr<int> pInt =   &ival;
    

    pInt can now be used as an int*

    You can create arrays with gcnew, for example to create an array of Bytes to return to C#:

    array<Byte>^ streambuf = gcnew array<Byte>(bufSize+16);
    

    You can now fill this with streambuf[index], repacking a C++ array in a loop, or you could pin it and do a memcpy before returning it to C#.

    I do not usually return structs. I usually define each parameter in the C++/CLI function wrapper. For example if a struct had two ints and a string, I’d make each a parameter with ref keyword (% in C++/CLI, e.g. int% ival). if you must return a large number of values it’s easiest to return a class you create with gcnew, not a struct.

    Using unsafe code is the way to go if you are trying to speed up array access and your C# code will be straightforward. If you already have C++ code and it uses the STL and is very complex and you don’t want to rewrite it, especially if you have lots of pointer arithmetic and casts, stick with a C++/CLI wrapper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings! I'm working on wrapping my head around LINQ. If I had some XML
I am wrapping existing C++ code from a BSD project in our own custom
I am wrapping a native C++ class, which has the following methods: class Native
I am wrapping up a project in which I used jQuery for the first
I'm still wrapping my head around DDD, and one of the stumbling blocks I've
I am having a problem with links wrapping. How do I prevent this? ![Wordwrap][1]
Should I make my own framework by wrapping up the STL classes and/or Boost
I'm wondering if word wrapping should be applied in text emails? And what about
Been having a heated debate with a colleague about his practice of wrapping most
I'd like to know when i should and shouldn't be wrapping things in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.