gpt4 book ai didi

c# - C# 中 native C++ dll 的问题

转载 作者:行者123 更新时间:2023-11-30 02:33:29 26 4
gpt4 key购买 nike

我有 native C++ dll,它具有查找连接到计算机的相机数量并返回其序列号的功能。我正在尝试在 C# 应用程序中使用 native C++ dll,但我不断收到访问冲突错误(尝试读取或写入 protected 内存)。

有问题的功能是

    uint32_t GetSerialNumList(char** theBufList, int theBufSize, int theListLength);

我使用PInvoke的方式如下:

 [DllImport(CameraDll, EntryPoint = "GetSerialNumList", CallingConvention = CallingConvention.Cdecl)]
private static extern uint GetSerialNumList(out byte[] pBuf, int BufSize, int ListLength);

如果我创建 native C++ 应用程序以使用 dll 并按如下方式使用函数:

char* theSerialNumb;    
theSerialNumb = (char *) malloc(sizeof(char)* 8);
status = TRI_GetSerialNumList(&theSerialNumb, 8, 1);

它工作正常,但是,如果我在 C# 中使用如下,它会给我上面提到的错误:

 byte[] BufList;
BufList = new byte[8];
rv = GetSerialNumList(out BufList, 8, 1);

最佳答案

您在 C# 中传递的参数是一个指向字节数组的指针。你在 C++ 中传递的是一个指向字节数组的指针。此外,在 C++ 示例中,您将数据传递给函数,但在 C# 示例中,您将它作为 out 而不是 ref 传递。

虽然我不确定这是否可行,但我会尝试创建一个包含字节数组的结构并将该结构传递给外部函数。

为了回答上面的一些评论,由于程序创建堆的方式不同,这些函数通常会修改传递给它的内存,而不是尝试分配额外的内存。

关于c# - C# 中 native C++ dll 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385870/

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