使用Delphi中的enum参数调用DLL中的C函数

dom*_*mer 4 delphi dll enums

我有一个用C语言编写的第三方(Win32)DLL,它公开了以下接口:

DLL_EXPORT typedef enum
{
  DEVICE_PCI = 1,
  DEVICE_USB = 2
} DeviceType;

DLL_EXPORT int DeviceStatus(DeviceType kind);
Run Code Online (Sandbox Code Playgroud)

我希望从德尔福那里得到它.

如何在Delphi代码中访问DeviceType常量?或者,如果我应该直接使用值1和2,我应该使用什么Delphi类型的"DeviceType类型"参数?整数?字?

PA.*_*PA. 6

在C中从外部DLL声明接口的常用方法是在.H头文件中公开其接口.然后,要从C访问DLL,.H头文件必须是#includeC源代码中的d.

转换为Delphi术语,您需要创建一个以pascal术语描述相同接口的单元文件,将c语法转换为pascal.

对于您的情况,您将创建一个文件,如...

unit xyzDevice;
{ XYZ device Delphi interface unit 
  translated from xyz.h by xxxxx --  Copyright (c) 2009 xxxxx
  Delphi API to libXYZ - The Free XYZ device library --- Copyright (C) 2006 yyyyy  }

interface

type
  TXyzDeviceType = integer;

const
  xyzDll = 'xyz.dll';
  XYZ_DEVICE_PCI = 1;
  XYZ_DEVICE_USB = 2;

function XyzDeviceStatus ( kind : TXyzDeviceType ) : integer; stdcall; 
   external xyzDLL; name 'DeviceStatus';

implementation
end.
Run Code Online (Sandbox Code Playgroud)

您可以在uses源代码的子句中声明它.并以这种方式调用函数:

uses xyzDevice;

...

  case XyzDeviceStatus(XYZ_DEVICE_USB) of ...
Run Code Online (Sandbox Code Playgroud)