473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1830
Plater
7,872 Expert 4TB
Can you do it with a byte[] array? And then just cast (on the unmanaged c++ side) and parse it (on the managed C# side)

Although you can create structs in c# too, and probably do something with Serialization for it.
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++
Expand|Select|Wrap|Line Numbers
  1. public struct xyz
  2.  
  3. {
  4.  
  5. public:
  6.  
  7. int x;
  8.  
  9. int y; 
  10.  
  11. };
  12.  

Method in MC++ invoking C# Method say

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

..............
Expand|Select|Wrap|Line Numbers
  1. CsharpDLLNamespace::CSharpeClass^  ObjectofSharpMethod= gcnew CsharpDLLNamespace::CSharpeClass();
  2.  
  3.  
  4.  
  5. xyz obj;
  6.  
  7. obj.x = 10 ;
  8.  
  9. obj.y = 20 ;
  10.  
  11. ObjectofSharpMethod>CsharpMethod((System::IntPtr)&obj);
  12.  

In C#


Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  2.  
  3. public class xyz
  4.  
  5. {
  6.  
  7. public int x;
  8.  
  9. public int y;
  10.  
  11. }
  12.  
  13.  
  14.  
  15. public void CsharpMethod(IntPtr pob )
  16.  
  17. {
  18.  
  19. xyz obj = new xyz();
  20.  
  21. Marshal.PtrToStructure(pob, obj);
  22.  
  23. Console.WriteLine("Converted Successfully" + "x : " + obj.x+ "y :" + obj.y);
  24.  
  25. }
  26.  
Jan 23 '08 #3

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

Similar topics

2
by: Chris | last post by:
Hi, I seem to be having a problem I can't quite figure out. Currently, I have the following code (below). In the SendMsg function, I create a 'Qtkmsg' which converts the string 'text' into a...
0
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential,...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
2
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.