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

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

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

DLL_EXPORT int DeviceStatus(DeviceType kind);

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

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

解决方法

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

转换为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.

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

uses xyzDevice;

...

  case XyzDeviceStatus(XYZ_DEVICE_USB) of ...

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...