我有一个用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 ...