com和接口定义的文件:
unit Unit2;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj, Project1_TLB, StdVcl;
const
Class_MaComStrServer:TGUID='{55450001-39E0-4532-9A35-16A906374849}';
type
IMaComStrServer=interface(IUnknown)
['{2AD548D4-948E-4334-AB03-71A9127D3995}']
function GetValue():string;stdcall;
procedure SetValue(const value:string);stdcall;
function UpperCase1():string;stdcall;
function LowerCase1():string;stdcall;
property Text:string read GetValue write SetValue;
end;
TMaComStrServer = class(TTypedComObject, IMaComStrServer)
private
FText:string;
protected
{Declare IMaComStrServer methods here}
public
function GetValue():string;virtual;stdcall;
procedure SetValue(const value:string);virtual;stdcall;
function UpperCase1():string;virtual;stdcall;
function LowerCase1():string;virtual;stdcall;
property Text:string read GetValue write SetValue;
end;
implementation
uses ComServ,SysUtils;
{ TMaComStrServer }
function TMaComStrServer.GetValue: string;
begin
result:=FText;
end;
function TMaComStrServer.LowerCase1: string;
begin
result:=LowerCase(FText);
end;
procedure TMaComStrServer.SetValue(const value: string);
begin
FText:=value;
end;
function TMaComStrServer.UpperCase1: string;
begin
result:=UpperCase(FText);
end;
initialization
TTypedComObjectFactory.Create(ComServer, TMaComStrServer, Class_MaComStrServer,ciMultiInstance, tmApartment);//运行时出错,抛出那个异常
end.
///***************客户端程序***************/
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit2,ComObj;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
iStrSvr1,istrSvr2:IMaComStrServer;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
application.Terminate;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
iStrSvr1:=CreateComObject(class_MaComStrServer) as IMaComStrServer;
iStrSvr1.Text:='';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
iStrSvr1.Text:=edit1.Text;
edit2.Text:=iStrSvr1.UpperCase1();
end;
end.