Ono
Ono

Reputation: 1357

Passing pointer from C# to C++

I am trying to pass a 2D mask (all 0s, expect for a region of interest as 1s) from C# ( as short[]) to C++ (as unsigned short*), but I cannot get the right value in C++.

C#

[DllImport("StatsManager.dll", EntryPoint = "SetStatsMask")]
private static extern int SetStatsMask(IntPtr mask, int imgWidth, int imgHeight);

short[] mask;
mask = new short[8*8];
// some operation here making a ROI in mask all 1.  ex 0000111100000000 in 1D 
IntPtr maskPtr = Marshal.AllocHGlobal(2 * mask.Length);
Marshal.Copy(mask, 0, maskPtr, mask.Length);
SetStatsMask(maskPtr, width, height);

C++

long StatsManager::SetStatsMask(unsigned short *mask, long width, long height)
{
    //create memory to store the incoming mask
    //memcpy the mask to the new buffer 
    //pMask = realloc(pMask,width*height*sizeof(unsigned short));

    long ret = TRUE;

    if (NULL == _pMask)
    {
        _pMask = new unsigned short[width * height];
    }
    else
    {
        realloc(_pMask,width*height*sizeof(unsigned short));
    }

    memcpy(mask,_pMask,width*height*sizeof(unsigned short));

    SaveBuffer(_pMask,  width,  height);

    return ret;
}

But all I can see for mask in C++ using watch window is 52536 instead of 0000111100000000, so I am wondering where I messed up? Anyone can help? Thanks.

Upvotes: 0

Views: 1625

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

I believe you misplaced the parameters of memcpy:

memcpy(mask,_pMask,width*height*sizeof(unsigned short));

As I understand you want to copy from mask to _pMask, so you should write:

memcpy(_pMask, mask, width*height*sizeof(unsigned short));

Upvotes: 2

Related Questions