Hi,
I have a c++ dll having one structure as follows
//*************************//
typedef unsigned int NodeID;
struct inputNode
{
NodeID id;
BOOLEAN isExit;
};
//************************//
And here is a method as below
//***********************//
void convertToInternalDataStructure (struct inputEdge*, struct inputNode*);
//**********************//
I wrote the wrapper class in C# having structure as below
//Structure
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct inputNode
{
public uint id;
public bool isExit;
}
//ApiCall
[DllImport(DLL_FILENAME, PreserveSig = false)]
public static extern void convertToInternalDataStructure(
IntPtr inputNodeList, IntPtr inputEdgeList);
//Method
public void ConvertToInternalDataStructure(IList<inputNode> inputNodeList,
IList<inputEdge> inputEdgeList)
{
IntPtr ptrNodeList =new IntPtr() ;
IntPtr ptrEdgeList = new IntPtr();
int MyDataSize = Marshal.SizeOf(inputNodeList[0]) * inputNodeList.Count;
ptrNodeList = Marshal.AllocHGlobal(MyDataSize);
MyDataSize = Marshal.SizeOf(inputEdgeList[0]) * inputEdgeList.Count;
ptrEdgeList = Marshal.AllocHGlobal(MyDataSize);
System.Runtime.InteropServices.Marshal.StructureToPtr(inputNodeList, ptrNodeList, true);
System.Runtime.InteropServices.Marshal.StructureToPtr(inputEdgeList, ptrEdgeList, true);
convertToInternalDataStructure(ptrNodeList, ptrEdgeList);
}
the above method getting inputs as a List<structures> But conversion get failed.
How can i fix it? Is it the sufficient way to call this Api.

1 answers
The first thing that strikes me is that the unmanaged function is expecting the arguments in the order: inputEdge, inputNode but your C# method is providing them the other way around.
Also are you sure that the function is expecting arrays of structs, rather than pointers to single structs, as there's no obvious way to determine how many structs are in the array unless this is predefined?
If it's just a single struct you could use ref parameters rather than IntPtrs and call the function repeatedly for each struct to be converted.
If it is an array, then I'd use arrays rather than generic lists as the latter are more than just simple wrappers for resizable arrays.
answered 2 years ago by:
17279
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!