gpt4 book ai didi

c# - 将指针从 C# 传递到 C++

转载 作者:行者123 更新时间:2023-11-28 02:41:44 25 4
gpt4 key购买 nike

我正在尝试将 2D 掩码(全为 0,期望感兴趣区域为 1)从 C#(作为 short[])传递给 C++(作为 unsigned short*),但我无法在 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;
}

但是我在 C++ 中使用监 window 口只能看到 52536 而不是 0000111100000000,所以我想知道我在哪里搞砸了?任何人都可以帮忙吗?谢谢。

最佳答案

我相信你放错了 memcpy 的参数:

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

据我了解,您想从 mask 复制到 _pMask,因此您应该这样写:

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

关于c# - 将指针从 C# 传递到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25733844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com