哪里有详细介绍三层结构中在服务器端定义接口及调用的文章?(100分)

  • 主题发起人 主题发起人 Ai2
  • 开始时间 开始时间
A

Ai2

Unregistered / Unconfirmed
GUEST, unregistred user!
哪里有详细介绍三层结构中在服务器端定义接口及调用的文章?
各位高手帮忙啦!!!!!
先送100分,有好文章再加分!
 
李维的第一本书上有
 
服务器端:
unit PDynamicSQLDemo_TLB;
{$TYPEDADDRESS OFF}
interface
uses Windows, ActiveX, Classes, Graphics, OleServer, OleCtrls, StdVCL,
MIDAS;
// *********************************************************************//
// 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
PDynamicSQLDemoMajorVersion = 1;
PDynamicSQLDemoMinorVersion = 0;
LIBID_PDynamicSQLDemo: TGUID = '{8C16E961-5515-11D3-8FB9-0080C88AAA0D}';
IID_IDynamicSQLDemoServer: TGUID = '{8C16E962-5515-11D3-8FB9-0080C88AAA0D}';
CLASS_DynamicSQLDemoServer: TGUID = '{8C16E964-5515-11D3-8FB9-0080C88AAA0D}';
type
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
IDynamicSQLDemoServer = interface;
IDynamicSQLDemoServerDisp = dispinterface;
// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
DynamicSQLDemoServer = IDynamicSQLDemoServer;

// *********************************************************************//
// Interface: IDynamicSQLDemoServer
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {8C16E962-5515-11D3-8FB9-0080C88AAA0D}
// *********************************************************************//
IDynamicSQLDemoServer = interface(IAppServer)
['{8C16E962-5515-11D3-8FB9-0080C88AAA0D}']
procedure GetAllTables(var vTables: OleVariant);
safecall;
end;

// *********************************************************************//
// DispIntf: IDynamicSQLDemoServerDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {8C16E962-5515-11D3-8FB9-0080C88AAA0D}
// *********************************************************************//
IDynamicSQLDemoServerDisp = dispinterface
['{8C16E962-5515-11D3-8FB9-0080C88AAA0D}']
procedure GetAllTables(var vTables: OleVariant);
dispid 1;
function AS_ApplyUpdates(const ProviderName: WideString;
Delta: OleVariant;
MaxErrors: Integer;
out ErrorCount: Integer;
var OwnerData: OleVariant): OleVariant;
dispid 20000000;
function AS_GetRecords(const ProviderName: WideString;
Count: Integer;
out RecsOut: Integer;
Options: Integer;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant): OleVariant;
dispid 20000001;
function AS_DataRequest(const ProviderName: WideString;
Data: OleVariant): OleVariant;
dispid 20000002;
function AS_GetProviderNames: OleVariant;
dispid 20000003;
function AS_GetParams(const ProviderName: WideString;
var OwnerData: OleVariant): OleVariant;
dispid 20000004;
function AS_RowRequest(const ProviderName: WideString;
Row: OleVariant;
RequestType: Integer;
var OwnerData: OleVariant): OleVariant;
dispid 20000005;
procedure AS_Execute(const ProviderName: WideString;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant);
dispid 20000006;
end;

// *********************************************************************//
// The Class CoDynamicSQLDemoServer provides a Create and CreateRemote method to
// create instances of the default interface IDynamicSQLDemoServer exposed by
// the CoClass DynamicSQLDemoServer. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoDynamicSQLDemoServer = class
class function Create: IDynamicSQLDemoServer;
class function CreateRemote(const MachineName: string): IDynamicSQLDemoServer;
end;

