[Delphi][DLL] Invalid floating point operation.

0

Pisze program do wizualizacji ruchu wahadla matematycznego.
Wszystkie funkcje powinny byc w pliku dll.
Gdy znajduja sie w programie wszystko jest ok ale jak przezuce je do dll
wywala blad: Invalid floating point operation.

To jest kod dll:

library matma;

uses
  Windows;

{$R *.res}

const pi=3.14159;g=9.81;

function Okres(l : real) : real;
{
  Liczy okres wahadla.
  We:         l - dlugosc wahadla
  wy:         Okres wahadla matematycznego
}
begin
  Okres := 2*pi*sqrt(l/g);
end;

//zamienia real na stringa
function tostr(a:real):string;
var
    stri:string;
begin
str(a:0:5,stri);
tostr:=stri;
end;

//oblicza wspolzedne x
function xx(al0,tt,l:real):real;
var ok,alf:real;
begin
ok:=2*pi*sqrt(l/g);
alf:=al0*cos(2*pi*tt/ok);
xx:=l*sin(pi*alf/180);
end;

//oblicza wspolzedne y
function yy(al0,tt,l:real):real;
var ok,alf:real;
begin
ok:=2*pi*sqrt(l/g);
alf:=al0*cos(2*pi*tt/ok);
yy:=l*cos(pi*alf/180);
end;


//deklaracja zewnetrznych funkcji
exports
yy,
xx,
okres,
tostr;

begin
end.

A to programu:

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    el: TEdit;
    en: TEdit;
    Label1: TLabel;
    Label6: TLabel;
    wah: TImage;
    Button1: TButton;
    lokres: TLabel;
    timer: TTimer;
    ealf0: TEdit;
    Label2: TLabel;
    Button2: TButton;
    lxx: TLabel;
    lyy: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure timerTimer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const pi=3.14159;g=9.81;

var
  Form1: TForm1;
  l,n,max:real;
  blad,k:integer;
  alf,x,y,xp,yp,t   :real;
  alf0,ok       :real;

implementation

uses inf;

{$R *.dfm}

function Okres(l : real) : real;  stdcall external 'matma.dll';
function yy(al0,tt,l:real):real;  stdcall external 'matma.dll';
function xx(al0,tt,l:real):real;  stdcall external 'matma.dll';
function tostr(a:real):string;  stdcall external 'matma.dll';






procedure TForm1.Button1Click(Sender: TObject);
var okr:string;
begin
//start przycisk
t:=0;
val(el.Text,l,blad);
val(en.Text,n,blad);
val(ealf0.Text,alf0,blad);
str(okres(l):5:5,okr);
lokres.Caption:='T: '+okr;
timer.Enabled:=true;
xp:=l*sin(alf0*pi/180);
yp:=l*cos(alf0*pi/180);
wah.Canvas.moveto(320,10,);
wah.Canvas.lineto(320+round(400*xp),10+round(400*yp));
wah.Canvas.Ellipse(320+round(400*xp)-10,10+round(400*yp)-10,320+round(400*xp)+10,10+round(400*yp)+10);
end;

procedure TForm1.timerTimer(Sender: TObject);
begin
//timer
wah.Canvas.Pen.Color:=65535;
wah.Canvas.Pen.Style := psSolid;
wah.Canvas.Rectangle(0,0,639,479);
 if t<=ok then
  begin
lxx.Caption:='X:'+tostr(xx(alf0,t,l));
lyy.Caption:='Y:'+tostr(yy(alf0,t,l));
wah.Canvas.Pen.Color:=0;
wah.Canvas.moveto(320,10,);
wah.Canvas.lineto(320+round(400*xx(alf0,t,l)),10+round(400*yy(alf0,t,l)));
wah.Canvas.Ellipse(320+round(400*xx(alf0,t,l))-10,10+round(400*yy(alf0,t,l))-10,320+round(400*xx(alf0,t,l))+10,10+round(400*yy(alf0,t,l))+10);
  t:=t+ok/n;
  end;
   if(t>=ok) then t:=0;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
inf.Form2.Show;
end;

end.

Z gory dziekuje za odpowiedz.

0

może się myle ale w deklaracjach funkcji w dll powinno być stdcall
np. function Okres(l : real) : real;stdcall;

0

oczywiście info od Borlanda postanowiłeś olać z góry na dół

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

poza tym jak pisał bwf stdcall zgubiłeś w dll - w projekcie masz

