Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding a DLL in Delphi for calling from other languages

This will be my first attempt to code a DLL in Delphi for calling from other languages.

I have done a lot of googling and see a few good pointers, but nothing exhaustive (at least, I have questions that don't find answered in the Embarcadero material).

I wonder if this list of caveats is exhhaustive, or if I missed anything, and if anyone can anser my questions below.

  • if I want my DLL to be callable from other languages, then i can't use the ShareMem unit.
  • I can't use the String type and should stick with PChar
  • if I want to return a string, the caller should pass me a buffer to write it to (I should not alloctae the memory for it myself, even if I provide a routine to free it afterwards)
  • I should stick to simple types like integetr and PChar (any others?)

A few questions:

  • the comipler forbids me to export enums and constants. Am I just not declaring them correctly? I would like calling Delphi s/w to be able to use the enum elements and all callers to be able to use constants.
  • I should not use any structures because of possible differences in byte alignment between compilers. So, if I can't accept strctures as parameters. I guess I should just have a long parameter list of integers and PChars?
  • can I accept arrays as parameters, or does boundary alignment make that dangerous?
  • can I accept/return floats/doubles ?
  • booleans? Or am I stuck with "zer0 === false and all else is true"?

  • is there anything else I should know?

Thanks in advance for any help

like image 984
Mawg says reinstate Monica Avatar asked Jan 18 '26 19:01

Mawg says reinstate Monica


1 Answers

The compiler forbids me to export enums and constants. Am I just not declaring them correctly? I would like calling Delphi s/w to be able to use the enum elements and all callers to be able to use constants.

You cannot export enumerated types and constants. The caller will have to declare them again. The same goes for types.

I should not use any structures because of possible differences in byte alignment between compilers.

Feel free to use records. Alignment is standardised across compilers on the same platform. Do make sure that you use a modern Delphi, XE2 or later, that follows the platform standard for alignment correctly.

So, if I can't accept strctures as parameters. I guess I should just have a long parameter list of integers and PChars?

You can receive records as parameters.

Can I accept arrays as parameters, or does boundary alignment make that dangerous?

You can accept arrays. You need to receive a pointer to the first element, and the number of elements. Array elements are always layed out immediately following the previous elements.

Can I accept/return floats/doubles? Booleans? Or am I stuck with "zero === false and all else is true"?

Yes you can use floating point types and booleans.

Is there anything else I should know?

Almost certainly.

I'll mention one more issue, but there surely are more. Don't use records as return types to functions. Delphi does not follow the platform standard in its handling of function return types for records. Use an out parameter instead.


A good way to learn by example what can be done is to study the Windows API.

like image 139
David Heffernan Avatar answered Jan 20 '26 16:01

David Heffernan