Mega Code Archive

 
Categories / Delphi / Examples
 

Using a c++ dll in delphi [activex control]

Question: I have this DLL TblAnalysis.DLL that is written in C++, how can I use it in Delphi? I have a list of exported functions as a .h header file, but when I try to define them in Delphi as 'external', I get runtime errors. The documentation speaks about an object that publishes those functions as methods. Answer: Very likely this TblAnalysis.DLL could be an Active-X control - which technically is a DLL with a well-defined interface. To verify this, you can either Try to register it from the commandline with regsvr32 TblAnalysis.DLL If this works, then you are looking at an ActiveX control. or - better: Get a list of all functions with ordinals exported from the DLL. From the commandline, use Borland's TDump tool to get such a list. tdump -ee tblanalysis.dll The output: c:\temp> tdump -ee tblanalysis.dll Turbo Dump Version 5.0.16.12 Copyright (c) 1988, 2000 Inprise Corporation Display of File TBLANALYIS.DLL Export ord:0001 = 'DLLConUnloadNow' Export ord:0002 = 'DLLGetClassObject' Export ord:0003 = 'DLLRegisterServer' Export ord:0004 = 'DLLUnregisterServer' Again, if the output looks like this, then the you are looking at an ActiveX control. So now you hopefully verified that you're looking at an ActiveX control, not at a regular DLL. How do you deal with it then? Instead of importing and calling functions, you will have to create an instance of that ActiveX control and then you can call its methods. Make sure to register it on your machine. This also has to be done on any computer where the application will run - so make this part of your install program: regsvr32.exe TBLANALYSIS.DLL A messagebox should appear saying that the registration succeeded. Unregistering an ActiveX control goes with the /U option: regsvr32.exe /u TBLANALYSIS.DLL Convert the type library that should be included in the DLL to a Delphi stub. In Delphi, close your project, start a new project and select Import Type Library from the project menu. A dialog will show up containing a list of all registered ActiveX libraries on your system. Locate the one that belongs the the DLL (the name should be in your documents) and select it. You can then click install (make sure generate component wrapper is checked) and a new component will be installed on your component palette. You use this component in your application and use its methods as described in the DLL's documentation.