分享
 
 
 

COM设计与实现范例

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

COM设计与实现范例

原创作者: 小王志东,王健。

创作日期:2002年4月5日星期五

组件对象模型是一种跨平台技术,是现代面向对象分析与设计的技术实现手段之一,就现实系统应用来看,COM技术有着很强的实用价值。

OOA -> OOD -> OOP 用纯面向对象技术分析设计实现系统

现就给出一个简单的范例来展示其魅力。系统描述:本系统是一个多用户登录管理系统。要求用户通过输入用户编号和密码登录系统,如登录成功则显示成功消息,否则显示错误消息。系统分析:类的抽取,根据关键词抽取出用户类,根据用户类衍生出用户管理员类。

COM接口定义

unit UnionComs_TLB;

// ************************************************************************ //

// WARNING

// -------

// The types declared in this file were generated from data read from a

// Type Library. If this type library is explicitly or indirectly (via

// another type library referring to this type library) re-imported, or the

// 'Refresh' command of the Type Library Editor activated while editing the

// Type Library, the contents of this file will be regenerated and all

// manual modifications will be lost.

// ************************************************************************ //

// PASTLWTR : $Revision: 1.130 $

// File generated on 2002-4-5 11:34:29 from Type Library described below.

// ************************************************************************ //

// Type Lib: D:\stevenbob\exmines\UnionComGroup\Coms\UnionComs.tlb (1)

// LIBID: {A5265797-1AE4-4465-B5BA-67CB3E5F615E}

// LCID: 0

// Helpfile:

// DepndLst:

// (1) v2.0 stdole, (C:\WINNT\System32\stdole2.tlb)

// (2) v4.0 StdVCL, (C:\WINNT\System32\STDVCL40.DLL)

// ************************************************************************ //

