如何将 C# 数组传递给 C++并将其返回给 C# 并附加其他项目?

发布于 2024-12-07 18:19:59 字数 1038 浏览 4 评论 0原文

我有一个使用 C++ dll 的 C# 项目。 (在 Visual Studio 2010 中)

我必须将一个 int 数组从 C# 代码传递给 C++ 函数,C++ 函数将在数组中添加一些元素,当控制权返回到 C# 代码时,C# 代码也会在同一数组中添加元素。

最初我在 C# 代码中声明了一个数组(大小为 10000),C++ 代码能够添​​加元素(因为它只是一个 int 数组,内存分配是相同的),但问题是由于访问而出现运行时错误数组之外。

我可以将大小增加到 100000,但我再次不知道 C++ 代码将添加多少元素(即使它可以只是 1 个元素)。

那么是否有一个通用的数据结构(动态数组)可供两者或其他方式使用?我正在使用 Visual studio 2010。

我想做这样的事情。
PS:不是编译的代码,这里我使用 char 数组而不是 int 数组。

C# 代码

[DllImport("example1.dll")]
private static extern int fnCPP (StringBuilder a,int size)
...

private void fnCSHARP(){
    StringBuilder buff = new StringBuilder(10000);
    int size=0;
    size = fnCPP (buff,size);
    int x = someCSHARP_fu();
    for ( int i=size; i < x+size; i++) buff[i]='x';// possibility of run time error
}

C++ 代码

int fnCPP (char *a,int size){
  int x = someOtherCpp_Function();
  for( int i=size; i < x+size ; i++) a[ i ] = 'x'; //possibility of run time error 
  return size+x;
}

I have a C# project which is using a C++ dll. (in visual studio 2010)

I have to pass a array of int from C# code to C++ function and C++ function will add few elements in array, when control comes back to C# code, C# code will also add elements in same array.

Initially i declared a array(of size 10000) in C# code and C++ code is able to add elements (because it was just an array of int, memory allocation is same), but the problems is i have got run time error due to accessing out side of array.

I can increase size to 100000 but again i don't know how much elements C++ code will add( even it can be just 1 element).

So is there a common data structure (dynamic array) exist for both or other way to do? I am using Visual studio 2010.

Something like this i want to do.
PS: not compiled code, and here i used char array instead of int array.

C# code

[DllImport("example1.dll")]
private static extern int fnCPP (StringBuilder a,int size)
...

private void fnCSHARP(){
    StringBuilder buff = new StringBuilder(10000);
    int size=0;
    size = fnCPP (buff,size);
    int x = someCSHARP_fu();
    for ( int i=size; i < x+size; i++) buff[i]='x';// possibility of run time error
}

C++ code

int fnCPP (char *a,int size){
  int x = someOtherCpp_Function();
  for( int i=size; i < x+size ; i++) a[ i ] = 'x'; //possibility of run time error 
  return size+x;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-12-14 18:19:59

此处有一篇关于在托管和非托管代码之间传递数组的很好的 MSDN 文章。问题是,为什么首先需要将数组从 C# 传递到 C++?为什么不能在 C++ 端(在 fnCPP 方法中)进行分配,并返回指向 C# 代码的指针,而不仅仅是使用 Marshal.Copy( source, destination, 0, size )另一个 Stackoverflow 问题 ?与 fnCSHARP 方法相比,您可以将数组的内容复制到某些可变长度的数据结构(例如列表)。

There's a good MSDN article about passing arrays between managed and unmanaged code Here. The question is, why would you need to pass the array from C# to C++ in the first place? Why can't you do the allocation on the C++ side (in your fnCPP method), and return a pointer to the C# code, and than just use Marshal.Copy( source, destination, 0, size ) as in yet another Stackoverflow question? Than in your fnCSHARP method you could copy the contents of the array to some varaiable length data structure (e.g. List).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文