Interop with embedded array within C/C++ data structure

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all

I am looking for a way to retrieve data from the unmanaged code (legacy) DLL to the .NET code as described below. I could NOT find any articles that would help me to do so successfully. Please help

Unmanaged-Code DLL (LEGACY.DLL)
---------------------------

struct EMBEDDED_ARRAY_DATA_STRUCTUR

short * pValueArray
INT32 nElementCount
}

void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData

pData->nElementCount = 5
pData->pValueArray = new short [pData->nElementCount]
for (short i = 0; i < pData->nElementCount; i++
pData->pValueArray = i



Managed-Code
------------------

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)
public struct EMBEDDED_ARRAY_DATA_STRUCTUR

public IntPtr pValueArray
public Int32 nElementCount
}

public class MyLegac

...
[DllImport("legacy.dll", CharSet=Charset.Ansi)
public static extern void FillData(ref EMBEDDED_ARRAY_DATA_STRUCTURE pData)
...
}

...

EMBEDDED_ARRAY_DATA_STRUCTURE pData = new EMBEDDED_ARRAY_DATA_STRUCTURE
MyLegacy.FillData(ref pData)
short [] myShortValueArray = new short [pData->nElementCount]
for (int i = 0; i < pData->nElementCount; i++

myShortValueArray = Marshal.ReadInt16(pData->pValueArray, (int) i)


// The problem is that the contents of myShortValueArray is bogus! Please point out what I've done wrong or if there is
// better way to do so....I am desperately needing help!!!!

Peter N
 
Peter,
void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData)
{
pData->nElementCount = 5;
pData->pValueArray = new short [pData->nElementCount];

You must provide a function to free the memory you allocate with new
in your C++ code, or you'll leak.

short [] myShortValueArray = new short [pData->nElementCount];
for (int i = 0; i < pData->nElementCount; i++)
{
myShortValueArray = Marshal.ReadInt16(pData->pValueArray, (int) i);


ReadInt16 takes a byte offset from the pointer, not an element sized
offset. So the last parameter would have to be i * 2 (2 being
sizeof(short)).

But I'd recommend reading the entire array at once with Marshal.Copy
instead, it should perform better.



Mattias
 
Thank you, Mattias, for your suggestion. I will try it out

Peter N


----- Mattias Sjögren wrote: ----

Peter
void WINAPI FillData(EMBEDDED_ARRAY_DATA_STRUCTURE * pData

pData->nElementCount = 5
pData->pValueArray = new short [pData->nElementCount]

You must provide a function to free the memory you allocate with ne
in your C++ code, or you'll leak

short [] myShortValueArray = new short [pData->nElementCount]
for (int i = 0; i < pData->nElementCount; i++

myShortValueArray = Marshal.ReadInt16(pData->pValueArray, (int) i)


ReadInt16 takes a byte offset from the pointer, not an element size
offset. So the last parameter would have to be i * 2 (2 bein
sizeof(short))

But I'd recommend reading the entire array at once with Marshal.Cop
instead, it should perform better



Mattia
 
Back
Top