我做了个实验,,是在D6里的。。
工程文件:
// Project1.dpr
//
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Project1_TLB in 'Project1_TLB.pas',
Unit2 in 'Unit2.pas' {Test: CoClass},
Unit3 in 'Unit3.pas' {A: CoClass};
{$R *.TLB}
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
////////////////////////////////////////////////////////////
// Unit2.pas 测试用的 Automation Test 单元文件, 相当于你的 B;
/////////////////////////////////////////////////////////////
unit Unit2;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, Project1_TLB, StdVcl;
type
TTest = class(TAutoObject, ITest)
private
FName : Widestring;
protected
function Get_Name: WideString; safecall;
procedure Set_Name(const Value: WideString); safecall;
{ Protected declarations }
end;
implementation
uses ComServ;
function TTest.Get_Name: WideString;
begin
Result := FName;
end;
procedure TTest.Set_Name(const Value: WideString);
begin
FName := Value;
end;
initialization
TAutoObjectFactory.Create(ComServer, TTest, Class_Test,
ciMultiInstance, tmApartment);
end.
////////////////////////////////////////////////
// Unit3.pas 另一个测试 Automation A
///////////////////////////////////////////////
unit Unit3;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, Project1_TLB, StdVcl, Unit2;
type
TA = class(TAutoObject, IA)
private
FName: WideString;
FTest : ITest;
protected
function Get_TestName: ITest; safecall;
{ Protected declarations }
end;
implementation
uses ComServ;
function TA.Get_TestName: ITest;
begin
if FTest = nil then
FTest := CoTest.Create;
Result := FTest;
end;
initialization
TAutoObjectFactory.Create(ComServer, TA, Class_A,
ciMultiInstance, tmApartment);
end.
///////////////////////////////////////////////////
// Project1_TLB.pas
//////////////////////////////////////////////////
unit Project1_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-4-29 9:50:55 from Type Library described below.
// ************************************************************************ //
// Type Lib: C:/Program Files/Borland/Delphi6/Projects/DFW/COM/Project1.tlb (1)
// LIBID: {C973560E-3E3E-4BD5-8B62-E467A32431D4}
// 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
Project1MajorVersion = 1;
Project1MinorVersion = 0;
LIBID_Project1: TGUID = '{C973560E-3E3E-4BD5-8B62-E467A32431D4}';
IID_ITest: TGUID = '{9A3F2805-3406-48D8-98DC-31AF1B0753F9}';
CLASS_Test: TGUID = '{EBCF5E95-1D11-4FA1-8F27-9A6E7269E8BA}';
IID_IA: TGUID = '{DE631DDC-A798-4C43-A11A-7F5E655417CA}';
CLASS_A: TGUID = '{F22E6C6E-5209-4E08-89CA-61AAF7353951}';
type
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ITest = interface;
ITestDisp = dispinterface;
IA = interface;
IADisp = dispinterface;
// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
Test = ITest;
A = IA;
// *********************************************************************//
// Interface: ITest
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {9A3F2805-3406-48D8-98DC-31AF1B0753F9}
// *********************************************************************//
ITest = interface(IDispatch)
['{9A3F2805-3406-48D8-98DC-31AF1B0753F9}']
function Get_Name: WideString; safecall;
procedure Set_Name(const Value: WideString); safecall;
property Name: WideString read Get_Name write Set_Name;
end;
// *********************************************************************//
// DispIntf: ITestDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {9A3F2805-3406-48D8-98DC-31AF1B0753F9}
// *********************************************************************//
ITestDisp = dispinterface
['{9A3F2805-3406-48D8-98DC-31AF1B0753F9}']
property Name: WideString dispid 1;
end;
// *********************************************************************//
// Interface: IA
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {DE631DDC-A798-4C43-A11A-7F5E655417CA}
// *********************************************************************//
IA = interface(IDispatch)
['{DE631DDC-A798-4C43-A11A-7F5E655417CA}']
function Get_TestName: ITest; safecall;
property TestName: ITest read Get_TestName;
end;
// *********************************************************************//
// DispIntf: IADisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {DE631DDC-A798-4C43-A11A-7F5E655417CA}
// *********************************************************************//
IADisp = dispinterface
['{DE631DDC-A798-4C43-A11A-7F5E655417CA}']
property TestName: ITest readonly dispid 2;
end;
// *********************************************************************//
// The Class CoTest provides a Create and CreateRemote method to
// create instances of the default interface ITest exposed by
// the CoClass Test. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoTest = class
class function Create: ITest;
class function CreateRemote(const MachineName: string): ITest;
end;
// *********************************************************************//
// The Class CoA provides a Create and CreateRemote method to
// create instances of the default interface IA exposed by
// the CoClass A. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoA = class
class function Create: IA;
class function CreateRemote(const MachineName: string): IA;
end;
implementation
uses ComObj;
class function CoTest.Create: ITest;
begin
Result := CreateComObject(CLASS_Test) as ITest;
end;
class function CoTest.CreateRemote(const MachineName: string): ITest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_Test) as ITest;
end;
class function CoA.Create: IA;
begin
Result := CreateComObject(CLASS_A) as IA;
end;
class function CoA.CreateRemote(const MachineName: string): IA;
begin
Result := CreateRemoteComObject(MachineName, CLASS_A) as IA;
end;
end.
/////////////////////////////////////////////////////////
// 测试程序代码
//////////////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
project1_TLB;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
tmp : IA;
begin
Tmp := CoA.Create;
tmp.TestName.Name := 'Test..'; // 在这儿写入属性,,
button1.Caption := Tmp.TestName.Name; //这里读取属性到 Button 的标签上,,
end;
end.
我的环境 Windows 2000 Server ,,D6
测试通过可以实现。。