Lazarus

Programming => Operating Systems => Linux => Topic started by: Nafees on February 13, 2016, 06:37:50 pm

Title: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 13, 2016, 06:37:50 pm
I used to program on Windows, but when I switched to Linux (Ubuntu), Dynamic libraries won't load. I've even created a new library (shared library in Linux), and tried to load it, but the LodaLibrary from dynlibs just returns 0. This is my code:

Handle := LoadLibrary(ExFile);
 if Handle=0 then
  ShowMessage('Could not load library: '+#13#10+ExFile+#13#10+
'QIDE will    not function properly.');
 OnSave := TOnSave(GetProcedureAddress(Handle,PChar('OnSave')));

The ShowMessage is triggered, meaning that the library was never loaded. Any ways to solve this?
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 13, 2016, 06:40:46 pm
@Nafees

Please try this(see attachment): http://forum.lazarus.freepascal.org/index.php/topic,31477.msg201742.html#msg201742 . It should work just fine under Windows, Linux, OSX.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 13, 2016, 06:46:51 pm
Still not working.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 13, 2016, 06:50:56 pm
You should at least download and try it!  :D Same download count as yesterday.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Thaddy on February 13, 2016, 06:58:01 pm
And you are very, very sure you are not trying to load a windows .dll instead of a linux .so?

Because libraries are not THAT shared, they have different formats. Try what Getmem suggested a little harder...
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: marcov on February 13, 2016, 07:02:32 pm
You do use unit dynlibs ?
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 13, 2016, 07:05:57 pm
I just copied the code, didn't download the zip.

And yes, I compiled it on Linux to a ".so"

And yes, I'm using Dynlibs.

EDIT:
I don't get it, I downloaded your attachment, that code works, but mine is the same as yours (almost), but doesn't work. I attached the files.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 01:28:08 am
@Nafees
You cannot pass managed types(strings for example) across the library(dll, so) boundaries. No wonder your example is not working.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 08:22:38 am
@Nafees
You cannot pass managed types(strings for example) across the library(dll, so) boundaries. No wonder your example is not working.
But the first procedure (OnSave) should be loaded, and, the library isn't loaded, instead of that specific procedure.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 08:32:46 am
And how can I send those unmanaged data types?
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 08:36:43 am
Quote
@Nafees
But the first procedure (OnSave) should be loaded, and, the library isn't loaded, instead of that specific procedure.
Yes, at least the library should be loaded. The only logical explanation is EXFile points to a non existing file. Please debug it!
Here is another example which is more similar to your requirements: http://forum.lazarus.freepascal.org/index.php/topic,30275.msg192494.html#msg192494 . You can see how events + manged types are passed across the library boundaries.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 09:07:11 am
Quote
@Nafees
But the first procedure (OnSave) should be loaded, and, the library isn't loaded, instead of that specific procedure.
Yes, at least the library should be loaded. The only logical explanation is EXFile points to a non existing file. Please debug it!
Here is another example which is more similar to your requirements: http://forum.lazarus.freepascal.org/index.php/topic,30275.msg192494.html#msg192494 . You can see how events + manged types are passed across the library boundaries.
I've made it sure, the path I provide does point to that library, yet, not working.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 09:33:31 am
Then please attach a working example(without binaries) so I can debug it.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 09:48:08 am
Then please attach a working example(without binaries) so I can debug it.
I've attached it. I haven't defined one procedure in the library which the application uses, but that doesn't affect my problem.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 10:55:12 am
First of all the library doesn't even compile. If you want to use forms, frames, etc... you need to add LCL to the required packages and interfaces to the uses clauses. You also need to call application initialize, otherwise nothings gonna work. Secondly the library is loaded as you can see in the attachment, but you cannot call OnSave,  I already explained why in my previous post.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 11:24:50 am
First of all the library doesn't even compile. If you want to use forms, frames, etc... you need to add LCL to the required packages and interfaces to the uses clauses. You also need to call application initialize, otherwise nothings gonna work. Secondly the library is loaded as you can see in the attachment, but you cannot call OnSave,  I already explained why in my previous post.
Where do I call Application.Initialize? Because I will be creating the same frame many times, do I call it just once or everytime?
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 11:34:12 am
Quote
@Nafees
Where do I call Application.Initialize? Because I will be creating the same frame many times, do I call it just once or everytime?
Only once. This plugin like library topic was also covered here(Plugin.zip): http://forum.lazarus.freepascal.org/index.php/topic,30252.msg192411.html#msg192411  :D
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 14, 2016, 05:12:50 pm
Quote
@Nafees
Where do I call Application.Initialize? Because I will be creating the same frame many times, do I call it just once or everytime?
Only once. This plugin like library topic was also covered here(Plugin.zip): http://forum.lazarus.freepascal.org/index.php/topic,30252.msg192411.html#msg192411  :D
I think it's a Ubuntu releated bug. That attachment also isn't loading the library, LibHandle is still nil after loading the library.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 14, 2016, 05:42:07 pm
Quote
I think it's a Ubuntu releated bug. That attachment also isn't loading the library, LibHandle is still nil after loading the library.
Is loading fine at my side, I'm also using ubuntu 14.04 LTS.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 15, 2016, 10:28:35 am
Quote
I think it's a Ubuntu releated bug. That attachment also isn't loading the library, LibHandle is still nil after loading the library.
Is loading fine at my side, I'm also using ubuntu 14.04 LTS.
I also have the same version.
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: balazsszekely on February 15, 2016, 10:33:11 am
@Nafees
Here is the proof:
https://youtu.be/f1iq0iSjIho
Title: Re: Dynamic Libraries not loading, just returns 0
Post by: Nafees on February 15, 2016, 11:38:33 am
@Nafees
Here is the proof:
https://youtu.be/f1iq0iSjIho
I didn't meant to say that I doubt you. I think I know why it won't work with me: I'm new to lazarus and don't know how to add lcl to the packages. Do you just put lcl in uses or is it somewhere from the IDE?

EDIT: Solved the whole problem. The problem was that LCL package, just found out how to add it.

EDIT: I successfully loaded a dummy library, but that "plugin" library still won't load. Now, it gives this error on LoadLibrary:
Code: [Select]
Project QIDE raised exception class 'External: SIGSEGV'.

 In file './include/winapi.inc' at line 621
Any ideas why this is caused? I still remember it was fairly easy to do this on Delphi, but Delphi isn't on Linux.

EDIT: Fixed that too.
TinyPortal © 2005-2018