COM中如何定义事件,并且在另一个应用程序中进行调用

解决方案 »

  1.   

    Okay, now let's illustrate the whole thing by a practical example.Create a new ATL-COM AppWizard Project and name it ConnectionCOM. 
    Right-click on the class view and create a new ATL object. 
    Name it Add (interface IAdd).Before you click the OK button, be sure to check the Support Connection point checkbox.Click OK.Note the classes generated in the class view. You will find one IAdd and _IAddEvents. The latter is just a proxy class; it is to be implemented on the client side. It came because we ticked the Connection_Points check box.Add a method 'Add(int a,int b) ' to IAdd interface and a method 'ExecutionOver(int Result)' to the _IAddEventsInterface. The class view will be as shown below.But because we have selected a dual interface and we do not need all that hassle, let's take out the support for IDispatch by editing the IDL file. Below is the original file.//===========================================================
    // ConnectionCOM.idl : IDL source for ConnectionCOM.dll
    //
      :
      :library CONNECTIONCOMLib
    {
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");[
      uuid(AFE854B0-246F-4B66-B26F-A1060225C71C),
      helpstring("_IAddEvents Interface")
    ]
    // Old block - take this out
    // dispinterface _IAddEvents
    // {
    // properties:
    // methods:
    // [id(1), helpstring("method ExecutionOver")]
    // HRESULT ExecutionOver(intResult);
    // };
    //To this one -put this in
    interface _IAddEvents : IUnknown
      {
      [id(1), helpstring("method ExecutionOver")] HRESULT
              ExecutionOver(intResult);
      };
      [
        uuid(630B3CD3-DDB1-43CE-AD2F-4F57DC54D5D0),
        helpstring("Add Class")
      ]
      coclass Add
      {
      [default] interface IAdd;
      //[default, source] dispinterface _IAddEvents; take this line
      //out and put the line below in
      [default, source] interface _IAddEvents ;
      };
    };//================================================================Whew! The client side is almost finished now. Now, do a build because we need the type library to do a neat thing with ATL. Now, right-click on the CoClass and click Implement Connection Point.Check _IAddEvents in the ensuing dialog box.A CProxy_IAddEvets class is generated with the Fire_ExecutionOver(int result) method. This will take care of how the COM object will call the client interface (and takes care of multiple clients calling the same COM DLL and other such issues). Now, let's implement our old IAdd Interface Add method.//=====================================================STDMETHODIMP CAdd::Add(int a, int b)
    {
    // TODO: Add your implementation code hereSleep(2000);   // to simulate a long process//OK, process over now; let's notify the clientFire_ExecutionOver(a+b);return S_OK;
    }
    //======================================================Do a build and the COM is ready. Make sure that the COM is registered.
      

  2.   

    看懂了?
    比如作个hello world
    1 Create a new ATL-COM AppWizard Project and name it helloworld
    2 insert->new atl object->simple object,起名叫helo 选则支持 connection point
    3 编译一遍
    4 right-click on the CoClass and click Implement Connection Point.
    5 right-click on the IheloEvents add method --sayhello and and ::MessageBox(0,"hello",0,0);
    6 right-click on the Ihelo add method --say and add Fire_Sayhello();
    再编译一遍 可以用了
      

  3.   

    client的原理如下:
    The steps for establishing a connection between a client and a connectable object are as follows: The client queries for IConnectionPointContainer on the object to determine whether the object is connectable. If this call is successful, the client holds a pointer to the IConnectionPointContainer interface on the connectable object and the connectable object reference counter has been incremented. Otherwise, the object is not connectable and does not support outgoing interfaces. 
    If the object is connectable, the client next tries to obtain a pointer to the IConnectionPoint interface on a connection point within the connectable object. There are two methods for obtaining this pointer, both in IConnectionPointContainer::FindConnectionPoint and in IConnectionPointContainer::EnumConnectionPoints. There are a few additional steps needed if EnumConnectionPoints is used. (See Using IConnectionPointContainer for more information.) If successful, the connectable object and the client both support the same outgoing interface. The connectable object defines it and calls it, and the client implements it. The client can then communicate through the connection point within the connectable object. 
    The client then calls IConnectionPoint::Advise on the connection point to establish a connection between its sink interface and the object's connection point. After this call, the object's connection point holds a pointer to the outgoing interface on the sink. 
    The code inside IConnectionPoint::Advise calls QueryInterface on the interface pointer that is passed in, asking for the specific interface identifier to which it connects. 
    The object calls methods on the sink's interface as needed, using the pointer held by its connection point. 
    The client calls IConnectionPoint::Unadvise to terminate the connection. Then the client calls IConnectionPoint::Release to free its hold on the connection point and, therefore, the main connectable object also. The client must also call IConnectionPointContainer::Release to free its hold on the main connectable object.
      

  4.   

    我的vc7最近有点问题,用atl时老是退出,作例子就等等把
      

  5.   

    Create a new ATL-COM AppWizard Project and name it ConnectionCOM. 
    Right-click on the class view and create a new ATL object. 
    Name it Add (interface IAdd).Before you click the OK button, be sure to check the Support Connection point checkbox.Click OK.Note the classes generated in the class view. You will find one IAdd and _IAddEvents. The latter is just a proxy class; it is to be implemented on the client side. It came because we ticked the Connection_Points check box.Add a method 'Add(int a,int b) ' to IAdd interface and a method 'ExecutionOver(int Result)' to the _IAddEventsInterface. The class view will be as shown below.But because we have selected a dual interface and we do not need all that hassle, let's take out the support for IDispatch by editing the IDL file. Below is the original file.//===========================================================
    // ConnectionCOM.idl : IDL source for ConnectionCOM.dll
    //
      :
      :library CONNECTIONCOMLib
    {
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");[
      uuid(AFE854B0-246F-4B66-B26F-A1060225C71C),
      helpstring("_IAddEvents Interface")
    ]
    // Old block - take this out
    // dispinterface _IAddEvents
    // {
    // properties:
    // methods:
    // [id(1), helpstring("method ExecutionOver")]
    // HRESULT ExecutionOver(intResult);
    // };
    //To this one -put this in
    interface _IAddEvents : IUnknown
      {
      [id(1), helpstring("method ExecutionOver")] HRESULT
              ExecutionOver(intResult);
      };
      [
        uuid(630B3CD3-DDB1-43CE-AD2F-4F57DC54D5D0),
        helpstring("Add Class")
      ]
      coclass Add
      {
      [default] interface IAdd;
      //[default, source] dispinterface _IAddEvents; take this line
      //out and put the line below in
      [default, source] interface _IAddEvents ;
      };
    };//================================================================Whew! The client side is almost finished now. Now, do a build because we need the type library to do a neat thing with ATL. Now, right-click on the CoClass and click Implement Connection Point.Check _IAddEvents in the ensuing dialog box.A CProxy_IAddEvets class is generated with the Fire_ExecutionOver(int result) method. This will take care of how the COM object will call the client interface (and takes care of multiple clients calling the same COM DLL and other such issues). Now, let's implement our old IAdd Interface Add method.//=====================================================STDMETHODIMP CAdd::Add(int a, int b)
    {
    // TODO: Add your implementation code hereSleep(2000);   // to simulate a long process//OK, process over now; let's notify the clientFire_ExecutionOver(a+b);return S_OK;
    }
    //======================================================Do a build and the COM is ready. Make sure that the COM is registered.
      

  6.   

    事件我是定义成功了,在vb中可以正确的使用,可是在vc中无法使用。1.在另一个ATL COM中不好使
    2.在对话框应用程序中不好使请各位帮帮忙。
      

  7.   

    http://expert.csdn.net/Expert/topic/1701/1701466.xml?temp=.1661951