Menu

How to create a SWbemSink like callback object and listen to a permanent event notification for COM server

Mike J
2013-09-12
2014-12-31
  • Mike J

    Mike J - 2013-09-12

    we can create a com server stub through WbemScripting.SWbemLocator. then the j-interop code as the client can talks with the com server. No problem for it.
    Next we want to listen to interesting events on the com server. And we'd like to subscribe a permanent event notification for some events. In order to do so, we choose a asynchronous mechanism. And a callback object based on SWbemSink would be implemented.
    My question is: how to do it?

    I have studied the examples, like, MSInternetExplorer.java and DWebBrowserEvent2.java. it's a good example.
    first it defines a com server (WebBrowser2);
    then create a java com object for DWebBrowserEvent2;
    then implement the DWebBrowserEvent2's API;
    then attach the event listener to the com server.

    Very neat as a sample code.

    However, for a generic COM server case. how can I do a similar thing? For example, if I have a query string, like, "select * from CIM_AlertIndication". my questions are,

    which com server should be created and attached by an event listener?
    which event class should the event listener handle?

    Thanks.

     
    • Vikram Roopchand

      Hi,

      The easiest way to settle this is trying to find a similar VBScript or C++
      (but with the Scripting locator) example and mimic the same in java. If you
      have one we can help you create a Java sample.

      best regards,
      Vikram

      On Fri, Sep 13, 2013 at 2:22 AM, Mike J mikej1688@users.sf.net wrote:

      we can create a com server stub through WbemScripting.SWbemLocator. then
      the j-interop code as the client can talks with the com server. No problem
      for it.
      Next we want to listen to interesting events on the com server. And we'd
      like to subscribe a permanent event notification for some events. In order
      to do so, we choose a asynchronous mechanism. And a callback object based
      on SWbemSink would be implemented.
      My question is: how to do it?

      I have studied the examples, like, MSInternetExplorer.java and
      DWebBrowserEvent2.java. it's a good example.
      first it defines a com server (WebBrowser2);
      then create a java com object for DWebBrowserEvent2;
      then implement the DWebBrowserEvent2's API;
      then attach the event listener to the com server.

      Very neat as a sample code.

      However, for a generic COM server case. how can I do a similar thing? For
      example, if I have a query string, like, "select * from
      CIM_AlertIndication". my questions are,

      which com server should be created and attached by an event listener?
      which event class should the event listener handle?

      Thanks.

      How to create a SWbemSink like callback object and listen to a permanent
      event notification for COM serverhttps://sourceforge.net/p/j-interop/discussion/600729/thread/152e6272/?limit=25#8f52


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
  • Mike J

    Mike J - 2013-09-13

    Thanks for the reply.
    This case is a little different from MSInternetExceplorer example.
    Here is an example in vb script. The event would be an intrinsic type.

    1). Asynchronously monitor specific processes
    Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
    Set objWMIService = GetObject("winmgmts:\.\root\cimv2")
    objWMIService.ExecNotificationQueryAsync
    SINK,

    "SELECT * FROM __InstanceCreationEvent WITHIN 1 " & _
    "WHERE TargetInstance ISA 'Win32_Process' and TargetInstance.Name='calc.exe'"
    Do
    WScript.Sleep 10000
    Loop
    Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
    Wscript.Echo "Process Name: " & objLatestEvent.TargetInstance.Name
    End Sub

    the detail info for it can be found from the link,

    http://technet.microsoft.com/en-us/library/ee692849.aspx

     
    • Vikram Roopchand

      Hi,

      I think this will be more or less similar to the MSInternetExplorer
      example. You will need to create a j-interop CoClass representing SWbemSink
      and then pass an instance of that when doing the query execution just like
      the example.

      thanks,
      best regards,
      Vikram

      On Fri, Sep 13, 2013 at 8:11 PM, Mike J mikej1688@users.sf.net wrote:

      Thanks for the reply.
      This case is a little different from MSInternetExceplorer example.
      Here is an example in vb script. The event would be an intrinsic type.

      1). Asynchronously monitor specific processes
      Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
      Set objWMIService = GetObject("winmgmts:.\root\cimv2")
      objWMIService.ExecNotificationQueryAsync
      SINK,

      "SELECT * FROM __InstanceCreationEvent WITHIN 1 " & _
      "WHERE TargetInstance ISA 'Win32_Process' and
      TargetInstance.Name='calc.exe'"
      Do
      WScript.Sleep 10000
      Loop
      Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
      Wscript.Echo "Process Name: " & objLatestEvent.TargetInstance.Name
      End Sub

      the detail info for it can be found from the link,

      http://technet.microsoft.com/en-us/library/ee692849.aspx

      How to create a SWbemSink like callback object and listen to a permanent
      event notification for COM serverhttps://sourceforge.net/p/j-interop/discussion/600729/thread/152e6272/?limit=25#242f


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
  • Mike J

    Mike J - 2013-09-16

    Thanks for the reply. still need a little more advice.
    in MSinternetExplorer.java, the IE browser is the com object, and DWebBrowserEvents2 is the event interface which can be implemented by the sink object. and the IID of the DWebBrowserEvents2 is passed to
    identifier = JIObjectFactory.attachEventHandler(ieObject, "34A715A0-6587-11D0-924A-0020AFC7AC4D",JIObjectFactory.buildObject(session, javaComponent));

    that is,
    ieObject - IE browser
    34A715A0-6587-11D0-924A-0020AFC7AC4D - IID of the DWebBrowserEvents2
    javaComponent - the sink object which implements the DWebBrowserEvents2

    For my async case, my questions are,

    1). What is the com object? SwbemLocator? Win32_Process?
    2). What is the event's interface my sink object would implement?
    3). What is the IID of the event's interface?
    4). in the vb script, people can create a SWbemSink object by

    Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")

    how can we explicitly create a SWbemSink object in j-interop at a similar way? or should we do this way,

    JILocalCoClass javaComponent = new JILocalCoClass(new JILocalInterfaceDefinition(
    "34A715A0-6587-11D0-924A-0020AFC7AC4D"), DWebBrowserEvents2.class);

    it seemed for me that the created event sink object does not extend from SwbemLocator.SWbemSink object explicitly. May in java we don't need to do so?

    thanks.

     
  • Mike J

    Mike J - 2013-09-16

    Furthermore, in general, if the query string refers to an extrinsic event like,
    "select * from CIM_AlertIndication". and if I still use the above scenario, which com object should I focus on, and which event interface should I implement for a sink object and what's the IID of the event interface? I'd like to know a general way using j-interop.

     
  • Mike J

    Mike J - 2013-09-25

    If we use the same thing in MSInternetExplorer,
    //IID for ISWbemSink.
    private static final String SWBEMSINK_IID = "75718C9F-F029-11D1-A1AC-00C04FB6C223";
    //IID for ISWbemSinkEvents
    private static final String SWBEMSINK_EVENTS_IID= "75718CA0-F029-11d1-A1AC-00C04FB6C223";

    JISession session = JISession.createSession(domain, user, password);
    JIComServer comServer = new JIComServer(JIProgId.valueOf("WbemScripting.SWbemLocator"), address, session);
    IJIComObject swbemLocatorObject = comServer.createInstance();

    JIObjectFactory.attachEventHandler(swbemLocatorObject , SWBEMSINK_IID, comObjectSWbemSink);

    Here the comObjectSWbemSink implements cancel(), OnObjectReady(), OnProgress(), OnCompleted(), and OnObjectPut() methods, which are defined in the interfaces ISWbemSink and ISWbemSinkEvents. when contstructing the comObjectSWbemSink object, I also added a list with the following interfaces,

    List list = new ArrayList();
    list.add(SWBEMSINK_IID);
    list.add(IJIDispatch.IID);
    list.add(SWBEMSINK_EVENTS_IID);
    swbemSinkCoClass.setSupportedEventInterfaces(list);
    comObjectSWbemSink = JIObjectFactory.buildObject(session, swbemSinkCoClass);

    But when I ran the app, the line with JIObjectFactory.attachedEventHandler(...) caused exceptions,

    org.jinterop.dcom.common.JIException: No such interface supported. [0x80004002]
    at org.jinterop.dcom.core.JIComServer.getInterface(JIComServer.java:825)
    at org.jinterop.dcom.core.JIComObjectImpl.queryInterface(JIComObjectImpl.java:89)
    at org.jinterop.dcom.core.JIFrameworkHelper.attachEventHandler(JIFrameworkHelper.java:273)
    at org.jinterop.dcom.impls.JIObjectFactory.attachEventHandler(JIObjectFactory.java:61)
    at com.hp.uca.protocolmanager.WMIProtocol.SWbemSinkTest.testOp(SWbemSinkTest.java:180)
    at com.hp.uca.protocolmanager.WMIProtocol.SWbemSinkTest.main(SWbemSinkTest.java:235)
    Caused by: org.jinterop.dcom.common.JIRuntimeException: No such interface supported. [0x80004002]
    at org.jinterop.dcom.core.JIRemUnknown.read(JIRemUnknown.java:94)
    at ndr.NdrObject.decode(NdrObject.java:36)
    at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:137)
    at rpc.Stub.call(Stub.java:113)
    at org.jinterop.dcom.core.JIComServer.getInterface(JIComServer.java:812)

    My question is: the first argument in the method, attachEventHandler(...), is the swbemLocatorObject. Is it equal to the ieObject in MSInternetExplorer? My doubt is that the SwbemLocatorObject is just a locator connecting to the com server, while the ieObject represents an IE Browser.

     
  • Mike J

    Mike J - 2013-10-03

    any body can answer the question?
    The problem is: if we pass swbemLocatorObject (a IJIComObject for SWbemLocator) to the method, JIObjectFactory.attachEventHandler(swbemLocatorObject, SWBEMSINK_IID, comObjectSWbemSink), you can go into the method, and see it calls a line in JIFrameworkHelper.java, like,
    IJIComObject connectionPointContainer = (IJIComObject)comObject.queryInterface("B196B284-BAB4-101A-B69C-00AA00341D07"); //the iid of the IConnectionPointContainer

    this line causes an exception:
    org.jinterop.dcom.common.JIException: No such interface supported. [0x80004002]

    It implies that the SWbemLoctor object (the IJIComObject) doesn't implement the IConnectionPointContainer interface. Thus SWbemLoctor is not also a connectable object.

    But we always use SwbemLoctor as a com server object and start everything from it for WMI. If it is not a connectable object, how can we pass a SWbemSink object for an async event notification?
    any other scripting object can be used as a connectable object here? otherwise, the method,
    JIObjectFactory.attachEventHandler(...) cannot be used.

    Any helps?

     
    • Vikram Roopchand

      Dear Mark,

      I presume you have a VB script code already running (to a remote machine),
      can you share the Wireshark capture from the same depicting this behaviour
      ? We can check what goes in there and work backwards to j-interop.

      best regards,
      Vikram

      On Fri, Oct 4, 2013 at 2:51 AM, Mike J mikej1688@users.sf.net wrote:

      any body can answer the question?
      The problem is: if we pass swbemLocatorObject (a IJIComObject for
      SWbemLocator) to the method,
      JIObjectFactory.attachEventHandler(swbemLocatorObject, SWBEMSINK_IID,
      comObjectSWbemSink), you can go into the method, and see it calls a line in
      JIFrameworkHelper.java, like,
      IJIComObject connectionPointContainer =
      (IJIComObject)comObject.queryInterface("B196B284-BAB4-101A-B69C-00AA00341D07");
      //the iid of the IConnectionPointContainer

      this line causes an exception:
      org.jinterop.dcom.common.JIException: No such interface supported.
      [0x80004002]

      It implies that the SWbemLoctor object (the IJIComObject) doesn't
      implement the IConnectionPointContainer interface. Thus SWbemLoctor is not
      also a connectable object.

      But we always use SwbemLoctor as a com server object and start everything
      from it for WMI. If it is not a connectable object, how can we pass a
      SWbemSink object for an async event notification?
      any other scripting object can be used as a connectable object here?
      otherwise, the method,
      JIObjectFactory.attachEventHandler(...) cannot be used.

      Any helps?

      How to create a SWbemSink like callback object and listen to a permanent
      event notification for COM serverhttps://sourceforge.net/p/j-interop/discussion/600729/thread/152e6272/?limit=25#a385


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
  • Mike J

    Mike J - 2013-10-08

    Thanks for the reply.
    Before collecting the log msg from wireshark, I'd like to point out that like shown the above, the code execution remains on the client side, seemed to have nothing to do with networking operation. The code tried to determine if SWbemLocator is a connectable object, that is, whether it implements the IConnectionPointContainer interface or not.
    I checked the IDL for Scripting API for WMI, looked like no obvious relationship between SWbemLocator and IConnectionPointContainer. If SWbemLocator doesn't implement IConnectionPointContainer, then an exception of "no such interface supported" would always be thrown. How should we avoide it?
    I also used SwbemServices.execEventNotificationAsyn(...) but the socket was automatically closed a few second later. People on this forum had found same issues long time ago.

    All I'd like to get from using j-interop is to create a permanent event notification subscription on the wmi device, after the subscription. The session between the wmi device and the j-interop app would be disconnected and discarded . Then the app works as a wmi server waiting for the event indications sent by the wmi device which now works as a client.

     
  • Mike J

    Mike J - 2013-10-14

    Dear Vikram,
    Can you shed light on this generic question? calling the attachEventHandler(...) always checks to see if the SWbemLocator implements the interface, IConnectionPointContainer or not. Definitely it's going to fail because the scripting class seemed not. How should we get through it?

     
    • Vikram Roopchand

      Hi Mike,

      Not sure if I follow. Without a connection point container how will the COM
      server get access to an event handler interface ?

      best regards,
      Vikram

      On Mon, Oct 14, 2013 at 8:59 PM, Mike J mikej1688@users.sf.net wrote:

      Dear Vikram,
      Can you shed light on this generic question? calling the
      attachEventHandler(...) always checks to see if the SWbemLocator implements
      the interface, IConnectionPointContainer or not. Definitely it's going to
      fail because the scripting class seemed not. How should we get through it?


      How to create a SWbemSink like callback object and listen to a permanent
      event notification for COM serverhttps://sourceforge.net/p/j-interop/discussion/600729/thread/152e6272/?limit=25#5458


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
  • Mike J

    Mike J - 2013-10-15

    Hi Vikram,
    That's a very good question. The class "SWbemLocator" is the entry class (com server) in Scripting API for WMI. We have to use it as the object on which the events would happen. But like what's found, it doesn't implement the IConnectionPointContainer interface. It seemed like some things were broken.

    But the point is: if SWbemLocator seemed not to implement IConnectionPointContainer, why does the native method,SWbemServices.eventNotificationQueryAsync(...) work fine?

    Appreciate any helps.

     
    • Vikram Roopchand

      Hi ,

      Can you provide us with a wireshark capture from a remote Vbscript or COM
      code doing this? I think its time to look at the wire.

      Best regards,
      Vikram
      On Oct 16, 2013 12:00 AM, "Mike J" mikej1688@users.sf.net wrote:

      Hi Vikram,
      That's a very good question. The class "SWbemLocator" is the entry class
      (com server) in Scripting API for WMI. We have to use it as the object on
      which the events would happen. But like what's found, it doesn't implement
      the IConnectionPointContainer interface. It seemed like some things were
      broken.

      But the point is: if SWbemLocator seemed not to implement
      IConnectionPointContainer, why does the native
      method,SWbemServices.eventNotificationQueryAsync(...) work fine?

      Appreciate any helps.

      How to create a SWbemSink like callback object and listen to a permanent
      event notification for COM serverhttps://sourceforge.net/p/j-interop/discussion/600729/thread/152e6272/?limit=25#b426


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
  • Mike J

    Mike J - 2013-10-16

    Hi Vikram,
    I have sent an output file from Wireshark to your email address @j-interop.org. please find the output file from WS for testing the vbs file which is also attached.

    When I set the filtering of WS, I chosen tcp.destination.port = 135. It might not be good for getting the complete log data. I would choose tcp.destination.ipaddress or tcp.soruce.ipaddress, but no such filtering options. Do you have any suggestions for the filtering setting?
    
    Please let me know if you need more things.
    
    Thanks.
    
     
  • Tristan Zhou

    Tristan Zhou - 2014-12-31

    Guys,
    I'm facing the same issue. But you didn't follow up the final solution. Could you please share some code snippets about this?

    Thanks!

     

Log in to post a comment.