views:

834

answers:

4

This is complicated, at least for me, so please bare with me. I'll also preface with I've spent a day searching, to no avail.

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 to provide our application(s) with said service. All is fine, I think.

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

I've built (I think correctly) 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, but 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, yet the .NET class shows a base of IDispatch. I'm not sure if this a sign, indication, helpful, something else.

Any help would be tremendously appreciated.

Thanks.

A: 

That is what I'm trying to do, however that's not the problem, I think. I guess my explanation wasn't as clear as I'd hope.

To elaborate, in my case, I have also written what in your case is the MSXML. To phrase another way, I've written the legacy COM component I'm deriving from and the (hopefully) newer .NET component. I'm thinking that the problem might actually be in the original COM component.

I get that exception when I make the analogous call to pIndrectPtr->hasFeature( ... )

Here is a more elaborated version of that COM IDL. I suspect the problem is there, but I'm not sure.

[
    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'm still researching, experimenting, and when I find an answer, I'll be sure to post.

Thanks again, any help is appreciated, as you never know what might help, even if it's indirect.

DevSolo
I removed the MSXML implementation and I added a second answer using the IDL you provided.I followed exactly the steps as before and besides a naming clash (IServiceProvider),everything works as expected.
ilitirit
A: 

You may have to specify additional attributes on your class to have it marshal correctly. I would look through available attributes here and maybe look at this tutorial if you haven't done so already.

Richard Nienaber
+3  A: 

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

}
ilitirit
IServiceProvider is actually defined in the System namespace - that is probably where the naming collisions is coming from.
Eli
A: 

I did, forgot/neglected to mention the interface names where changed to protect the guilty. I'm not sure if sharing the names would violate my contract, so I abstracted the names. Sorry for not mentioning that first. That said, based on our interface naming convention, I'd be very suprised if that was the cause. Good call though.

I appreciate the time you must have spent looking at this. I'm going to build from scratch the code you supplied. Based on schedules, it might take a few days, however I'll let you know what I found.

I did find on mention (lost the link) of the ExecutionEngineException error being something that app developers should never see (for what that's worth). Another link talked about somethigng similar being an actual bug in the interop code in .NET 1.x. I was unsuccessful in determining if that exact issue was fixed.

Thanks again.

DevSolo