Recent

Author Topic: I have created dll in golang want to use inside lazarus freepascal  (Read 1527 times)

Packs

  • Sr. Member
  • ****
  • Posts: 402
Code: C  [Select][+][-]
  1. package main
  2.  
  3. import "C"
  4. import (
  5.         "fmt"
  6.         "math"
  7. )
  8.  
  9.  
  10. // export RoundFloat
  11. func RoundFloat(val float64, precision uint) float64 {
  12.         ratio := math.Pow(10, float64(precision))
  13.         return math.Round(val*ratio) / ratio
  14. }
  15.  
  16.  
  17. func main() {
  18. }
  19.  
  20.  

this is the command for compile the code in golang
go build -o easy.dll -buildmode=c-shared main.go

I would like to use the dll inside the lazarus free pascal

« Last Edit: December 11, 2024, 07:56:20 am by Packs »

Zvoni

  • Hero Member
  • *****
  • Posts: 2792
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #1 on: December 05, 2024, 12:42:29 pm »
What'S the calling convention? cdecl? Something else?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #2 on: December 05, 2024, 12:48:48 pm »
in line 10 you have: // export RoundFloat
you need to remove the space between // and export otherwise it will not export the function

srvaldez

  • Full Member
  • ***
  • Posts: 117
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #3 on: December 05, 2024, 01:09:43 pm »
example
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$h+}
  2.  
  3. program easy_dll_test;
  4.  
  5. function RoundFloat(x:double; prec:integer): double; cdecl; external 'easy' name 'RoundFloat';
  6.  
  7. var
  8.         x, y:double;
  9.         prec:integer;
  10.        
  11. begin
  12.         x:=3.1415926535897932;
  13.         prec:=4;
  14.         y:=RoundFloat(x, prec);
  15.         writeln(y);
  16. end.
  17.  

output: 3.1415999999999999E+000

Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #4 on: December 05, 2024, 01:11:27 pm »
Thank you .

It is working

function abcRoundFloat(val: Double; precision: UInt8) :Double ;  external 'easy.dll' name 'RoundFloat';

Y := abcRoundFloat(12.345,2); 

Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #5 on: December 11, 2024, 07:58:20 am »
Code: Pascal  [Select][+][-]
  1.  
  2. //export HelloWorld
  3. func HelloWorld(sString *C.char) *C.char {
  4.         xyz := C.GoString(sString)
  5.         return C.CString("Hello " + xyz)
  6. }
  7.  
  8.  

I want my function should return me string value .

Code: Pascal  [Select][+][-]
  1.  
  2. function abcHelloWorld(xyz: string): string;
  3.   external 'easy.dll' Name 'HelloWorld';
  4.  
  5.   xyz := abcHelloWorld('abcd');
  6.   DebugLn('hello world : ' + xyz);
  7.  

it is not returning string value .



cdbc

  • Hero Member
  • *****
  • Posts: 1756
    • http://www.cdbc.dk
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #6 on: December 11, 2024, 08:09:42 am »
Hi
Try this:
Code: Pascal  [Select][+][-]
  1. function HelloWorld(xyz: pchar): pchar;
  2.   external 'easy.dll' Name 'HelloWorld';
  3.  
  4.   xyz := string(HelloWorld(pchar('abcd')));
  5.   DebugLn('hello world : ' + xyz);
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #7 on: December 11, 2024, 10:20:12 am »
Thank you Sir . it is working .

Code: Pascal  [Select][+][-]
  1. func Overallcgpa_Array(arr_data []float64, arr_size int, config_point int) *C.char {
  2.  
  3.  
  4.         var (
  5.                 i       int
  6.                 Totalcg float64
  7.         )
  8.         myRoundFloat := func(val float64, precision uint) float64 {
  9.                 //https://gosamples.dev/round-float/
  10.  
  11.                 ratio := math.Pow(10, float64(precision))
  12.  
  13.                 return math.Round(val*ratio) / ratio
  14.         }
  15.  
  16.         for i = 0; i < arr_size-1; i++ {
  17.                 Totalcg += arr_data[i]
  18.         }
  19.  
  20.  
  21.         cgpa := myRoundFloat(Totalcg/float64(config_point), 2)
  22.  
  23.         scgpa := fmt.Sprintf("%.2f", cgpa)
  24.  
  25.  
  26.         return C.CString(scgpa)
  27. }
  28.  

Code: Pascal  [Select][+][-]
  1. function Overallcgpa_Array(Array_data: array of real; array_size: integer;
  2.   precision: int64): PChar; external 'easy.dll' Name 'Overallcgpa_Array';
  3.  
  4.   SArray[1] := 8.42;
  5.   SArray[2] := 9.25;
  6.   SArray[3] := 9.17;
  7.   SArray[4] := 9.83;
  8.   SArray[5] := 0.0;
  9.   SArray[6] := 0.0;
  10.  
  11.   xyz := string(Overallcgpa_Array(SArray, 6, nConfigPoint));
  12.   DebugLn('Overallcgpa_array : ' + xyz);
  13.  
  14.  
  15.  

what is the issue in my code . it is giving me access violoation error

bytebites

  • Hero Member
  • *****
  • Posts: 694
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #8 on: December 11, 2024, 10:40:21 am »
array of double, int, int  versus array of real, int, int64

cdbc

  • Hero Member
  • *****
  • Posts: 1756
    • http://www.cdbc.dk
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #9 on: December 11, 2024, 11:12:06 am »
Hi
...and maybe (building on bytebites) call it like this:
Code: Pascal  [Select][+][-]
  1. xyz := string(Overallcgpa_Array(SArray[1], 6, nConfigPoint));
  2.   DebugLn('Overallcgpa_array : ' + xyz);
