I am using COM to communicate between C++ and C#.
I have the following classes in C#
Email Address
/// <summary> /// Email Address /// </summary> public class EmailAddress { /// <summary> /// SMTP Address /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Address; /// <summary> /// Name /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Name; }
Mailbox
/// <summary> /// MailBox Entity /// </summary> public struct MailBoxEntity { /// <summary> /// SMTP Address /// </summary> [MarshalAs(UnmanagedType.BStr)] public string SMTPAddress; /// <summary> /// Mailbox Display Name /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Name; /// <summary> /// Mailbox Server Name /// </summary> [MarshalAs(UnmanagedType.BStr)] public string sServerName; }
EmailEntity (yet to implement IEmailEntity which will contain properties for each field)
/// <summary> /// Class for Email Entity /// </summary> public class EmailEntity : IEmailEntity { /// <summary> /// BccRecipients /// </summary> [MarshalAs(UnmanagedType.ByValArray)] public EmailAddress[] BccRecipients; /// <summary> /// Body /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Body; /// <summary> /// CcRecipients /// </summary> [MarshalAs(UnmanagedType.ByValArray)] public EmailAddress[] CcRecipients; /// <summary> /// Culture /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Culture; /// <summary> /// DateTimeCreated /// </summary> [MarshalAs(UnmanagedType.BStr)] public string DateTimeCreated; /// <summary> /// DateTimeReceived /// </summary> [MarshalAs(UnmanagedType.BStr)] public string DateTimeReceived; /// <summary> /// DateTimeSent /// </summary> [MarshalAs(UnmanagedType.BStr)] public string DateTimeSent; /// <summary> /// FromAddress /// </summary> [MarshalAs(UnmanagedType.BStr)] public string FromAddress; /// <summary> /// FromName /// </summary> [MarshalAs(UnmanagedType.BStr)] public string FromName; /// <summary> /// HasAttachments /// </summary> [MarshalAs(UnmanagedType.BStr)] public string HasAttachments; /// <summary> /// Id /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Id; /// <summary> /// Importance /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Importance; /// <summary> /// LastModifiedName /// </summary> [MarshalAs(UnmanagedType.BStr)] public string LastModifiedName; /// <summary> /// LastModifiedTime /// </summary> [MarshalAs(UnmanagedType.BStr)] public string LastModifiedTime; /// <summary> /// MimeContent /// </summary> [MarshalAs(UnmanagedType.BStr)] public string MimeContent; /// <summary> /// ParentFolderId /// </summary> [MarshalAs(UnmanagedType.BStr)] public string ParentFolderId; /// <summary> /// Original Mailbox /// </summary> [MarshalAs(UnmanagedType.BStr)] public MailBoxEntity OriginalMailBox; /// <summary> /// ParentFolderName /// </summary> [MarshalAs(UnmanagedType.BStr)] public string ParentFolderName; /// <summary> /// ReceivedByAddress /// </summary> [MarshalAs(UnmanagedType.BStr)] public string ReceivedByAddress; /// <summary> /// ReceivedByName /// </summary> [MarshalAs(UnmanagedType.BStr)] public string ReceivedByName; /// <summary> /// Size /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Size; /// <summary> /// Subject /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Subject; /// <summary> /// ToRecipients /// </summary> [MarshalAs(UnmanagedType.ByValArray)] public EmailAddress[] ToRecipients; }
How do I access access an array of EmailEnity objects (in C++) returned from a function in C# and read the fields of the EmailEntity class. Please note that EmailEntity class contains members like EmailAddress[] which itself is an array of objects. I was planning to implement interface IEmailEntity that contains properties to access fields of EmailEntity and use it to access fields in C++. Is that the right way ?
How do I access complex fields members like array of EmailAddress (EmailAddress[] ToRecipients) in C++.
Please suggest.
Thanks
You could simplify the .NET Code like this (remove all MarshalAs that should be done automatically):
To use it with C++, you need to register the DLL and build a .TLB (Type Library file) as explained in a similar answer here: Implement a C# DLL COM File In Unmanaged C++ Program
Then, you can access these classes in C++, like this: