Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Bitmap into DLL function in C++ and Delphi?

I want to create a DLL plugins to use with Delphi and other languages (mostly C++). How can I pass bitmaps in a C++ and Delphi-friendly way? Can it just be a handle to the Delphi TBitmap? C++ program should be able to decode it using WinApi, right?

like image 883
Tom Avatar asked Mar 25 '13 21:03

Tom


1 Answers

You cannot pass a Delphi TBitmap object since that is only meaningful to Delphi code. What you need to pass is an HBITMAP, a handle to a Windows bitmap.

The Delphi TBitmap class is just a wrapper around the Windows bitmap and can provide HBITMAP handles. The thing you need to watch out for is the ownership of those handles.

If you have a Delphi TBitmap you can get an HBITMAP by calling the ReleaseHandle method of a TBitmap. The handle returned by ReleaseHandle is no longer owned and managed by the TBitmap object which is exactly what you want. You pass that handle to the C++ code and let it become the owner. It is responsible for disposing of that handle.

The documentation for ReleaseHandle says:

Returns the handle to the bitmap so that the TBitmap object no longer knows about the handle.

Use ReleaseHandle to disassociate the bitmap from the bitmap handle. Use it when you need to give a bitmap handle to a routine or object that will assume ownership (or destroy) the bitmap handle.

In the other direction your Delphi code would receive an HBITMAP from the C++ code and take on ownership. Do that by assigning to the Handle property of a TBitmap instance.

The details will vary from language to language, but no matter what, all will be able to deal with an HBITMAP.

like image 155
David Heffernan Avatar answered Sep 19 '22 17:09

David Heffernan