c# to c++ marshalling int[] array

M

mitchell.chrisj

Is there any specific method for calling a c++ dll that requires a int
array from c#. I have tried using int[] array1 = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; but the values aren't being
received correctly by the c++ dll.
 
J

Jeroen Mostert

Is there any specific method for calling a c++ dll that requires a int
array from c#. I have tried using int[] array1 = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; but the values aren't being
received correctly by the c++ dll.

Without the signature of the C++ function and the way you've attempted to
call it it's impossible to help you.
 
P

Peter Duniho

Is there any specific method for calling a c++ dll that requires a int
array from c#. I have tried using int[] array1 = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; but the values aren't being
received correctly by the c++ dll.

See the MarshalAsAttribute attribute:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx

Here is some more detail on dealing with arrays in interop:
http://msdn.microsoft.com/en-us/library/z6cfh6e6.aspx
 
M

mitchell.chrisj

Is there any specific method for calling a c++ dll that requires a int
array from c#. I have tried using int[] array1 = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; but the values aren't being
received correctly by the c++ dll.

Without the signature of the C++ function and the way you've attempted to
call it it's impossible to help you.

Hi Thanks for the response here is the code used on the c# side.

[DllImport("mydll.dll", CharSet = CharSet.Auto, EntryPoint = "Init")]
public static extern unsafe IntPtr Init(byte[] dir, int Number1, int
Number2, int Number3, byte[] byteArray1, byte[] byteArray2, int[]
intarray1, byte singlebyte, int Number4, int Number5, int SingleInt,
byte SingleByte);

Calling it:

int[] intarray1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16 };
//Initialise
handle = InitChannel(dir, Number1, Number2, Number3, byteArray,
byteArray2, intarray1, 0, 0, 0, 0, 1);

C++ side in the dll.

extern "C" MYEXPORT void* __stdcall Init(char *dir, int Number1, int
Number2, int Number3, bool *byteArray, bool *byteArray2, int *
intarray1,
bool boolsingle, int intsingle, int intsingle2, int intsingle3,
bool boolsingle2)

All of the other arguments are received properly just not the int[]
array. Any help would be appreciated.
 
J

Jeroen Mostert

Is there any specific method for calling a c++ dll that requires a int
array from c#. I have tried using int[] array1 = { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; but the values aren't being
received correctly by the c++ dll.
Without the signature of the C++ function and the way you've attempted to
call it it's impossible to help you.

Hi Thanks for the response here is the code used on the c# side.

[DllImport("mydll.dll", CharSet = CharSet.Auto, EntryPoint = "Init")]
public static extern unsafe IntPtr Init(byte[] dir, int Number1, int
Number2, int Number3, byte[] byteArray1, byte[] byteArray2, int[]
intarray1, byte singlebyte, int Number4, int Number5, int SingleInt,
byte SingleByte);
There appears to be no argument here to specify the size of any of the
arrays, unless one of the cryptically named "number" parameters is supposed
to be it. If you are obfuscating names to prevent us from learning Business
Secrets (tm), you have unfortunately succeeded in making the scenario
incomprehensible.
Calling it:

int[] intarray1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16 };
//Initialise
handle = InitChannel(dir, Number1, Number2, Number3, byteArray,
byteArray2, intarray1, 0, 0, 0, 0, 1);

C++ side in the dll.

extern "C" MYEXPORT void* __stdcall Init(char *dir, int Number1, int
Number2, int Number3, bool *byteArray, bool *byteArray2, int *
intarray1,
bool boolsingle, int intsingle, int intsingle2, int intsingle3,
bool boolsingle2)

All of the other arguments are received properly just not the int[]
array. Any help would be appreciated.
There is nothing special about marshalling an int[], and if the byte[]
arguments do get received correctly, so should the int[] be. It's most
likely you're not supplying the correct arguments because you are
misunderstanding the way the C++ function interprets them.

Peter gave you the two links that will allow you to solve the problem
yourself. In particular, pay attention to applying the [In] and [Out]
attributes to indicate to the marshaller whether an argument is an input or
an output argument, and the SizeParamIndex and SizeConst properties of the
MarshalAs attribute for passing arrays.
 

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