[solved] Strange behavior of DyLibLoad()

General FreeBASIC programming questions.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

[solved] Strange behavior of DyLibLoad()

Postby D.J.Peters » Jan 03, 2019 11:08

I don't know what's going on here !

I can load dynamic FBSound and other libraries like so: (NOTE: no ".dll" or ".so" needed)

Code: Select all

#ifndef __FB_64BIT__
  hFBS = DyLibLoad( "fbsound-32" )
  if hFBS=0 then print "error: lib fbsound-32 not loaded !" : exit sub
#else
  hFBS = DyLibLoad( "fbsound-64" )
  if hFBS=0 then print "error: lib fbsound-64 not loaded !" : exit sub
#endif
But if I load fltk-c I must add ".dll" ( Linux ".so" not tested ATM):

Code: Select all

#ifndef __FB_64BIT__
  hFltkCLib = DyLibLoad("fltk-c-1.3.3-32.dll")
#else
  hFltkCLib = DyLibLoad("fltk-c-1.3.3-64.dll")
#endif
Without ".dll" hFltkCLib will be NULL !
Compiled with -g and a gdb session does not show a error message or any unresolved references.

Joshy

here are the full sequence of the none working code:

Code: Select all

dim shared as any ptr hFltkCLib

sub FLTK_OOP_RuntimeUnload destructor
  if hFltkCLib then DyLibFree(hFltkCLib) : hFltkCLib = 0
end sub

sub FLTK_OOP_RuntimeLoad constructor
#if defined(__FB_DARWIN__) or defined(__FB_DOS__)
 #error 666: Target host must be a Windows or Linux PC or ARM6 device !
#else
 #ifndef __FB_64BIT__
  #ifndef __FB_ARM__
   #define FLTK_C_LIBNAME "fltk-c-1.3.3-32"
  #else
   #define FLTK_C_LIBNAME "fltk-c-1.3.3-32-arm"
  #endif
 #else
  #define FLTK_C_LIBNAME "fltk-c-1.3.3-64"
 #endif
#endif
  var oldPath = CurDir()
  Chdir(ExePath()) ' <--- I tried this also without success
  if hFltkCLib then exit sub
  hFltkCLib = DyLibLoad(FLTK_C_LIBNAME)
  if hFltkCLib=0 then
    print "fatal error: " & FLTK_C_LIBNAME & " not loaded !"
    beep : sleep : end 1
  end if
  Chdir(oldPath)
#undef FLTK_C_LIBNAME
end sub
And here are the working (wrong) version:

Code: Select all

#ifndef __FLTK_OOP_BI__
#define __FLTK_OOP_BI__

dim shared as any ptr hFltkCLib

sub FLTK_OOP_RuntimeUnload destructor
  if hFltkCLib then DyLibFree(hFltkCLib) : hFltkCLib = 0
end sub

sub FLTK_OOP_RuntimeLoad constructor
#if defined(__FB_DARWIN__) or defined(__FB_DOS__)
 #error 666: Target host must be a Windows or Linux PC or ARM6 device !
#else
 #ifndef __FB_64BIT__
  #ifndef __FB_ARM__
   #ifdef __FB_WIN32__
    #define FLTK_C_LIBNAME "fltk-c-1.3.3-32.dll"
   #else
    #define FLTK_C_LIBNAME "fltk-c-1.3.3-32"
   #endif
  #else
   #define FLTK_C_LIBNAME "fltk-c-1.3.3-32-arm"
  #endif
 #else
  #ifdef __FB_WIN32__
   #define FLTK_C_LIBNAME "fltk-c-1.3.3-64.dll"
  #else
   #define FLTK_C_LIBNAME "fltk-c-1.3.3-64"
  #endif
 #endif
#endif
  if hFltkCLib then exit sub
  var oldPath = CurDir()
  Chdir(ExePath())
  hFltkCLib = DyLibLoad(FLTK_C_LIBNAME)
  if hFltkCLib=0 then
    print "fatal error: " & FLTK_C_LIBNAME & " not loaded !"
    beep : sleep : end 1
  else
    print "loaded " & FLTK_C_LIBNAME & " successful"
  end if
  Chdir(oldPath)
#undef FLTK_C_LIBNAME
end sub

#endif ' __FLTK_OOP_BI__
In this case the output is: "loaded fltk-c-1.3.3-32.dll successful"
Last edited by D.J.Peters on Jan 03, 2019 11:43, edited 1 time in total.
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Strange behavior of DyLibLoad()

Postby dodicat » Jan 03, 2019 11:38

Isn't dylibload just loadlibrary from windows.(Looks like it in the source code)
Extract from microsoft;
If no file name extension is specified in the lpFileName parameter, the default library extension .dll is appended. However, the file name string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for loaded modules whose base name matches the base name of the module to be loaded. If the name matches, the load succeeds. Otherwise, the function searches for the file.
The Linux version uses formatting, from sys_dylib.c
...

char *libnameformat[] = { "%s",
"lib%s",
"lib%s.so",
"./%s",
"./lib%s",
"./lib%s.so",
NULL };

...
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: Strange behavior of DyLibLoad()

Postby D.J.Peters » Jan 03, 2019 11:42

I see it interprets ".3-32" or ".3-64" as a file extension thank you !

Joshy
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: [solved] Strange behavior of DyLibLoad()

Postby D.J.Peters » Jan 03, 2019 12:00

@fxm can you add this behavior to the wiki page please ?
https://www.freebasic.net/wiki/wikka.ph ... dLibraries

Thank you.

Joshy
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved] Strange behavior of DyLibLoad()

Postby fxm » Jan 03, 2019 13:41

Rather on this page?
DYLIBLOAD
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: [solved] Strange behavior of DyLibLoad()

Postby D.J.Peters » Jan 03, 2019 14:09

Yes Sir :-)
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved] Strange behavior of DyLibLoad()

Postby fxm » Jan 03, 2019 14:19

KeyPgDylibload → fxm [Added a note on the filename strings that include a character dot]
dodicat
Posts: 5757
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: [solved] Strange behavior of DyLibLoad()

Postby dodicat » Jan 03, 2019 14:37

Well put fxm.
You should have been a lawyer.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: [solved] Strange behavior of DyLibLoad()

Postby D.J.Peters » Jan 03, 2019 18:22

Why only Windows dynamic library format and why "Loads to" ?

"Loads to a Dynamic Link Library (DLL) into memory at runtime"

I would prefer:

"Loads a Dynamic Library (*.dll or *.so) into memory at link it at runtime"

Joshy
fxm
Posts: 8957
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved] Strange behavior of DyLibLoad()

Postby fxm » Jan 03, 2019 19:34

'Loads at runtime a dynamic link library into memory'

Return to “General”

Who is online

Users browsing this forum: albert and 2 guests