Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a C function in a DLL with enum parameters from Delphi

Tags:

enums

dll

delphi

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?

like image 944
dommer Avatar asked Apr 15 '10 07:04

dommer


1 Answers

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 #included 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 ...
like image 84
PA. Avatar answered Sep 29 '22 08:09

PA.