468,513 Members | 1,874 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Passing structure from Managed C++ to C#,

Hi,

i am new bie to managed C++ , i have an C# DLL , which i need to invoke from managed C++ and pass a structure as a parameter , Can any one help me out on this issue...



Thanks in advance...
Jan 21 '08 #1
2 3503
weaknessforcats
9,207 Expert Mod 8TB
You will need to read the interoperability pages in MSDN.
Jan 21 '08 #2
Hi All,

I have found the solution for this problem , the simplest way of passing structure from Managed C++ to C# is ..



Eg:

In MC++

public struct xyz

{

public:

int x;

int y;

};



Method in MC++ invoking C# Method say

...............

..............

CsharpDLLNamespace::CSharpeClass^ ObjectofSharpMethod= gcnew CsharpDLLNamespace::CSharpeClass();



xyz obj;

obj.x = 10 ;

obj.y = 20 ;

ObjectofSharpMethod>CsharpMethod((System::IntPtr)& obj);



In C#



[StructLayout(LayoutKind.Sequential, Pack = 2)]

public class xyz

{

public int x;

public int y;

}



public void CsharpMethod(IntPtr pob )

{

xyz obj = new xyz();

Marshal.PtrToStructure(pob, obj);

Console.WriteLine("Converted Successfully" + "x : " + obj.x+ "y :" + obj.y);

}
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Gregory Khrapunovich | last post: by
reply views Thread by nygiantswin2005 | last post: by
22 posts views Thread by Arne | last post: by
5 posts views Thread by renjini | last post: by
6 posts views Thread by Andy Baker | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.