返回COM中从C#到C的对象数组 我用COM实现C++与C语言之间的通信。

返回COM中从C#到C的对象数组 我用COM实现C++与C语言之间的通信。,c#,c++,com,C#,C++,Com,我有以下C语言的课程# 电子邮件地址 /// <summary> /// Email Address /// </summary> public class EmailAddress { /// <summary> /// SMTP Address /// </summary> [MarshalAs(UnmanagedType.BStr)] public string Address;

我有以下C语言的课程#

  • 电子邮件地址

    /// <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;
    }
    
    //
    ///电子邮件地址
    /// 
    公共类电子邮件地址
    {
    /// 
    ///SMTP地址
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串地址;
    /// 
    ///名字
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串名称;
    }
    
  • 信箱

    /// <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;
    }
    
    //
    ///邮箱实体
    ///    
    公共结构邮箱实体
    {
    /// 
    ///SMTP地址
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串SMTPAddress;
    /// 
    ///邮箱显示名称
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串名称;
    /// 
    ///邮箱服务器名称
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串名称;
    }
    
  • EmailEntity(尚未实现包含每个字段属性的IEmailEntity)

    //
    ///电子邮件实体的类
    /// 
    公共类EmailEntity:IEmailEntity
    {
    /// 
    ///B当事人
    /// 
    [Marshallas(UnmanagedType.ByValArray)]
    公共电子邮件地址[]b客户;
    /// 
    ///身体
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共机构;
    /// 
    ///收信人
    /// 
    [Marshallas(UnmanagedType.ByValArray)]
    公共电子邮件地址[]收件人;
    /// 
    ///培养
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共文化;
    /// 
    ///DateTimeCreated
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串DateTimeCreated;
    /// 
    ///DateTimeReceived
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串DateTimeReceived;
    /// 
    ///日期时间段
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串DateTimeSent;
    /// 
    ///发件人地址
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串FromAddress;
    /// 
    ///FromName
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串FromName;
    /// 
    ///有附件
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串附件;
    /// 
    ///身份证
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串Id;
    /// 
    ///重要性
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串的重要性;
    /// 
    ///LastModifiedName
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串LastModifiedName;
    /// 
    ///上次修改时间
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串LastModifiedTime;
    /// 
    ///模拟内容
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串内容;
    /// 
    ///亲本
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串ParentFolderId;
    /// 
    ///原始邮箱
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共邮箱实体原始邮箱;
    /// 
    ///ParentFolderName
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串ParentFolderName;
    /// 
    ///收到的地址
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串ReceivedByAddress;
    /// 
    ///ReceivedByName
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串ReceivedByName;
    /// 
    ///大小
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串大小;
    /// 
    ///主题
    /// 
    [Marshallas(UnmanagedType.BStr)]
    公共字符串主题;
    /// 
    ///收件人
    /// 
    [Marshallas(UnmanagedType.ByValArray)]
    公众电子邮件地址[]收件人;
    }
    
  • 如何访问从C中的函数返回的EmailEnity对象数组(C++中)# 并读取EmailEntity类的字段。请注意,EmailEntity类包含以下成员 EmailAddress[],它本身是一个对象数组。 我计划实现接口IEmailEntity,它包含访问EmailEntity字段的属性 并使用它访问C++中的字段。这样对吗

    <>我如何访问C++中的EMAILATION(EMAILATION[]TURECOIIONIDENS)数组之类的复杂字段成员?< /P> 请建议


    谢谢

    您可以像这样简化.NET代码(删除所有应自动完成的封送):

    [ComVisible(true)]
    公共类MyRootClass:IMyRootClass//
    {
    公共IEmailEntity[]获取实体()
    {
    列表=新列表();
    对于(int i=0;i<10;i++)
    {
    EmailEntity=新的EmailEntity();
    列表地址=新列表();
    address.Add(新的电子邮件地址{Name=“Joe”+i});
    entity.bccreciients=地址.ToArray();
    entity.Body=“hello world”+i;
    列表。添加(实体);
    }
    return list.ToArray();
    }   
    }
    [ComVisible(true)]
    公共接口IMyRootClass
    {
    IEmailEntity[]GetEntities();
    }
    公共类EmailEntity:IEmailEntity
    {
    公共IEmailAddress[]bccreciients{get;set;}
    公共字符串体{get;set;}
    }
    公共类电子邮件地址:IEmailAddress
    {
    公共字符串地址{get;set;}
    公共字符串名称{get;set;}
    }
    [ComVisible(true)]
    公共接口IEmailAddress
    {
    字符串地址{get;set;}
    字符串名称{get;set;}
    }
    [ComVisible(true)]
    公共接口IEmailEntity
    {
    IEmailAddress[]BCCreciients{get;set;}
    字符串体{get;set;}
    //待续。。。
    }
    

    使用C++,需要登记DLL并构建一个.TLB(类型库文件),如这里类似的答案:

    这样,你可以在C++中访问这些类,比如:

    #include "stdafx.h"
    #import  "c:\MyPathToTheTlb\YourAssembly.tlb" // import the COM TLB
    
    using namespace YourAssembly;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      CoInitialize(NULL);
      IMyRootClassPtr ptr(__uuidof(MyRootClass));
      CComSafeArray<IUnknown*> entities = ptr->GetEntities(); // CComSafeArray needs atlsafe.h in the PCH
      for(int i = entities.GetLowerBound(0); i <= entities.GetUpperBound(0); i++)
      {
        IEmailEntityPtr entity;
        entities.GetAt(i).QueryInterface(&entity);
        _bstr_t body = entity->Body;
        printf("%S\n", body.GetBSTR());
    
        CComSafeArray<IUnknown*> recipients = entity->BccRecipients;
        for(int j = recipients.GetLowerBound(0); j <= recipients.GetUpperBound(0); j++)
        {
          IEmailAddressPtr address;
          recipients.GetAt(j).QueryInterface(&address);
          _bstr_t name = address->Name;
          printf(" %S\n", name.GetBSTR());
        }
      }
      CoUninitialize();
    }
    
    #包括“stdafx.h”
    #导入“c:\MyPathToTheTlb\yoursassembly.tlb”//导入COM tlb
    使用名称空间组件;
    int _tmain(int argc,_TCHAR*argv[]
    {
    共初始化(空);
    imyrootclassptrptr(uuuidof(MyRootClass));
    CComSafeArray entities=ptr->GetEntities();//CComSafeArray需要PCH中的atlsafe.h
    对于(int i=entities.GetLowerBo)
    
    [ComVisible(true)]
    public class MyRootClass : IMyRootClass // some class to start with
    {
        public IEmailEntity[] GetEntities()
        {
            List<IEmailEntity> list = new List<IEmailEntity>();
            for(int i = 0; i < 10; i++)
            {
                EmailEntity entity = new EmailEntity();
                List<IEmailAddress> addresses = new List<IEmailAddress>();
                addresses.Add(new EmailAddress { Name = "Joe" + i });
                entity.BccRecipients = addresses.ToArray();
                entity.Body = "hello world " + i;
                list.Add(entity);
            }
            return list.ToArray();
        }   
    }
    
    [ComVisible(true)]
    public interface IMyRootClass
    {
        IEmailEntity[] GetEntities();
    }
    
    public class EmailEntity : IEmailEntity
    {
        public IEmailAddress[] BccRecipients { get; set; }
        public string Body { get; set; }
    }
    
    public class EmailAddress : IEmailAddress
    {
        public string Address { get; set; }
        public string Name { get; set; }
    }
    
    [ComVisible(true)]
    public interface IEmailAddress
    {
        string Address { get; set; }
        string Name { get; set; }
    }
    
    [ComVisible(true)]
    public interface IEmailEntity
    {
        IEmailAddress[] BccRecipients { get; set; }
        string Body { get; set; }
        // to be continued...
    }
    
    #include "stdafx.h"
    #import  "c:\MyPathToTheTlb\YourAssembly.tlb" // import the COM TLB
    
    using namespace YourAssembly;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      CoInitialize(NULL);
      IMyRootClassPtr ptr(__uuidof(MyRootClass));
      CComSafeArray<IUnknown*> entities = ptr->GetEntities(); // CComSafeArray needs atlsafe.h in the PCH
      for(int i = entities.GetLowerBound(0); i <= entities.GetUpperBound(0); i++)
      {
        IEmailEntityPtr entity;
        entities.GetAt(i).QueryInterface(&entity);
        _bstr_t body = entity->Body;
        printf("%S\n", body.GetBSTR());
    
        CComSafeArray<IUnknown*> recipients = entity->BccRecipients;
        for(int j = recipients.GetLowerBound(0); j <= recipients.GetUpperBound(0); j++)
        {
          IEmailAddressPtr address;
          recipients.GetAt(j).QueryInterface(&address);
          _bstr_t name = address->Name;
          printf(" %S\n", name.GetBSTR());
        }
      }
      CoUninitialize();
    }