如何在Delphi中调用VC6.0开发的COM
2006-07-21 11:44:49 来源:WEB开发网2、对其上面生成的程序进行编译,把生成的IDL文件用在Delphi下用Pascal描述:
VC生成的IDL文件:
// MaAtl.idl : IDL source for MaAtl.dll
//
// This file will be processed by the MIDL tool to
// produce the type library (MaAtl.tlb) and marshalling code.
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(78313A6E-FBA7-11D5-8094-00E04C4EA60F),
dual,
helpstring("ITestCom Interface"),
pointer_default(unique)
]
interface ITestCom : IDispatch
{
[id(1), helpstring("method ShowMsg")] HRESULT ShowMsg();
};
[
uuid(78313A62-FBA7-11D5-8094-00E04C4EA60F),
version(1.0),
helpstring("MaAtl 1.0 Type Library")
]
library MAATLLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(78313A6F-FBA7-11D5-8094-00E04C4EA60F),
helpstring("TestCom Class")
]
coclass TestCom
{
[default] interface ITestCom;
};
};
Delphi手工转换的Pascal文件:
unit MaAtlDll_TLB;
// *********************************************************************//
const
// TypeLibrary Major and minor versions
MaAtlMajorVersion = 1;
MaAtlMinorVersion = 0;
LIBID_MaAtl: TGUID = ''''{78313A62-FBA7-11D5-8094-00E04C4EA60F}'''';
IID_ITestCom: TGUID = ''''{78313A6E-FBA7-11D5-8094-00E04C4EA60F}'''';
CLASS_TestCom: TGUID = ''''{78313A6F-FBA7-11D5-8094-00E04C4EA60F}'''';
type
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
ITestCom = interface;
// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
TestCom= ITestCom;
// *********************************************************************//
// Interface: IMaAtlCom
// Flags: (256) OleAutomation
// *********************************************************************//
ITestCom = interface(IDispatch)
[''''{78313A6E-FBA7-11D5-8094-00E04C4EA60F}'''']
function ShowMsg(): HResult; stdcall;
end;
// *********************************************************************//
CoTestCom = class
class function Create: ITestCom;
class function CreateRemote(const MachineName: string): ITestCom;
end;
implementation
uses ComObj;
class function CoTestCom.Create: ITestCom;
begin
Result := CreateComObject(CLASS_TestCom) as ITestCom;
end;
class function CoTestCom.CreateRemote(const MachineName: string): ITestCom;
begin
Result := CreateRemoteComObject(MachineName, CLASS_TestCom) as ITestCom;
end;
end.
看它们转换是不是很简单呀!
3、生成一个Delphi工程,在引用中引用刚才手工写的描述文件MaAtlDll_TLB文件。这样引用单元中就可以定义接口如入:
var
pS : ITestCom;
这样就可以创建接口:
pS := CreateComObject(CLASS_TestCom) as ITestCom;//如果在编译中提示没定义 //CreateComObject()这是因为你在引用中没引用ComObj单元。
调用方面ShowMsg();
别忘了退出时把接释放呀!
pS := nil;
对工程进行编译,还不能运行。因为你还没有注册我们的接口maAtl.dll。用regsrv32进行注册后就可以运行了。
谢谢能抽空看,如果有什么问题可写信给我。没有VC的朋友,不能编译MaAtl时,在DyVCcom下有编译好的MaAtl.dll,只要注册它后就可以运行了。
更多精彩
赞助商链接