I have a third-party (Win32) DLL, written in C, that exposes the following interface:
DLL_EXPORT typedef enum
{
DEVICE_PCI = 1,
DEVICE_USB = 2
} DeviceType;
DLL_EXPORT int DeviceStatus(DeviceType kind);
I wish to call it from Delphi.
How do I get access to the DeviceType constants in my Delphi code? Or, if I should just use the values 1 and 2 directly, what Delphi type should I use for the "DeviceType kind" parameters? Integer? Word?
The usual way to declare the interface from an external DLL in C is to expose its interface in a .H header file. Then, to access the DLL from C the .H header file has to be #include
d in the C source code.
Translated to Delphi terms, you need to create a unit file that describes the same interface in pascal terms, translating the c syntax to pascal.
For your case, you would create a file such as...
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.
And the you would declare it in the uses
clause of your source code. And invoke the function this way:
uses xyzDevice;
...
case XyzDeviceStatus(XYZ_DEVICE_USB) of ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With