Primus Primus - 3 months ago 27
C Question

Enum problems when loading 64bit c DLLs in 64bit Delphi

I'm making 64bit Delphi export programme which uses 64bit c DLLs. The problem I get is that c DLL doesn't recognise Delphi enums and returns error for the wrong data type. I've tried using {$packenums} or {$Z} directives to Delphi compiler but still same error was returned. I'm using xe8 Delphi. The enum looks like:

type
DDCDataType=(
DDC_notype = 0,
DDC_UInt8 = 5, // unsigned char
DDC_Int16 = 2, // short
DDC_Int32 = 3, // int
DDC_Float = 9, // float -> single
DDC_Double = 10, // double
DDC_String = 23 // string
);


in the c header looks like

typedef enum {
DDC_UInt8 = 5, // unsigned char
DDC_Int16 = 2, // short
DDC_Int32 = 3, // int
DDC_Float = 9, // float
DDC_Double = 10, // double
DDC_String = 23, // string
DDC_Timestamp = 30, // timestamp (Year/Month/Day/Hour/Minute/Second/Millisecond components)
} DDCDataType;


I hope this makes sense :)
Thanks!

Answer

In the C code, DDCDataType is simply an int. Which means it has size 4. In your Delphi code, with default compiler settings, the enumerated type has size 1. You should use {$Z4} to match the C code.

You probably don't want to use {$Z4} throughout your project, so you should just place it in your Delphi interop unit. The unit that defines the types and functions that are imported. If you have mixed up that interop code with your other application logic, take the chance now to maintain a clear separation.

You haven't translated the type faithfully though. You added a value, and missed a value. It should be:

{$Z4}
type  
  DDCDataType=(
    DDC_UInt8 = 5,      // unsigned char
    DDC_Int16 = 2,      // short
    DDC_Int32 = 3,      // int
    DDC_Float = 9,      // float
    DDC_Double = 10,    // double
    DDC_String = 23,    // string
    DDC_Timestamp = 30, // timestamp (Year/Month/Day/Hour/Minute/Second/Millisecond components)
  );

With that declaration you can be confident that this type matches your C code exactly. There are quite possibly other problems in your program. If the rest of your program fails, please don't be surprised. I've just answered the question that you have asked here.

To be completely clear, if you use the declaration above, and your program still fails, then you know to look elsewhere for the cause.

Comments