将结构数组BYRE从C#传递到C ,然后将其恢复
Pass a Struct array ByRef from C# to C++ and get it back
呼叫CPP dll时我一个非常奇怪的问题。
我可以调用通过结构数组(带有多个位置(编组并循环并将数据设置为其的方法。但是,当代码返回C#时,数组只有一个位置。
例如,我在C dll中创建一个具有3个位置(0、1、2(的结构的数组阵列上只有一个位置,第一个项目。
代码:
c 方面:
extern "C" __declspec(dllexport) int ViewItems(int * nTotalItems, void ** paramStruct)
{
// TODO nothing here.
return 1;
}
c#侧:
[DllImport(@"MyCustomLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ViewItems(ref int resultItemsCount, ref MyStruct[] MyStruct);
调用:
var resultSize = 3;
var resultStruct = new MyStruct[3];
for (int i = 0; i < resultSize; i++)
{
resultStruct[i].SomePropValue = i+2;
}
// Before this, the resultStruct.Lenght is 3
var resultCall = ViewItems(ref resultSize, ref resultStruct);
// After this, the resultStruct.Lenght is
结构:
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
public int SomePropValue;
}
我尝试以下方面没有成功:
- [MARSHALAS(UNMANAGGEDTYPE.LPARRAY(]
- [IN,OUT]
- 阅读其他问题,但没有一个问题。
有人对我错过的东西有任何了解吗?
预先感谢!
我解决了问题。
我从DllImport
中删除了ref
,然后将[MarshalAs(UnmanagedType.LPArray)]
和[In, Out]
装饰器放入MyStruct
参数。
最后,我更改为C 中的void * paramStruct
,最后都起作用了!
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 用C++将哈希表写入文件并从文件中恢复
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- C++:如何读取分离变量,然后读取向量
- 为什么我的递归函数按降序打印,然后按升序打印?
- 等待整个 omp 块完成,然后再调用第二个函数
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- Opencv 恢复到比我设置的更高的分辨率
- 变量在使用赋值语句赋值后恢复为以前的值
- 是否可以序列化 QSettings 然后恢复它们?
- 将结构数组BYRE从C#传递到C ,然后将其恢复
- MFC:在 CRichEditCtrl 中突出显示一条线一段时间,然后恢复
- 如果头文件被更改然后恢复,如何停止/欺骗 cmake 不重建
- 如何将网格规格化为-1到1,然后从规格化网格恢复为原始网格
- 停止一个函数循环,然后恢复它们
- (C++)如何挂起主线程,然后在其中恢复另一个线程