Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
Marshalling C# byte[] a C++ DLL con DllImport

Estoy experimentando un comportamiento extraño al organizar una matriz de bytes [] de C# en C++.

Cuando se pasa el byte[] como argumento, obtengo los datos esperados en C++. (Ver ReportData)

Cuando el byte [] está envuelto en una estructura, obtengo valores extraños. (Ver ReportBuffer)

¿Qué está causando esta diferencia en el comportamiento y hay alguna forma de corregirlo, ya que necesito tener los datos envueltos en un caso de uso más complejo?

C# Código de llamada

 public struct Buffer { public int DataLength; public byte[] Data; public Buffer(byte[] data) : this() { Data = data; DataLength = data.Length; } } internal class Program { [DllImport(@"C:\Users\lawsm\source\repos\MarshallingTest\Debug\Test.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void ReportBuffer(Buffer buffer); [DllImport(@"C:\Users\lawsm\source\repos\MarshallingTest\Debug\Test.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void ReportData(byte[] data, int dataCount); private static void Main(string[] args) { byte[] data = new byte[] {0, 1, 2, 3, 4, 5}; Buffer buffer = new Buffer(data); Console.WriteLine("Report Buffer"); ReportBuffer(buffer); Console.WriteLine("\n\nReport Data"); ReportData(data, data.Length); Console.ReadKey(); } }

Código DLL de C++

 #include <cstdint> #include <iostream> struct Buffer { public: int DataLength; uint8_t* Data; }; extern "C" { _declspec(dllexport) void ReportBuffer(const Buffer& buffer) { for (int i = 0; i < buffer.DataLength; i++) { std::cout << (int)buffer.Data[i] << std::endl; } } _declspec(dllexport) void ReportData(uint8_t* data, int dataLength) { for (int i = 0; i < dataLength; i++) { std::cout << (int)data[i] << std::endl; } } }

Salida de consola

 Report Buffer 1 0 128 0 1 0 Report Data 0 1 2 3 4 5
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Descubrí una solución cambiando la matriz byte[] a un IntPtr, asignando el espacio y copiando los datos.

 [StructLayout(LayoutKind.Sequential)] public struct Buffer { public int DataLength; public IntPtr Data; public Buffer(byte[] data) : this() { Data = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, Data, data.Length); DataLength = data.Length; } } [DllImport("Buffer.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void ReportBuffer(Buffer buffer);

El código C++ sigue siendo el mismo:

 struct Buffer { public: int DataLength; uint8_t* Data; }; extern "C" { _declspec(dllexport) void ReportBuffer(const Buffer& buffer) { std::cout << buffer.DataLength << std::endl; for (int i = 0; i < buffer.DataLength; i++) { std::cout << (int)buffer.Data[i] << std::endl; } } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!