Software & Finance





C# - Step 5 Define Interface and Implement Class





 

There are 3 COM interface type listed below:

ComInterfaceType.InterfaceIsIUnknown - This COM interface type supports only early binding.

ComInterfaceType.InterfaceIsIDispatch - This COM interface type supports only late binding.

ComInterfaceType.InterfaceIsDual - COM interface types supports both IDispatch and IUnknown mode.

 

There are 3 ClassInterfaceType listed below:

 

ClassInterfaceType.None (No class interface will be generated so that COM clients need always to go through the interface explicitly defined in our case it is IMathCtrl.

ClassInterfaceType.AutoDispatch (for dispinterface for the class used in late binding for COM Client)

ClassInterfaceType.AutoDual (Discouraged to use since it might produce versioning conflicts and compilation problems)

 

The sample interface definition is given on this below: 

 

using System;

using System.Runtime.InteropServices;

 

namespace SFTCSServer

{

    [Guid("12D1EDAC-20C0-4faa-A774-B6F4C300B47E")]

    [InterfaceType(ComInterfaceType.InterfaceIsDual)]

    public interface IMathCtrl

    {

        [DispId(1)]

        int AddNumbers(int a, int b);

    }

 

    [Guid("282902B4-5FB9-461d-9CD0-FE8DD851F979")]

    [ClassInterface(ClassInterfaceType.None)]

    [ProgId("SFTCSServer.MathCtrl")]

    public class MathCtrl : IMathCtrl

    {

        public MathCtrl() { }

 

        public int AddNumbers(int a, int b)

        {

            return (a+b);

        }

 

        public int MuliplyNumbers(int a, int b)

        {

            return (a * b);

        }

    }

}

 

Back to Index of Steps for C# class Library Server and C++ Index

 

Click here to download the complete source code of C# Class Library Server and C++ Client