tags:

views:

113

answers:

3

Hi quick question. I want to implement a function in a dll that accepts a record as a parameter and this record as a few fields that hold pointers to callback routines. Would this be safe?

+3  A: 

I don't see why not. I think all the usual issues with procedure/method pointers apply, so the object needs to exist if it's a method pointer.

Cade Roux
A: 

It is safe but there are two issues that you should be aware about :

Records that declared as local variables are stored in the stack and they go away when the function returns. You should consider to allocate/dispose them on the heap with new/dispose functions.

If the DLL will be used by a program developed in other than delphi (or maybe even different versions of delpi), you have to use packed records.

AhmetC
You don't necessarily have to use packed records, but you do need to make sure you pay some attention to field alignment. That's not generally an issue when the members are all pointers.
Rob Kennedy
Yes it is not mandantory but we don't know exact structure of the record he has.
AhmetC
+3  A: 

Yes, it's perfectly safe to have a pointer to a record that holds other pointers.

Your title mentions methods, though. DLLs rarely request method pointers because method pointers only exist in Delphi and C++ Builder. DLLs written expecting other tools to be able to use them will request ordinary function pointers, so please beware that method pointers are not compatible with pointers to standalone subroutines. The compiler will usually balk if you try to mix them, but type-casting can quell that error message. As a rule of thumb, if you're type-casting a function pointer or method pointer, you're doing something wrong. If your type declarations and your function declarations are correct, you won't need to type-cast.

Likewise, if you're using the @ operator to create a function pointer or method pointer, you're probably doing it wrong. In most cases, the compiler can detect and assign compatible code pointers automatically, without you telling it that there's a pointer. Using @ may suppress some of Delphi's type checking.

Rob Kennedy
Ok so I've got to pay special attention to the use of the @ symbol when creating the pointer to the function or method. Thanks.
yozey