Author Topic: Error with using DLL written in Lazarus  (Read 2490 times)


  • New member
  • *
  • Posts: 10
Error with using DLL written in Lazarus
« on: September 22, 2017, 03:16:54 pm »

I need some help. Long time ago, i've wrote some application, with hudge part of code. Application isn't up to date now, but this function was very usefull.

For using it in other applications i've decided to write my first DLL. I've read some informations on it, copied intresting fuctions to library, and correctly builded it.

I've wrote some test application, which using this DLL, and almost all fuctions working fine, but one (main) of them working only partialy. In some cases of inputs i've got External: SIGSEGV error, as below
775E6889 ff4014                   incl   0x14(%eax)

I've even took code from library, and put it in the Console Application, to test it, but there everything working fine. I'm confused. May i please for some help?

If my explanations are not quite good, i can answer some extra questions, or even publish source of library and test application. It isn't nothing extraordinary.

Best regards, xtm


  • Hero Member
  • *****
  • Posts: 2750


  • New member
  • *
  • Posts: 10
Re: Error with using DLL written in Lazarus
« Reply #2 on: September 22, 2017, 03:57:47 pm »
I need some help.

I believe structure od my dll is quite similar. Maybe but those "decorations" e.g. Getfoo@4

And some part of the code working fine. One of the inputs is working well, and the others making application crash. I didn't mentioned, that this input decides, which part of the fucntion is executed. But there is nothing complicated. I believe, there is some newbie mistake.

I've used:
Code: Pascal  [Select]
  1. uses
  2.   Classes, Windows, db, sqldb, sqlite3conn, SysUtils, Dateutils

And part of the code, which crashes application looks like this (between {...}):
Code: Pascal  [Select]
  1. function sprawdzpoprawnosc(numer: string; rodzaj: string): string; stdcall;
  2.   var
  3.   skonwert, wagi, wazone, t, s, pods: string;
  4.   i, j, selstart, suma, suma2, selend, kontrolna, kontrolna2: integer;
  5.   Conn: TSQLConnector;
  6.   Trans: TSQLTransaction;
  7.   Query: TSQLQuery;
  8.   begin
  9.   //--Utworzenie łącza do bazy danych
  10.     Conn := TSQLConnector.Create(nil);
  11.     with Conn do begin
  12.       ConnectorType := 'SQLite3';
  13.       HostName := '';
  14.       DatabaseName := 'wrn.ekm';
  15.       UserName := '';
  16.       Password := '';
  17.     end;
  19.     Trans := TSQLTransaction.Create(nil);
  20.     Conn.Transaction := Trans;
  22.     Query := TSQLQuery.Create(nil);
  23.     Query.DataBase := Conn;
  24.   //--
  25.   skonwert:=konwertujznaki(rozbij(numer));
  26. {...}
  27. If rodzaj='NIP' then
  28.   begin
  29.     if length(rozbij(numer))=20 then
  30.     begin
  31.    //--obliczanie cyfry kontrolnej
  32.      wagi:='6 5 7 2 3 4 5 6 7 ';
  33.      wazone:='';
  34.     for i:=1 to length(wagi) do
  35.     begin
  36.      if wagi[i] in ['0'..'9'] then wazone:=wazone+inttostr(strtoint(wagi[i])*strtoint(skonwert[i]))
  37.      else wazone:=wazone+wagi[i];
  38.     end;
  39.     selstart:=0;
  40.     suma:=0;
  41.     for i:=1 to length(wazone) do
  42.     begin
  43.      if wazone[i]=' ' then
  44.      begin
  45.       selend:=i;
  46.       suma:=suma+strtoint(copy(wazone,selstart+1,selend-selstart-1));
  47.       selstart:=selend;
  48.      end;
  49.     end;
  50.     kontrolna:=suma mod 11;
  51.     //--sprawdzanie numeru NIP;
  52.     //--wpisana cyfra kontrolna zgodna z obliczoną
  53.      if kontrolna=10 then result:='Numer NIP jest nieprawdziwy! (Cyfra kontrolna nie może wynosić 10)'
  54.     else if (numer[length(numer)]=inttostr(kontrolna)) then
  55.     begin
  56.         begin
  57.          Query.Close;
  58.          Query.SQL.Text:='Select * from numery where rodzaj='+quotedstr(rodzaj)+' and wyroznik like '+quotedstr(copy(numer,1,3)+'%');
  59.          Query.Open;
  60.         end;
  61.         if Query.RecordCount>0 then
  62.         begin
  63.          pods:=Query.fieldbyname('opis_f').AsString;
  64.          pods:=stringreplace(pods,'%jednostka1',Query.fieldbyname('jednostka1').AsString,[rfReplaceAll, rfIgnoreCase]);
  65.          result:=pods;
  66.         end
  67.         else result:='Numer NIP jest prawidłowy.';
  68.     end
  69.     else result:='Numer NIP jest nieprawdziwy! (Cyfra kontrolna jest nieprawidłowa. Prawidłowa = '+inttostr(kontrolna)+')';
  70.    end else result:='Numer NIP jest nieprawdziwy! (Ilość znaków jest nieprawidłowa)';
  71.   end
  72.   else
  73. {...}


  • Hero Member
  • *****
  • Posts: 7195
Re: Error with using DLL written in Lazarus
« Reply #3 on: September 22, 2017, 04:14:27 pm »
You should not use type string from a dll if you are a beginner. Use type PChar. Always use type PChar if your dll needs to interface with other languages. (That goes for all type of programmers!, not only beginners). Or PWideChar etc... as long it is clear...
The Pascal string type is a managed type. It can not easily be "managed" from inside the dll, especially when it is exported, so use a type that is unmanaged: A PChar or PWideChar, but never string in a dll..

It is also called rule number one.
« Last Edit: September 22, 2017, 04:21:14 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.


  • New member
  • *
  • Posts: 10
Re: Error with using DLL written in Lazarus
« Reply #4 on: September 22, 2017, 11:06:10 pm »
Thank you very much... I'll need to check it out.