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

  • SEARCH
  • Home
  • 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 6173697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:17+00:00 2026-05-23T23:37:17+00:00

I have an application that is written in C# and uses a DLL written

  • 0

I have an application that is written in C# and uses a DLL written in C.
Through some delegate (function pointer), I have managed to invoke a C function. This function is expected to do a lot of processing on some data and then return the processed binary data back to C# code along with size of data.

The prototype for managed c# function is:

private unsafe delegate void MyCallback (IntPtr cs_buf, Int32 cs_size);

And I am calling this from my C code as:

void* c_buf = NULL;
int c_size = 0;

.... some processing here to fill buf and size......

MyCallback (c_buf, c_size);

In the managed C# code, I need to call a function from MyCallback that has the prototype:

void foo (byte[] cs_buf, int cs_size)

Now there is no problem with the cs_size value, but what is the correct way to use/pass the binary buffer from C code to C# code so that it can be used as a byte[] in C# code.

If what I am doing is the correct way, what should be the recommended way of converting the received IntPtr cs_buf to byte[]?

Thanks,
Vikram

  • 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-23T23:37:18+00:00Added an answer on May 23, 2026 at 11:37 pm

    You should use Marshal.Copy

    Marshal.Copy Method (IntPtr, Byte[], Int32, Int32)

    Copies data from an unmanaged memory pointer to a managed 8-bit
    unsigned integer array.

    Assuming that cs_size is the size in bytes :

    var array = new byte[cs_size];
    Marshal.Copy(pointer, array, 0, cs_size);
    foo(array, cs_size);
    

    By the way int this case foo() don’t need to take a cs_size parameter as it could use the .Length property of the array instead.


    Also with this, as the C# code copy the array, you could free() the buffer from the C code just after calling the callback.

    Otherwise you will need either to export a mylib_free() method from C or use a known memory allocator (NOT malloc) like LocalAlloc (From C) and Marshal.FreeHGlobal (From C#) under Windows.

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

Sidebar

Related Questions

I have an application written in PHP that uses a COM dll written in
I have written a C# console application that uses the Interop.domino.dll assembly to interact
I have a VB6 application that uses a COM DLL. The DLL is written
I have written a PHP application that uses Objects heavily. Added, deleting, updating etc..
I have written a little console application that uses s#arp. I can create an
I have an application written using C++ Builder 5 that uses the TDocument class.
I have a Qt application written in C++ that uses a SSL-connection (QSslSocket) with
I have C# application that makes use of some C libaries(which I have written
I have written an application that uses the Tinymce editor. The administrator can create
I have never written any code that uses threads. I have a web application

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.