To me, it seems to be the array-data and not the array-pointer... the 'go' func accepts  %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11984
  • FPC developer.
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #10 on: December 11, 2024, 11:22:17 am »
Is it a C array (a pointer) or a Pascal open array?


Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #11 on: December 11, 2024, 02:28:50 pm »
Dear Team,

Please guide me what is mistake in my code

Code: Pascal  [Select][+][-]
  1.  
  2.           DebugLn('Table Value ---------------- ');
  3.           DebugLn('first ' + floattostr(vt_column.FieldByName('mul_sgpa_ce1').AsFloat));
  4.           DebugLn('second ' + floattostr(vt_column.FieldByName('mul_sgpa_ce2').AsFloat));
  5.           DebugLn('third ' + floattostr(vt_column.FieldByName('mul_sgpa_ce3').AsFloat));
  6.           DebugLn('four ' + floattostr(vt_column.FieldByName('mul_sgpa_ce4').AsFloat));
  7.                  
  8.         nConfigPoint := MyQ_batchexam.FieldByName('Overallcreditpoints').AsInteger;
  9.         DebugLn('nConfigPoint ' + floattostr(nConfigPoint));
  10.  
  11.  
  12.         DebugLn('forumla ' + '(Fsgpa_tot / nConfigPoint)');
  13.         Fcgpa_round := (Fsgpa_tot / nConfigPoint);
  14.         DebugLn('forumla value ' + floattostr(Fcgpa_round));
  15.         //DebugLn(Pasfilename + 'Fcgpa_round ' + floattostr(Fcgpa_round));
  16.  
  17.         DebugLn('golang round founction : ' + 'RoundFloat(Fcgpa_round, 2)');
  18.         Round_value := RoundFloat(Fcgpa_round, 2);
  19.         DebugLn('Golang round ' + floattostr(Round_value));
  20.  
  21.         DebugLn('Hard Coded values ------------------ ' );
  22.         nConfigPoint := 96 ;
  23.         DebugLn('nConfigPoint : ' + FloatToStr(nConfigPoint));
  24.  
  25.         Fsgpa_tot := 211.92 + 211.92 + 216 + 240;
  26.         DebugLn('Hard coded Total value : ' + FloatToStr(Fsgpa_tot));
  27.  
  28.         DebugLn('forumla ' + '(Fsgpa_tot / nConfigPoint)');
  29.         Fcgpa_round := (Fsgpa_tot / nConfigPoint);
  30.         DebugLn('Hard coded  Fcgpa_round : ' + FloatToStr(Fcgpa_round));
  31.  
  32.         DebugLn('golang round founction : ' + 'RoundFloat(Fcgpa_round, 2)');
  33.         Round_value := RoundFloat(Fcgpa_round, 2);
  34.         DebugLn('Golang round : ' + FloatToStr(Round_value));
  35.  
  36.  


Code: Text  [Select][+][-]
  1. Table Value ----------------
  2. first 211.92
  3. second 211.92
  4. third 216
  5. four 240
  6. Total 879.84
  7. nConfigPoint 96
  8. forumla (Fsgpa_tot / nConfigPoint)
  9. forumla value 9.165
  10. golang round founction : RoundFloat(Fcgpa_round, 2)
  11. Golang round 9.17
  12. Hard Coded values ------------------
  13. nConfigPoint : 96
  14. Hard coded Total value : 879.84
  15. forumla (Fsgpa_tot / nConfigPoint)
  16. Hard coded  Fcgpa_round : 9.165
  17. golang round founction : RoundFloat(Fcgpa_round, 2)
  18. Golang round : 9.16
  19.  
  20.  

It is returning two different value .
I have shared the log details too.

Code: Pascal  [Select][+][-]
  1. //export RoundFloat
  2. func RoundFloat(val float64, precision uint) float64 {
  3.         //https://gosamples.dev/round-float/
  4.         ratio := math.Pow(10, float64(precision))
  5.         return math.Round(val*ratio) / ratio
  6. }
  7.  


Shared Golang code too

I should get answer 9.16 from table value it is giving 9.17

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #12 on: December 11, 2024, 08:56:12 pm »
IMHO, problem is that floating point values can't be represented exactly.
When you calculate Fsgpa_tot, some small errors starts to accumulate (very small, in order 10e-13 or 10e-14), and results probably goes just a tiny bit over 9,165, enough to round it to 9,17.
When you try with hard coded values, compiler calculates Fsgpa_tot in advance, maybe using single precision or maybe just different order. Now results goes just a tiny bit under 9,165, resulting in rounding to 9,16.

Try to add this after line 15 to see if this is what is going on:
Code: Pascal  [Select][+][-]
  1.   WriteLn('Total - 879.84 = ' + floattostr(Fsgpa_tot - 879.84));
  2.   WriteLn('Formula - 9.165 = ' + floattostr(Fcgpa_round - 9.165));

P.S.:
Why aren't you using Free Pascal's rounding functions?

Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #13 on: December 11, 2024, 11:14:14 pm »
Application is written in golang.

And we are creating report in Lazarus.

So in this part of the application there is formula.

So we have used same golang formula

Packs

  • Sr. Member
  • ****
  • Posts: 402
Re: I have created dll in golang want to use inside lazarus freepascal
« Reply #14 on: December 12, 2024, 03:58:14 pm »
I have created demo application. it is working properly .
but on main application it is not working .
I am not getting any clue to solve this issue .

 

TinyPortal © 2005-2018