Runtime: Com Interop in .netcore

Created on 18 May 2016  路  14Comments  路  Source: dotnet/runtime

Hi,
Is COM interop possible in .net core? I am trying to create a instance of an com interface using RunTimeEnvironment.GetRuntimeInterfaceAsObject. Looks like the api is not present.

question

Most helpful comment

Is it possible to make a COM server in .NET Core, by using something like ComVisible?

Additionally, is it possible to compile this COM-visible assembly to a native Windows dll? (By using CoreRT)

All 14 comments

/cc @yizhang82

@acesiddhu COM interop is supported in WIndows only for .NET Core, and it does not have full support (such as IDispatch) as full .NET framework.
RuntimeEnvironment.GetRuntimeInterfaceAsObject API is not part of .NET Core, because .NET Core does not have the same Com-based runtime interfaces (they don't make sense in cross-plat context) - we are moving to a more flat C-style hosting API. @janvorli should be able to point you to the right set of APIs depending on exactly what you need.

Is there way to Get instance of IUnknown interface? Specifically I am looking to use IMetaDataDispenser interface.

@acesiddhu You can use Marshal.GetObjectForIUnknown to convert it to a RCW (assuming that's what you mean by "get instance of IUnknown interface", or simply have your p/invoke (if any) returns IMetadataDispenser interface. Just keep in mind it is windows only :)

@yizhang82
the interface resides in kernel32 and not owned by us. Also I need to create an instance of that interface and not actually get :). I saw a similar usage in one of the repo. Is this approach supported?

PS: I am writing code which is build against .net core but for now it is currently available on windows hence I am trying to use COM as of today.

I can also Call CoCreateInstance. Just wanted to check which one is preferred way in .Net core (given that requirement is to run only on Windows :))

@acesiddhu Yes, CoCreateInstance should work if you want to create the object:

    [DllImport("ole32.dll")]
    private static extern int CoCreateInstance([in]ref Guid rclsid, IntPtr pUnkOuter,
                                       Int32 dwClsContext,
                                       [in]ref Guid riid,
                                       [MarshalAs(UnmanagedType.Interface)] out object ppv);

Personally that's the way I would go, assuming that you need to create the underlying COM object given a GUID, and simply cast that to a ISomeComInterface to make calls.

@acesiddhu I'm closing this issue for now. Let me know if you have further questions and feel free to re-open this.

@yizhang82
I cannot get load a vb6 style com dll at all. I get a class not registered even though it is. So a vb6 com dll is not supported at all?

` public static void Main(string[] args)
{
var r = Ole32Methods.CoInitialize(new IntPtr(0));

        const uint CLSCTX_INPROC_SERVER = 1;

        Guid clsid = new Guid("C31CBF72-92C6-4A47-A4C4-935706C26D34");

        // GUID of the required interface
        Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

        object instance = null;

        uint hResult = Ole32Methods.CoCreateInstance(ref clsid, null,
                       CLSCTX_INPROC_SERVER, ref IID_IUnknown, out instance);

        _ICommonUI sidCUI = instance as _ICommonUI;

        sidCUI.Show(1, null);
    }  `

@wxb1 My first guess is that you are running .NET Core app under 64-bit (that's the only flavor we support today officially) and your VB6 COM is registered under 32-bit (since it is 32-bit only). The 64-bit/32-bit registry are in different places in registry.

Thanks yizhang82. .Net core is 64-bit only?! I can't use it for my purposes then... Is there any plans for 32-bit? Is there is a time frame?

@gkhanna79 can you comment on 32-bit plan?

We do have .NET Core for Windows x86. The installers/downloads are available at https://www.microsoft.com/net/download#core.

Is it possible to make a COM server in .NET Core, by using something like ComVisible?

Additionally, is it possible to compile this COM-visible assembly to a native Windows dll? (By using CoreRT)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jzabroski picture jzabroski  路  3Comments

omajid picture omajid  路  3Comments

aggieben picture aggieben  路  3Comments

EgorBo picture EgorBo  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments