Search code examples
c#delphicominterop

COM interop, C#, Visual Studio 2010 -> embed interop types


My C# program accesses SAP via Nco3 (sapnco.dll). This program also needs to work with Delphi. Some of my methods return types from the sapnco.dll:

public void IRfcTable table(...) { ... }

in Delphi this method shows up as

function table(...): IUnknown { ... }

I suppose this IUnknown is because my TLB does not include the sapnco.dll. I tried "Embed Interop Types = true" in Visual Studio, but then this error occurs:

Error Interoptypen aus Assembly "C:\..." können nicht eingebettet werden, weil das ImportedFromTypeLibAttribute-Attribut oder das PrimaryInteropAssemblyAttribute-Attribut fehlt. c:...\sapnco.dll

(Interop Types could not be embedded because some attributes are missing).

Is this the right way? If so, where to put these attributes?


Solution

  • sapnco.dll is a .NET dll, so it is not exposed to COM, so you cannot directly use this types in a COM environment. The solution to your problem is to create a library to wrap the sapnco.dll in COM exposed classes:

    As an example:

    [ComVisible(true)]
    public interface IComRfcTable
    {
        public void DoSomething();
    }
    
    [ComVisible(true)]
    public class ComRfcTable : IComRfcTable
    {
        private _rfcTable; // object to wrap
        public ComRfcTable(IRfcTable rfcTable)
        {
            _rfcTable = rfcTable
        }
    
        public void DoSomething()
        {
            _rfcTable.DoSomething();
        }
    }
    

    Then your method must be implemented like:

     public IComRfcTable table(...) { ... }