Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,

I have a data contract as shown below. I am trying to pass this from a C# application to a method in C++ (where i need to convert it to native C++ and then to manged C++ (C++/CLI).

My question is in this data contract i have a list of data contracts inside one data contract. While I am trying to send this to native C++ and then to Manged C++ (i have converted the Datacontract to a structure in C++.

But i am not sure how handle (Marshall) data types like object (in C#) and List of dataContracts (in C#) to native and then Manged C++.

I know that if it is a string from C# , i can Marshall as as follows to convert it to Managed C++. but i do not know how to Marshall C# data types like "object" and list of datacontracts. to manged C++.

wsdc->UserID = Marshal::PtrToStringAnsi((IntPtr) (char *) sdc.UserID);

XML
In short , I need to convert the attached C# datacontract to native C++ code.
In the attached C# data contract , i have data types like "List<datacontacts>"  and "object" .
I would like to know how we can convert the above data contracts,list of data contracts ,object datatypes to native C++ datatypes like structures etc..

Please let me know your suggestions/ideas on this.

Appreciate your help.

Thanks,
Sudhakar

<pre lang="c#">
   [DataContract(Name = "TopicDetails")]
    [Serializable]
    public class TopicDetails
    {
        protected object baseObjectType;

        [DataMember]
        public object BaseObjectType
        {
            get
            {
                return baseObjectType;
            }
            set
            {
                baseObjectType = value;
            }
        }
       
        static public TopicDetails CreateTopic<T, mT>(IComparable<T> objectType, IComparable<mT> objectID)
        {
            var topicDetails = new TopicDetails();
            topicDetails.BaseObjectType = objectType;
            return topicDetails;
        }

    }

    [DataContract(Name = "SubscriptionTopic")]
    [KnownType(typeof(List<TopicDetails>))]
     [Serializable]
    public class SubscriptionTopic
    {       
        private object creator;

        [DataMember]
        public object DBHandle
        {
            get
            {
                return creator;
            }
            set
            {
                creator = value;
            }
        }        
        static public SubscriptionTopic CreateSubscriptions<T, mT, nT>(IList<TopicDetails> topic, IComparable<mT> target, IComparable<nT> handle)
        {
            var subscriptionTopic = new SubscriptionTopic();           
            subscriptionTopic.DBHandle = handle;
            return subscriptionTopic;
        }

    }

    [DataContract(Name = "SubscriptionData")]
    [KnownType(typeof(List<SubscriptionTopic>))]
    [Serializable]
    public class SubscriptionDataContracts : IExtensibleDataObject
    {
        private ExtensionDataObject extensionDataObjectValue;

        [DataMember]
        public List<SubscriptionTopic> TopicList
        {
            get;
            set;
        }        
        public ExtensionDataObject ExtensionData
        {
            get
            {
                return extensionDataObjectValue;
            }
            set
            {
                extensionDataObjectValue = value;
            }
        }
    }
Posted
Updated 28-Apr-15 21:01pm
v4
Comments
Sergey Alexandrovich Kryukov 27-Apr-15 8:03am     CRLF
There is no such thing as "Managed C++" anymore... —SA
Member 3975629 27-Apr-15 8:08am     CRLF
But still , in some of our projects ,we are marshalling (converting to Managed )C# data types to C++/CLI as follows: wsdc->UserID = Marshal::PtrToStringAnsi((IntPtr) (char *) sdc.UserID); Now i need to do the same for C# data types such as object and List of datacontracts.
Member 3975629 29-Apr-15 3:00am     CRLF
In short , I need to convert the attached C# datacontract to native C++ code. In the attached C# data contract , i have data types like "List<datacontacts>" and "object" . I would like to know how we can convert the above data contracts,list of data contracts ,object datatypes to native C++ datatypes like structures etc.. Appreciate your help. Thanks, Sudhakar

1 solution

You don't need to convert C# (managed) to C++/CLI. C++/CLI is managed already. You do need to do a conversion from managed to unmanaged if you are going to native C++.

Normally, what I have done is to write a wrapper DLL in C++/CLI that is then used by the native C++ application. All translation is done inside of the wrapper DLL. That way I control exactly how things go from one to the other.

It also works well going from native the native application to the managed code in the wrapped DLL.
 
Share this answer
 
Comments
Member 3975629 28-Apr-15 3:09am     CRLF
Thanks for your idea. Currently my requirement is I need to pass a "list of data contracts" and a C# data type "object" to native C++ . And then I need to convert them from native C++ to Managed C++. Here we are facing some problems while we are converting C# data types like “object” and “List of datacontracts” from subscription data contract to Native C++ structures and then converting to Managed C++ data types. we have infrastructure/idea for converting normal data contract and normal data types like (string,int) to C strutures for converting to Native C++ and then we can Marshall them to Manged C++ as follows. wsdc->UserID = Marshal::PtrToStringAnsi((IntPtr) (char *) sdc.UserID); but we do not know how to convert the list of data contracts to C Structures and data type like "object" to native C++ and then to Manged C++. Please let me know any ideas /suggestions on this.Please take a look at the data contract i attached above with this question. Thanks, Sudhakar
Member 3975629 29-Apr-15 3:00am     CRLF
In short , I need to convert the attached C# datacontract to native C++ code. In the attached C# data contract , i have data types like "List<datacontacts>" and "object" . I would like to know how we can convert the above data contracts,list of data contracts ,object datatypes to native C++ datatypes like structures etc.. Appreciate your help. Thanks, Sudhakar

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