Einzelnen Beitrag anzeigen

Nic2012

Registriert seit: 2. Mai 2012
62 Beiträge
 
#9

AW: C# Delegates in Delphi konsumieren

  Alt 18. Feb 2013, 14:49
Basierend nach diesem Vorschlag http://www.systemwidgets.com/Blog/ta...M-clients.aspx klappt es:

Der C# Com-Server:
Code:
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace ComDog
{
    [ComVisible(false)]
    public delegate void DogEventHandler();

    [ComVisible(true)]
    [Guid("2406DD50-A3CE-43A6-9F20-112B621CB784")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IDogEvents
    {
        [DispId(1)]
        void Bark();
        [DispId(2)]
        void Howl();
        [DispId(3)]
        void Eat();
    }

    [ComVisible(true)]
    [Guid("8C6DAD17-0612-4166-AD35-3A55DDEAF62E")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComSourceInterfaces(typeof(IDogEvents))]
    public class Dog : MarshalByRefObject
    {
        [ComVisible(false)]
        private Timer _timer;

        public event DogEventHandler Bark;
        public event DogEventHandler Howl;
        public event DogEventHandler Eat;

        public Dog()
        {
            _timer = new Timer(new TimerCallback(timer_tick));
            _timer.Change(5000, 2000);
        }

        private void timer_tick(object state)
        {
            MakeDogBark();
        }

        public void MakeDogBark()
        {
            if (Bark != null)
            {
                Bark();
            }
        }

        public void MakeDogHowl()
        {
            if (Howl != null)
            {
                Howl();
            }
        }

        public void MakeDogEat()
        {
            if (Eat != null)
            {
                Eat();
            }
        }
    }
}
Und aus einer Delphi7 Client Anwendung:
Code:
...
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComDog_TLB, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
  protected
    procedure Barked(Sender: TObject);
    procedure Howled(Sender: TObject);
    procedure Ate(Sender: TObject);
  public
  end;

var
  Form1: TForm1;
  TestDog: TDog;
  iDog: _Dog;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  iDog := CoDog.Create;

  TestDog := TDog.Create(self);
  TestDog.ConnectTo(iDog);

  TestDog.OnBark := Barked;
  TestDog.OnHowl := Howled;
  TestDog.OnEat := Ate;
end;

procedure TForm1.Barked(Sender: TObject);
begin
  Memo1.Lines.Add('Event from COM: Dog Barked');
end;

procedure TForm1.Howled(Sender: TObject);
begin
  Memo1.Lines.Add('Event from COM: Dog Howled');
end;

procedure TForm1.Ate(Sender: TObject);
begin
  Memo1.Lines.Add('Event from COM: Dog ate something');
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
  if (TestDog <> nil) then
    TestDog.Free;
end;
...
Erhalte ich 5sec nachdem der COM-Server instanziert wurde alle 2 sec das Event "Bark" angezeigt im Memo.

Sieht gut aus oder ?
Gruß,
Nic

Geändert von Nic2012 (18. Feb 2013 um 15:23 Uhr)
  Mit Zitat antworten Zitat