相关资料:
1.http://my.oschina.net/u/582827/blog/284766
2.http://www.cnblogs.com/findumars/p/5277561.html
3.http://www.360doc.com/content/12/1208/18/9200790_252887530.shtml
4.http://www.360doc.com/content/12/1208/18/9200790_252887530.shtml
5.http://blog.csdn.net/yoie01/article/details/12194367
结果:
1.Server端与Client端中的接口单元代码都是自动生成的,不用手动增加。
2.在Server中增加方法不能手动写(手写的保存时就不见了),必须用Delphi提供的编辑器(相关资料有提到)。
操作步伐:
Server端:
1.File->New->VCL Forms Application - Delphi for Win32->-Sava All。
2.File->New->Other...->Delphi Projects->ActiveX->Automation Object->CoClass Name->D2007Test->OK。
3.在Project1.tlb->选中“ID2007Test”->“New Method”->选中“Paramerers”->点“Add”->增加自己想要的方法。
4.保存Project1.tlb->Project1_TLB中就会有相关的接口方法了。
5.Unit2中编写对应方法的实现体(Variants必须引入)。
6.保存全部
7.生成EXE,在同目录中放一个“RegCom.cmd”文件,里面写上“D2007ComServer.exe -regserver”,双击此文件注册Server。
Client端:
1.File->New->VCL Forms Application - Delphi for Win32->-Sava All。
2.
D7->Project->Import Type Library...->选中“D2007ComServer”->Create Unit。
D2007->Component->Import Component...->选中“Import a Type Library”->Next->选中“D2007ComServer”->Next->Unit Dir Name->Next。
3.Unit1单元就可以编写调用代码了(D2007ComServer_TLB, ActiveX, OleServer必须引入)。
实例代码
Server端:
unit Unit2; {$WARN SYMBOL_PLATFORM OFF} interface uses
ComObj, ActiveX, D2007ComServer_TLB, StdVcl,
Variants;//Variants必须引入
type
TD2007TestD2007Test = class(TAutoObject, ID2007Test)
private
function TextToOleData(const AText: string): OleVariant;
function OleDataToText(const AData: OleVariant): string;
public
//在此不得不说,COM中用的数据类型与咱们之前在DLEPHI的程序中有所不同。必须注意
//A+B 加法
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant); safecall;//整型数据处理
//A&B 拼接
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant); safecall;//字符串数据处理
end; implementation uses ComServ; { Td7test } procedure TD2007TestD2007Test.AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant);
begin
AReturn := ANumber1 + ANumber2;
end; procedure TD2007TestD2007Test.AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant);
begin
AReturn := AString1 + ' ' + AString2;
end; function TD2007TestD2007Test.OleDataToText(const AData: OleVariant): string;
var
nSize: Integer;
pData: Pointer;
begin
if AData = Null then
Result := ''
else begin
nSize := VarArrayHighBound(AData, ) - VarArrayLowBound(AData, ) + ;
SetLength(Result, nSize);
pData := VarArrayLock(AData);
try
Move(pData^, Pchar(Result)^, nSize);
finally
VarArrayUnlock(AData);
end;
end;
end; function TD2007TestD2007Test.TextToOleData(const AText: string): OleVariant;
var
nSize: Integer;
pData: Pointer;
begin
nSize := Length(AText);
if nSize = then
Result := Null
else begin
Result := VarArrayCreate([, nSize - ], varByte);
pData := VarArrayLock(Result);
try
Move(Pchar(AText)^, pData^, nSize);
finally
VarArrayUnlock(Result);
end;
end;
end; initialization
TAutoObjectFactory.Create(ComServer, TD2007TestD2007Test, Class_D2007Test,
ciMultiInstance, tmApartment);
end.
Server端接口:
unit D2007ComServer_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.
// ************************************************************************ // // $Rev: 8291 $
// File generated on 2016/8/15 14:49:21 from Type Library described below. // ************************************************************************ //
// Type Lib: E:\D2007Com\D2007ComServer\D2007ComServer.tlb (1)
// LIBID: {DF65EF04-7A4E-4A68-9132-7D357AF71708}
// LCID: 0
// Helpfile:
// HelpString: D2007ComServer Library
// DepndLst:
// (1) v2.0 stdole, (C:\Windows\SysWOW64\stdole2.tlb)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface uses Windows, ActiveX, Classes, Graphics, StdVCL, Variants; // *********************************************************************//
// 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
D2007ComServerMajorVersion = ;
D2007ComServerMinorVersion = ; LIBID_D2007ComServer: TGUID = '{DF65EF04-7A4E-4A68-9132-7D357AF71708}'; IID_ID2007Test: TGUID = '{BA65E10E-369E-4285-B7B7-4FE85678EAC0}';
CLASS_D2007Test: TGUID = '{3C531DD1-361D-4F28-B817-3EA79DE2205A}';
type // *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ID2007Test = interface;
ID2007TestDisp = dispinterface; // *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
D2007Test = ID2007Test; // *********************************************************************//
// Interface: ID2007Test
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {BA65E10E-369E-4285-B7B7-4FE85678EAC0}
// *********************************************************************//
ID2007Test = interface(IDispatch)
['{BA65E10E-369E-4285-B7B7-4FE85678EAC0}']
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant); safecall;
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant); safecall;
end; // *********************************************************************//
// DispIntf: ID2007TestDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {BA65E10E-369E-4285-B7B7-4FE85678EAC0}
// *********************************************************************//
ID2007TestDisp = dispinterface
['{BA65E10E-369E-4285-B7B7-4FE85678EAC0}']
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant); dispid ;
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant); dispid ;
end; // *********************************************************************//
// The Class CoD2007Test provides a Create and CreateRemote method to
// create instances of the default interface ID2007Test exposed by
// the CoClass D2007Test. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoD2007Test = class
class function Create: ID2007Test;
class function CreateRemote(const MachineName: string): ID2007Test;
end; implementation uses ComObj; class function CoD2007Test.Create: ID2007Test;
begin
Result := CreateComObject(CLASS_D2007Test) as ID2007Test;
end; class function CoD2007Test.CreateRemote(const MachineName: string): ID2007Test;
begin
Result := CreateRemoteComObject(MachineName, CLASS_D2007Test) as ID2007Test;
end; end.
Client端:
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
D2007ComServer_TLB, ActiveX, OleServer, jpeg, ExtCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
oD2007Test :TD2007Test;
iInteger1, iInteger2: sysint;
iResult: OleVariant;
sString1, sString2: string;
sResult: OleVariant;
begin
oD2007Test := TD2007Test.Create(nil);
oD2007Test.ConnectKind := ckRunningOrNew;
//数字处理
iInteger1 := ;
iInteger2 := ;
iResult := ;
oD2007Test.AddNumber(iInteger1, iInteger2, iResult);
Label1.Caption := IntToStr(iResult);
//字符处理
sString1 := '';
sString2 := '';
sResult := '';
oD2007Test.AddString(sString1, sString2, sResult);
Label2.Caption := sResult;
end; end.
Clinet端接口:
unit D2007ComServer_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.
// ************************************************************************ // // $Rev: 8291 $
// File generated on 2016/8/15 14:29:34 from Type Library described below. // ************************************************************************ //
// Type Lib: E:\D2007Com\D2007ComServer\D2007ComServer.exe (1)
// LIBID: {DF65EF04-7A4E-4A68-9132-7D357AF71708}
// LCID: 0
// Helpfile:
// HelpString: D2007ComServer Library
// DepndLst:
// (1) v2.0 stdole, (C:\Windows\SysWOW64\stdole2.tlb)
// ************************************************************************ //
// *************************************************************************//
// NOTE:
// Items guarded by $IFDEF_LIVE_SERVER_AT_DESIGN_TIME are used by properties
// which return objects that may need to be explicitly created via a function
// call prior to any access via the property. These items have been disabled
// in order to prevent accidental use from within the object inspector. You
// may enable them by defining LIVE_SERVER_AT_DESIGN_TIME or by selectively
// removing them from the $IFDEF blocks. However, such items must still be
// programmatically created via a method of the appropriate CoClass before
// they can be used.
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface uses Windows, ActiveX, Classes, Graphics, OleServer, StdVCL, Variants; // *********************************************************************//
// 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
D2007ComServerMajorVersion = ;
D2007ComServerMinorVersion = ; LIBID_D2007ComServer: TGUID = '{DF65EF04-7A4E-4A68-9132-7D357AF71708}'; IID_ID2007Test: TGUID = '{BA65E10E-369E-4285-B7B7-4FE85678EAC0}';
CLASS_D2007Test: TGUID = '{3C531DD1-361D-4F28-B817-3EA79DE2205A}';
type // *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ID2007Test = interface;
ID2007TestDisp = dispinterface; // *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
D2007Test = ID2007Test; // *********************************************************************//
// Interface: ID2007Test
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {BA65E10E-369E-4285-B7B7-4FE85678EAC0}
// *********************************************************************//
ID2007Test = interface(IDispatch)
['{BA65E10E-369E-4285-B7B7-4FE85678EAC0}']
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant); safecall;
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant); safecall;
end; // *********************************************************************//
// DispIntf: ID2007TestDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {BA65E10E-369E-4285-B7B7-4FE85678EAC0}
// *********************************************************************//
ID2007TestDisp = dispinterface
['{BA65E10E-369E-4285-B7B7-4FE85678EAC0}']
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant); dispid ;
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant); dispid ;
end; // *********************************************************************//
// The Class CoD2007Test provides a Create and CreateRemote method to
// create instances of the default interface ID2007Test exposed by
// the CoClass D2007Test. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
CoD2007Test = class
class function Create: ID2007Test;
class function CreateRemote(const MachineName: string): ID2007Test;
end; // *********************************************************************//
// OLE Server Proxy class declaration
// Server Object : TD2007Test
// Help String : d7test Object
// Default Interface: ID2007Test
// Def. Intf. DISP? : No
// Event Interface:
// TypeFlags : (2) CanCreate
// *********************************************************************//
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
TD2007TestProperties= class;
{$ENDIF}
TD2007Test = class(TOleServer)
private
FIntf: ID2007Test;
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps: TD2007TestProperties;
function GetServerProperties: TD2007TestProperties;
{$ENDIF}
function GetDefaultInterface: ID2007Test;
protected
procedure InitServerData; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect; override;
procedure ConnectTo(svrIntf: ID2007Test);
procedure Disconnect; override;
procedure AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant);
procedure AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant);
property DefaultInterface: ID2007Test read GetDefaultInterface;
published
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
property Server: TD2007TestProperties read GetServerProperties;
{$ENDIF}
end; {$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
// *********************************************************************//
// OLE Server Properties Proxy Class
// Server Object : TD2007Test
// (This object is used by the IDE's Property Inspector to allow editing
// of the properties of this server)
// *********************************************************************//
TD2007TestProperties = class(TPersistent)
private
FServer: TD2007Test;
function GetDefaultInterface: ID2007Test;
constructor Create(AServer: TD2007Test);
protected
public
property DefaultInterface: ID2007Test read GetDefaultInterface;
published
end;
{$ENDIF} procedure Register; resourcestring
dtlServerPage = '(none)'; dtlOcxPage = '(none)'; implementation uses ComObj; class function CoD2007Test.Create: ID2007Test;
begin
Result := CreateComObject(CLASS_D2007Test) as ID2007Test;
end; class function CoD2007Test.CreateRemote(const MachineName: string): ID2007Test;
begin
Result := CreateRemoteComObject(MachineName, CLASS_D2007Test) as ID2007Test;
end; procedure TD2007Test.InitServerData;
const
CServerData: TServerData = (
ClassID: '{3C531DD1-361D-4F28-B817-3EA79DE2205A}';
IntfIID: '{BA65E10E-369E-4285-B7B7-4FE85678EAC0}';
EventIID: '';
LicenseKey: nil;
Version: );
begin
ServerData := @CServerData;
end; procedure TD2007Test.Connect;
var
punk: IUnknown;
begin
if FIntf = nil then
begin
punk := GetServer;
Fintf:= punk as ID2007Test;
end;
end; procedure TD2007Test.ConnectTo(svrIntf: ID2007Test);
begin
Disconnect;
FIntf := svrIntf;
end; procedure TD2007Test.DisConnect;
begin
if Fintf <> nil then
begin
FIntf := nil;
end;
end; function TD2007Test.GetDefaultInterface: ID2007Test;
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 TD2007Test.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps := TD2007TestProperties.Create(Self);
{$ENDIF}
end; destructor TD2007Test.Destroy;
begin
{$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
FProps.Free;
{$ENDIF}
inherited Destroy;
end; {$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
function TD2007Test.GetServerProperties: TD2007TestProperties;
begin
Result := FProps;
end;
{$ENDIF} procedure TD2007Test.AddNumber(ANumber1: Integer; ANumber2: Integer; out AReturn: OleVariant);
begin
DefaultInterface.AddNumber(ANumber1, ANumber2, AReturn);
end; procedure TD2007Test.AddString(const AString1: WideString; const AString2: WideString;
out AReturn: OleVariant);
begin
DefaultInterface.AddString(AString1, AString2, AReturn);
end; {$IFDEF LIVE_SERVER_AT_DESIGN_TIME}
constructor TD2007TestProperties.Create(AServer: TD2007Test);
begin
inherited Create;
FServer := AServer;
end; function TD2007TestProperties.GetDefaultInterface: ID2007Test;
begin
Result := FServer.DefaultInterface;
end; {$ENDIF} procedure Register;
begin
RegisterComponents(dtlServerPage, [TD2007Test]);
end; end.