How to pass Bitmap into DLL function in C++ and De

2019-06-28 07:38发布

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?

1条回答
▲ chillily
2楼-- · 2019-06-28 08:22

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.

查看更多
登录 后发表回答