Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.335 Beiträge
 
Delphi 12 Athens
 
#7

AW: Problem mit Aufruf einer Funktion in externen dll

  Alt 14. Aug 2011, 06:44
Man kann das Ganze jetzt noch etwas delphitypischer anpssen.

Delphi-Quellcode:
interface

type
  MatrixDisplay = record
    const Matrix = 'matrix.dll';
    class function Init(SizeX, SizeY: Byte; S: AnsiString; var OK: Boolean): PAnsiChar; stdcall; static;
    class function Done: PAnsiChar; stdcall; static;
    class procedure Write(Str: AnsiString); stdcall; static;
    class function DefaultParameters: PAnsiChar; stdcall; static;
    class function SetPosition(x, y: Byte): PAnsiChar; stdcall; static;
    class procedure SetBrightness(Brightness: Byte); stdcall; static;
    class procedure SetContrast(Contrast: Byte); stdcall; static;
    class procedure SetGPO(GPO: Byte; GPOOn: Boolean); stdcall; static;
    class procedure SetFan(T1, T2: Byte); stdcall; static;
    class procedure SetBacklight(LightOn: Boolean); stdcall; static;
    class procedure CustomChar(Chr: Byte; Data: TCustomArray); stdcall; static;
  end;

implementation

class function MatrixDisplay.Init; external MatrixDisplay.Matrix name 'DISPLAYDLL_Init';
class function MatrixDisplay.Done; external MatrixDisplay.Matrix name 'DISPLAYDLL_Done';
class procedure MatrixDisplay.Write; external MatrixDisplay.Matrix name 'DISPLAYDLL_Write';
class function MatrixDisplay.DefaultParameters; external MatrixDisplay.Matrix name 'DISPLAYDLL_DefaultParameters';
class function MatrixDisplay.SetPosition; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetPosition';
class procedure MatrixDisplay.SetBrightness; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetBrightness';
class procedure MatrixDisplay.SetContrast; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetContrast';
class procedure MatrixDisplay.SetGPO; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetGPO';
class procedure MatrixDisplay.SetFan; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetFan';
class procedure MatrixDisplay.SetBacklight; external MatrixDisplay.Matrix name 'DISPLAYDLL_SetBacklight';
class procedure MatrixDisplay.CustomChar; external MatrixDisplay.Matrix name 'DISPLAYDLL_CustomChar';
Und nochmal, wegen dem immernoch defekten Delphi-Tags:
Code:
interface

type
  MatrixDisplay = record
    const Matrix = 'matrix.dll';
    class function  Init(SizeX, SizeY: Byte; S: AnsiString; var OK: Boolean): PAnsiChar; stdcall; static;
    class function  Done: PAnsiChar; stdcall; static;
    class procedure Write(Str: AnsiString); stdcall; static;
    class function  DefaultParameters: PAnsiChar; stdcall; static;
    class function  SetPosition(x, y: Byte): PAnsiChar; stdcall; static;
    class procedure SetBrightness(Brightness: Byte); stdcall; static;
    class procedure SetContrast(Contrast: Byte); stdcall; static;
    class procedure SetGPO(GPO: Byte; GPOOn: Boolean); stdcall; static;
    class procedure SetFan(T1, T2: Byte); stdcall; static;
    class procedure SetBacklight(LightOn: Boolean); stdcall; static;
    class procedure CustomChar(Chr: Byte; Data: TCustomArray); stdcall; static;
  end;

implementation

class function  MatrixDisplay.Init;              external MatrixDisplay.Matrix name 'DISPLAYDLL_Init';
class function  MatrixDisplay.Done;              external MatrixDisplay.Matrix name 'DISPLAYDLL_Done';
class procedure MatrixDisplay.Write;             external MatrixDisplay.Matrix name 'DISPLAYDLL_Write';
class function  MatrixDisplay.DefaultParameters; external MatrixDisplay.Matrix name 'DISPLAYDLL_DefaultParameters';
class function  MatrixDisplay.SetPosition;       external MatrixDisplay.Matrix name 'DISPLAYDLL_SetPosition';
class procedure MatrixDisplay.SetBrightness;     external MatrixDisplay.Matrix name 'DISPLAYDLL_SetBrightness';
class procedure MatrixDisplay.SetContrast;       external MatrixDisplay.Matrix name 'DISPLAYDLL_SetContrast';
class procedure MatrixDisplay.SetGPO;            external MatrixDisplay.Matrix name 'DISPLAYDLL_SetGPO';
class procedure MatrixDisplay.SetFan;            external MatrixDisplay.Matrix name 'DISPLAYDLL_SetFan';
class procedure MatrixDisplay.SetBacklight;      external MatrixDisplay.Matrix name 'DISPLAYDLL_SetBacklight';
class procedure MatrixDisplay.CustomChar;        external MatrixDisplay.Matrix name 'DISPLAYDLL_CustomChar';
Statt record könnte man auch class
PAnsiChar als Result wird automatisch von Delphi in einen String convertiert, wenn man es an einen String zuweisen will
und der AnsiString-Parameter wird quasi auch automatisch konvertiert, denn der String-Typ ist PChar-kompatibel aufgebaut.
PBoolean ist eine Referenz auf einen Boolean, was ein Var-Parameter ebenfalls ist.


Beim Boolean mußt du aufpassen.
Wie ist der denn im Original deklariert?

Boolean, bzw. ByteBool = 1 Byte
LongBool = 4 Byte (Integer)
und das BOOL in C++ ist ein LongBool im Delphi.



Was ist denn TCustomArray?
Delphi-Quellcode:
MatrixDisplay.Init(...);

// ********

type
  MD = MatrixDisplay;

MD.Init(...);

// ********

var
  MD: MatrixDisplay; // ist 0 Byte groß uns läßt sich sehr schön als lokale Variable nutzen (records sind toll)

MD.Init(...);
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (14. Aug 2011 um 06:57 Uhr)
  Mit Zitat antworten Zitat