call Pascal DLL functions from D

Basile B. b2.temp at bb.temp.ll
Wed Oct 25 04:30:12 UTC 2017


On Wednesday, 25 October 2017 at 03:12:56 UTC, Oleg B wrote:
> 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`

No Pascal's "Byte" is D's "ubyte". But unless NominalID goes over 
127 this is not the source of the problem.

> 2. `array[A..B] of TFoo` is `TFoo[B-A+1]` (static array)

No A-B. In Pascal the upper bound of a range (like here but i'm 
not sure this i called like that in the grammar) or of a slice is 
inclusive.

>
> 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];

48 !

> // TVoltageCurrentOneFaza
> struct PhaseInfo
> {
>     // Must I align fields and/or structs?

No, unless the record is packed.

>     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?

yes

> }
>
> 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

Most likely the problem is the array length.


More information about the Digitalmars-d-learn mailing list