Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.3K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda