Why function do not work correctly in Delphi WinApi?

Good day. The task is for Delphi to "Find the interval [a,b] the number having the largest number of divisors". Need to do all this with dynamic and static loading of the DLL libraries.
On the page of the DLL is a function that calculates the divisors. In the page unit and this function is called.
Now the result is displayed wrong:
2becc193cf674147998e57c37c5780ed.jpg

In the interval 1..10 should be displayed: number of divisors = 3 are the numbers: 6 8 10.

What reason can not understand. The function code is sort of correct, tested separately.
With Delphi work recently, so need help.

Page DLL:

library PDLL;

uses
 ShareMem, // Plug-in ShareMem
myArray,
SysUtils,
Dialogs,
Classes
Math;
{$R *.res}

// stdcall - used if call Winapi f-iy (passing parameters from right to left)
// Output:
// result - returns the result

getDividers function(n:integer):integer;stdcall;//function to calculate divisors
 var i,k:integer;
begin
 k:=1;//while divisor is 1, the number
 for i:=1 to trunc(sqrt(n))do//from 1 to the number of Crna(then it makes no sense to check)
 if n mod i=0 then inc(k,1);//if the given number is divisible by another, we add
 if frac(sqrt(n))=0 then dec(k);
Result:=k;
end;

// Export function
exports getDividers;

begin
end.


Code from the page unit a:

unit Lab2Dynamic;
interface
uses
 {my_list,} Windows, Messages, SysUtils,
 Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Math;

type
 TForm1 = class(TForm)
 btnOpenDlgModal: TButton;
 lblAverageValue: TLabel;
 lblAverageValue2: TLabel;
 Edit1: TEdit;
 Edit2: TEdit;
 Label1: TLabel;
 Label2: TLabel;
 btnOpenDlgModalClick procedure(Sender: TObject);
private
 { Private declarations }
public
 { Public declarations }
end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

// stdcall - used if call Winapi f-iy (passing parameters from right to left)
getDividers function(var p:integer):integer; stdcall;
external '..\Lab3Dll\PDLL.dll';

procedure TForm1.btnOpenDlgModalClick(Sender: TObject);
var
 getDividers: function(var p:integer):integer; stdcall;
 // Handle to dll file
 hPDLL, hPDlgModalDLL: THandle;
m,n,i:integer;
mx:integer;
begin
//try
 hPDLL := LoadLibrary('..\Lab3Dll\PDLL.dll');
 // If library loaded
 if hPDLL <> 0 then begin
 // Loaded function address
 @getDividers := GetProcAddress(hPDLL, 'getDividers');
 // If the function ShowModaldialog is present in PDlgModalDLL.dll
 if Addr(getDividers) <> nil then begin
 m:= StrToInt(Form1.Edit1.Text);
 n:= StrToInt(Form1.Edit2.Text);
 mx:=1;//while the maximum number of divisors=1
 for i:=m to n do//go through the interval
 if getDividers(i)>mx then mx:=getDividers(i);//looking for much as there are divisors of these numbers
 lblAverageValue.Caption := 'the Maximum number of divisors =' + IntToStr(mx) + ' have the numbers:';
 for i:=m to n do//then go through the interval
 if getDividers(i)=mx then
 lblAverageValue2.Caption := lblAverageValue2.Caption + ' '+IntToStr(i);
end;

end;

{finally
 FreeLibrary(hPDlgModalDLL); // Unload dll
end;}
//finally

 // end;
end;

end.
July 2nd 19 at 13:50
1 answer
July 2nd 19 at 13:52
And you are importing is not correct, remove var.
// stdcall - used if call Winapi f-iy (passing parameters from right to left)
function getDividers(<b>var </b>p:integer):integer; stdcall;
external '..\Lab3Dll\PDLL.dll';

Find more questions by tags ProgrammingDelphi