call Pascal DLL functions from D

Oleg B code.viator at gmail.com
Wed Oct 25 03:12:56 UTC 2017


Hello. I have DLL written on Pascal and "headers" for use it 
(types and functions signatures definitions). How I can port 
"headers" to D?

Calling convention is `stdcall` (`extern (Windows)` for D), 
arguments of some functions is structs of structs and etc with 
static array fields:

```Pascal
type
   TOneHarmonic = record
     Koef: Single;
     Angle: Single;
   end;

   THarmonicsArray = array[2..50] of TOneHarmonic;
   TInterharmonicsArray = array[1..49] of TOneHarmonic;

   TVoltageCurrentOneFaza = record
     KalibKoef: Single;
     IncludeMainFreq: Boolean;
     MainFreqVoltCur: TOneHarmonic;
     Harmonics: THarmonicsArray;
     Interharmonics: TInterharmonicsArray;
   end;

   TVoltageCurrent = record
     Enable: Boolean;
     NominalID: Byte;
     Faza_A: TVoltageCurrentOneFaza;
     Faza_B: TVoltageCurrentOneFaza;
     Faza_C: TVoltageCurrentOneFaza;
   end;

   TSignal = record
     HasInterharmonics: Boolean;
     Voltage: TVoltageCurrent;
     Current: TVoltageCurrent;
     ...
   end;
   ...
```

in other file

```Pascal
function CheckSignalData(SignalData: PUserSignalData): Word;
   stdcall; external SIGNALK2MDLL;
```
PUserSignalData is pointer to TUserSignalData, first field of 
TUserSignalData is Signal, other fields is pointers to other types

If I understand correctly
1. `Single` is `float` in D, `Byte` is `byte`, `Boolean` is 
`bool`, `Word` is `ushort`
2. `array[A..B] of TFoo` is `TFoo[B-A+1]` (static array)

Can I rewrite records to structs like this?

```d
alias Word = short;
alias Single = float;
alias Byte = byte;
alias Boolean = bool;

struct OneHarmonic
{
     Single coef;
     Single angle;
}

alias HarmonicArray = OneHarmonic[49];

// TVoltageCurrentOneFaza
struct PhaseInfo
{
     // Must I align fields and/or structs?
     Single calibCoef;
     Boolean includeMainFreq;
     OneHarmonic mainFreqVoltCur;
     HarmonicArray harmonics; // Can I replace Pascal static 
arrays with D static arrays?
     HarmonicArray interharmonics;
}

// TVoltageCurrent
struct ChannelInfo
{
     Boolean enable;
     Byte nominalID;
     PhaseInfo[3] phase; // Can I replace 3 fields to static array?
}

struct Signal
{
     Boolean hasInterharmonics;
     ChannelInfo voltage;
     ChannelInfo current; // #1
     ...
}
```

In this case I have error about current values (#1) and I think I 
badly rewrite Pascal records to D structs (ChannelInfo, 
PhaseInfo, OneHarmonic) and lose alignments and/or something else.

PS original pascal naming is very bad, I know


More information about the Digitalmars-d-learn mailing list