Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a following code in a C++ DLL

C++
typedef struct _MYPOINT
{
    int nX;
    int nY;
} MYPOINT;
 
extern "C"  __declspec(dllexport) void ModifyPointArray (int nSize, MYPOINT *ptArr[]) 
{ 
    for ( int nI = 0; nI < nSize; nI++ )
    {
	ptArr[nI]->nX +=5;
	ptArr[nI]->nY +=5;
    }
}


======================================

I have defined a Wrapper Class for the DLL something like this.

C#
namespace MarshalDemo
{
    [StructLayout(LayoutKind.Sequential)]
    public class MyPoint
    {
        public int nX;
        public int nY;
        
        public MyPoint()
        {
            this.nX = 0;
            this.nY = 0;
        }
        public MyPoint(int x, int y)
        {
            this.nX = x;
            this.nY = y;
        }
    }
   
    public static class MarshalDllWrapper
    {
     
      [DllImport("MarshalDll.dll")]
      public static extern void ModifyPointArray(int nSize, IntPtr [] arrPts);
    }
}


What I have tried:

Now i am trying to call this function using Marshalling using IntPtr, my .NET client code (C#) is something like this.

C#
MyPoint[] pointArr = { new MyPoint(5, 6), new MyPoint(7, 8), new MyPoint(9, 10) };
int nCount = pointArr.Length;
IntPtr[] ptrArr = new IntPtr[nCount];
for (int nI=0; nI< nCount; nI++)
{
     ptrArr[nI] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(MyPoint)));
     Marshal.StructureToPtr(pointArr[nI], ptrArr[nI], true);
}

MarshalDllWrapper.DisplayPointArray(nCount, ptrArr);
MyPoint[] pointArr2 = new MyPoint[nCount];
for (int nI = 0; nI < nCount; nI++)
{
     Marshal.PtrToStructure(ptrArr[nI], pointArr2[nI]);
     Marshal.FreeCoTaskMem(ptrArr[nI]);
}


I am getting an Error on line
//F:\VS2010\VCS2010\Marshalling\MarshalDll\MarshalDllClient\frmMarshalDemo.cs:line XXX
C#
Marshal.PtrToStructure(ptrArr[nI], pointArr2[nI]);


with Error Message

System.ArgumentNullException: Value cannot be null.
Parameter name: structure
at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
at System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr ptr, Object structure)
at MarshalDemo.frmMarshalDemo.btnPointArray_Click(Object sender, EventArgs e) in F:\VS2010\VCS2010\Marshalling\MarshalDll\MarshalDllClient\frmMarshalDemo.cs:line 150


PS : I know if i write a Wrapper something like this.
public static extern void ModifyPointStructArray(int nSize, [In, Out] MyPoint[] arrStruc);

it works like a breeze, but i want to do with IntPtr, am i doing something conceptually wrong ?

What I am doing wrong ?
Any Help will will apreciated.

Thanks.

Praveen.
Posted
Updated 3-Dec-19 3:11am

1 solution

structure: The object to which the data is to be copied. This must be an instance of a formatted class.
You've created a new array of MyPoint, but you haven't initialized it. Therefore, every slot within the array is null, and you're trying to pass null into the structure parameter.

Initialize the value before you pass it in:
C#
MyPoint[] pointArr2 = new MyPoint[nCount];
for (int nI = 0; nI < nCount; nI++)
{
    pointArr2[nI] = new MyPoint();
    Marshal.PtrToStructure(ptrArr[nI], pointArr2[nI]);
    Marshal.FreeCoTaskMem(ptrArr[nI]);
}
 
Share this answer
 
Comments
Praveen Kumar Katiyar 3-Dec-19 9:20am    
You saved my day, what a silly mistake.

Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900