Com进程通信(Delphi2007)
相关资料:
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.
Com进程通信(Delphi2007)的更多相关文章
- Linux学习笔记(13)-进程通信|命名管道
匿名管道只能在具有亲属关系的进程间通信,那么如果想要在不具有亲戚关系,想在陌生人之间通信,那又该怎么办呢? 别慌,Linux身为世界上*强大的操作系统,当然提供了这种机制,那便是命名管道-- 所谓命名 ...
- Android随笔之——跨进程通信(一) Activity篇
在Android应用开发中,我们会碰到跨进程通信的情况,例如:你用QQ通讯录打电话的时候会调用系统的拨号应用.某些新闻客户端可以将新闻分享到QQ.微信等应用,这些都是跨进程通信的情况.简而言之,就是一 ...
- Android 进程通信机制之 AIDL
什么是 AIDL AIDL 全称 Android Interface Definition Language,即 安卓接口描述语言.听起来很深奥,其实它的本质就是生成进程间通信接口的辅助工具.它的存在 ...
- Windows进程通信 -- 共享内存(1)
共享内存的方式原理就是将一份物理内存映射到不同进程各自的虚拟地址空间上,这样每个进程都可以读取同一份数据,从而实现进程通信.因为是通过内存操作实现通信,因此是一种最高效的数据交换方法. 共享内存在 W ...
- MINIX3 进程通信分析
MINIX3 进程通信分析 6.1MINIX3 进程通信概要 MINIX3 的进程通信是 MINIX3 内核部分最重要的一个部件,我个人认为其实这 是内核中的“内核”,怎么来理解这个概念呢?其实 MI ...
- Linux系统编程@进程通信(一)
进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...
- Linux下进程通信的八种方法
Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...
- WinForm实现跨进程通信的方法
public class WinMessageHelper { private struct COPYDATASTRUCT { public IntPtr dwData; public int cbD ...
- 进程通信之一 使用WM_COPYDATA C++及C#实现(转)
进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C#程序相互通信的二种实现方式.这样消息的接收端可以用C++实现,发送端可以用C++或C#实现. 发送WM_COPYD ...
随机推荐
- js实现ppt
实现ppt的js框架有很多,这里推荐几个: impress.js impress.js demo webSlide.js webSlide.js demo reveal.js ...
- devDependencies和dependencies的区别
我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,比如: --save-dev --save 在 package.json 文件里面 ...
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...
- bzoj3955
首先,最短路不同的两辆车一定不会发生堵塞 对于最短路相同的点,我们把属于最短路径上的边拎出来建图跑最大流即可 然后我TLE了…… 因为很明显建出来图很大,而真正流的流量很小 普通的初始标号都是0的sa ...
- BZOJ_1025_[SHOI2009]_游戏_(素数表+最小公倍数+DP)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1025 分析 对于\(n\),转一圈回来之后其实是好几个环各转了整数圈.这些环中的数为\(1,2 ...
- js学习总结
转自 http://blog.sina.com.cn/s/blog_75cf5f3201011csu.html 一: 关于基本数据类型在栈内存和堆内存中的关系 基本数据对于栈内存和堆内存是可以复制的, ...
- objective-c里的方法指针IMP的用法
SGPopSelectView.h @interface SGPopSelectView : UIView @property (nonatomic, assign) SEL selector; @p ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- Java Socket(2): 异常处理
1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...
- LeetCode: pow
Title: https://leetcode.com/problems/powx-n/ 思路:二分.使用递归或者非递归.非递归有点难理解.pow(0,0)=1 递归的方法是将n为负数的用除法解决.有 ...