哪位dx能说说用delphi做COM的方法
俺基本了解COM,但不太懂delphi,请集中在delphi角度
谢谢!
俺基本了解COM,但不太懂delphi,请集中在delphi角度
谢谢!
解决方案 »
- 求助!语法错误!
- 用delphi自带的TExcelApplication控件如何在工作表中插入一行?
- 如何以流的方式读取internet上的文件?
- http://community.csdn.net/Expert/topic/3096/3096870.xml?temp=.2118341欢迎讨论
- query和数据库连接,不用dbgrid和datasource,为什么我用query.recordcount得到的是0呢?
- 用Delphi控制autocad的OLE特性、方法
- 菜鸟的小问题!!
- 关于数值计算后的显示问题
- 文件拷贝
- 怎么写这个SQL语句?急用!!
- 急!800分求《试题库管理系统》 详情见贴!!
- 怎么用字符串为系统分配权限?
library FirstCom;uses
ComServ,
NumIntf in 'NumIntf.pas',
NumServ in 'NumServ.pas',
FirstCom_TLB in 'FirstCom_TLB.pas';exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;{$R *.TLB}{$R *.RES}begin
end.unit NumServ;interfaceuses
Windows, ActiveX, ComObj, NumIntf;type
TNumber = class(TComObject, INumber)
private
fValue: Integer;
public
function GetValue: Integer; virtual; stdcall;
procedure SetValue (New: Integer); virtual; stdcall;
procedure Increase; virtual; stdcall;
procedure Initialize; override;
destructor Destroy; override;
end;const
Class_Number: TGUID = '{5B2EF181-3AAE-11D3-B9F1-00000100A27B}';implementationuses ComServ;{ TNumber }destructor TNumber.Destroy;
begin
inherited;
MessageBox (0, 'Object Destroyed',
'TDLLNumber', mb_OK); // API call
end;function TNumber.GetValue: Integer;
begin
Result := fValue;
end;procedure TNumber.Increase;
begin
Inc (fValue);
end;procedure TNumber.Initialize;
begin
inherited;
fValue := 10;
end;procedure TNumber.SetValue(New: Integer);
begin
fValue := New;
end;initialization
TComObjectFactory.Create(ComServer, TNumber, Class_Number,
'Number', 'Number Server', ciMultiInstance, tmApartment);
end.unit NumIntf;interfacetype
INumber = interface
['{B4131140-7C2F-11D0-98D0-444553540000}']
function GetValue: Integer; stdcall;
procedure SetValue (New: Integer); stdcall;
procedure Increase; stdcall;
end;implementationend.unit FirstCom_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.88.1.0.1.0 $
// File generated on 02-1-15 10:34:54 from Type Library described below.// ************************************************************************ //
// Type Lib: D:\liujing\delphi\范例程序\delphi_book_sample\Part4\15\FirstCom\FirstCom.tlb (1)
// IID\LCID: {5B2EF182-3AAE-11D3-B9F1-00000100A27B}\0
// Helpfile:
// DepndLst:
// (1) v2.0 stdole, (C:\WINDOWS\SYSTEM\STDOLE2.TLB)
// (2) v4.0 StdVCL, (C:\WINDOWS\SYSTEM\STDVCL40.DLL)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
interfaceuses Windows, ActiveX, Classes, Graphics, OleServer, OleCtrls, StdVCL;// *********************************************************************//
// 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
FirstComMajorVersion = 1;
FirstComMinorVersion = 0; LIBID_FirstCom: TGUID = '{5B2EF182-3AAE-11D3-B9F1-00000100A27B}';
implementationuses ComObj;end.
1. 在delphi中调用一个普通的com组件,大概过程如何?
如何初始化COM,如何生成实例,如何调用方法,如何取得方法的返回值(关键是COM中的BSTR类型的)?2. 如果我有两个组件A,B。A有个接口方法是需要B对象,在A中该方法声明的参数是Idispath*,该方法将调用B的一些方法。如果,在delphi已经有了这两个组件的实例,那么如何把B传递给A?还请指教。
再声明,俺是绝对的生手,别怪俺的问题浅薄:)
这个方法最简单。
利用Delphi的Com向导,会自动生成2个类和一个接口,第一个类是实现Com功能的类,你的函数代码要写在这里面,一个接口就是Com的函数接口,还有一个类是创建Com实例的类,其实它就是封装了创建类的方法,方便Com使用者创建。Com接口和创建Com实例类的代码都通过向导自动生成在YOUCOMNAME_TLB.pas文件中,发布Com时,这个文件也要同时发布;Com实现代码在其他单元中。
假如你的Com叫YouComName,Delphi自动生成TYouComName(代码实现类)、IYouComName(接口)、CoYouComName(创建实例类)。在调用时,你首先要引用YOUCOMNAME_TLB.pas,具体代码如下:uses
...,
YOUCOMNAME_TLB;var
ComObj: IYouComName;procedure ComInit;
begin
ComObj := CoYouComName.Create;
end; 只要你的Com没问题(别忘了注册),就可以用它了。2、我没看大明白,是ComA中要调用ComB吗?如果是那样,我也没能实现。建议是用其他方式实现,比如主程序调用ComB,然后把ComA所需的数据传给ComA。
第2个问题,正如你理解的那样
但俺坚决认为,delphi是应该能处理这个问题的,因为这是COM技术非常重要的能力
但俺不会做:(具体更深入点的问题请见俺的另外一个问题:
http://expert.csdn.net/Expert/topic/1835/1835664.xml?temp=.9194452还有没有其他高手提供有效的办法
“2.如果我有两个组件A,B。A有个接口方法是需要B对象,在A中该方法声明的参数是Idispath*,该方法将调用B的一些方法。如果,在delphi已经有了这两个组件的实例,那么如何把B传递给A?”
俺已经解决,目前只测试了使用IUnknown接口来传递,但俺想IDispatch应该也没有问题、
其实也非常简单,方法如下:
假设有两个接口IComA,IComB,
对于IComA,假设方法为
SetObject(IUnknown* pObj)//idl定义,
在方法的实现中
var theComB:IComB
pObj.QueryInterface(IID_IComB,theComB);
即可在ComA中使用ComB了。客户端,
创建ComA,ComB的实例
直接调用,ComA.SetObject(ComB)就行了谢谢各位大侠的关注!
另外一个问题:
http://expert.csdn.net/Expert/topic/1835/1835664.xml?temp=.9194452
还请关注!
先把问题挂在这里,一起结账。