// *********************************************************************//
// OLE Server Proxy class declaration
// Server Object : TDynamicSQLDemoServer
// Help String : DynamicSQLDemoServer Object
// Default Interface: IDynamicSQLDemoServer
// Def. Intf. DISP? : No
// Event Interface:
// TypeFlags : (2) CanCreate
// *********************************************************************//
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
TDynamicSQLDemoServerProperties= class;
{$ENDIF}
TDynamicSQLDemoServer = class(TOleServer)
private
FIntf: IDynamicSQLDemoServer;
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps: TDynamicSQLDemoServerProperties;
function GetServerProperties: TDynamicSQLDemoServerProperties;
{$ENDIF}
function GetDefaultInterface: IDynamicSQLDemoServer;
protected
procedure InitServerData;
override;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure Connect;
override;
procedure ConnectTo(svrIntf: IDynamicSQLDemoServer);
procedure Disconnect;
override;
function AS_ApplyUpdates(const ProviderName: WideString;
Delta: OleVariant;
MaxErrors: Integer;
out ErrorCount: Integer;
var OwnerData: OleVariant): OleVariant;
function AS_GetRecords(const ProviderName: WideString;
Count: Integer;
out RecsOut: Integer;
Options: Integer;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant): OleVariant;
function AS_DataRequest(const ProviderName: WideString;
Data: OleVariant): OleVariant;
function AS_GetProviderNames: OleVariant;
function AS_GetParams(const ProviderName: WideString;
var OwnerData: OleVariant): OleVariant;
function AS_RowRequest(const ProviderName: WideString;
Row: OleVariant;
RequestType: Integer;
var OwnerData: OleVariant): OleVariant;
procedure AS_Execute(const ProviderName: WideString;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant);
procedure GetAllTables(var vTables: OleVariant);
property DefaultInterface: IDynamicSQLDemoServer read GetDefaultInterface;
published
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
property Server: TDynamicSQLDemoServerProperties read GetServerProperties;
{$ENDIF}
end;

