2012-06-06 13 views
5

Udało mi się utworzyć obiekt C# COM ze zdarzeniami. Proszę znaleźć kod poniżejObsługa zdarzeń C# COM w C++

[Guid("1212674-38748-45434")] 
    public interface ICalculator 
    { 
     int Add(int Num1, int Num2); 
    } 

    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
    [Guid("3453674234-84444-84784")] 
    public interface ICalculatorEvents 
    { 
     [DispId(1)] 
     void Completed(int Result); 
    } 

    [ClassInterface(ClassInterfaceType.None)] 
    [ComSourceInterfaces(typeof(ICalculatorEvents))] 
    [Guid("87457845-945u48-4954")] 
    public class Calculator : ICalculator 
    { 
     public delegate void CompletedDelegate(int result); 
     public event CompletedDelegate Completed; 
     public Add(int Num1, int Num2) 
     { 
      int Result = Num1 + Num2; 
      if(Completed != null) 
       Completed(Result); 
     } 
    } 

mam importowane tego obiektu COM w aplikacji konsoli C++ i jest w stanie wywołać 'Add() metody. Nie wiem, jak obsługiwać zdarzenie "Completed" w mojej aplikacji C++. Czy możesz doradzić w tej sprawie? Szukam wyświetlić wartość wyniku w konsoli, gdy wystąpi to zdarzenie.

Proszę znaleźć kod aplikacji C++ poniżej. Wydarzenie "Completed" nigdy nie zostanie tu wykorzystane. To przechodzi w nieskończoną pętlę.

#import "Calculator.tlb" 
    using namespace Calculator; 
    int Flag = 0; 
    class HandleEvent : public ICalculatorEvent 
    { 
     public: 
      HandleEvent(void); 
      ~HandleEvent(void); 
      HRESULT __stdcall QueryInterface(const IID &, void **); 
      ULONG __stdcall AddRef(void) { return 1; } 
      ULONG __stdcall Release(void) { return 1; } 
      HRESULT __stdcall Completed(int Result); 
    }; 

    HandleEvent::HandleEvent(void) 
    { 
    } 

    HRESULT HandleEvent::Completed(int Result) 
    { 
     printf("Addition Completed, Result: %d", Result); 
     Flag = 1; 
    } 

    HRESULT HandleEvent::QueryInterface(const IID & iid,void ** pp) 
    { 
     if (iid == __uuidof(ICalculatorEvent) || iid == __uuidof(IUnknown)) 
     { 
      *pp = this; 
      AddRef(); 
      return S_OK; 
     } 
     return E_NOINTERFACE; 
    } 

    int _tmain(int argc, _TCHAR* argv[]) 
    { 
     CoInitialize(NULL); 
     Flag = 0; 
     ICalculatorPtr pCalc(__uuidof(Calculator)); 
     pCalc->Add(5, 6); 

     do 
     { 
     }while(Flag == 0); 

     CoUninitialize(); 
     return 0; 
    } 

Z góry dziękuję.

+0

Chyba Zakończony Obiekt zdarzenia nie będzie sprawdzony, ponieważ jest zawsze puste. Która klasa jest zaimplementowana w interfejsie ICalculatorEvents? –

+0

Program ICalculatorEvents jest zaimplementowany w aplikacji C++. Proszę znaleźć kod C++ poniżej, – GeekCandy

+0

Gdzie jest kod C++? –

Odpowiedz

0

Jeśli chcesz korzystać z delegatów, nie musisz zadeklarować interfejsu. Zmień _tmain funkcji() tak:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    CoInitialize(NULL); 
    Flag = 0; 

    EventHandler evh ; 
    ICalculatorPtr pCalc(__uuidof(Calculator)); 
    pCalc->Completed = &evh.Completed() ; 
    pCalc->Add(5, 6); 

    do 
    { 
    }while(Flag == 0); 

    CoUninitialize(); 
    return 0; 
} 

Jeśli chcesz użyć interfejs spróbować.

[ClassInterface(ClassInterfaceType.None)] 
[ComSourceInterfaces(typeof(ICalculatorEvents))] 
[Guid("87457845-945u48-4954")] 
public class Calculator : ICalculator 
{ 
    public ICalculatorEvents callbackObject ; 

    public Add(int Num1, int Num2) 
    { 
     int Result = Num1 + Num2; 
     if(callbackObject != null) 
      callbackObject.Completed(Result); 
    } 
} 

i zmień metodę _tmain() na tę.

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    CoInitialize(NULL); 
    Flag = 0; 

    EventHandler evh ; 
    ICalculatorPtr pCalc(__uuidof(Calculator)); 
    pCalc->callbackObject = &evh ; 
    pCalc->Add(5, 6); 

    do 
    { 
    }while(Flag == 0); 

    CoUninitialize(); 
    return 0; 
} 
0

I okazało się, że inicjowanie COM w C++ klienta powinny być wykonywane przy użyciu

CoInitializeEx(NULL, COINIT_MULTITHREADED); 

dla asynchronicznego przetwarzania zdarzeń z C# (.NET) COM serwera, inaczej C++ klient otrzymuje zdarzenia dopiero po CoUninitialize() wezwanie .

obsługi zdarzeń klasy:

class EventWrapper : public IDispEventSimpleImpl<1, EventWrapper, &DIID_RumCardCOMEvents > 
    { 
    public: 
     // now you need to declare a sink map - a map of methods handling the events 
     BEGIN_SINK_MAP(EventWrapper) 
      SINK_ENTRY_INFO(1, DIID_RumCardCOMEvents, 0x1, isCardInserted, &cardInserted) 
      SINK_ENTRY_INFO(1, DIID_RumCardCOMEvents, 0x2, isCardRemoved, &cardRemoved) 
      // event interface id (can be more than 1)---+  |  |     | 
      // must match dispid of your event -----------------+  |     | 
      // method which handles the event ------------------------+     | 
      // type information for event, see below --------------------------------------+ 
     END_SINK_MAP() 

    // declare the type info object. You will need one for each method with different signature. 
     // it will be defined in the .cpp file, as it is a static member 
     static _ATL_FUNC_INFO cardInserted; // 'placeholder' object to carry event information (see below) 
     static _ATL_FUNC_INFO cardRemoved; // 'placeholder' object to carry event information (see below) 

     // method which handles the event 
     STDMETHOD (isCardInserted)(unsigned char type) 
     { 
      // usually it is defined it in the .cpp file 
      cout << "isCardInserted: " << (int)type << endl; 
      return 0; 
     } 

     STDMETHOD (isCardRemoved)() 
     { 
      // usually it is defined it in the .cpp file 
      cout << "isCardRemoved" << endl; 
      return 0; 
     } 
    }; 

Główne:

int main() 
    { 
     CoInitializeEx(NULL, COINIT_MULTITHREADED); 
     try 
     { 
      EventWrapper ev; 
      ev.DispEventAdvise(/*COM interface*/); 
      // receiving events 
      ev.DispEventUnadvise(/*COM interface*/); 
     } 
     catch (_com_error& e) 
     { 
      cout << "Exception: " << e.ErrorMessage() << endl; 
     } 

     CoUninitialize(); 
     return 0; 
    }