Click here to Skip to main content
14,391,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a following code in a C++ DLL

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.

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.

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
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 4:11am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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]);
}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100