gpt4 book ai didi

c# - 使用 ATL/COM 将托管字节 [] 转换为非托管字节数组

转载 作者:太空狗 更新时间:2023-10-29 20:44:25 28 4
gpt4 key购买 nike

我想使用 ATL/COM 将一些图像数据从 C# 代码传递到非托管 C++

从 C# 代码方面我做了这样的事情:

void SendFrame([In, MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_UI1)] ref byte[] frameData);

但我不确定我应该如何在我的 C++ 代码中处理这个函数。

现在我有这样的东西:

_ATL_FUNC_INFO OnSendFrameDef = { CC_STDCALL, VT_EMPTY, 1, { VT_SAFEARRAY | VT_UI1 } };

void __stdcall OnSendFrame(SAFEARRAY* ppData)
{
BYTE* pData;
SafeArrayAccessData(ppData, (void **) &pData);

// Doing some stuff with my pData

SafeArrayUnaccessData(ppData);
}

任何人都可以给我一些建议,我可以如何让这个东西发挥作用吗?

谢谢。

最佳答案

SAFEARRAY已经编码到您的非托管代码,并且您正在使用 ATL,您可以使用 CComSafeArray class ,因为它在处理 SAFEARRAY 实例时处理所有清理工作:

_ATL_FUNC_INFO OnSendFrameDef = { CC_STDCALL, VT_EMPTY, 1, 
{ VT_SAFEARRAY | VT_UI1 } };

void __stdcall OnSendFrame(SAFEARRAY* ppData)
{
// Wrap in a CComSafeArray.
// On the stack means all calls to cleanup
// will be cleaned up when the stack
// is exited.
CComSafeArray<byte> array;
array.Attach(ppData);

// Work with the elements, get the first one.
byte b = array.GetAt(0);

// And so on. The destructor for CComSafeArray
// will be called here and cleaned up.
}

关于c# - 使用 ATL/COM 将托管字节 [] 转换为非托管字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291267/

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