7

I need to return objects from a DLL made in Delphi, to an app made in Delphi, too. The objective is to do a subsystem that can be modify in the future without to modify the main app. So, I imagine developing the subsystem in a DLL is a (good??) idea... i am programming in Windows XP, Delphi 7. I did read DLLs only return basic data type, but there must to be a way to do that...

Best regards.

DelphiProgrammer
  • 269
  • 6
  • 11
  • 6
    This has been discussed *several times* already on StackOverflow. Search for questions tagged [delphi] and [dll] or [bpl]. There's probably nothing new to be said on the subject... – mghie Nov 04 '09 at 21:09
  • 2
    Oh!! Great answer man!!! And how to found a good reply in hundreds of them???? – DelphiProgrammer Nov 04 '09 at 22:17
  • 2
    Look at http://stackoverflow.com/questions/1596704/how-to-return-an-instance-from-a-dll – Gerry Coll Nov 04 '09 at 22:50
  • 4
    @MLB and by creating another duplicate, you're just adding to the problem. You could try reading some of the responses. – mrduclaw Nov 04 '09 at 23:16
  • 3
    @MLB: I've briefly toyed with the idea of starting a community answer with some of this information condensed, but your attitude makes me somewhat glad I didn't invest the time. If you can't be bothered to read the answers to some of the hottest topics in those search results I can't be bothered to help you either. But maybe you have the time to read this: http://slash7.com/pages/vampires – mghie Nov 05 '09 at 05:24
  • 1
    @mghie: Please don't get discouraged by people like this. For each of them there are 100th that do a search first, find their answer and are gone again. You never hear from them. – dummzeuch Nov 05 '09 at 09:29
  • I think it is ok to return an oject from a dll IF the compiler used to compile the DLL and the EXE is the same. Can anyone confirm / deny this? – Gabriel Dec 05 '18 at 12:21

4 Answers4

2

I prefer to apply COM to such a model, which allows you to create external "objects" which you then can direct from within your application. Creating COM objects in Delphi is extremely simple, use your ActiveX creation methods to create an ActiveX library and then create COM objects. You then use the interface unit in your main application, and when you CoCreate an instance of the object it loads the appropriate DLL. The only tricky part of this is that your com objects must be registered on the system to function properly... which in the Win7/Vista world requires elevated access... although once this is done, it is seamless.

skamradt
  • 15,366
  • 2
  • 36
  • 53
2

The BEST way is to use a COM wrapper as suggested by skamradt.

It is possible, but not a good idea to pass object references as pointers to DLL's. Refer particularly to Peter Haas's comments.

If you do pass an object from a Delphi DLL to a Delphi app you will have the following problems:

You must use the same version of Delphi for the app and DLL.

Both app and DLL MUST have the same implementation of the object - or at least identical layout of all fields in the class - OK if you are using standard objects such as TStringList.

You should use shared memory or runtime packages, otherwise you will get weird access violations.

I have had to maintain code where this was done - it was nightmarish as you couldn't change classes without a lot of re-compiling.

Gerry Coll
  • 5,867
  • 1
  • 27
  • 36
2

You can use interfaces and most of your problems, wrt compiler/rtl versions or even other languages just go away. Delphi's interfaces are always IUnknown-compatible, which makes them compatible with most OO-enabled languages on Windows.

One thing to keep in mind, though: Don't use AnyString, stick to WideString, which is the Stringtype used by COM.

Robert Giesecke
  • 4,314
  • 21
  • 22
0

A platform- and language independent way could be to exchange serialized objects.

It has a performance impact, but it has advantages too: the DLL works without modifications with other languages and platforms like .Net or Java (via JNA Java Native Access). It does not depend on any special features of the operating system so it can as well be used on Linux or MacOS if you compile the library with Free Pascal.

For the serialization, you could use JSON or XML. There are open source libraries for Delphi, for example SuperObject and OmniXML.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • How can I exchange serialized objects from/to application/dll with delphi. Do you have an example about it (with SuperObject)? – Martin Dec 20 '12 at 09:04