{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
// *********************************************************************//
// OLE Server Properties Proxy Class
// Server Object : TDynamicSQLDemoServer
// (This object is used by the IDE's Property Inspector to allow editing
// of the properties of this server)
// *********************************************************************//
TDynamicSQLDemoServerProperties = class(TPersistent)
private
FServer: TDynamicSQLDemoServer;
function GetDefaultInterface: IDynamicSQLDemoServer;
constructor Create(AServer: TDynamicSQLDemoServer);
protected
public
property DefaultInterface: IDynamicSQLDemoServer read GetDefaultInterface;
published
end;
{$ENDIF}

procedure Register;
implementation
uses ComObj;
class function CoDynamicSQLDemoServer.Create: IDynamicSQLDemoServer;
begin
Result := CreateComObject(CLASS_DynamicSQLDemoServer) as IDynamicSQLDemoServer;
end;

class function CoDynamicSQLDemoServer.CreateRemote(const MachineName: string): IDynamicSQLDemoServer;
begin
Result := CreateRemoteComObject(MachineName, CLASS_DynamicSQLDemoServer) as IDynamicSQLDemoServer;
end;

procedure TDynamicSQLDemoServer.InitServerData;
const
CServerData: TServerData = (
ClassID: '{8C16E964-5515-11D3-8FB9-0080C88AAA0D}';
IntfIID: '{8C16E962-5515-11D3-8FB9-0080C88AAA0D}';
EventIID: '';
LicenseKey: nil;
Version: 500);
begin
ServerData := @CServerData;
end;

procedure TDynamicSQLDemoServer.Connect;
var
punk: IUnknown;
begin
if FIntf = nil then
begin
punk := GetServer;
Fintf:= punk as IDynamicSQLDemoServer;
end;
end;

procedure TDynamicSQLDemoServer.ConnectTo(svrIntf: IDynamicSQLDemoServer);
begin
Disconnect;
FIntf := svrIntf;
end;

procedure TDynamicSQLDemoServer.DisConnect;
begin
if Fintf <> nil then
begin
FIntf := nil;
end;
end;

function TDynamicSQLDemoServer.GetDefaultInterface: IDynamicSQLDemoServer;
begin
if FIntf = nil then
Connect;
Assert(FIntf <> nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call ''Connect'' or ''ConnectTo'' before this operation');
Result := FIntf;
end;

constructor TDynamicSQLDemoServer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps := TDynamicSQLDemoServerProperties.Create(Self);
{$ENDIF}
end;

destructor TDynamicSQLDemoServer.Destroy;
begin
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps.Free;
{$ENDIF}
inherited Destroy;
end;

{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
function TDynamicSQLDemoServer.GetServerProperties: TDynamicSQLDemoServerProperties;
begin
Result := FProps;
end;
{$ENDIF}
function TDynamicSQLDemoServer.AS_ApplyUpdates(const ProviderName: WideString;
Delta: OleVariant;
MaxErrors: Integer;
out ErrorCount: Integer;
var OwnerData: OleVariant): OleVariant;
begin
Result := DefaultInterface.AS_ApplyUpdates(ProviderName, Delta, MaxErrors, ErrorCount, OwnerData);
end;

function TDynamicSQLDemoServer.AS_GetRecords(const ProviderName: WideString;
Count: Integer;
out RecsOut: Integer;
Options: Integer;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant): OleVariant;
begin
Result := DefaultInterface.AS_GetRecords(ProviderName, Count, RecsOut, Options, CommandText,
Params, OwnerData);
end;

function TDynamicSQLDemoServer.AS_DataRequest(const ProviderName: WideString;
Data: OleVariant): OleVariant;
begin
Result := DefaultInterface.AS_DataRequest(ProviderName, Data);
end;

function TDynamicSQLDemoServer.AS_GetProviderNames: OleVariant;
begin
Result := DefaultInterface.AS_GetProviderNames;
end;

function TDynamicSQLDemoServer.AS_GetParams(const ProviderName: WideString;
var OwnerData: OleVariant): OleVariant;
begin
Result := DefaultInterface.AS_GetParams(ProviderName, OwnerData);
end;

function TDynamicSQLDemoServer.AS_RowRequest(const ProviderName: WideString;
Row: OleVariant;
RequestType: Integer;
var OwnerData: OleVariant): OleVariant;
begin
Result := DefaultInterface.AS_RowRequest(ProviderName, Row, RequestType, OwnerData);
end;

procedure TDynamicSQLDemoServer.AS_Execute(const ProviderName: WideString;
const CommandText: WideString;
var Params: OleVariant;
var OwnerData: OleVariant);
begin
DefaultInterface.AS_Execute(ProviderName, CommandText, Params, OwnerData);
end;

procedure TDynamicSQLDemoServer.GetAllTables(var vTables: OleVariant);
begin
DefaultInterface.GetAllTables(vTables);
end;

{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
constructor TDynamicSQLDemoServerProperties.Create(AServer: TDynamicSQLDemoServer);
begin
inherited Create;
FServer := AServer;
end;

function TDynamicSQLDemoServerProperties.GetDefaultInterface: IDynamicSQLDemoServer;
begin
Result := FServer.DefaultInterface;
end;

{$ENDIF}
procedure Register;
begin
RegisterComponents('Servers',[TDynamicSQLDemoServer]);
end;

end.
 
服务器端自定义的接口:GetAllTables
unit urdmDynamicSQLServer;
interface
uses
Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
DBClient, PDynamicSQLDemo_TLB, StdVcl, Provider, Db, ADODB;
type
TDynamicSQLDemoServer = class(TRemoteDataModule, IDynamicSQLDemoServer)
adocDSQL: TADOConnection;
ADOQuery1: TADOQuery;
dspGeneral: TDataSetProvider;
private
{ Private declarations }
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
procedure GetAllTables(var vTables: OleVariant);
safecall;
public
{ Public declarations }
end;

implementation
{$R *.DFM}
class procedure TDynamicSQLDemoServer.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;

procedure TDynamicSQLDemoServer.GetAllTables(var vTables: OleVariant);
var
tblList : TStringList;
iCount : Integer;
begin
try
tblList := TStringList.Create;
adocDSQL.GetTableNames(tblList);
vTables := VarArrayCreate([0, tblList.Count - 1], varVariant);
for iCount := 0 to tblList.Count - 1do
begin
vTables[iCount] := tblList.Strings[iCount];
end;
finally
tblList.Free;
end;
end;

initialization
TComponentFactory.Create(ComServer, TDynamicSQLDemoServer,
Class_DynamicSQLDemoServer, ciMultiInstance, tmApartment);
end.
 
客户端调用GetAllTables方法:
procedure TForm2.btnConnectClick(Sender: TObject);
var
vtblList : Variant;
iCount : Integer;
begin
SocketConnection1.Connected := True;
SocketConnection1.AppServer.GetAllTables(vtblList);
if (VarIsArray(vtblList)) then
begin
for iCount := VarArrayLowBound(vtblList, 1) to VarArrayHighBound(vtblList, 1) do
begin
lbTables.Items.Add(vtblList[iCount]);
end;
end;
end;
 
有没有中文的呢?
 
客户端调用用luohongjun的例子
定义一个
var
IMyinterface:IDynamicSQLDemoServerDisp ;
Imyinterface:=createremotecomobject(CLASS_DynamicSQLDemoServer) as IDynamicSQLDemoServerDisp ;
以后就可以调用匀称接口重的所有方法函数,而且比调用appserver要快一倍
 
baxp:
你的方式属于晚期绑定,肯定要比使用AppServer慢你可以测试一下,不过
这样不需要_TBL.pas文件了更环保一些。
Ai2:这就是中文的,第一个帖子是IDE自动生成的。

 
luhongjun:
探讨一下
如果用appserver的话,每一次用都需要调用一次接口,如果需要频繁的调用速度速度就会减慢
可以参考一下李维的系统篇
 
多人接受答案了。
 
后退
顶部