views:

323

answers:

4

Hello buddies: I am programming a DLL. I have to return an instance of TBitmap to the host application. The DLL has another UNIT, wich is a Form, that it has a TImageList for storing images. I wrote a function that I want to return an Image from the TImageList (from the DLL to the host application. How can I do this?

Thanks, Yulién.

A: 

You can't return an object per se, but you can return a pointer to an object. Refer to Mastering Delphi 6 and this description of Vtables in delphi.

DaveParillo
Question was about Delphi, not C or VB.
Lars D
OK, I can accept that. But the real problem here is return an object, which is not possible. The language the dll is written in is irrelevant.
DaveParillo
In Delphi, "return an object" means "return a pointer to the data structure that defines the object", and it is easily doable.
Lars D
So, how can I do that in Delphi?
Yulien
On a related note, this page -> http://pages.cs.wisc.edu/~rkennedy/vmt is a good, short explanation of Delhi's VTables.
DaveParillo
@Lars. OK. As long as your object has not been subclassed, the methods you want to use are vitual, and you've properly shadowed the Delphi Object in VB.
DaveParillo
@Yulien - The anser given by @MaD70 is a good start. Look at the example in his 'Edit' section, the Google Books link.
DaveParillo
There are differences between what one can return from a DLL, and what one should return from a DLL. Objects should never be returned from a DLL IMHO, as this requires the DLL to use the same memory manager as the host application to be on the safe side. Much better to create and destroy the object in the DLL, and return an opaque handle to the object from the DLL only, which is then used for all access.
mghie
Best advice on this comment thread yet.
DaveParillo
A: 

Objects are combinations of code and data. A normal DLL does not support that, because it only supports the C Application Binary Interface (C ABI) but there is a kind of DLLs that does: BPL files. In other words, you need to create a .bpl file and not a .dll file. This requires both that file and the user to be Delphi, of course.

Lars D
It also requires that the BPL developer and the host application are using the same version of Delphi.
DaveParillo
+4  A: 

Read this old thread on borland.public.delphi.nativeapi: Delphi Object in DLL - does this work?.

The link to a .pdf in the last message is gone, but thanks to Internet Archive Wayback Machine one can download it (look at Exporting Objects from DLLs at page 412).

Edit: it turns out that book's interesting part, for our purpose, is also available @ Google Books, so one can read it on-line.

MaD70
+2  A: 

Basically what you need to do is not return an object. In this case you want to return bitmap, why not just return HBitmap handle?

Darkerstar
Ok, and what if I have to return any other instace type???
Yulien
For all Windows resource objects, like Window handle, File handle, thread id, brush etc, you can pass the native handle. If you need to pass complex object written in Delphi. You can do that by passing the object pointer. However both of your host app and dll must use the same unit that declares the object, which kind of defeats the purpose of dll.
Darkerstar