gpt4 book ai didi

c# - 将字节数组从 C# 传递到 C++ (COM)

转载 作者:行者123 更新时间:2023-11-30 03:34:44 24 4
gpt4 key购买 nike

我正在尝试将一个字节数组从 c# 传递到 c++。我正在使用组件对象模型。这是我第一次体验 COM。如何将字节数组从 C# 传递到 C++?有什么建议吗?当我尝试传递二进制数组以外的其他类型(字符串、整数等)时没有问题

谢谢

我得到的错误

1-)error C2440: '=' : cannot convert from 'SAFEARRAY' to 'byte'     
2-)IntelliSense: no suitable conversion function from "SAFEARRAY" to "byte" exists

这是我写的代码

C# 端,

public byte[] GetImage()
{

try
{

SqlCommand command = new SqlCommand("this command returns Varbinary ", conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
ImgBlobDT = new DataTable("ImgBlobDT");
dataAdapter.Fill(ImgBlobDT);
DataRow dr = ImgBlobDT.Rows[0];
imgBytes = (byte[])dr["ImgBinary"];

}
catch ()
{
//some codes
}


return imgBytes;

}

C++ 方面

CoInitialize(NULL);

IDBCPtr obj;

obj.CreateInstance(__uuidof(DBC));

byte bytesArr[] = obj->GetImage();

CoUninitialize();

最佳答案

您要么需要按照@KimKulling 的回答建议分配不安全内存,要么将数组作为适当标记的SafeArray 发送。下面摘录了一个演示三种不同方法的示例,并且 available on github .

C# 界面看起来像这样:

[ComVisible(true)]
[Guid("7FE927E1-79D3-42DB-BE7F-B830C7CD32AE")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IImageProvider
{
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
byte[] GetImage(int foo);

IntPtr GetImageAsUnmanaged(int foo, out int cbBuff);

void GetImageAsUnmanagedPreallocated(int foo, ref int cbBuff, IntPtr pBuff);
}

使用 C# 服务器实现:

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("D133B928-A98B-4006-8B00-4AA09BD042E7")]
[ProgId("CSByteArrayServer.ImageProvider")]
public class ImageProvider : IImageProvider
{
private const int E_INVALIDARG = unchecked((int)0x80070057);

RNGCryptoServiceProvider crng = new RNGCryptoServiceProvider();

[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
public byte[] GetImage(int foo)
{
return GetBytes();
}

private byte[] GetBytes()
{
byte[] data = new byte[500];
crng.GetBytes(data);
return data;
}

public IntPtr GetImageAsUnmanaged(int foo, out int cbBuff)
{
var data = GetBytes();

var result = Marshal.AllocCoTaskMem(data.Length);
Marshal.Copy(data, 0, result, data.Length);

cbBuff = data.Length;
return result;
}

public void GetImageAsUnmanagedPreallocated(int foo, ref int cbBuff, IntPtr pBuff)
{
var data = GetBytes();

if (cbBuff < data.Length)
{
cbBuff = data.Length;
throw Marshal.GetExceptionForHR(E_INVALIDARG);
}

cbBuff = data.Length;
Marshal.Copy(data, 0, pBuff, data.Length);
}
}

还有一个 C++ 客户端:

int main()
{
CoInitialize(NULL);

IImageProvider *pImgProvider;
CoCreateInstance(__uuidof(ImageProvider), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IImageProvider), (LPVOID*)&pImgProvider);

int imageId = 0;

// with SafeArray
{
SAFEARRAY *pSafeArray;
auto hr = pImgProvider->GetImage(0, &pSafeArray);
assert(hr == S_OK);
CComSafeArray<BYTE> safeArray;
safeArray.Attach(pSafeArray);
ManipulateData(&safeArray);
// CComSafeArray will free the memory allocated by pSafeArray
}

// with CoTaskMemAlloc
{
char *pData;
int cbData;
auto hr = pImgProvider->GetImageAsUnmanaged(0, (long*)&cbData, (long*)&pData);
assert(hr == S_OK);

ManipulateData(pData, cbData);
CoTaskMemFree(pData);
}

// with caller allocate
{
int cbData;
auto hr = pImgProvider->GetImageAsUnmanagedPreallocated(imageId, (long*)&cbData, (long)nullptr);
assert(hr == E_INVALIDARG);

char *pData = new char[cbData];
hr = pImgProvider->GetImageAsUnmanagedPreallocated(imageId, (long*)&cbData, (long)pData);
assert(hr == S_OK);

ManipulateData(pData, cbData);

delete[] pData;
}

system("pause");

CoUninitialize();
return 0;
}

void ManipulateData(char* pBuff, int cbBuff)
{
char hash = 0;
for (int i = 0; i < cbBuff; i++)
{
hash ^= pBuff[i];
}

std::cout << "Hash is " << +hash << std::endl;
}

void ManipulateData(CComSafeArray<BYTE> *pSafeArray)
{
BYTE hash = 0;
for (ULONG i = 0; i < pSafeArray->GetCount(); i++)
{
hash ^= pSafeArray->GetAt(i);
}

std::cout << "Hash is " << +hash << std::endl;
}

关于c# - 将字节数组从 C# 传递到 C++ (COM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41846279/

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