How to invoke VC6.0 developed COM in Delphi

Source: Internet
Author: User

Last wrote how to write in VC6.0 under the COM call, originally wanted to write in Delphi to call VC6.0 developed COM, because in the writing case program encountered a very strange problem, on my machine with VC write interface program compile can pass. But the call will be a problem, (in the VC down with the same appearance). But with Delphi written interface program compiled, whether in VC or in Delphi down with no problem. Later I put VC development interface program compiled, copied to other machines on the test, strange, no problem at all. After summing up only to know that I have a problem with the machine. I have not yet solved why on my machine can not, on the other machine. (If any friend has any idea, please contact me, I think this question is probably because of the reason of registration) not much to say. Let's just talk about it!

When the interface is developed under VC6.0, a corresponding file name is generated. IDL (Interface Definition) interface description file. (IDL has a very simple syntax, but it is important in the development of interfaces.) I read a book, a very familiar to COM cattle xx Foreigner said, the development of COM everything from IDL began. Of course, we're not like that. Because we do not cow xx. So I can't do that. Or to the software to write it! After getting this IDL file, you can use the IDLtoPas.exe tool (NND, I find this tool for half a year have not found, and now have no, heard in the Delphi6.0 have. So only by hand to the IDL file with Pascal to describe, it is not difficult, there are symbols of the conversion work, the IDL file into the file described in Pascal. So that we can make a call to its interface. Of course, when the interface is called, it is necessary to interface. DLL files and. idl files, IDL files are used to generate the corresponding Pascal file, the IDL will not be able to generate after the good. The. dll file is a dynamic library after the interface is compiled. This is what everyone knows. That's all you have to say. Let's give it a little example!

1, use VC6.0 to generate an interface program, here I do not say, I generated this program only one interface called Itestcom one of the methods is: ShowMsg (), displays a message dialog box.

2, the above generated the program to compile, the generated IDL file used in Delphi described with Pascal:

VC generated IDL file:

123456789101112131415161718192021222324252627282930313233343536373839 // 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 hand-Converted Pascal files:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 unit MaAtlDll_TLB;// *******************************************************//const// TypeLibrary Major and minor versionsMaAtlMajorVersion = 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 = classclass function Create: ITestCom;class function CreateRemote(const MachineName: string): ITestCom;end;implementationuses ComObj;class function CoTestCom.Create: ITestCom;beginResult := CreateComObject(CLASS_TestCom) as ITestCom;end;class function CoTestCom.CreateRemote(const MachineName: string): ITestCom;beginResult := CreateRemoteComObject(MachineName, CLASS_TestCom) as ITestCom;end;end. 

It's easy to see if they convert.

3, to generate a Delphi project, in the reference to refer to the manual written description file Maatldll_tlb file. This allows you to define an interface such as an entry in a reference cell:

Var

ps:itestcom;

This allows you to create an interface:

PS: = Createcomobject (class_testcom) as itestcom;//if prompted in the compilation does not define//createcomobject () This is because you do not reference the Comobj unit in the reference.

Call Aspect showmsg ();

Don't forget to release the answer when you exit!

PS: = nil;

The project is compiled and cannot be run. Because you have not registered our interface MaAtl.dll. After registering with REGSRV32, you are ready to run.

Thank you for taking time to see, if you have any questions, write to me. No VC friends, can not compile Maatl, under the dyvccom has compiled good MaAtl.dll, as long as the registration of it can be run.

http://www.vckbase.com/module/articleContent.php?id=119

How to invoke VC6.0 developed COM in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.