Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could simplify the .NET Code like this (remove all MarshalAs that should be done automatically):</p> <pre><code>[ComVisible(true)] public class MyRootClass : IMyRootClass // some class to start with { public IEmailEntity[] GetEntities() { List&lt;IEmailEntity&gt; list = new List&lt;IEmailEntity&gt;(); for(int i = 0; i &lt; 10; i++) { EmailEntity entity = new EmailEntity(); List&lt;IEmailAddress&gt; addresses = new List&lt;IEmailAddress&gt;(); 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... } </code></pre> <p>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: <a href="https://stackoverflow.com/questions/15793668/implement-a-c-sharp-dll-com-file-in-unmanaged-c-program">Implement a C# DLL COM File In Unmanaged C++ Program</a></p> <p>Then, you can access these classes in C++, like this:</p> <pre><code>#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&lt;IUnknown*&gt; entities = ptr-&gt;GetEntities(); // CComSafeArray needs atlsafe.h in the PCH for(int i = entities.GetLowerBound(0); i &lt;= entities.GetUpperBound(0); i++) { IEmailEntityPtr entity; entities.GetAt(i).QueryInterface(&amp;entity); _bstr_t body = entity-&gt;Body; printf("%S\n", body.GetBSTR()); CComSafeArray&lt;IUnknown*&gt; recipients = entity-&gt;BccRecipients; for(int j = recipients.GetLowerBound(0); j &lt;= recipients.GetUpperBound(0); j++) { IEmailAddressPtr address; recipients.GetAt(j).QueryInterface(&amp;address); _bstr_t name = address-&gt;Name; printf(" %S\n", name.GetBSTR()); } } CoUninitialize(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload