Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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**
C++
typedef struct INFO
{
    //std::string name; 
    char name[20];	
    int Count;	
} info;


**abc.cpp** :The dll
C++
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;
}


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

**CsharpApp.cs**

I created the structure as below:
C#
public struct INFO
{
  [MarshalAsAttribute(UnmanagedType.LPStr)]    	
  //std::string name; 
  char name[20];	
  int Count;	
} info;

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 :
C++
typedef struct INFO
{
  std::string name; 
  //char name[20];	
  int Count;	
  INFo(std::string n,int c):
  name(n),
  Count(c){}
} info;

5. The data for this structure that I want will be as follows :
Name	Count
CA1	100
CA2	50
CA3	500
CA4	50

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.
Posted
Updated 1-Jun-15 22:45pm
v2
Comments
StM0n 2-Jun-15 4:46am    
Could you please use code tags next time?

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.
 
Share this answer
 
not a solution, but a bug, you have
C++
i > 7
where it should be <
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900