是否可以将结构列表从C 传递到C#
Is it possible to pass struct list from c++ to C#
我有兴趣从我的C 代码调用.NET代码。.NET代码分为" 生成com可见"的分离的dll,它由接受"项目" struct的列表的方法组成。
类似的东西:
struct item {
int a;
int b;
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public interface Ifoo {
int GetFoo(ref IntPtr items,int nItems);
}
public class foo : Ifoo {
int GetFoo(ref IntPtr items,int nItems)
{
for (int ix = 0; ix < numMatches; ix++)
{
it = (item)Marshal.PtrToStructure((IntPtr)((long)items + Marshal.SizeOf(typeof(item))*ix)),typeof(item));
}
}
}
在C 部分中,我有类似的东西..
std::vector<struct> foo;
foo.push_back(stru1); // stru1 is initialized anywhere...
foo.push_back(stru2); // and so on...
netInstance->GetFoo(reinterpret_cast<long*>(foo.data()),foo.size());
但这是行不通的。.
我得到的只是记忆异常。我还尝试了其他方法,例如Safearray,但似乎与自定义结构不起作用。
谢谢!
在窗口上,长长可能是32位整数。您可以尝试在C 代码中将long*
更改为long long*
,以使数组的大小匹配数据的大小。
您是否控制C#部分和C 部分?而不是元帅阵列,而是将界面更改为与单个项目一起使用。调试将更容易。
相关文章:
- 没有为自己的结构调用列表推回方法
- 访问类lintalizer列表中的结构元素
- 将大括号括起来的初始值设定项列表作为结构返回
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- C++-将具有引用的长参数列表重构为结构
- 在C++中循环访问自定义结构列表的小问题
- 访问列表向量中的结构
- c ++数据输入并读回列表中的结构结构中的数组
- 如何在结构初始值设定项列表中实例化类
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 使用列表进行动态结构分配
- 如何从结构列表中筛选信息
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- 用于筛子的最佳数据结构是什么(即一些被划掉的数字列表)?
- C/C++intellisense 0.26.1不显示结构成员列表
- C++使用旧编译器GCC 4.4.0在结构列表中查找字符串
- 列表结构和数组的奇怪问题
- 释放包含STL类的链接列表结构的内存
- c++游戏的列表结构