Can a pointer be marshalled to an array when passing a struct? - c#

In the below example, the dataArray defined in C++ works if defined as an array, but not as a pointer (Just turns out garbage data). Is there another way to marshal the C# array so that it reads the pointer in as an array?
C#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CSharpFoo{
int alpha;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5]
public int[] dataArray;
int beta;
}
C++
struct CPPFoo{
int alpha;
//int* dataArray; //Doesn't work, even though initialized to an array elsewhere
int dataArray[5];
int beta;
}
Being passed through a function like this
C#
[DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public extern static bool InitializeDLL([MarshalAs(UnmanagedType.FunctionPtr)] ResultCallback callbackPointer);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void ResultCallback(CSharpFoo value);
C++
//Callback
typedef void(__stdcall * ResultCallback)(CPPFoo);
__declspec(dllexport) bool InitializeDLL(ResultCallback callback);
Thanks in advance!
Edit::
Because "initialized to an array elsewhere" wasn't clear:
CPPFoo(int dummy){ //Constructor
alpha = 32;
dataArray = new int[5];
for (int i = 0; i < 5; i++){
dataArray[i] = i;
}
beta = 13;
}
//dataArray C++ {0,1,2,3,4}
//alpha C# 32
//dataArray C# {Total random garbage} (dataArray[3] is 13!)
//beta C# 0
PS, the CPPFoo struct is a complex struct that comes from a DLL, so I cannot change it. For now, to get things working, I copy it to a more appropriate array like in NonCreature0714's answer, but this results in all of the data being copied--twice. It's this double copy that I'm trying to avoid.
Another edit:
While it seems that for a struct containing a single array the values get passed properly, for a complex struct, garbage gets thrown in.
I have updated the code to reflect a more complex struct!

I did a couple of tests on my local machine, as far as I understood your code. I had no problems with the following code
c# side
struct Foo
{
public int alpha;
public IntPtr Data;
public int beta;
public void GetData(ref int[] buffer,int length)
{
Marshal.Copy(Data,buffer,0,length);
}
}
class Program
{
[DllImport("MyPtr.dll",EntryPoint = "InitializeDLL", CallingConvention = CallingConvention.Cdecl)]
public static extern bool InitializeDLL([MarshalAs(UnmanagedType.FunctionPtr)]ResultCallback callbackPointer);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void ResultCallback(ref Foo value);
static void CallBackMe(ref Foo value)
{
var buffer = new int[5];
value.GetData(ref buffer,buffer.Length);
}
static void Main(string[] args)
{
InitializeDLL(CallBackMe);
}
}
C++ side
struct CPPFoo {
int* dataArrayPtr;
CPPFoo()
{
dataArrayPtr = new int[5];
for (int i = 0; i < 5; i++) {
dataArrayPtr[i] = i;
}
}
};
typedef void(__stdcall * ResultCallback)(CPPFoo);
extern "C" __declspec(dllexport) bool InitializeDLL(ResultCallback callback)
{
CPPFoo f;
callback(f);
return true;
}
Code in action

My experience in C# is very limited but it seems that it works with int dataArray[5]; because the code agrees to the type of CSharpFoo and CPPFoo. The size of both is five ints. When you change the type to a pointer then one is the size of five ints and the other is a single pointer which would be less then five ints.
Most likely passing a dynamically sized array will need a size and to get a pointer you may need to do something with unsafe code but I don't write in C# so that is just a guess.
struct CPPFoo {
int* dataArray = nullptr;
int size = 0;
}

Use a vector.
#include <vector>
struct CPPFoo {
std::vector<int> dataArray;
};
Vector is more readable and more safe. It only slighty increases the size of the code, and is only slightly slower than using a primitive array. You also don’t need to worry about keeping around a variable for size, and updating it every time it’s changes.

Related

Marshal char* from double pointer Struct from C++ to C#

Goal: Retrieve the array of structs from c++, from the c++ side it's finalized to return pointer to struct pointer (Double pointer)(Not in my control).
Sample c++ code :
struct Output
{
char* Name;
};
extern "C"
{
__declspec(dllexport) Output** getoutput()
{
Output* items = (Output*)malloc(sizeof(Output) * 4);
items->Name = "Hello World";
return &items;
}
}
c# Side code :
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Output
{
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Name;
};
[DllImport(#"CPPInvokeExposed.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr getoutput();
static void Main(string[] args)
{
var output = Program.getoutput();
Output[] outputs = new Output[1];
MarshalUnmananagedArray2Struct<Output>(output, 1, out outputs);
**outputs[0]// this has junk chars**
}
public static void MarshalUnmananagedArray2Struct<T>(IntPtr unmanagedArray, int length, out T[] mangagedArray)
{
var size = Marshal.SizeOf(typeof(T));
mangagedArray = new T[length];
for (int i = 0; i < length; i++)
{
IntPtr ins = new IntPtr(unmanagedArray.ToInt64() + i * size);
mangagedArray[i] = Marshal.PtrToStructure<T>(ins);
}
}
Is not clear whether the problem is in c++ or c# code. What should be the correct way get the Char* from c++ which exists in struct.
One strange thing is, for the same code if c++ code return single pointer(Output*) instead double pointer(Output**), there is no junk character, getting the correct assigned value. looks like something wrong while returning double pointer from c++.
You C++ code causes undefined behaviour:
return &items;
This returns the address of an automatic variable.
That variable will not exist anymore when the function returns. Accessing that memory location is illegal and causes undefined behaviour.
A clean solution would be to return the pointer itself, not the address. But in your question you state that the return type is not under your control.
In that case you must create the second level of indirection by yourself:
__declspec(dllexport) Output** getoutput()
{
Output* items = (Output*)malloc(sizeof(Output) * 4);
items->Name = "Hello World";
Output **retval = (Output**)malloc(sizeof(Output*))
*retval = items;
return retval;
}
Of course you also need to take care about freeing both levels of memory allocation afterwards.
BTW:
You allocate memory for 4 structs but only assign a value to member of the first element.

Referencing an object array in C# PInvoke

I'm building a spectrometry application which uses a C# GUI and a native C++ logical dll. I'm trying to make the dll fill an array of simple C++ structs passed by reference from the C# side. However, when I try to print the [supposed to be filled] array elements, I'm getting System.NullReferenceExceptions and the array elements are marked as null in memory.
Here is the C++ struct definition and method implementation:
typedef struct intensitytype {
unsigned short usEchelleOrder; // echelle order
unsigned short usPixel; // horizontal camera pixel index (unbinned !!)
double dIntensity; // intensity
double dWaveLen; // wave length [nm]
} intensitytype;
void CameraControl::getResults(intensitytype* graphData)
{
graphData = _spectroData; // _spectroData is a pointer to a dynamic intensitytype array.
}
Here are the C# class definition and signature
[StructLayout(LayoutKind.Sequential)]
public class intensitytype
{
public ushort usEchelleOrder = 0; // echelle order
public ushort usPixel = 0; // horizontal camera pixel index (unbinned !!)
public double dIntensity = 0; // intensity
public double dWaveLen = 0; // wave length [nm]
}
[DllImport(#"Elemission.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void getResults(bool freeData, out intensitytype[] graphData);
I'm not sure what type of C# reference identifier is needed in this instance, or even if manual pointer marshalling is required. If one of you guys can point me in the right direction, I'd be forever grateful.
I finally found the solution to my problems, and am posting this for anyone who would need some clarification:
First, as David pointed out, simply treating the referenced argument as a pointer and assigning it the array's address does not work. You have to copy the entire array contents into the referenced array. Easily fixed.
The second mistake was in the C# method signature; the descripton that was needed here was "[Out]", with the brackets, compared to simply "out" (Once again, thanks David).
So, the end result:
The structs do not change, but the function signature in C# does:
[DllImport(#"Elemission.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void getResults([Out] intensityType[] graphData);
and here is a snippet of the function call:
int arraySize;
DllMethods.executeAcquisition(out arraySize); // arraySize is the Struct array length
intensityType[] resultData = new intensityType[arraySize];
DllMethods.getResults(resultData);
Meanwhile in C++, the C# call is received by the wrapper and passed to the member function...
__declspec(dllexport) void getResults(intensitytype* graphData)
{
MainControl::getInstance()->getCamera()->getResults(graphData);
}
...and lo and behold, the struct array is filled.
void CameraControl::getResults(intensitytype* graphData)
{
for (int i = 0; i < _spectroDataLength; i++)
graphData[i] = _spectroData[i];
}

Pointer of struct as return value for c++ dll function

I've been several days into this, and I've read a lot of questions that helped me to arrive to where I am right now. But I still need some help.
I will explain. I have a C++ DLL that I want to wrap for use it in c#. I have the documentation of the DLL, but I can't change anything of it. A lot of functions work with basic dllimport setup, but I have some functions that doesn't work properly, this is one of them:
DLL documentation
struct stChannel LookForAvailableChannels (const char *dataBaseFolder, int serialNumber, double firstLogTime, double lastLogTime)
I also have these structs:
struct stChannelInfo
{
char ChannelTag[17];
char ChannelEnabled;
}
struct stChannel
{
int ChannelNumber;
struct stChannelInfo *ChannelInfo;
}
So trying different things, and after reading a lot, I've come to a solution that "partialy" works:
C# Code
[StructLayout(LayoutKind.Sequential)]
public struct stChannelInfo
{
public IntPtr ChannelTag;
public byte ChannelEnabled;
};
[StructLayout(LayoutKind.Sequential)]
public struct stChannel {
public int ChannelNumber;
public stChannelInfo ChannelInfo;
};
[DllImport("NG.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern stChannel LookForAvailableChannels(string dataBaseFolder, int serialNumber, double firstLogTime, double lastLogTime);
stChannel Estructura = new stChannel();
I have a button that calls fires up this code:
Estructura = LookForAvailableChannels("C:\\Folder", 12345678, FechaInicio, FechaFinal);
Then I marshal Estructura.ChannelInfo.ChannelTag:
string btListFile = Marshal.PtrToStringAnsi(Estructura.ChannelInfo.ChannelTag);
This actually works, it returns data I know it's correct. But I'm only receiving the first element of an array, because stChannelInfo struct inside stChannel is a pointer, and I don't know how to handle this in c#.
It should be done in a way that this code I use right now:
Marshal.PtrToStringAnsi(Estructura.ChannelInfo.ChannelTag);
Should be
Marshal.PtrToStringAnsi(Estructura.ChannelInfo[i].ChannelTag);
But everything i've used right now just doesn't work. I would appreciate any help.
Thank you.
EDIT:
Thanks to user Adriano Repetti now I have this:
C# Code
[StructLayout(LayoutKind.Sequential)]
public struct stChannelInfo
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 17)]
public string ChannelTag;
public byte ChannelEnabled;
};
[StructLayout(LayoutKind.Sequential)]
public struct stChannel {
public int ChannelNumber;
public IntPtr ChannelInfo;
};
[DllImport("NG.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern stChannel LookForAvailableChannels(string dataBaseFolder, int serialNumber, double firstLogTime, double lastLogTime);
stChannel Estructura = new stChannel();
I have a button that calls fires up this code:
Estructura = LookForAvailableChannels("C:\\Folder", 12345678, FechaInicio, FechaFinal);
var channelinf = (stChannelInfo)Marshal.PtrToStructure(Estructura.ChannelInfo, typeof(stChannelInfo));
for (int i = 0; i < 4; i++)
{
var ptr = IntPtr.Add(Estructura.ChannelInfo, Marshal.SizeOf(typeof(stChannelInfo)) * i);
var channelll = (stChannelInfo)Marshal.PtrToStructure(ptr, typeof(stChannelInfo));
}
The problem now it's that I get an AccessViolationException here:
Estructura = LookForAvailableChannels("C:\\Folder", 12345678, FechaInicio, FechaFinal);
But I don't really know why, I would appreciate any help.
Unfortunately your array ChannelInfo has not fixed size then automatic marshaling with [MarshalAs(UnamangedType.LPArray)] doesn't work. Doing marshaling manually means that ChannelInfo must be declared as IntPtr:
[StructLayout(LayoutKind.Sequential)]
public struct stChannel {
public int ChannelNumber;
public IntPtr ChannelInfo;
};
When you need it you need to convert it to a struct:
var channelInfo = (stChannelInfo)Marshal.PtrToStructure(
Estructura.ChannelInfo,
typeof(stChannelInfo));
Now you're accessing to first element, to access array items you need an offset:
var ptr = IntPtr.Add(Estructura.ChannelInfo,
Marshal.SizeOf(typeof(stChannelInfo)) * itemIndex);
And then use Marshal.PtrToStructure() over it. You may want to write an helper method:
static GetUnmanagedArrayItem<T>(IntPtr baseAddress, int index) {
var ptr = IntPtr.Add(baseAddress, Marshal.SizeOf(typeof(T)) * index);
return (T)Marshal.PtrToStructure(ptr, typeof(T));
}
Used like this:
var channelInfo = GetUnamangedArrayItem<stChannelInfo>(Estructura.ChannelInfo, 1);
You didn't explicitly ask but note that you can't manually marshal unamanged char* string with IntPtr for a fixed length char array
You may declare it as string decorating that field with `UnmanagedType.LPStr`:
[MarshalAs(UnmanagedType.LPStr, SizeConst=17)]
public string ChannelTag;
EDIT: I was wrong about this, String can't be used in return values because it's not blittable, of course IntPtr is wrong because you have a fixed length array, I'd suggest to use:
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 17)]
public byte[] ChannelTag;
You can get original string simply decoding this array with Encoding.ASCII.GetString(yourStruct.ChannelTag);. As alternative you may follow what suggested by JaredPar in this post.

Pinvoking a native function with array arguments

I am completely confused with how to go about calling functions in native dll with array arguments.
Example:
The function is defined in the C# project as:
[DllImport("Project2.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern void modifyArray([MarshalAs(UnmanagedType.LPArray)] int[] x, int len);
And the function call is:
modifyArray(arr, 3)
where arr = {4,5,6}
The native C++ function definition is as follows:
extern "C" _declspec(dllexport) void modifyArray(int* x,int len)
{
int arr[] = {1,2,3};
x = arr;
}
Why in the C# project, the array is not pointing to the new array after the function call? It still remains {4,5,6}.
I tried this to but failed
[DllImport("Project2.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern void modifyArray([In,Out] int[] x, int len);
This pinvoke fails whenever I try modifying arguments passed to these functions using pointers. Otherwise, I have had success passing ref array arguments for native dll sort functions where there is no pointer changes to newly created types.
Your C++ code is broken. The caller allocates the array, and the callee populates it. Like this:
extern "C" _declspec(dllexport) void modifyArray(int* x, int len)
{
for (int i=0; i<len; i++)
x[i] = i;
}
As far as your p/invoke call goes, SetLastError should not be true. The function is not calling SetLastError. It should be:
[DllImport("Project2.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void modifyArray(int[] x, int len);
This has nothing to do with PInvoke, this is just a plain old C issue. You would have the exact same problem if you called modifyArray from C
int* pArray = NULL;
modifyArray(pArray, len);
pArray == NULL; // true!
In modifyArray you are trying to change where x points to. This change won't be visible to the calling function because the pointer is passed by value. In order to change where it points to you need to pass a double pointer
void modifyArray(int** x, int len) {
*x = ...;
}
Note that you are currently trying to return stack allocated memory instead of heap allocated memory. That is incorrect and will lead to problems down the line

Passing an array of unknown size of structs from c# to c++ dll and back

I asked a similar question yesterday, but this is slightly different. I am having problems passing arrays of struct from c# to c++, and getting this back again.
Here is the c++ code. Firstly, the struct:
struct Tri
{
public:
int v1, v2, v3;
}
and now the c++ dll part:
extern "C" __declspec(dllexport) void Dll_TriArray(Tri *tri)
{
int num = 10;
tri = new Tri[num];
for (int i = 0; i < num; i++)
{
tri[i].v1 = i + 5;
tri[i].v2 = i + 10;
tri[i].v3 = i + 25;
}
}
and here's the c# code, again starting with the struct:
[StructLayout(LayoutKind.Sequential)]
public struct Tri
{
public int v1, v2, v3;
}
public class Testing
{
[DllImport("testing.dll")]
static extern void Dll_TriArray(out Tri[] tryArray);
public GetTriArray()
{
Tri[] triArray;
Dll_TriArray(out triArray);
}
}
So the triArray i get when calling the GetTriArray method will come back as null. I have thought about passing an IntPtr in as the argument, but then how does one marshal an intptr into/from an array of struct?
BTW - at this stage, i'm not interested in memory leaks.
I'm not an expert (by any means) in C# but the C++ part gets passed a pointer to Tri-struct, which in C++ can be used like an dynamic array you allocate and fill that correctly but you don't have a way to get it back, because from C-perspective you'd need to modify the caller's (C#) pointer but you only get a copy and not a reference to the original.
In C++ the closest thing to what you are tying to do, would be to change the prototype to void Dll_TriArray(Tri *&tri) (call by ref, not call by copy) but I'm not sure how to interface that with C# (probably Dll_TriArray(ref triArray); ).

Categories

Resources