Wednesday, 21 February 2018

C# - .net versions of COM interfaces

Currently poking around in C# and COM development and one great resource I have discovered is that compiler errors can detail the C# versions of COM interfaces.  So, open a C# Class library project and declare the default class (Class1) as implementing a classic COM interface, e.g. IAdviseSink so my code looks like this

namespace ComInterfacesInCSharp
{
    public class Class1 : System.Runtime.InteropServices.ComTypes.IAdviseSink 
    {
    }
}

Then attempt to compile, you will get the following error messages ...

error CS0535: 'Class1' does not implement interface member ...
  ... System.Runtime.InteropServices.ComTypes.IAdviseSink ...
OnDataChange(ref System.Runtime.InteropServices.ComTypes.FORMATETC, 
              ref System.Runtime.InteropServices.ComTypes.STGMEDIUM)
OnViewChange(int, int)
OnRename(System.Runtime.InteropServices.ComTypes.IMoniker)
OnSave()
OnClose()

So now you can dig out these method signatures and put them in your class, I used full qualification whilst I progressed through compilation errors. I used void as the return type and supplied some parameter identifiers to get to this

namespace ComInterfacesInCSharp
{
    public class Class1 : System.Runtime.InteropServices.ComTypes.IAdviseSink 
    {
        void System.Runtime.InteropServices.ComTypes.IAdviseSink.OnDataChange(
                      ref System.Runtime.InteropServices.ComTypes.FORMATETC form, 
                      ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { }
        void System.Runtime.InteropServices.ComTypes.IAdviseSink.OnViewChange(
                      int i1, int i2) {}
        void System.Runtime.InteropServices.ComTypes.IAdviseSink.OnRename(
                         System.Runtime.InteropServices.ComTypes.IMoniker mon) {}
        void System.Runtime.InteropServices.ComTypes.IAdviseSink.OnSave() {}
        void System.Runtime.InteropServices.ComTypes.IAdviseSink.OnClose() {}
    }
}

And that compiled! Yay! Of course we can then economise

using System.Runtime.InteropServices.ComTypes;

namespace ComInterfacesInCSharp
{
    public class Class1 : IAdviseSink 
    {
        void IAdviseSink.OnDataChange(ref FORMATETC form, 
                      ref STGMEDIUM medium) { }
        void IAdviseSink.OnViewChange(int i1, int i2) {}
        void IAdviseSink.OnRename(IMoniker mon) {}
        void IAdviseSink.OnSave() {}
        void IAdviseSink.OnClose() {}
    }
}

Ok, so I'm not claiming there won't be problems ahead going down this path ut it does give a starting point and then we can use Google and StackOverflow to iron out difficulties.

*** EDIT *** EDIT *** EDIT *** EDIT ***

So I asked about another interface on StackOverflow that wasn't playing ball and my new favourite Stackoverflow user gave both an excellent link to Microsoft Reference Source site and also a new gif to remind me that the C# IDE can be very very helpful and write out the interface for you from the menu. Here is the gif ...

No comments:

Post a Comment