{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.

{$WARN SYMBOL_PLATFORM OFF}

{$WRITEABLECONST ON}

interface

uses ActiveX, Classes, Graphics, StdVCL, Variants, Windows;

// *********************************************************************//

// GUIDS declared in the TypeLibrary. Following prefixes are used:

// Type Libraries : LIBID_xxxx

// CoClasses : CLASS_xxxx

// DISPInterfaces : DIID_xxxx

// Non-DISP interfaces: IID_xxxx

// *********************************************************************//

const

// TypeLibrary Major and minor versions

UnionComsMajorVersion = 1;

UnionComsMinorVersion = 0;

LIBID_UnionComs: TGUID = '{A5265797-1AE4-4465-B5BA-67CB3E5F615E}';

IID_IUserGroup: TGUID = '{D168EB08-8C53-41F7-9B4C-B1EA0120C1DA}';

CLASS_UserGroup: TGUID = '{91648D17-4920-48A1-BC8E-94E6A70AA31B}';

IID_IUser: TGUID = '{8CC77A6A-0D6B-4646-8DD2-D3E3853C46B7}';

CLASS_User: TGUID = '{EA6025AF-6F8C-4201-8DF4-BE5DB86D061A}';

type

// *********************************************************************//

// Forward declaration of types defined in TypeLibrary

// *********************************************************************//

IUserGroup = interface;

IUser = interface;

IUserDisp = dispinterface;

// *********************************************************************//

// Declaration of CoClasses defined in Type Library

// (NOTE: Here we map each CoClass to its Default Interface)

// *********************************************************************//

UserGroup = IUserGroup;

User = IUser;

// *********************************************************************//

// Interface: IUserGroup

// Flags: (4096) Dispatchable

// GUID: {D168EB08-8C53-41F7-9B4C-B1EA0120C1DA}

// *********************************************************************//

IUserGroup = interface(IDispatch)

['{D168EB08-8C53-41F7-9B4C-B1EA0120C1DA}']

function CreateUser(ID: Integer; const UserName: WideString; const UserPwd: WideString;

out ppUser: User): HResult; stdcall;

function GetCount(out plCount: Integer): HResult; stdcall;

end;

// *********************************************************************//

// Interface: IUser

// Flags: (4416) Dual OleAutomation Dispatchable

// GUID: {8CC77A6A-0D6B-4646-8DD2-D3E3853C46B7}

// *********************************************************************//

IUser = interface(IDispatch)

['{8CC77A6A-0D6B-4646-8DD2-D3E3853C46B7}']

function Get_Name: WideString; safecall;

procedure Set_Name(const Value: WideString); safecall;

function Get_ID: Integer; safecall;

procedure Set_ID(Value: Integer); safecall;

function Get_Password: WideString; safecall;

function ChangePassword(const OldPwd: WideString; const NewPwd: WideString;

const CfmPwd: WideString): Integer; safecall;

property Name: WideString read Get_Name write Set_Name;

property ID: Integer read Get_ID write Set_ID;

property Password: WideString read Get_Password;

end;

// *********************************************************************//

// DispIntf: IUserDisp

// Flags: (4416) Dual OleAutomation Dispatchable

// GUID: {8CC77A6A-0D6B-4646-8DD2-D3E3853C46B7}

// *********************************************************************//

IUserDisp = dispinterface

['{8CC77A6A-0D6B-4646-8DD2-D3E3853C46B7}']

property Name: WideString dispid 2;

property ID: Integer dispid 3;

property Password: WideString readonly dispid 4;

function ChangePassword(const OldPwd: WideString; const NewPwd: WideString;

const CfmPwd: WideString): Integer; dispid 5;

end;

// *********************************************************************//

// The Class CoUserGroup provides a Create and CreateRemote method to

// create instances of the default interface IUserGroup exposed by

// the CoClass UserGroup. The functions are intended to be used by

// clients wishing to automate the CoClass objects exposed by the

// server of this typelibrary.

// *********************************************************************//

CoUserGroup = class

class function Create: IUserGroup;

class function CreateRemote(const MachineName: string): IUserGroup;

end;

// *********************************************************************//

// The Class CoUser provides a Create and CreateRemote method to

// create instances of the default interface IUser exposed by

// the CoClass User. The functions are intended to be used by

// clients wishing to automate the CoClass objects exposed by the

// server of this typelibrary.

// *********************************************************************//

CoUser = class

class function Create: IUser;

class function CreateRemote(const MachineName: string): IUser;

end;

implementation

uses ComObj;

class function CoUserGroup.Create: IUserGroup;

begin

Result := CreateComObject(CLASS_UserGroup) as IUserGroup;

end;

class function CoUserGroup.CreateRemote(const MachineName: string): IUserGroup;

begin

Result := CreateRemoteComObject(MachineName, CLASS_UserGroup) as IUserGroup;

end;

class function CoUser.Create: IUser;

begin

Result := CreateComObject(CLASS_User) as IUser;

end;

class function CoUser.CreateRemote(const MachineName: string): IUser;

begin

Result := CreateRemoteComObject(MachineName, CLASS_User) as IUser;

end;

end.

COM接口的实现

unit uUnionComsImpl;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses

// ActiveX, Mtsobj, Mtx, ComObj, UnionComs_TLB, StdVcl;

Windows, ActiveX, Classes, ComObj, UnionComs_TLB, StdVcl;

type

TUserRec = record

Intf: IUser;

bUsed: Boolean;

end;

TUserGroup = class(TAutoObject, IUserGroup)

private

FUserArray: array of TUserRec;

protected

function CreateUser(ID: Integer; const UserName, UserPwd: WideString;

out ppUser: User): HResult; stdcall;

function GetCount(out plCount: Integer): HResult; stdcall;

public

destructor Destroy; override;

procedure Initialize; override;

end;

implementation

uses ComServ;

function TUserGroup.CreateUser(ID: Integer; const UserName,

UserPwd: WideString; out ppUser: User): HResult;

var

AIntf: IUser;

begin

SetLength(FUserArray, Length(FUserArray) + 1);

AIntf := CoUser.Create as IUser;

FUserArray[Length(FUserArray) - 1].Intf := AIntf;

FUserArray[Length(FUserArray) - 1].bUsed := True;

AIntf.Name := UserName;

AIntf.ID := ID;

ppUser := FUserArray[Length(FUserArray) - 1].Intf;

end;

destructor TUserGroup.Destroy;

var

i: Integer;

begin

for i := Low(FUserArray) to High(FUserArray) do

begin

FUserArray[i].Intf := nil;

FUserArray[i].bUsed := False;

end;

SetLength(FUserArray, 0);

inherited Destroy;

end;

function TUserGroup.GetCount(out plCount: Integer): HResult;

begin

plCount := Length(FUserArray);

end;

procedure TUserGroup.Initialize;

begin

inherited;

end;

initialization

TAutoObjectFactory.Create(ComServer, TUserGroup, Class_UserGroup,

ciMultiInstance, tmApartment);

end.

客户端程序的实现

Create User Button 的代码实现

procedure TForm1.Button1Click(Sender: TObject);

var

AUserGroup: IUserGroup;

AUser: IUser;

begin

AUser := nil;

AUserGroup := CoUserGroup.Create;

AUserGroup.CreateUser(10, 'WZD', '912', AUser);

ShowMessage(AUser.Name);

Auser.ChangePassword('', '912', '912');

ShowMessage(AUser.Password);

end;

实例下载: http://www.megspace.com/computers/coreware/

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有