/
unit ServMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ExtCtrls, DBCtrls, DBTables;
type
TForm1 = class(TForm)
Table1: TTable;
DBNavigator1: TDBNavigator;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Memo1: TMemo;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses TOleAnimalPas;
{$R *.dfm}
end.
uses
Windows, Messages, SysUtils, Variants, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ExtCtrls, DBCtrls, DBTables
,ComObj, ActiveX, AxCtrls, Classes, OleAnimalserver_TLB, StdVcl;
type
TOleAnimal = class(TAutoObject, IConnectionPointContainer, IOleAnimal)
private
public
AnimalNames: Tstrings;
procedure Initialize; override;
protected
property ConnectionPoints: TConnectionPoints read FConnectionPoints
implements IConnectionPointContainer;
procedure EventSinkChanged(const EventSink: IUnknown); override;
function Get_BOF: WordBool; safecall;
function Get_EOF: WordBool; safecall;
function GetFieldValue(const Fieldname: WideString):WideString; safecall;
function Getnames:IStrings; safecall;
function Locate(const name: WideString):WordBool; safecall;
procedure Next; safecall;
procedure Prior; safecall;
procedure DataChange(Sender:Tobject;Field:Tfield);
end;
implementation
uses ComServ, ServMain;
procedure TOleAnimal.EventSinkChanged(const EventSink: IUnknown);
begin
FEvents := EventSink as IOleAnimalEvents;
end;
procedure TOleAnimal.Initialize;
begin
inherited Initialize;
FConnectionPoints := TConnectionPoints.Create(Self);
if AutoFactory.EventTypeInfo <> nil then
begin
FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
AutoFactory.EventIID, ckSingle, EventConnect);
AnimalNames:=TStringList.Create;
Form1.table1.next;
form1.datasource1.ondatachange:=Datachange;
form1.memo1.lines.add('AutoObj initialize');
end
else
FConnectionPoint := nil;
end;
Procedure ToleAnimal.dataChange(Sender:TObject;Field:TField);
begin
Form1.Memo1.lines.add('Event
ataChange');
if FEvents<>nil then FEvents.onDataChange;
end;
function TOleAnimal.Get_BOF: WordBool;
begin
Result:=Form1.Table1.Bof;
Form1.Memo1.Lines.Add('Property:Bof');
end;
function TOleAnimal.Get_EOF: WordBool;
begin
Result:=Form1.Table1.EOF;
Form1.Memo1.Lines.Add('Property:EOF');
end;
function TOleAnimal.GetFieldValue(const Fieldname: WideString):WideString;
var ws:Array[0..1023] of WideChar;
begin
StringToWideChar(Form1.Table1.FieldByName(FieldName).AsString,Ws,1023);
Form1.Memo1.Lines.Add('Method:GetFieldName('+FieldName+')');
Result:=Ws;
end;
function TOleAnimal.Getnames:Istrings;
begin
GetOleStrings(AnimalNames,Result);
Form1.Memo1.Lines.Add('Method:GetNames');
end;
function TOleAnimal.Locate(const name: WideString):WordBool;
begin
Result:=Form1.Table1.Locate('name',name,[loCaseInsensitive]);
Form1.Memo1.Lines.Add('method: Locate(name='+name+')');
end;
procedure TOleAnimal.Next;
begin
Form1.Table1.Next;
Form1.Memo1.Lines.Add('Method:next');
end;
procedure TOleAnimal.Prior;
begin
Form1.Table1.Prior;
Form1.Memo1.Lines.Add('method
rior');
end;
initialization
TAutoObjectFactory.Create(ComServer, TOleAnimal, Class_OleAnimal,
ciMultiInstance, tmApartment);
end.
unit OleAnimalserver_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 2003-2-13 12:12:59 from Type Library described below.
// ************************************************************************ //
// Type Lib: F:/新建文件夹/OleAnimalserver.tlb (1)
// LIBID: {4AEEB894-16C8-437B-92EB-797C7916E3F3}
// 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
OleAnimalserverMajorVersion = 1;
OleAnimalserverMinorVersion = 0;
LIBID_OleAnimalserver: TGUID = '{4AEEB894-16C8-437B-92EB-797C7916E3F3}';
IID_IOleAnimal: TGUID = '{63962B73-0213-4D3E-A22E-639A7AF52CFA}';
DIID_IOleAnimalEvents: TGUID = '{5FB4F015-9ED5-4ED9-B968-566F5A49A7C6}';
CLASS_OleAnimal: TGUID = '{849210C0-BB01-4055-8914-C3308890428B}';
type
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
IOleAnimal = interface;
IOleAnimalDisp = dispinterface;
IOleAnimalEvents = dispinterface;
// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
OleAnimal = IOleAnimal;
// *********************************************************************//
// Interface: IOleAnimal
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {63962B73-0213-4D3E-A22E-639A7AF52CFA}
// *********************************************************************//
IOleAnimal = interface(IDispatch)
['{63962B73-0213-4D3E-A22E-639A7AF52CFA}']
function Get_BOF: WordBool; safecall;
function Get_EOF: WordBool; safecall;
procedure Prior; safecall;
procedure Next; safecall;
function Locate(const name: WideString):WordBool; safecall;
function GetFieldValue(const Fieldname: WideString):WideString; safecall;
function Getnames:IStrings; safecall;
property BOF: WordBool read Get_BOF;
property EOF: WordBool read Get_EOF;
end;
// *********************************************************************//
// DispIntf: IOleAnimalDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {63962B73-0213-4D3E-A22E-639A7AF52CFA}
// *********************************************************************//
IOleAnimalDisp = dispinterface
['{63962B73-0213-4D3E-A22E-639A7AF52CFA}']
property BOF: WordBool readonly dispid 1;
property EOF: WordBool readonly dispid 2;
procedure Prior; dispid 3;
procedure Next; dispid 4;
function Locate(const name: WideString):WordBool; dispid 5;
function GetFieldValue(const Fieldname: WideString):WideString; dispid 6;
function Getnames:IStrings; dispid 7;
end;
// *********************************************************************//
// DispIntf: IOleAnimalEvents
// Flags: (4096) Dispatchable
// GUID: {5FB4F015-9ED5-4ED9-B968-566F5A49A7C6}
// *********************************************************************//
IOleAnimalEvents = dispinterface
['{5FB4F015-9ED5-4ED9-B968-566F5A49A7C6}']
procedure onDataChange; dispid 1;
end;
// *********************************************************************//
// The Class CoOleAnimal provides a Create and CreateRemote method to
// create instances of the default interface IOleAnimal exposed by
// the CoClass OleAnimal. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoOleAnimal = class
class function Create: IOleAnimal;
class function CreateRemote(const MachineName: string): IOleAnimal;
end;
implementation
uses ComObj;
class function CoOleAnimal.Create: IOleAnimal;
begin
Result := CreateComObject(CLASS_OleAnimal) as IOleAnimal;
end;
class function CoOleAnimal.CreateRemote(const MachineName: string): IOleAnimal;
begin
Result := CreateRemoteComObject(MachineName, CLASS_OleAnimal) as IOleAnimal;
end;
end.
unit Client;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,comobj,axctrls;
type
TForm1 = class(TForm)
Search: TGroupBox;
Name: TLabel;
cmbNames: TComboBox;
BtnQuery: TSpeedButton;
BtnPrior: TSpeedButton;
BtnNext: TSpeedButton;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
EdtName: TEdit;
EdtSize: TEdit;
EdtWeight: TEdit;
EdtArea: TEdit;
procedure FormCreate(Sender: TObject);
Procedure GetDataFromServer;
procedure BtnQueryClick(Sender: TObject);
procedure BtnPriorClick(Sender: TObject);
procedure BtnNextClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
// Animals:IOleAnimal;
end;
var
animals:variant;
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
//Ts:TStrings;
begin
{animals:=CoOleAniamal.create;
Ts:=TStringlist.create;
SetOleStrings(Ts,animals.Getnames);
CmbNames.Items.Assign(ts);
Ts.free;
GEtDataFromServer; }
animals:=CreateOleObject('oleAnimalserver.OleAnimal');
For i:=0 to Animals.GetNames.Count-1 do
CmbNames.Items.Add(animals.GetNames.item
);
GetDataFromServer
end;
Procedure TForm1.GetDataFromServer;
Begin
edtName.text:=animals.getFieldValue('name');
edtSize.text:=animals.getFieldValue('Size');
edtWeight.text:=animals.getFieldValue('Weight');
edtArea.text:=animals.getFieldValue('Area');
end;
procedure TForm1.BtnQueryClick(Sender: TObject);
begin
if not animals.Locate(cmbNames.text) then
showMessage('can not find');
GetDataFromServer;
end;
procedure TForm1.BtnPriorClick(Sender: TObject);
begin
if not animals.BoF then animals.Prior;
GetdataFromServer;
end;
procedure TForm1.BtnNextClick(Sender: TObject);
begin
if not animals.BOF then animals.Next;
GetdataFromServer;
end;
end.