Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how to use this in C#


i have a structure in C++,
i creates a typedef of array of this structure
now i want to use this typedef as argument in a C# function as input.
e.g.
C++
struct node
{
int x;
string y;
};

typedef node pyu=new node[65];


void func(pyu a,char c)
{
}



now i want this functionality to be done C#.
if anyone has any idea then please guide me .
thanks
Posted
Updated 5-Dec-12 19:00pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Dec-12 1:04am    
You you familiar with P/Invoke in general? Is this C++ or C++/CLI?
--SA
vaibhavbvp 6-Dec-12 1:16am    
this is C++
want to write a same version in C#,without using P/invoke
Argonia 6-Dec-12 4:44am    
why just don't you make a class instead of this struct in c#?

You should use native string type instead of stl string:
In C++ it will be looks:
C++
struct node
{
    int x;
    char y[256];
};

And in C# it will be wrapped
C#
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct node
{
    public int x;
    [MarshalAs(UnmanagedType.ByValTStr,SizeConst=256)]
    public string y;
};

For Unicode you should change type char to wchar_t in C++ structure and CharSet attribute value in C# structure.
More info:
http://msdn.microsoft.com/en-us/library/s9ts558h.aspx[^]

Regards,
Maxim.
 
Share this answer
 
Comments
vaibhavbvp 6-Dec-12 1:22am    
hi Maxim
i dont want to use C++ structure
i want to create the separate structure in c# then used that structure as separate datatype(typdef in c++),then using this in the datatype of C# arguments
are you talking about

C#
using IntList = List<int>;</int>
 
Share this answer
 
It's more or less the same in C#, see the documentation[^].
 
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