如何在C#中填充结构数组并将其发送到C ++ Dll [英] How to fill an array of structure in C# and send it to C++ Dll

查看:79
本文介绍了如何在C#中填充结构数组并将其发送到C ++ Dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多次尝试后,我还要弄清楚如何使用C#填充结构数组并将其动态传递给C ++ Dll。



关于如何使用结构的编组,我感到非常困惑。

我有一系列结构。我必须将这个结构发送到C ++并收回它。



以下是我在C ++中的代码:



我创建了一个调用以下结构的类库:



** abc.h **

After a lot of attempts I' m yet to figure out how should I use to fill the array of structures in C# and pass it dynamically to C++Dll.

I m pretty confused in regards to how to use the marshalling of the structures.
I have an array of structures. I have to send this structure to C++ and recieve it back .

Following is my code in C++:

I have created a class library that calls the below structure:

**abc.h**

typedef struct INFO
{
    //std::string name; 
    char name[20];	
    int Count;	
} info;





** abc.cpp **:dll



**abc.cpp** :The dll

int CAinfo(info *inf)
{
    inf = (info*)malloc(sizeof(info));
    for(int i=0;i>7;i++)
    {
        //inf->name = "TEST";
        // strcpy(info->name ,"TEST"); //applicable for char*
        //inf->name ="TEST"; //applicable for std::string
        memcpy(inf->name,"TEST",10);
        inf->Count =0;
    }
    return 0;
}





我正在尝试从C#应用程序填充这些结构



** CsharpApp.cs **



我创建了如下结构:



I'm trying to fill these structure from the C# Application

**CsharpApp.cs**

I created the structure as below:

public struct INFO
{
  [MarshalAsAttribute(UnmanagedType.LPStr)]    	
  //std::string name; 
  char name[20];	
  int Count;	
} info;



我想在结构中分配值,我该怎么做?



1.如何在C#中编组这些结构。

2.将数据从C#发送到C ++的最佳方式。

3.我应该考虑将数据作为指针数组发送到结构或指向结构数组的指针???

4.我使用构造函数来定义C ++本身的结构,内存分配是否仅适用于C ++之类的这个:


I want to assign the values within the structure ,how should I do this???

1. How should I marshal these structures in C#.
2. The best way to send data from C# to C++.
3. Should I consider sending data as array of pointer to structure or pointer to array of structure???
4. I use a constructor in defining the structure in C++ itself ,will the memory allocation would be only for C++ like this :

typedef struct INFO
{
  std::string name; 
  //char name[20];	
  int Count;	
  INFo(std::string n,int c):
  name(n),
  Count(c){}
} info;



5.我想要的这个结构的数据如下:


5. The data for this structure that I want will be as follows :

Name	Count
CA1	100
CA2	50
CA3	500
CA4	50



6.此数据必须通过从C#到C ++必须存储在已分配的内存中。

无论何时调用函数CAinfo(),都必须随时检索存储的数据。只有计数值会被更改,如果有任何更改完成它必须再次这样做。



如何编组C#中的数据...我是否需要为C#中的结构分配内存。

数据是否会存储在C ++的基址指针地址中,那么我怎样才能在C#中检索它?



任何帮助都会很多赞。


6. This data must be passed from C# to C++ and must be stored in the allocated memory.
The stored data must be retrieved at any time whenever the function CAinfo() is called .Only the count value would be changed and if any changes is done it must again do the same .

How should I marshal the data in C#... should I need to allocate the memory for structure in C#.
Will the data be stored at the base pointer address in C++,then how can I must retrieve it in C#?

Any help would be much appreciated.

推荐答案

最好分配你将获得结果的内存,让我从另一边填写。



点击文章及其示例代码。
It is better to allocate the memory where you will have the results and let it fill from the other side.

Take a loke at the article and its sample code.


不是解决方案,而是一个错误,你有
not a solution, but a bug, you have
i > 7

应该在哪里<


这篇关于如何在C#中填充结构数组并将其发送到C ++ Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