IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

S'abonner a un event d'un objet COM C#


Sujet :

MFC

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    391
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 391
    Par défaut S'abonner a un event d'un objet COM C#
    Bonjour

    Dans une application windows based, j'essaye d'acceder aux fonctions et event d'un objet com C#.

    J'arrive bien a créer le pointer vers l'objet, acceder a ses fonctions et proprietés mais je sèche completement comment
    déclarer la fontion delegate de mon objet afin d'intercepter des evenements.

    Le .tlh me donne ceci pour la partie event
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    struct __declspec(uuid("4794d615-be51-4a1e-b1ba-453f6e9337c4"))
    SC_ComObject;
        // interface _Object
        // [ default ] interface IComOjbect
        // [ default, source ] dispinterface IComEvents
     
    struct __declspec(uuid("eca5dd1d-096e-440c-ba6a-0118d351650b"))
    IComEvents : IDispatch
    {
        //
        // Wrapper methods for error-handling
        //
     
        // Methods:
        HRESULT DeviceEvent (
            _bstr_t SerialNumber,
            long eventType,
            _bstr_t args );
    };
    J'ai deja ce code la

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    SC_ComComponent::IComOjbectPtr pDevicePtr;
    HRESULT hRes = pDevicePtr.CreateInstance(SC_ComComponent::CLSID_SC_ComObject);
    	if (hRes != S_OK)
    	{
    		MessageBox("Error Create instance Com Object");
    		CoUninitialize();
    	}
    Comment je le modifie pour creer la function DeviceEvent et m y abonner

    Cordialement

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    391
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 391
    Par défaut
    pour ceux que ca interesse,

    a jouter ce .h a votre projet

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
     
    #ifndef TEVENT_HANDLER_H
      #define TEVENT_HANDLER_H
     
    #include <windows.h>
    //#include <SHLOBJ.H>
    //#include <memory>
    //#include <string>
     
    namespace TEventHandlerNamespace
    {
    // Generic event handler template class (especially useful (but not limited to) for non-ATL clients).
    template <class event_handler_class, typename device_interface, typename device_event_interface>
    class TEventHandler : IDispatch
    {
      friend class class_event_handler;
     
      typedef HRESULT (event_handler_class::*parent_on_invoke)
      (
        TEventHandler<event_handler_class, device_interface, device_event_interface>* pthis,
    	DISPID dispidMember, 
    	REFIID riid,
    	LCID lcid, 
    	WORD wFlags, 
    	DISPPARAMS* pdispparams, 
    	VARIANT* pvarResult,
    	EXCEPINFO* pexcepinfo, 
    	UINT* puArgErr
      );
     
    public :
      TEventHandler
      (
        event_handler_class& parent,
    	device_interface* pdevice_interface,  // Non-ref counted.
    	parent_on_invoke parent_on_invoke_function
      ) :
        m_cRef(1),
        m_parent(parent),
    	m_parent_on_invoke(parent_on_invoke_function),
    	m_pIConnectionPoint(0),
    	m_dwEventCookie(0)
      {
        SetupConnectionPoint(pdevice_interface);
      }
     
      ~TEventHandler()
      {
    	// Call ShutdownConnectionPoint() here JUST IN CASE connection points are still 
    	// alive at this time. They should have been disconnected earlier.
        ShutdownConnectionPoint();
      }
     
      STDMETHOD_(ULONG, AddRef)()
      {
    	InterlockedIncrement(&m_cRef);
     
    	return m_cRef;  
      }
     
      STDMETHOD_(ULONG, Release)()
      {
    	InterlockedDecrement(&m_cRef);
     
    	if (m_cRef == 0)
    	{
    		delete this;
    		return 0;
    	}
     
    	return m_cRef;
      }
     
      STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject)
      {
    	if (riid == IID_IUnknown)
    	{
    		*ppvObject = (IUnknown*)this;
    		AddRef();
    		return S_OK;
    	}
     
    	if ((riid == IID_IDispatch) || (riid == __uuidof(device_event_interface)))
    	{
    		*ppvObject = (IDispatch*)this;
    		AddRef();
    		return S_OK;
    	}
     
    	return E_NOINTERFACE;
      }
     
      STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
      {
    	return E_NOTIMPL;
      }
     
      STDMETHOD(GetTypeInfo)(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
      {
    	return E_NOTIMPL;
      }
     
      STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
    		   LCID lcid, DISPID* rgdispid)
      {
    	return E_NOTIMPL;
      }
     
      STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
    		   LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
    		   EXCEPINFO* pexcepinfo, UINT* puArgErr)
      {
    	return (m_parent.*m_parent_on_invoke)(this, dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
      }
     
    protected :
      LONG						m_cRef;
     
      // Pertaining to the owner of this object.
      event_handler_class&		m_parent;  // Non-reference counted. This is to prevent circular references.
     
      // Pertaining to connection points.
      IConnectionPoint*			m_pIConnectionPoint;  // Ref counted of course.
      DWORD						m_dwEventCookie;
      parent_on_invoke			m_parent_on_invoke;
     
      void SetupConnectionPoint(device_interface* pdevice_interface)
      {
        IConnectionPointContainer*	pIConnectionPointContainerTemp = NULL;
        IUnknown*					pIUnknown = NULL;
     
        // QI this object itself for its IUnknown pointer which will be used 
        // later to connect to the Connection Point of the device_interface object.
        this -> QueryInterface(IID_IUnknown, (void**)&pIUnknown);
     
        if (pIUnknown)
        {
          // QI the pdevice_interface for its connection point.
          pdevice_interface -> QueryInterface (IID_IConnectionPointContainer, (void**)&pIConnectionPointContainerTemp);
     
          if (pIConnectionPointContainerTemp)
          {
    	    pIConnectionPointContainerTemp -> FindConnectionPoint(__uuidof(device_event_interface), &m_pIConnectionPoint);
    	    pIConnectionPointContainerTemp -> Release();
    	    pIConnectionPointContainerTemp = NULL;
          }
     
          if (m_pIConnectionPoint)
          {
    	    m_pIConnectionPoint -> Advise(pIUnknown, &m_dwEventCookie);
          }
     
          pIUnknown -> Release();
    	  pIUnknown = NULL;
        }
      }
     
    public :
     
      void ShutdownConnectionPoint()
      {
        if (m_pIConnectionPoint)
        {
    	  m_pIConnectionPoint -> Unadvise(m_dwEventCookie);
    	  m_dwEventCookie = 0;
    	  m_pIConnectionPoint -> Release();
    	  m_pIConnectionPoint = NULL;
        }
      }
    };
     
    };
     
     
     
     
     
    #endif
    et dans votre .h
    importer votre objet,
    definir le delegate de la fonction,


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    #import "C:\\tmp\\COM Object\\SC_ComComponent.tlb" named_guids raw_interfaces_only
    using namespace SC_ComComponent;
     
    // ***** include our TEventHandler.h header class and use the TEventHandlerNamespace namespace. *****
    #include "TEventHandler.h"
    using namespace TEventHandlerNamespace;
     
    // ***** Make a forward declaration so that our TEventHandler template class can use it. *****
    class CCppSampleAppDlg;
    // ***** Declare an event handling class using the TEventHandler template. *****
    typedef TEventHandler <CCppSampleAppDlg, IComOjbect, IComEvents> IComOjbectEventHandler;
    et s'abonner a l'event dans le cpp apres crees son object

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    	HRESULT hRes = pDevicePtr.CreateInstance(SC_ComComponent::CLSID_SC_ComObject);
    	if (hRes != S_OK)
    	{
    		MessageBox("Error Create instance Com Object");
    		CoUninitialize();
    	}
     
    	m_pIComOjbectEventHandler = new IComOjbectEventHandler(*this, pDevicePtr, &CCppSampleAppDlg::OnDeviceEventInvoke);
    il ne rest plus qu'a faire la fonction recevant l"event

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    HRESULT CCppSampleAppDlg::OnDeviceEventInvoke
    	(
    	  IComOjbectEventHandler* pEventHandler,
    	  DISPID dispidMember, 
    	  REFIID riid,
    	  LCID lcid, 
    	  WORD wFlags, 
    	  DISPPARAMS* pdispparams, 
    	  VARIANT* pvarResult,
    	  EXCEPINFO* pexcepinfo, 
    	  UINT* puArgErr
    	)
    	{
    ........

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Event.observe (plusieurs objets)
    Par FrankOVD dans le forum Général JavaScript
    Réponses: 14
    Dernier message: 10/06/2008, 14h14
  2. Events + Récupération d'objet
    Par seblo_scoqi dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 08/12/2006, 10h21
  3. Gestion des event avec un objet ole MsExcel
    Par rdemont dans le forum Delphi
    Réponses: 3
    Dernier message: 03/07/2006, 16h08
  4. [COM] Gestion d'events contenus dans un objet COM
    Par Marmottoc dans le forum Delphi
    Réponses: 1
    Dernier message: 19/05/2006, 14h43
  5. [Débutant - JAVASCRIPT] L'objet event et l'objet xEvent
    Par sempire dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 08/11/2005, 15h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo