Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deriving COM interfaces in .NET

Due to company constraints out of my control, I have the following scenario:

A COM library that defines the following interface (no CoClass, just the interface):

[
    object,
    uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
    dual,
    nonextensible,
    helpstring("IService Interface"),
    pointer_default(unique)
]
IService : IDispatch
{
  HRESULT DoSomething();
}

[
    object,
    uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
    dual,
    nonextensible,
    helpstring("IProvider Interface"),
    pointer_default(unique)
]
IServiceProvider : IDispatch
{
  HRESULT Init( IDispatch *sink, VARIANT_BOOL * result );
  HRESULT GetService( LONG serviceIndicator, IService ** result );
};


[
    uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
    version(1.0),
]
library ServiceLibrary
{
    importlib("stdole2.tlb");

   interface IService;
   interface IServiceProvider;
};

I have a COM (written w/ C++) that implements both interfaces and provides our application(s) with said service. All is fine, I think.

I'm trying to build a new IProvider and IService in .NET (C#).

I've built a Primary Interop Assembly for the COM library, and implemented the following C#:

[ComVisible( true )]
[Guid( "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" )]
public interface INewService : IService
{
   //  adds a couple new properties
}

[ComVisible( true )]
[Guid( "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" )]
public class NewService : INewService
{
   //  implement interface
}

[ComVisible( true )]
[Guid( "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" )]
public interface INewProvider : IServiceProvider
{
   //  adds nothing, just implements
}

[ComVisible( true )]
[Guid( "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" )]
public class NewProvider : INewProvider
{
   //  implement interface
}

When I attempt to slip this into the existing runtime, I am able to create the NewProvider object from COM (C++), and QueryInterface for IServiceProvider. When I attempt to call a method on the IServiceProvider, a System.ExecutionEngineException is thrown.

The only other thing I can find, is by looking at the .tlh files created by the #import, shows the legacy COM IExistingProvider class correctly shows that it is derived from IServiceProvider. However the .NET class shows a base of IDispatch. I'm not sure if this a sign, indication, helpful, something else.

like image 877
DevSolo Avatar asked Oct 12 '08 14:10

DevSolo


People also ask

Does interface needs to be derived?

No need, when your base class is implementing interface or other classes all the properties and functionality introduced in base class will inherit to derived class.

What is interface C#?

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. An interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.

How many types of interfaces are there in C#?

Some of the interface types in C# include. IEnumerable − Base interface for all generic collections. IList − A generic interface implemented by the arrays and the list type. IDictionary − A dictionary collection.

Can interface implement interface C#?

C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.


1 Answers

It could be a problem with the name IServiceProvider. Check that you haven't already imported an interface with the same name.

When I create an COM Interface library using your IDL, and then try to import it from another client, I get the warning:

Warning 65  warning C4192: automatically excluding 'IServiceProvider' while importing type library 'ServiceLibrary.dll'

Otherwise, you can try renaming it to IServiceProvider2. That's what I did, and everything works fine. I'm using Visual Studio 2008.

If this code runs properly on your machine (it works perfectly on mine) then the problem could be in your implementation.

IDL:

import "oaidl.idl";

[
    object,
    uuid(9219CC5B-31CC-4868-A1DE-E18300F73C43),
    dual,
    nonextensible,
    helpstring("IService Interface"),
    pointer_default(unique)
]
interface IService : IDispatch
{
  HRESULT DoSomething(void);
}

[
    object,
    uuid(9219CC5B-31CC-4868-A1DE-E18300F73C44),
    dual,
    nonextensible,
    helpstring("IProvider Interface"),
    pointer_default(unique)
]
interface IServiceProvider2 : IDispatch
{
  HRESULT Init( IDispatch *sink, VARIANT_BOOL * result );
  HRESULT GetService( LONG serviceIndicator, IService ** result );
};

[
    uuid(9219CC5B-31CC-4868-A1DE-E18300F73C45),
    version(1.0),
]
library ServiceLibrary
{
    importlib("stdole2.tlb");

    interface IService;
    interface IServiceProvider2;
};

C#:

using System.Runtime.InteropServices;
using System.Windows.Forms;
using ServiceLibrary;
using IServiceProvider=ServiceLibrary.IServiceProvider2;

namespace COMInterfaceTester
{
    [ComVisible(true)]
    [Guid("2B9D06B9-EB59-435e-B3FF-B891C63108B2")]
    public interface INewService : IService
    {
        string ServiceName { get; }
    }

    [ComVisible(true)]
    [Guid("2B9D06B9-EB59-435e-B3FF-B891C63108B3")]
    public class NewService : INewService
    {
        public string _name;

        public NewService(string name)
        {
            _name = name;
        }

        //  implement interface
        #region IService Members

        public void DoSomething()
        {
            MessageBox.Show("NewService.DoSomething");
        }

        #endregion

        public string ServiceName
        {
            get { return _name; }
        }
    }

    [ComVisible(true)]
    [Guid("2B9D06B9-EB59-435e-B3FF-B891C63108B4")]
    public interface INewProvider : IServiceProvider
    {
        //  adds nothing, just implements
    }

    [ComVisible(true)]
    [Guid("2B9D06B9-EB59-435e-B3FF-B891C63108B5")]
    public class NewProvider : INewProvider
    {
        //  implement interface
        public void Init(object sink, ref bool result)
        {
            MessageBox.Show("NewProvider.Init");
        }

        public void GetService(int serviceIndicator, ref IService result)
        {
            result = new NewService("FooBar");
            MessageBox.Show("NewProvider.GetService");
        }
    }
}    

C++ Client:

#include "stdafx.h"
#include <iostream>
#include <atlbase.h>
#import "COMInterfaceTester.tlb" raw_interfaces_only
#import "ServiceLibrary.dll" raw_interfaces_only

using std::cout;

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);   //Initialize all COM Components
    COMInterfaceTester::INewProviderPtr pNewProvider(__uuidof(COMInterfaceTester::NewProvider));
    ServiceLibrary::IServiceProvider2 *pNewProviderPtr;

    HRESULT hr = pNewProvider.QueryInterface(__uuidof(ServiceLibrary::IServiceProvider2), (void**)&pNewProviderPtr);

    if(SUCCEEDED(hr))
    {       
        VARIANT_BOOL result = VARIANT_FALSE;
        int *p = NULL;

        hr = pNewProviderPtr->Init((IDispatch*)p, &result);

        if (FAILED(hr))
        {
            cout << "Failed to call Init";
        }

        ServiceLibrary::IService *pService = NULL;
        hr = pNewProviderPtr->GetService(0, &pService);

        if (FAILED(hr))
        {
            cout << "Failed to call GetService";
        }
        else
        {
            COMInterfaceTester::INewService* pNewService = NULL;
            hr = pService->QueryInterface(__uuidof(COMInterfaceTester::INewService), (void**)&pNewService);

            if (SUCCEEDED(hr))
            {
                CComBSTR serviceName;
                pNewService->get_ServiceName(&serviceName); 

                if (serviceName == "FooBar")
                {
                    pService->DoSomething();
                }
                else
                    cout << "Unexpected service";

                pNewService->Release();

            }

            pService->Release();
        }

        pNewProviderPtr->Release();
    }
    else
        cout << "Failed to query for IServiceProvider2";

    pNewProvider.Release();
    CoUninitialize ();   //DeInitialize all COM Components

}
like image 109
ilitirit Avatar answered Sep 21 '22 16:09

ilitirit