简体   繁体   English

在64位Delphi中加载64位c DLL时出现枚举问题

[英]Enum problems when loading 64bit c DLLs in 64bit Delphi

I'm making 64bit Delphi export programme which uses 64bit c DLLs. 我正在制作使用64位c DLL的64位Delphi导出程序。 The problem I get is that c DLL doesn't recognise Delphi enums and returns error for the wrong data type. 我得到的问题是c DLL无法识别Delphi枚举,并为错误的数据类型返回错误。 I've tried using {$packenums} or {$Z} directives to Delphi compiler but still same error was returned. 我尝试对Delphi编译器使用{$ packenums}或{$ Z}指令,但仍然返回相同的错误。 I'm using xe8 Delphi. 我正在使用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 在C标头中看起来像

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! 我希望这是有道理的:)谢谢!

In the C code, DDCDataType is simply an int . 在C代码中, DDCDataType只是一个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. 这意味着它的大小为4。在您的Delphi代码中,使用默认的编译器设置,枚举类型的大小为1。您应该使用{$Z4}来匹配C代码。

You probably don't want to use {$Z4} throughout your project, so you should just place it in your Delphi interop unit. 您可能不想在整个项目中使用{$Z4} ,因此应将其放在Delphi interop单元中。 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. 使用该声明,您可以确信此类型与您的C代码完全匹配。 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. 要完全清楚,如果您使用上面的声明,而您的程序仍然失败,那么您知道在其他地方查找原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM