Skip to content

Using COM (interop) types in dynamo packages

pinzart90 edited this page Feb 20, 2024 · 10 revisions

Some general information about interop types

What are COM types? - https://learn.microsoft.com/en-us/windows/win32/com/the-component-object-model

The standard way to use COM types in C# is to reference and ship the primary interop assemblies (Basically a big collection of APIs) with your package.

An alternative to this is to embed the PIA (Primary interop assemblies) in your managed assembly. This basically only includes the types and members that are actually used by a managed assembly. However this approach has some other issues like type equivalence.

These posts describe the issue pretty well:

How Dynamo manages types equivalence

Dynamo delegates type equivalence to the .NET (dotnet) runtime. An example of this would be that 2 types with the same name and namespace, coming from different assemblies will not be considered equivalent, and Dynamo will give an error when loading the conflicting assemblies. As for interop types, Dynamo will check if the interop types are equivalent using the IsEquivalentTo API

How to avoid conflicts between embedded interop types

Some packages have already been created with embedded interop types (ex CivilConnection). Loading 2 packages with embedded interop assemblies that are not considered equivalent(different versions as defined here) will cause a package load error. Because of this, we suggest that package authors use dynamic binding (aka late binding) for interop types (solution is also described here).

You can follow this example:

public class Application
    {
        string m_sProdID = "SomeNamespace.Application";
        public dynamic m_oApp = null; // Use dynamic so you do not need the PIA types

        public Application()
        {
            this.GetApplication();
        }

        internal void GetApplication()
        {
            try

            {
                m_oApp = System.Runtime.InteropServices.Marshal.GetActiveObject(m_sProdID);
            }
            catch (Exception /*ex*/)
            {}
        }
    }
}

Releases

Roadmap

How To

Dynamo Internals

Contributing

Python3 Upgrade Work

Libraries

FAQs

API and Dynamo Nodes

Clone this wiki locally