unit SrvUnit2;

interface

uses
ComObj, ActiveX, AxCtrls, Classes, SrvEvent_TLB, StdVcl, Srvunit1; type
TSimpleEventServer = class(TAutoObject, IConnectionPointContainer, ISimpleEventServer)
private
{ Private declarations }
FConnectionPoints: TConnectionPoints;
FConnectionPoint: TConnectionPoint;
FEvents: ISimpleEventServerEvents;
{ note: FEvents maintains a *single* event sink. For access to more
than one event sink, use FConnectionPoint.SinkList, and iterate
through the list of sinks. }
public
procedure Initialize; override;
protected
{ Protected declarations }
property ConnectionPoints: TConnectionPoints read FConnectionPoints
implements IConnectionPointContainer;
procedure EventSinkChanged(const EventSink: IUnknown); override;
procedure CallServer; safecall;
end; implementation uses ComServ; procedure TSimpleEventServer.EventSinkChanged(const EventSink: IUnknown);
begin
FEvents := EventSink as ISimpleEventServerEvents;
end; procedure TSimpleEventServer.Initialize;
begin
inherited Initialize;
FConnectionPoints := TConnectionPoints.Create(Self);
if AutoFactory.EventTypeInfo <> nil then
FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
AutoFactory.EventIID, ckSingle, EventConnect)
else FConnectionPoint := nil;
end; procedure TSimpleEventServer.CallServer;
begin
Form1.Memo1.Lines.Add('I have been called by a client');
if FEvents <> nil then
begin
FEvents.EventFired;
Form1.Memo1.Lines.Add('I am firing an Event');
end;
end; initialization
TAutoObjectFactory.Create(ComServer, TSimpleEventServer, Class_SimpleEventServer,
ciMultiInstance, tmApartment);
end.
unit ClientUnit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,SrvEvent_TLB,ActiveX, ComObj, StdCtrls; type
TForm2 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FServer: ISimpleEventServer;
FEventSink: IUnknown; FConnectionToken: integer;
public
{ Public declarations }
procedure OnEventFired;
end; TEventSink = class(TInterfacedObject, IUnknown,IDispatch)
private
FController: TForm2;
{IUknown methods}
function QueryInterface(const IID: TGUID; out Obj):HResult;stdcall;
{Idispatch}
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
public
constructor Create(Controller: TForm2);
end; var
Form2: TForm2; implementation {$R *.dfm} procedure TForm2.OnEventFired;
begin
Memo1.Lines.Add('I have recieved an event');
end; constructor TEventSink.Create(Controller: TForm2);
begin
inherited Create;
FController := Controller;
end; function TEventSink.Invoke(DispID: integer; const IID: TGUID; LocaleID: integer; Flags: Word; var Params; VarResult,ExcepInfo,ArgErr:Pointer): HResult;
begin
Result := S_OK;
case DispID of
: FController.OnEventFired;
end;
end; function TEventSink.QueryInterface(const IID: TGUID; out Obj):HResult;stdcall;
begin
if GetInterFace(IID,Obj) then
Result := S_OK
else if IsEqualIID(IID,ISimpleEventServerEvents) then
Result := QueryInterface(IDispatch,Obj)
else
Result := E_NOINTERFACE;
end; function TEventSink.GetTypeInfoCount(out Count: Integer): HResult;
begin
Result := S_OK;
end;
function TEventSink.GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult;
begin
Result := S_OK;
end;
function TEventSink.GetIDsOfNames(const IID: TGUID; Names: Pointer;
NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
Result := S_OK;
end; procedure TForm2.FormCreate(Sender: TObject);
begin
FServer := CoSimpleEventServer.Create;
FEventSink := TEventSink.Create(form2);
InterfaceConnect(FServer, ISimpleEventServerEvents,FEventSink,FConnectionToken);
end; procedure TForm2.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add('I am calling the Server');
FServer.CallServer;
end; procedure TForm2.FormDestroy(Sender: TObject);
begin
InterfaceDisconnect(FServer,ISimpleEventServer,FConnectionToken);
FServer := nil;
FEventSink := nil;
end; end.

Delphi中COM自动化对象中使用事件的更多相关文章

  1. spring data mongodb中,如果对象中的属性不想加入到数据库字段中

    spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...

  2. silverlight中鼠标放在对象的提示事件

    1.xaml 中实现 <Rectangle x:Name="toolTip" Grid.Column="0" Grid.Row="1" ...

  3. Python 中Semaphore 信号量对象、Event事件、Condition

    Semaphore 信号量对象 信号量是一个更高级的锁机制.信号量内部有一个计数器而不像锁对象内部有锁标识,而且只有当占用信号量的线程数超过信号量时线程才阻塞.这允许了多个线程可以同时访问相同的代码区 ...

  4. 浅谈String中的==和对象中引用对象类型的==

    @Test public void test02() { StringBuffer sb = new StringBuffer(); sb.append('a'); sb.append(11); Sy ...

  5. js中的数组对象中的方法解析

    concat()方法:  合并两个数组,返回新对象的结果: join()方法 :  把数组内的所有元素加入到一个字符串中,传入的分隔符就是指定的分隔符 pop()方法: 删除数组并返回数组的最后一个元 ...

  6. c# 如何中List<object>中去掉object对象中的重复列数据?

    //去掉重复 var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.F ...

  7. python3+request接口自动化框架中自动发送邮件

    在上一篇中的自动化框架中没有放上自动发送测试结果到邮箱的功能,在这篇文章中在补一下,哈哈 1.上一篇的代码就不在一一介绍了,本篇只介绍发送邮件的功能代码 2.在public common 文件夹中创建 ...

  8. iOS探索:对NSArray中自定义的对象进行排序

    http://mobile.51cto.com/hot-434804.htm 我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天 ...

  9. (转载)OC学习篇之---Foundation框架中的NSObject对象

    前一篇文章讲到了OC中的代理模式,而且前几篇文章就介绍了OC中的类相关知识,从这篇文章开始我们开始介绍Foundation框架. OC中的Foundation框架是系统提供了,他就相当于是系统的一套a ...

随机推荐

  1. 怎么在Eclipse上运行静态网页

    1. 前言 习惯用Eclipse开发动态网站,现在有一个静态网页(只有Html,Js,CSS代码,无后台Java代码)想跑一下,自己通过网上查询然后自己也研究捣鼓了一下,发现有三种方式可以发布静态网页 ...

  2. winform(记事本--剪切复制等和打开)

  3. 判断iframe加载完成、用于当ifame加载完成时执行一些操作

    window.frames["iframec"].addEventListener( "load", function(){ window.frames[&qu ...

  4. VMware虚拟机 Ubuntu 16.04 安装

    第一步:VMware虚拟机 Ubuntu 16.04 安装 第二步: 解决窗口全屏问题 linux下给root用户设置密码 修改root用户的密码 $ sudo passwd root 密码会要求重复 ...

  5. Eclipse-debug时提示absent line number information的解决办法

    unable to install breakpoint in ...(file name) due to miss line number attributes. midify compliter ...

  6. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  7. [CQOI2017]老C的任务

    题解: 裸的主席树吧 要想避免带修主席树的话先排序一下吧

  8. HTML&CSS学习笔记

    <table> <thead> <tr>            // table row <th></th> // table head & ...

  9. 066 linux下时间的修改

    1.关于时间的修改,在linux还是很重要的,在这里只是介绍一个简单的常用的命令,并且时间不会写入到系统. 2.命令 3.如果想把时间写进系统 修改完成之后,输入clock -w 时间将会被写进CMO ...

  10. IdentityServer4-EF动态配置Client和对Claims授权(二)

    本节介绍Client的ClientCredentials客户端模式,先看下画的草图: 一.在Server上添加动态新增Client的API 接口. 为了方便测试,在Server服务端中先添加swagg ...