荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: cay (忧郁的年头), 信区: Program
标  题: 如何在Delphi中调用VC6.0开发的COM
发信站: 荔园晨风BBS站 (Thu Jan 10 10:38:30 2002), 转信

作者:xacn

下载例子源代码



    上次写了如何在VC6.0下对Delphi写的COM进行调用,原本想马上写如何在
Delphi中调用VC6.0开发的COM时,由于在写事例程序中碰到了个很怪的问题,在我
机子上用VC写的接口程序编译能通过。但是调用就会出现问题,(在VC下调用也是
一样的出现)。但是用Delphi写的接口程序编译后,不管是在VC下还是在Delphi下
调用都没有问题。后来我把VC开发的接口程序编译后,拷贝到其它机子上试,怪事
,完全没有问题了。总结后才知道是我机子有点问题。我到现在还没有解决为什么
在我的机子上不行,在其它机子上可以。(如果哪位朋友有什么见意,请和我联系
,我想这个问题很有可能是因为注册的原因)不多说了。还是说正事吧!
    在VC6.0下开发接口时,会生成一个对应文件名.idl(Interface Definition)
接口描述文件。(IDL其语法也很简单,但它在接口的开发中是很重要的。我看过本
书,一个对COM很熟的牛XX老外就说,开发COM一切从IDL开始。当然了我们可不是
这样的。因为我们不牛XX。所以办不到。还是交给软件写吧!)得到这个IDL文件
后,可用IDLtoPas.exe工具(NND,我找这个工具找了半年都有没找到,现在也没
有,听说在Delphi6.0中有。所以只有手工把IDL文件用Pascal来描述,也不难,都
有是符号的转换工作),把IDL文件转成用Pascal描述的文件。这样我们就可以对
其接口进行调用了。当然调用接口时,少不了要接口的.DLL文件和.IDL文件,IDL
文件用来生成对应的Pascal文件,生成好后,IDL就可以不要了。而.Dll文件是接口
编译后的动态库。这个大家都有知道。好就讲这么多。还是给个小例了吧!

1、用VC6.0生成一个接口程序,在这里我就不多说,我生成的这个程序只有一个接
口叫ITestCom其中有一个方法为:ShowMsg(),显示一个消息对话框。

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,只要注册它后就可以运行了。

--

        放弃是最容易的事

        Email: caiji@163.net          QQ: 80123

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.168]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店