function Okres(l : real) : real;  stdcall external 'matma.dll';
function yy(al0,tt,l:real):real;  stdcall external 'matma.dll';
function xx(al0,tt,l:real):real;  stdcall external 'matma.dll';
function tostr(a:real):string;  stdcall external 'matma.dll';

a w dll nie

0

Wielkie dzieki. Już dziala.
A jakby ktos potrzebowal tego kodu to prosze:

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    el: TEdit;
    en: TEdit;
    Label1: TLabel;
    Label6: TLabel;
    wah: TImage;
    Button1: TButton;
    lokres: TLabel;
    timer: TTimer;
    ealf0: TEdit;
    Label2: TLabel;
    Button2: TButton;
    lxx: TLabel;
    lyy: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure timerTimer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const pi=3.14159;g=9.81;

var
  Form1: TForm1;
  l,n,max:real;
  blad,k:integer;
  alf,x,y,xp,yp,t   :real;
  alf0,ok       :real;

implementation

uses inf;

{$R *.dfm}

function Okres(l : real) : real;  stdcall external 'matma.dll';
function yy(al0,tt,l:real):real;  stdcall external 'matma.dll';
function xx(al0,tt,l:real):real;  stdcall external 'matma.dll';
function tostr(a:real):string;  stdcall external 'matma.dll';






procedure TForm1.Button1Click(Sender: TObject);
var okr:string;
begin
//start przycisk
t:=0;
val(el.Text,l,blad);
val(en.Text,n,blad);
val(ealf0.Text,alf0,blad);
str(okres(l):5:5,okr);
lokres.Caption:='T: '+okr;
timer.Enabled:=true;
ok:=okres(l);
xp:=l*sin(alf0*pi/180);
yp:=l*cos(alf0*pi/180);
wah.Canvas.moveto(320,10,);
wah.Canvas.lineto(320+round(400*xp),10+round(400*yp));
wah.Canvas.Ellipse(320+round(400*xp)-10,10+round(400*yp)-10,320+round(400*xp)+10,10+round(400*yp)+10);
end;

procedure TForm1.timerTimer(Sender: TObject);
begin
//timer
wah.Canvas.Pen.Color:=65535;
wah.Canvas.Pen.Style := psSolid;
wah.Canvas.Rectangle(0,0,639,479);
 if t<=ok then
  begin
lxx.Caption:='X:'+tostr(xx(alf0,t,l));
lyy.Caption:='Y:'+tostr(yy(alf0,t,l));
wah.Canvas.Pen.Color:=0;
wah.Canvas.moveto(320,10,);
wah.Canvas.lineto(320+round(400*xx(alf0,t,l)),10+round(400*yy(alf0,t,l)));
wah.Canvas.Ellipse(320+round(400*xx(alf0,t,l))-10,10+round(400*yy(alf0,t,l))-10,320+round(400*xx(alf0,t,l))+10,10+round(400*yy(alf0,t,l))+10);
  t:=t+ok/n;
  end;
   if(t>=ok) then t:=0;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
inf.Form2.Show;
end;

end.
program wahadlo;

uses
  Sharemem,
  Forms,
  main in 'main.pas' {Form1},
  inf in 'inf.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
library matma;

uses
  Sharemem,Windows;

{$R *.res}

const pi=3.14159;g=9.81;

function Okres(l : real) : real;  stdcall;
{
  Liczy okres wahadla.
  We:         l - dlugosc wahadla
  wy:         Okres wahadla matematycznego
}
begin
  Okres := 2*pi*sqrt(l/g);
end;

//zamienia real na stringa
function tostr(a:real):string;  stdcall;
var
    stri:string;
begin
str(a:0:5,stri);
tostr:=stri;
end;

//oblicza wspolzedne x
function xx(al0,tt,l:real):real;  stdcall;
var ok,alf:real;
begin
ok:=2*pi*sqrt(l/g);
alf:=al0*cos(2*pi*tt/ok);
xx:=l*sin(pi*alf/180);
end;

//oblicza wspolzedne y
function yy(al0,tt,l:real):real;  stdcall;
var ok,alf:real;
begin
ok:=2*pi*sqrt(l/g);
alf:=al0*cos(2*pi*tt/ok);
yy:=l*cos(pi*alf/180);
end;


//deklaracja zewnetrznych funkcji
exports
yy,
xx,
okres,
tostr;

begin
end.

1 użytkowników online, w tym zalogowanych: 0, gości: 1