passing C# structs (with array) to unmanaged C++ (DLL)

G

Guest

Hi
since no forum can't help me, I hope this one does

My Problem

I have to use an unmanaged C++-DLL (Windows CE), which I do import by P/Invoke in my C# project. One of the DLL-functions needs as a parameter a reference to a struct.

I built this struct in my C# project, but unfortunatelly I can't set the array size (of byte) to
fix size (only in the constructor of the structure)

The struct looks like this
public struct sStruc

public byte number
public byte figure
public public byte[] byteArray

public sStruct(byte num, byte fig, int size

this.number = num
this.figure = fig
byteArray = new byte[size]


The prototyp after DLLimport looks like this
public static extern void miniDLL_ComplexReference(ref sStruct structure)

The problem is, that by call of the function the size of my struct is unknow
and the array inside the passed struct is not filled correctly

I tryed to put the struct into a class.... but I can't marshal it correctly, since C
doesn't suppoert "MarshalAs" or "FieldOffset"


Ist there a way to get this problem solved?
 
A

Alex Yakhnin [MVP]

What's the C++ prototype of your structure? The byte array inside of your
structure will not be marshalled correctly. The soltion for you could be
passing a byte array instead of the structure.
Take a look at my article for ideas how to do that:
http://msdn.microsoft.com/library/d.../dnnetcomp/html/ProcessManager.asp?frame=true

--
Alex Yakhnin .NET CF MVP
www.intelliprog.com | www.opennetcf.org

Mike Novy said:
Hi,
since no forum can't help me, I hope this one does.

My Problem:

I have to use an unmanaged C++-DLL (Windows CE), which I do import by
P/Invoke in my C# project. One of the DLL-functions needs as a parameter a
reference to a struct.
I built this struct in my C# project, but unfortunatelly I can't set the array size (of byte) to a
fix size (only in the constructor of the structure).


The struct looks like this:
public struct sStruct
{
public byte number;
public byte figure;
public public byte[] byteArray;

public sStruct(byte num, byte fig, int size)
{
this.number = num;
this.figure = fig;
byteArray = new byte[size];
}


The prototyp after DLLimport looks like this:
public static extern void miniDLL_ComplexReference(ref sStruct structure);


The problem is, that by call of the function the size of my struct is unknown
and the array inside the passed struct is not filled correctly.

I tryed to put the struct into a class.... but I can't marshal it correctly, since CF
doesn't suppoert "MarshalAs" or "FieldOffset".



Ist there a way to get this problem solved??
 
A

Alex Yakhnin [MVP]

The only way to handle complex marshalling in CF is the one I've already
mentioned.
 
G

Guest

ok... but what is with handling of an array of structs in a structure
I don't really know, how to create a byte array of this types of struct (array of structs)
 
G

Guest

i read your article, but still this one does not work

public class sStructWrappe

// Cons
private const int offsetNumber = 0; // 0..
private const int offsetFigure = 4; // 8..1
private const int offsetUCcount = 8; // 12..1
private const int offsetArray = 12; // 4..
private const int structSize = 20;

// Dat
public uint number
public uint[] myArray = new uint[2]
public uint figure
public uint ucCount
....
....



only the first element of the array is filled. here the c++ code
typedef struct sStruc

unsigned int number
unsigned int figure
unsigned int ucCount
unsigned int byteArray[2]

} STRUCTURE

and
MINIDLL_API void miniDLL_ComplexReferenceDouble(STRUCTURE *structure

structure->number = 123
structure->figure = 234
structure->byteArray[0] = 99
structure->byteArray[1] = 55
structure->ucCount = 11


What do I have to do?
 
G

Guest

ok, an array in the struct seems to work, i forgot to set an offset by extracting the whole array for my specieal array byte.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top