Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C# VC++
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.
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 5-Dec-12 19:56pm
Edited 5-Dec-12 20:00pm
v2
Comments
Sergey Alexandrovich Kryukov at 6-Dec-12 1:04am
   
You you familiar with P/Invoke in general? Is this C++ or C++/CLI?
--SA
vaibhavbvp at 6-Dec-12 1:16am
   
this is C++
want to write a same version in C#,without using P/invoke
Argonia at 6-Dec-12 4:44am
   
why just don't you make a class instead of this struct in c#?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should use native string type instead of stl string:
In C++ it will be looks:
struct node
{
    int x;
    char y[256];
};
And in C# it will be wrapped
[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.
  Permalink  
Comments
vaibhavbvp at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

are you talking about
 
using IntList = List<int>;</int>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It's more or less the same in C#, see the documentation[^].
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100