Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Subscribe to an event through COM GetEvent return null

Subscribe to an event through COM GetEvent return null

Scheduled Pinned Locked Moved C#
helpcomjson
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aymen Tn
    wrote on last edited by
    #1

    Hi I'm having a source code of an Api, but since we don't have to change it. I'm creating a windows service that register it as a COM component then it could be used from the client. I'll try to simplify my issue. I started with the creation of an interface having the function and an event (that uses delegate)

    [Guid(ApiServices.InterfaceId), ComVisible(true)]
    public interface IApiServices
    {
    [DispId(1)]
    ApiResult Function1();

     \[DispId(2)\]
     event TestEvent event1;
    

    }

    Then I created a class that implements this inteface

     \[
     Guid(ClassId), ComVisible(true)
     ClassInterface(ClassInterfaceType.None)
     ProgId("ApiService")   
     \]
    

    public class ImplApiServices : MarshalByRefObject, IApiServices
    {
    private ApiObject _apiObject;

    public ImplApiServices ()
    {
    _apiObject= new ApiObject();
    _apiObject.TestEvent += OnEvent1;
    }

    ApiResult Function1()
    {
    return _apiObject.Function1();
    }

    public event TestEvent event1;

    private void OnEvent1(object e)
    {
    if (event1 != null)
    event1(e);
    }
    }

    And then I did all the other steps to register it as a COM component and launch use it through a service. when I call it using a client, it is recognized and the function returns the result perfectly

    var _apiServiceObjType= Type.GetTypeFromProgID("ApiService");
    var _apiServiceObj= Activator.CreateInstance(_apiServiceObjType);

    var result = (ApiResult )_apiServiceObjType.InvokeMember("Function1",
    BindingFlags.InvokeMethod, null, _apiServiceObj, null);

    But when I want to subscribe a method to the event, I get a null eventInfo in the following instruction and can't go further

    EventInfo eventInfo = _apiServiceObjType.GetEvent(event1);

    Any link or recommendation that helps with this kind of problem is welcome. Thanks

    L 1 Reply Last reply
    0
    • A aymen Tn

      Hi I'm having a source code of an Api, but since we don't have to change it. I'm creating a windows service that register it as a COM component then it could be used from the client. I'll try to simplify my issue. I started with the creation of an interface having the function and an event (that uses delegate)

      [Guid(ApiServices.InterfaceId), ComVisible(true)]
      public interface IApiServices
      {
      [DispId(1)]
      ApiResult Function1();

       \[DispId(2)\]
       event TestEvent event1;
      

      }

      Then I created a class that implements this inteface

       \[
       Guid(ClassId), ComVisible(true)
       ClassInterface(ClassInterfaceType.None)
       ProgId("ApiService")   
       \]
      

      public class ImplApiServices : MarshalByRefObject, IApiServices
      {
      private ApiObject _apiObject;

      public ImplApiServices ()
      {
      _apiObject= new ApiObject();
      _apiObject.TestEvent += OnEvent1;
      }

      ApiResult Function1()
      {
      return _apiObject.Function1();
      }

      public event TestEvent event1;

      private void OnEvent1(object e)
      {
      if (event1 != null)
      event1(e);
      }
      }

      And then I did all the other steps to register it as a COM component and launch use it through a service. when I call it using a client, it is recognized and the function returns the result perfectly

      var _apiServiceObjType= Type.GetTypeFromProgID("ApiService");
      var _apiServiceObj= Activator.CreateInstance(_apiServiceObjType);

      var result = (ApiResult )_apiServiceObjType.InvokeMember("Function1",
      BindingFlags.InvokeMethod, null, _apiServiceObj, null);

      But when I want to subscribe a method to the event, I get a null eventInfo in the following instruction and can't go further

      EventInfo eventInfo = _apiServiceObjType.GetEvent(event1);

      Any link or recommendation that helps with this kind of problem is welcome. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I believe you need to use the GetEvent() with "binding flags":

      Quote:

      An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

      [Type.GetEvent Method (String, BindingFlags) (System)](https://msdn.microsoft.com/en-us/library/w7k6k1z6(v=vs.110).aspx)

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      A 1 Reply Last reply
      0
      • L Lost User

        I believe you need to use the GetEvent() with "binding flags":

        Quote:

        An event is considered public to reflection if it has at least one method or accessor that is public. Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

        [Type.GetEvent Method (String, BindingFlags) (System)](https://msdn.microsoft.com/en-us/library/w7k6k1z6(v=vs.110).aspx)

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        A Offline
        A Offline
        aymen Tn
        wrote on last edited by
        #3

        Hi Thanks for the answer. I already tried that but it's not working :(

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups