Marshal C++ int array to C#
[DllImport("wrapper_demo_d.dll")]
public static extern IntPtr fnwrapper_intarr();
IntPtr ptr = fnwrapper_intarr();
int[] result = new int[3];
Marshal.Copy(ptr, result, 0, 3);
You need also to write Release function in unmanaged Dll, which deletes pointer created by fnwrapper_intarr. This function must accept IntPtr as parameter.
DLL_EXPORT void fnwrapper_release(int* pArray)
{
delete[] pArray;
}
[DllImport("wrapper_demo_d.dll")]
public static extern void fnwrapper_release(IntPtr ptr);
IntPtr ptr = fnwrapper_intarr();
...
fnwrapper_release(ptr);
How To Marshal Int Arrays Or Pointers To Int Arrays
It isn't a SafeArray. A SafeArray is something connected to Variants and the good old times of OLE :-) It probably lives in the dictionary near the word "dodo".
It is:
[DllImport(dllName, CallingConvention=CallingConvention.StdCall)]
static extern void ReadStuff(int id, int[] buffer);
the marshaler will do the "right" thing.
or
[DllImport(dllName, CallingConvention=CallingConvention.StdCall)]
static extern void ReadStuff(int id, IntPtr buffer);
but then it's more complex to use.
The CallingConvention=CallingConvention.StdCall
is the default one, so it isn't necessary to write it out explicitly.
You use this way:
// call
int[] array = new int[12];
ReadStuff(1, array);
A ref int[]
would be a int**
(but it could be complex to pass, because normally you RECEIVE the array, not SEND the array :-) )
Note that your "interface" is quite poor: you can't tell to ReadStuff
the length of your buffer, nor can you receive the necessary length of the buffer, nor can you receive the number of characters of the buffer that were really used.
how to marshal c++ array to c# via IntPtr
The issue appears to be that C++ uint8_t
is an unsigned byte, and C# int
is a signed 4 byte integer. So you have a simple mismatch of types. The C# type that matches uint8_t
is byte
.
Your callback should be:
void callbackTester(IntPtr pData, uint length)
{
byte[] data = new byte[length];
Marshal.Copy(pData, data, 0, (int)length);
}
Marshal C++ struct array into C#
I would try adding some attributes to your struct decloration
[StructLayout(LayoutKind.Sequential, Size=TotalBytesInStruct),Serializable]
public struct LPRData
{
/// char[15]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
public string data;
/// int[15]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 15)]
public int[] prob;
}
*Note TotalBytesInStruct is not intended to represent a variable
JaredPar is also correct that using the IntPtr class could be helpful, but it has been quite awhile since I have used PInvoke so I'm rusty.
How do I marshal a structure containing a array of int of unknown size?
You can't.
You should allocate and free memory by same runtime library.
Related Topics
How Set Value a Property Selector Expression<Func<T,Tresult>>
Why Is "Null" Present in C# and Java
Use Decimal Values as Attribute Params in C#
Using a Wwwroot Folder (ASP.NET Core Style) in ASP.NET 4.5 Project
How to Kill a Session or Session Id (Asp.Net/C#)
Difference Between SQLdatareader.Read and SQLdatareader.Nextresult
Signed VS. Unsigned Integers for Lengths/Counts
Filesystemwatcher to Watch Unc Path
How to Access Resourcedictionary in Wpf from C# Code
How to Ensure Entries with Non-Overlapping Time Ranges
Local Variable and Expression Trees
How to Set PDF Paragraph or Font Line-Height with Itextsharp
Encryption Compatible Between Android and C#