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
{
char name[20];
int Count;
} info;
**abc.cpp** :The dll
int CAinfo(info *inf)
{
inf = (info*)malloc(sizeof(info));
for(int i=0;i>7;i++)
{
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:
public struct INFO
{
[MarshalAsAttribute(UnmanagedType.LPStr)]
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 :
typedef struct INFO
{
std::string name;
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.