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 9231959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:11:56+00:00 2026-06-18T06:11:56+00:00

In the past, I have passed a byte array from a C# method to

  • 0

In the past, I have passed a byte array from a C# method to an unmanaged C++ function. I am now trying to pass a pointer to a buffer of type unsigned char from a C++ method back into a C# method using reverse PInvoke, which uses a callback to get back to the C# code. I have tried several different ideas – like passing Ref Byte, Byte *, and IntPtr for the 2nd argument, but none of them seem to work. Here is my test code for using IntPtr:

C# code:

namespace TestPInvoke
{
   class Program
   {
      static void Main(string[] args)
      {
         foo f = new foo();
         f.DispMsg();
      }
   }

   unsafe public class foo
   {
      public delegate void callback(int NumBytes, IntPtr pBuf);

      public static void callee(int NumBytes, IntPtr pBuf)
      {
         System.Console.WriteLine("NumBytes = " + NumBytes.ToString() + ", pBuf = ");
         String s = "";
         Byte* p = (Byte*)pBuf.ToPointer();
         for (int Loop = 0; Loop < 50; Loop++)
         {
            s += p++->ToString() + "  ";
         }
         System.Console.WriteLine(s);
      }

      public void DispMsg()
      {
         caller(new callback(foo.callee));
      }

      [DllImport(@"C:\Users\Bob\Documents\Visual Studio 2008\Projects\AttackPoker1\Win32Client\TestPInvoke\bin\Debug\TestPInvokeDLLCPP.dll", CallingConvention = CallingConvention.StdCall)]
      public static extern void caller(callback call);
   }

}

C++ code:

#include <stdio.h>
#include <string.h>

typedef unsigned char Byte;

typedef void (__stdcall *callback)(const int bytesInMsg, Byte* pintBuf);

extern "C" __declspec(dllexport) void __stdcall caller(callback call)
{
   // Debug Test on how to pass a pointer to a byte buffer to a C# method.
   Byte* pBuf = new Byte[50];
   // Initialize the buffer to something.
   Byte* p = pBuf;
   for (Byte Loop = 0; Loop < 50; Loop++)
      *p = Loop;
   // Initiate the callback into the C# code.
   call(50, pBuf);
   // Delete pBuf later.
}

When the C++ code calls the C# callback callee method, the bytesInMsg argument is correct. But, the returned pointer does not point to the start of the buffer. Dereferencing the pointer always seems to point to the last value in the buffer (49 or 0x31), but after looking at it in the memory window, the rest of the bytes both before and after are garbage.

Does anyone have any suggestions on how I can get this to work without marshaling a large array? What I’m hoping to do is pass a pointer to a large buffer created on the C++ side one time to a C# class that will then be able to read data from that buffer efficiently.

If this can not be done, then I will have to allocate the memory buffers from C#, pin them, and pass them into the C++ methods.

  • 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-06-18T06:11:57+00:00Added an answer on June 18, 2026 at 6:11 am

    All the pinvoke is fine and works properly. You just have a silly bug in your C++ code, you are forgetting to increment the pointer so you only ever set the first element of the array. Use

       *p++ = Loop;
    

    Or the more sane version that simply indexes the array:

       // Initialize the buffer to something.
       for (int ix = 0; ix < 50; ++ix)
           pBuf[ix] = ix;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying for the past two days now to implement Admob ads
I have question regarding the use of function parameters. In the past I have
Excuse my limited knoweldge here. In the past I have used Steve Sanderson's method
For the past couple of days, I have been trying to get GWT to
I have a table the contains appointments. These appointments have different statuses (byte from
What I currently have: I built html based off options passed into a function
I have an int/string(s) that is passed from one fragment to another. For each
I have a C# application that needs to get variable-length data passed back from
In the past I have used MAPISendMail to launch Outlook (or whatever the desired
In the past I have used Managed Direct X and DirectShow.Net to play 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.