Browse Source

Move DynLibs support to System unit using a manager approach like for Threads, WideStrings, etc.. This is needed so that we can support features like "delayed" and loading of dynamic packages.
Each port that allows the usage of dynamic libraries should call InitSystemDynLibs during initialization of the System unit.
The DynLibs unit has been adjusted to be a forwarder for the functions that now reside in the System unit (for backwards compatiblity just in case). Additionally the unit will register the DynLibsManager if it doesn't reside in the System unit anyway.
Currently only the Windows targets (Win32, Win64 and WinCE) implement the DynLibsManager inside the System unit. For other systems using the LoadLibrary, etc. functions will lead to a run error/exception.
If a port wants to implement its own DynLibsManager then it needs to define DISABLE_NO_DYNLIBS_MANAGER. TLibHandle, NilHandle and optionally TOrdinalEntry (it's set to SizeUInt otherwise) should be defined in sysdlh.inc which needs to be enabled using the define FPC_SYSTEM_HAS_SYSDLH (because there are targets which have FEATURE_DYNLIBS enabled, but don't support them... e.g. powerpc-wii -.-).
The DynLibsManager contains methods for loading a library based on a RawByteString and based on a UnicodeString. BOTH should be implemented, but internally one can forward to the other.
The loading by ordinal *can* be be implemented. If it is set to Nil then the implementation of GetProcAddress(lib,ordinal) will return Nil.

+ new functions SetDynLibsManager and GetDynLibsManager to set and retrieve the current DynLibsManager
* provide interface of DynLibs unit in unit System

git-svn-id: trunk@29613 -

svenbarth 10 years ago
parent
commit
2d454dc8fa

+ 10 - 0
.gitattributes

@@ -8345,6 +8345,8 @@ rtl/inc/dos.inc svneol=native#text/plain
 rtl/inc/dosh.inc svneol=native#text/plain
 rtl/inc/dynarr.inc svneol=native#text/plain
 rtl/inc/dynarrh.inc svneol=native#text/plain
+rtl/inc/dynlib.inc svneol=native#text/plain
+rtl/inc/dynlibh.inc svneol=native#text/plain
 rtl/inc/dynlibs.pas svneol=native#text/plain
 rtl/inc/except.inc svneol=native#text/plain
 rtl/inc/exeinfo.pp svneol=native#text/plain
@@ -8854,6 +8856,7 @@ rtl/netware/rtldefs.inc svneol=native#text/plain
 rtl/netware/socklib.imp -text
 rtl/netware/streams.imp -text
 rtl/netware/sysdir.inc svneol=native#text/plain
+rtl/netware/sysdlh.inc svneol=native#text/plain
 rtl/netware/sysfile.inc svneol=native#text/plain
 rtl/netware/sysheap.inc svneol=native#text/plain
 rtl/netware/sysos.inc svneol=native#text/plain
@@ -8887,6 +8890,7 @@ rtl/netwlibc/nwsnut.imp -text
 rtl/netwlibc/nwsnut.pp svneol=native#text/plain
 rtl/netwlibc/rtldefs.inc svneol=native#text/plain
 rtl/netwlibc/sysdir.inc svneol=native#text/plain
+rtl/netwlibc/sysdlh.inc svneol=native#text/plain
 rtl/netwlibc/sysfile.inc svneol=native#text/plain
 rtl/netwlibc/sysheap.inc svneol=native#text/plain
 rtl/netwlibc/sysos.inc svneol=native#text/plain
@@ -9038,6 +9042,7 @@ rtl/os2/prt0.as svneol=native#text/plain
 rtl/os2/rtldefs.inc svneol=native#text/plain
 rtl/os2/so32dll.pas svneol=native#text/plain
 rtl/os2/sysdir.inc svneol=native#text/plain
+rtl/os2/sysdlh.inc svneol=native#text/plain
 rtl/os2/sysfile.inc svneol=native#text/plain
 rtl/os2/sysheap.inc svneol=native#text/plain
 rtl/os2/sysos.inc svneol=native#text/plain
@@ -9249,6 +9254,7 @@ rtl/unix/settimeo.inc svneol=native#text/plain
 rtl/unix/syscall.pp svneol=native#text/plain
 rtl/unix/syscgen.inc svneol=native#text/plain
 rtl/unix/sysdir.inc svneol=native#text/plain
+rtl/unix/sysdlh.inc svneol=native#text/plain
 rtl/unix/sysfile.inc svneol=native#text/plain
 rtl/unix/sysheap.inc svneol=native#text/plain
 rtl/unix/sysunixh.inc svneol=native#text/plain
@@ -9303,6 +9309,8 @@ rtl/win/fpcmemdll.pp svneol=native#text/plain
 rtl/win/messages.pp svneol=native#text/plain
 rtl/win/sharemem.pp svneol=native#text/plain
 rtl/win/sysdir.inc svneol=native#text/plain
+rtl/win/sysdl.inc svneol=native#text/plain
+rtl/win/sysdlh.inc svneol=native#text/plain
 rtl/win/sysfile.inc svneol=native#text/plain
 rtl/win/sysheap.inc svneol=native#text/plain
 rtl/win/sysos.inc svneol=native#text/plain
@@ -9368,6 +9376,8 @@ rtl/wince/dynlibs.inc svneol=native#text/plain
 rtl/wince/messages.pp svneol=native#text/plain
 rtl/wince/readme.txt svneol=native#text/plain
 rtl/wince/rtldefs.inc svneol=native#text/plain
+rtl/wince/sysdl.inc svneol=native#text/plain
+rtl/wince/sysdlh.inc svneol=native#text/plain
 rtl/wince/system.pp svneol=native#text/plain
 rtl/wince/sysutils.pp svneol=native#text/plain
 rtl/wince/windows.pp svneol=native#text/plain

+ 1 - 0
rtl/aix/system.pp

@@ -292,6 +292,7 @@ Begin
 { Arguments }
   SetupCmdLine;
   InitSystemThreads;
+  InitSystemDynLibs;
   { restore original signal handlers in case this is a library }
   if IsLibrary then
     RestoreOldSignalHandlers;

+ 1 - 0
rtl/amiga/system.pp

@@ -418,4 +418,5 @@ begin
 { Arguments }
   GenerateArgs;
   InitSystemThreads;
+  InitSystemDynLibs;
 end.

+ 1 - 0
rtl/aros/system.pp

@@ -462,4 +462,5 @@ begin
   { Arguments }
   GenerateArgs;
   InitSystemThreads;
+  InitSystemDynLibs;
 end.

+ 1 - 0
rtl/beos/system.pp

@@ -435,6 +435,7 @@ begin
 { Reset IO Error }
   InOutRes:=0;
   InitSystemThreads;
+  InitSystemDynLibs;
   setupexecname;
   { restore original signal handlers in case this is a library }
   if IsLibrary then

+ 1 - 0
rtl/bsd/system.pp

@@ -354,6 +354,7 @@ Begin
   SetupCmdLine;
   { threading }
   InitSystemThreads;
+  InitSystemDynLibs;
   { restore original signal handlers in case this is a library }
   if IsLibrary then
     RestoreOldSignalHandlers;

+ 2 - 0
rtl/emx/system.pas

@@ -591,6 +591,8 @@ begin
 
     InitSystemThreads;
 
+    InitSystemDynLibs;
+
     if os_Mode in [osDOS,osDPMI] then
         DosEnvInit;
 

+ 1 - 0
rtl/haiku/system.pp

@@ -469,6 +469,7 @@ begin
 { Reset IO Error }
   InOutRes:=0;
   InitSystemThreads;
+  InitSystemDynLibs;
   setupexecname;
   { restore original signal handlers in case this is a library }
   if IsLibrary then

+ 223 - 0
rtl/inc/dynlib.inc

@@ -0,0 +1,223 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2015 by the Free Pascal development team.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+{ ---------------------------------------------------------------------
+  OS - Independent declarations.
+  ---------------------------------------------------------------------}
+
+Var
+  CurrentDLM : TDynLibsManager;
+
+{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
+Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
+{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
+Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
+{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
+{$if defined(cpui386) or defined(cpux86_64)}
+  var
+    fpucw : Word;
+    ssecw : DWord;
+{$endif}
+  begin
+    try
+{$if defined(cpui386) or defined(cpux86_64)}
+      fpucw:=Get8087CW;
+{$ifdef cpui386}
+      if has_sse_support then
+{$endif cpui386}
+        ssecw:=GetMXCSR;
+{$endif}
+{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
+      Result:=CurrentDLM.LoadLibraryA(Name);
+{$else FPCRTL_FILESYSTEM_TWO_BYTE_API}
+      Result:=CurrentDLM.LoadLibraryU(Name);
+{$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
+      finally
+{$if defined(cpui386) or defined(cpux86_64)}
+      Set8087CW(fpucw);
+{$ifdef cpui386}
+      if has_sse_support then
+{$endif cpui386}
+        SetMXCSR(ssecw);
+{$endif}
+    end;
+  end;
+
+Function LoadLibrary(const Name : RawByteString) : TLibHandle;
+begin
+  Result:=CurrentDLM.LoadLibraryA(Name);
+end;
+
+Function LoadLibrary(const Name: UnicodeString) : TLibHandle;
+begin
+  Result:=CurrentDLM.LoadLibraryU(Name);
+end;
+
+{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
+
+Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
+begin
+  Result:=DoSafeLoadLibrary(Name);
+end;
+
+Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
+begin
+  Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
+end;
+
+{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
+
+Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
+begin
+  Result:=DoSafeLoadLibrary(UnicodeString(Name));
+end;
+
+Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
+begin
+  Result:=DoSafeLoadLibrary(Name);
+end;
+
+{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
+
+Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+begin
+  Result:=CurrentDLM.GetProcAddress(Lib, ProcName);
+end;
+
+
+Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
+begin
+  if Assigned(CurrentDLM.GetProcAddressOrdinal) then
+    Result:=CurrentDLM.GetProcAddressOrdinal(Lib, Ordinal)
+  else
+    Result:=Nil;
+end;
+
+
+Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+begin
+  Result:=CurrentDLM.UnloadLibrary(lib);
+end;
+
+
+function GetLoadErrorStr: String;
+begin
+  Result:=CurrentDLM.GetLoadErrorStr();
+end;
+
+
+Function FreeLibrary(Lib : TLibHandle) : Boolean;
+begin
+  Result:=UnloadLibrary(lib);
+end;
+
+
+Function GetProcAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+begin
+  Result:=GetProcedureAddress(Lib,Procname);
+end;
+
+Procedure GetDynLibsManager (Var Manager : TDynLibsManager);
+begin
+  Manager:=CurrentDLM;
+end;
+
+Procedure SetDynLibsManager (Const New : TDynLibsManager);
+begin
+  CurrentDLM:=New;
+end;
+
+
+Procedure SetDynLibsManager (Const New : TDynLibsManager; Var Old: TDynLibsManager);
+begin
+  Old:=CurrentDLM;
+  CurrentDLM:=New;
+end;
+
+{ ---------------------------------------------------------------------
+    DynLibsManager which gives run-time error. Use if no thread support.
+  ---------------------------------------------------------------------}
+
+{$ifndef DISABLE_NO_DYNLIBS_MANAGER}
+
+{ resourcestrings are not supported by the system unit,
+  they are in the objpas unit and not available for fpc/tp modes }
+const
+  SNoDynLibs = 'This binary has no dynamic library support compiled in.';
+  SRecompileWithDynLibs = 'Recompile the application with a dynamic-library-driver in the program uses clause before other units using dynamic libraries.';
+
+Procedure NoDynLibsError; {$ifndef ver2_6}noreturn;{$endif}
+begin
+{$ifndef EMBEDDED}
+{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
+  If IsConsole then
+    begin
+    Writeln(StdErr,SNoDynLibs);
+    Writeln(StdErr,SRecompileWithDynLibs);
+    end;
+{$endif FPC_HAS_FEATURE_CONSOLEIO}
+{$endif EMBEDDED}
+  RunError(235)
+end;
+
+Function NoLoadLibraryU(const Name: UnicodeString): TLibHandle;
+begin
+  NoDynLibsError;
+end;
+
+Function NoLoadLibraryA(const Name: RawByteString): TLibHandle;
+begin
+  NoDynLibsError;
+end;
+
+function NoGetProcAddress(Lib: TLibHandle; const Proc: AnsiString): Pointer;
+begin
+  NoDynLibsError;
+end;
+
+function NoGetProcAddressOrdinal(Lib: TLibHandle; Ordinal: TOrdinalEntry): Pointer;
+begin
+  NoDynLibsError;
+end;
+
+function NoGetLoadErrorStr: String;
+begin
+  NoDynLibsError;
+end;
+
+function NoUnloadLibrary(Lib: TLibHandle): Boolean;
+begin
+  NoDynLibsError;
+end;
+
+const
+  NoDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @NoLoadLibraryU;
+    LoadLibraryA: @NoLoadLibraryA;
+    GetProcAddress: @NoGetProcAddress;
+    GetProcAddressOrdinal: @NoGetProcAddressOrdinal;
+    UnloadLibrary: @NoUnloadLibrary;
+    GetLoadErrorStr: @NoGetLoadErrorStr;
+  );
+
+procedure SetNoDynLibsManager;
+begin
+  SetDynLibsManager(NoDynLibsManager);
+end;
+
+procedure InitSystemDynLibs;
+begin
+  SetNoDynLibsManager;
+end;
+
+{$endif DISABLE_NO_DYNLIBS_MANAGER}

+ 67 - 0
rtl/inc/dynlibh.inc

@@ -0,0 +1,67 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2015 by the Free Pascal development team
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+{$ifdef FPC_SYSTEM_HAS_SYSDLH}
+{$i sysdlh.inc}
+{$else FPC_SYSTEM_HAS_SYSDLH}
+{ These are fallback declarations }
+type
+  TLibHandle = THandle;
+
+const
+  NilHandle = TLibHandle(0);
+{$endif FPC_SYSTEM_HAS_SYSDLH}
+
+{ ---------------------------------------------------------------------
+  OS - Independent declarations.
+  ---------------------------------------------------------------------}
+type
+{$if not declared(TOrdinalEntry)}
+  TOrdinalEntry = SizeUInt;
+{$endif not declared(TOrdinalEntry)}
+
+  TLoadLibraryUHandler = function(const Name: UnicodeString): TLibHandle;
+  TLoadLibraryAHandler = function(const Name: RawByteString): TLibHandle;
+  TGetProcAddressHandler = function(Lib: TLibHandle; const ProcName: AnsiString): Pointer;
+  TGetProcAddressOrdinalHandler = function(Lib: TLibHandle; Ordinal: TOrdinalEntry): Pointer;
+  TUnloadLibraryHandler = function(Lib: TLibHandle): Boolean;
+  TGetLoadErrorStrHandler = function: String;
+
+  TDynLibsManager = record
+    LoadLibraryU: TLoadLibraryUHandler;
+    LoadLibraryA: TLoadLibraryAHandler;
+    GetProcAddress: TGetProcAddressHandler;
+    GetProcAddressOrdinal: TGetProcAddressOrdinalHandler;
+    UnloadLibrary: TUnloadLibraryHandler;
+    GetLoadErrorStr: TGetLoadErrorStrHandler;
+  end;
+
+Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
+Function LoadLibrary(const Name : RawByteString) : TLibHandle;
+Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
+Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
+
+Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
+Function GetProcedureAddress(Lib : TLibHandle; Ordinal: TOrdinalEntry) : Pointer;
+Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+Function GetLoadErrorStr: string;
+
+// Kylix/Delphi compability
+
+Function FreeLibrary(Lib : TLibHandle) : Boolean; inline;
+Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer; inline;
+
+Procedure GetDynLibsManager (Var Manager : TDynLibsManager);
+Procedure SetDynLibsManager (Const New : TDynLibsManager);
+Procedure SetDynLibsManager (Const New : TDynLibsManager; Var Old: TDynLibsManager);
+

+ 39 - 100
rtl/inc/dynlibs.pas

@@ -20,156 +20,95 @@ unit dynlibs;
 
 interface
 
-{$i rtldefs.inc}
-
-{ ---------------------------------------------------------------------
-  Read OS-dependent interface declarations.
-  ---------------------------------------------------------------------}
+Type
+  TLibHandle = System.TLibHandle;
 
-{ Note: should define the TOrdinalEntry type and define
-        DYNLIBS_SUPPORTS_ORDINAL if the operating system supports loading
-        functions by a ordinal like e.g. Windows or OS/2 do }
+Const
+  NilHandle = System.NilHandle;
+  SharedSuffix = System.SharedSuffix;
 
-{$define readinterface}
-{$i dynlibs.inc}
-{$undef  readinterface}
+Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle; inline;
+Function LoadLibrary(const Name : RawByteString) : TLibHandle; inline;
+Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle; inline;
+Function LoadLibrary(const Name : UnicodeString) : TLibHandle; inline;
 
-{ ---------------------------------------------------------------------
-  OS - Independent declarations.
-  ---------------------------------------------------------------------}
-{$IFNDEF DYNLIBS_SUPPORTS_ORDINAL}
-type
- TOrdinalEntry = SizeUInt;
-{$ENDIF DYNLIBS_SUPPORTS_ORDINAL}
-
-Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
-Function LoadLibrary(const Name : RawByteString) : TLibHandle;
-Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
-Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
-
-Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
-Function GetProcedureAddress(Lib : TLibHandle; Ordinal: TOrdinalEntry) : Pointer;
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
-Function GetLoadErrorStr: string;
+Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer; inline;
+Function GetProcedureAddress(Lib : TLibHandle; Ordinal: TOrdinalEntry) : Pointer; inline;
+Function UnloadLibrary(Lib : TLibHandle) : Boolean; inline;
+Function GetLoadErrorStr: string; inline;
 
 // Kylix/Delphi compability
 
-Function FreeLibrary(Lib : TLibHandle) : Boolean;
-Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
+Function FreeLibrary(Lib : TLibHandle) : Boolean; inline;
+Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer; inline;
 
 Type
   HModule = TLibHandle; 
 
 Implementation
 
-{ ---------------------------------------------------------------------
-  OS - Independent declarations.
-  ---------------------------------------------------------------------}
 
-{ Note: should define the TOrdinalEntry overload if the operating system
-        supports loading functions by a ordinal like e.g. Windows or OS/2 do }
+{ Should define a procedure InitDynLibs which sets up the DynLibs manager; optionally a
+  DoneDynLibs can be defined which is called during finalization }
 {$i dynlibs.inc}
 
-{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
-Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
-{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
-Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
-{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
-{$if defined(cpui386) or defined(cpux86_64)}
-  var
-    fpucw : Word;
-    ssecw : DWord;
-{$endif}
-  begin
-    try
-{$if defined(cpui386) or defined(cpux86_64)}
-      fpucw:=Get8087CW;
-{$ifdef cpui386}
-      if has_sse_support then
-{$endif cpui386}
-        ssecw:=GetMXCSR;
-{$endif}
-      Result:=doloadlibrary(Name);
-      finally
-{$if defined(cpui386) or defined(cpux86_64)}
-      Set8087CW(fpucw);
-{$ifdef cpui386}
-      if has_sse_support then
-{$endif cpui386}
-        SetMXCSR(ssecw);
-{$endif}
-    end;
-  end;
-
-{$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
 Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
 begin
-  Result:=DoSafeLoadLibrary(UnicodeString(Name));
+  Result:=System.SafeLoadLibrary(Name);
 end;
 
 Function LoadLibrary(const Name : RawByteString) : TLibHandle;
 begin
-  Result:=DoLoadLibrary(UnicodeString(Name));
+  Result:=System.LoadLibrary(Name);
 end;
 
-{$else not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
-
-Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
+Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
 begin
-  Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
+  Result:=System.SafeLoadLibrary(Name);
 end;
 
-Function LoadLibrary(const Name : RawByteString) : TLibHandle;
+Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
 begin
-  Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
+  Result:=System.LoadLibrary(Name);
 end;
-{$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
 
 
-{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
-Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
-begin
-  Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
-end;
-
-Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
+Function GetProcedureAddress(Lib : TLibHandle; const ProcName: AnsiString) : Pointer;
 begin
-  Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
+  Result:=System.GetProcedureAddress(Lib, ProcName);
 end;
 
-{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
-
-Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
+Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
 begin
-  Result:=DoSafeLoadLibrary(Name);
+  Result:=System.GetProcedureAddress(Lib, Ordinal);
 end;
 
-Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
+Function UnloadLibrary(Lib : TLibHandle) : Boolean;
 begin
-  Result:=DoLoadLibrary(Name);
+  Result:=System.UnloadLibrary(Lib);
 end;
-{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
 
-{$ifndef DYNLIBS_SUPPORTS_ORDINAL}
-{ OS does not support loading by ordinal (or it's not implemented yet), so by
-  default we simply return Nil }
-Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
+Function GetLoadErrorStr: String;
 begin
-  Result := Nil;
+  Result:=System.GetLoadErrorStr;
 end;
-{$endif not DYNLIBS_SUPPORTS_ORDINAL}
 
 Function FreeLibrary(Lib : TLibHandle) : Boolean;
 
 begin
-  Result:=UnloadLibrary(lib);
+  Result:=System.FreeLibrary(lib);
 end;
 
 Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
 
 begin
-  Result:=GetProcedureAddress(Lib,Procname);
+  Result:=System.GetProcedureAddress(Lib,Procname);
 end;
 
-
+initialization
+  InitDynLibs;
+finalization
+{$if declared(DoneDynLibs)}
+  DoneDynLibs;
+{$endif}
 end.

+ 17 - 0
rtl/inc/system.inc

@@ -1457,11 +1457,28 @@ end;
 {$endif FPC_HAS_FEATURE_THREADING}
 
 
+{*****************************************************************************
+                          Dynamic library support
+*****************************************************************************}
+
+
+{$ifdef FPC_HAS_FEATURE_DYNLIBS}
+{$i dynlib.inc}
+
+{$ifdef DISABLE_NO_DYNLIBS_MANAGER}
+{ OS Dependant implementation }
+{$i sysdl.inc}
+{$endif DISABLE_NO_DYNLIBS_MANAGER}
+{$endif FPC_HAS_FEATURE_DYNLIBS}
+
+
 {*****************************************************************************
                             File Handling
 *****************************************************************************}
 
 
+
+
 {$ifdef FPC_HAS_FEATURE_FILEIO}
 { Allow slash and backslash as separators }
 procedure DoDirSeparators(var p: pchar; inplace: boolean = true);

+ 8 - 0
rtl/inc/systemh.inc

@@ -1513,6 +1513,14 @@ const
 {$i threadh.inc}
 {$endif FPC_HAS_FEATURE_THREADING}
 
+{*****************************************************************************
+                          Dynamic library support
+*****************************************************************************}
+
+{$ifdef FPC_HAS_FEATURE_DYNLIBS}
+{$i dynlibh.inc}
+{$endif FPC_HAS_FEATURE_DYNLIBS}
+
 {*****************************************************************************
                           Resources support
 *****************************************************************************}

+ 2 - 0
rtl/linux/system.pp

@@ -368,6 +368,8 @@ begin
   InOutRes:=0;
   { threading }
   InitSystemThreads;
+  { dynamic libraries }
+  InitSystemDynLibs;
   { restore original signal handlers in case this is a library }
   if IsLibrary then
     RestoreOldSignalHandlers;

+ 1 - 0
rtl/morphos/system.pp

@@ -429,4 +429,5 @@ begin
 { Arguments }
   GenerateArgs;
   InitSystemThreads;
+  InitSystemDynLibs;
 end.

+ 25 - 25
rtl/netware/dynlibs.inc

@@ -14,29 +14,9 @@
  **********************************************************************}
 
 
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-Type
-  TLibHandle = System.THandle;
-
-Const
-  NilHandle = 0;
-// these are for easier crossplatform construction of dll names in dynloading libs.
-  SharedSuffix = 'nlm';
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
 Uses nwserv;
 
-Function DoLoadLibrary(const Name : RawByteString) : TlibHandle;
+Function SysLoadLibraryA(const Name : RawByteString) : TlibHandle;
 var args : array[0..1] of PAnsiChar;
 begin
   args[0] := PAnsiChar(Name);
@@ -44,23 +24,43 @@ begin
   Result:=spawnvp(P_NOWAIT,@args,nil);
 end;
 
-Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+Function SysLoadLibraryU(const Name: UnicodeString) : TLibHandle;
+begin
+  Result := SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
+end;
+
+Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
 
 begin
   Result:=ImportSymbol(GetNlmHandle, pchar(ProcName));
 end;
 
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
 
 begin
   Result:=false;
 end;
 
 
-Function GetLoadErrorStr: string;
+Function SysGetLoadErrorStr: string;
 
 begin  
   Result:='';
 end;
-{$endif}
+
+
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: Nil;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;
 

+ 24 - 0
rtl/netware/sysdlh.inc

@@ -0,0 +1,24 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2011 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+
+Type
+  TLibHandle = System.THandle;
+
+Const
+  NilHandle = 0;
+// these are for easier crossplatform construction of dll names in dynloading libs.
+  SharedSuffix = 'nlm';
+

+ 1 - 0
rtl/netware/system.pp

@@ -477,4 +477,5 @@ Begin
   IsConsole := TRUE;
   ExitCode  := 0;
   InitSystemThreads;
+  InitSystemDynLibs;
 End.

+ 25 - 25
rtl/netwlibc/dynlibs.inc

@@ -13,50 +13,50 @@
 
  **********************************************************************}
 
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-Type
-  TLibHandle = Pointer;
-
-Const
-  NilHandle = Nil;
-// these are for easier crossplatform construction of dll names in dynloading libs.
-  SharedSuffix = 'so';
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
 uses libc;
 
-Function DoLoadLibrary(const Name : RawByteString) : TLibHandle;
+Function SysLoadLibraryA(const Name : RawByteString) : TLibHandle;
 
 begin
   Result:=dlopen(PAnsiChar(Name),RTLD_LAZY);
 end;
 
-Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+Function SysLoadLibraryU(const Name: UnicodeString) : TLibHandle;
+
+begin
+  Result:=SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
+end;
+
+Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
 
 begin
   Result:=dlsym(lib,pchar(ProcName));
 end;
 
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
 
 begin
   Result:=dlClose(Lib)=0;
 end;
 
-Function GetLoadErrorStr: string;
+Function SysGetLoadErrorStr: string;
 
 begin  
   Result:='';
 end;
-{$endif}
+
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: Nil;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;
 

+ 23 - 0
rtl/netwlibc/sysdlh.inc

@@ -0,0 +1,23 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2004 by the Free Pascal development team
+
+    Implement OS-dependent part of dynamic library loading.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+Type
+  TLibHandle = Pointer;
+
+Const
+  NilHandle = Nil;
+// these are for easier crossplatform construction of dll names in dynloading libs.
+  SharedSuffix = 'so';
+

+ 1 - 0
rtl/netwlibc/system.pp

@@ -552,4 +552,5 @@ Begin
   IsConsole := TRUE;
   ExitCode  := 0;
   InitSystemThreads;
+  InitSystemDynLibs;
 End.

+ 2 - 0
rtl/objpas/sysconst.pp

@@ -80,6 +80,7 @@ resourcestring
   SInvalidVarOpWithHResultWithPrefix = 'Invalid variant operation (%s%.8x)'+LineEnding+'%s';
   SNoError               = 'No error.';
   SNoThreadSupport       = 'Threads not supported. Recompile program with thread driver.';
+  SNoDynLibsSupport      = 'Dynamic libraries not supported. Recompile program with dynamic library driver.';
   SMissingWStringManager = 'Widestring manager not available. Recompile program with appropriate manager.';
   SSigQuit               = 'SIGQUIT signal received.';
   SObjectCheckError      = 'Object reference is Nil';
@@ -228,6 +229,7 @@ begin
      231 : Result:=SExceptionStack;
      232 : Result:=SNoThreadSupport;
      233 : Result:=SMissingWStringManager;
+     235 : Result:=SNoDynLibsSupport;
 
      255 : Result:=SFallbackError;
 

+ 1 - 0
rtl/objpas/sysutils/sysutilh.inc

@@ -210,6 +210,7 @@ type
    ESafecallException = class(Exception);
    ENoThreadSupport = Class(Exception);
    ENoWideStringSupport = Class(Exception);
+   ENoDynLibsSupport = class(Exception);
    ENotImplemented = class(Exception);
 
    EArgumentException = class(Exception);

+ 3 - 2
rtl/objpas/sysutils/sysutils.inc

@@ -320,7 +320,7 @@ type
   end;
 
 const
-  exceptmap: array[0..28] of TExceptMapEntry = (
+  exceptmap: array[0..29] of TExceptMapEntry = (
     (code: 200; cls: EDivByZero;     msg: @SDivByZero),
     (code: 201; cls: ERangeError;    msg: @SRangeError),
     (code: 202; cls: EStackOverflow; msg: @SStackOverflow),
@@ -350,7 +350,8 @@ const
     (code: 231; cls: EConvertError;  msg: @SiconvError),
     (code: 232; cls: ENoThreadSupport; msg: @SNoThreadSupport),
     (code: 233; cls: ENoWideStringSupport; msg: @SSigQuit),
-    (code: 234; cls: ENoWideStringSupport; msg: @SMissingWStringManager)
+    (code: 234; cls: ENoWideStringSupport; msg: @SMissingWStringManager),
+    (code: 235; cls: ENoDynLibsSupport; msg: @SNoDynLibsSupport)
   );
 
 function FindExceptMapEntry(err: longint): PExceptMapEntry;

+ 28 - 31
rtl/os2/dynlibs.inc

@@ -14,29 +14,6 @@
  **********************************************************************}
 
 
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-{$DEFINE DYNLIBS_SUPPORTS_ORDINAL}
-
-type
- TLibHandle = longint;
- TOrdinalEntry = cardinal;
-
-const
- NilHandle = 0;
-// these are for easier crossplatform construction of dll names in dynloading libs.
- SharedSuffix  = 'dll';
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
 uses
  DosCalls;
 
@@ -44,7 +21,7 @@ threadvar
  DynLibErrNo: cardinal;
  DynLibErrPath: array [0..259] of char;
 
-function DoLoadLibrary (const Name: RawbyteString): TLibHandle;
+function SysLoadLibraryA (const Name: RawbyteString): TLibHandle;
 var
  Handle: longint;
 begin
@@ -60,7 +37,12 @@ begin
   end;
 end;
 
-function GetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
+function SysLoadLibraryU (const Name: UnicodeString): TLibHandle;
+begin
+  Result := SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
+end;
+
+function SysGetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
 var
  P: pointer;
 begin
@@ -75,7 +57,7 @@ begin
   end;
 end;
 
-function GetProcedureAddress (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
+function SysGetProcedureAddressOrdinal (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
 var
  P: pointer;
 begin
@@ -90,7 +72,7 @@ begin
   end;
 end;
 
-function UnloadLibrary (Lib: TLibHandle): boolean;
+function SysUnloadLibrary (Lib: TLibHandle): boolean;
 begin
  DynLibErrPath [0] := #0;
  DynLibErrNo := DosFreeModule (Lib);
@@ -104,7 +86,7 @@ begin
  GetDynLibsError := DynLibErrNo;
 end;
 
-function GetDynLibsErrorStr: string;
+function SysGetDynLibsErrorStr: string;
 const
  SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
 var
@@ -114,7 +96,7 @@ var
  RC: cardinal;
 begin
  if DynLibErrNo = 0 then
-  GetDynLibsErrorStr := ''
+  SysGetDynLibsErrorStr := ''
  else
   begin
    Result := '';
@@ -137,8 +119,23 @@ begin
   end;
 end;
 
-function GetLoadErrorStr: string;
+function SysGetLoadErrorStr: string;
 begin
  GetLoadErrorStr := GetDynLibsErrorStr;
 end;
-{$endif}
+
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: @SysGetProcedureAdressOrdinal;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;
+

+ 144 - 0
rtl/os2/sysdlh.inc

@@ -0,0 +1,144 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+
+{$ifdef readinterface}
+
+{ ---------------------------------------------------------------------
+    Interface declarations
+  ---------------------------------------------------------------------}
+
+{$DEFINE DYNLIBS_SUPPORTS_ORDINAL}
+
+type
+ TLibHandle = longint;
+ TOrdinalEntry = cardinal;
+
+const
+ NilHandle = 0;
+// these are for easier crossplatform construction of dll names in dynloading libs.
+ SharedSuffix  = 'dll';
+
+{$else}
+
+{ ---------------------------------------------------------------------
+    Implementation section
+  ---------------------------------------------------------------------}
+
+uses
+ DosCalls;
+
+threadvar
+ DynLibErrNo: cardinal;
+ DynLibErrPath: array [0..259] of char;
+
+function DoLoadLibrary (const Name: RawbyteString): TLibHandle;
+var
+ Handle: longint;
+begin
+ DynLibErrPath [0] := #0;
+ DynLibErrNo := DosLoadModule (@DynLibErrPath [0], SizeOf (DynLibErrPath),
+                                                         PAnsiChar (Name), Handle);
+ if DynLibErrNo = 0 then
+  Result := Handle
+ else
+  begin
+   Result := NilHandle;
+   OSErrorWatch (DynLibErrNo);
+  end;
+end;
+
+function GetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
+var
+ P: pointer;
+begin
+ DynLibErrPath [0] := #0;
+ DynLibErrNo := DosQueryProcAddr (Lib, 0, PChar (ProcName), P);
+ if DynLibErrNo = 0 then
+  Result := P
+ else
+  begin
+   Result := nil;
+   OSErrorWatch (DynLibErrNo);
+  end;
+end;
+
+function GetProcedureAddress (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
+var
+ P: pointer;
+begin
+ DynLibErrPath [0] := #0;
+ DynLibErrNo := DosQueryProcAddr (Lib, Ordinal, nil, P);
+ if DynLibErrNo = 0 then
+  Result := P
+ else
+  begin
+   Result := nil;
+   OSErrorWatch (DynLibErrNo);
+  end;
+end;
+
+function UnloadLibrary (Lib: TLibHandle): boolean;
+begin
+ DynLibErrPath [0] := #0;
+ DynLibErrNo := DosFreeModule (Lib);
+ Result := DynLibErrNo = 0;
+ if DynLibErrNo <> 0 then
+  OSErrorWatch (DynLibErrNo);
+end;
+
+function GetDynLibsError: longint;
+begin
+ GetDynLibsError := DynLibErrNo;
+end;
+
+function GetDynLibsErrorStr: string;
+const
+ SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
+var
+ VarArr: array [1..9] of PChar;
+ OutBuf: array [0..999] of char;
+ RetMsgSize: cardinal;
+ RC: cardinal;
+begin
+ if DynLibErrNo = 0 then
+  GetDynLibsErrorStr := ''
+ else
+  begin
+   Result := '';
+   VarArr [1] := @DynLibErrPath [0];
+   RC := DosGetMessage (@VarArr, 1, @OutBuf [0], SizeOf (OutBuf),
+                                     DynLibErrNo, @SysMsgFile [0], RetMsgSize);
+   if RC = 0 then
+    begin
+     SetLength (Result, RetMsgSize);
+     Move (OutBuf [0], Result [1], RetMsgSize);
+    end
+   else
+    begin
+     Str (DynLibErrNo, Result);
+     Result := 'Error ' + Result;
+     if DynLibErrPath [0] <> #0 then
+      Result := StrPas (@DynLibErrPath [0]) + ' - ' + Result;
+     OSErrorWatch (RC);
+    end;
+  end;
+end;
+
+function GetLoadErrorStr: string;
+begin
+ GetLoadErrorStr := GetDynLibsErrorStr;
+end;
+{$endif}

+ 2 - 0
rtl/os2/system.pas

@@ -30,6 +30,7 @@ interface
 {$DEFINE OS2UNICODE}
 {$define DISABLE_NO_THREAD_MANAGER}
 {$DEFINE HAS_GETCPUCOUNT}
+{$define FPC_SYSTEM_HAS_SYSDLH}
 
 {$I systemh.inc}
 
@@ -1383,6 +1384,7 @@ begin
   DefaultFileType := '';
 
   InitSystemThreads;
+  InitSystemDynLibs;
 
 {$IFDEF EXTDUMPGROW}
 {    Int_HeapSize := high (cardinal);}

+ 1 - 0
rtl/solaris/system.pp

@@ -286,6 +286,7 @@ Begin
 { Arguments }
   SetupCmdLine;
   InitSystemThreads;
+  InitSystemDynLibs;
   { restore original signal handlers in case this is a library }
   if IsLibrary then
     RestoreOldSignalHandlers;

+ 23 - 32
rtl/unix/dynlibs.inc

@@ -13,36 +13,9 @@
 
  **********************************************************************}
 
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-Type
-  { using PtrInt here is compliant with the other platforms }
-  TLibHandle = PtrInt;
-
-Const
-  NilHandle = TLibHandle(0);
-// these are for easier crossplatform construction of dll names in dynloading libs.
-{$if defined(Darwin)}
-  SharedSuffix = 'dylib';
-{$elseif defined(aix)}
-  SharedSuffix = 'a';
-{$else}
-  SharedSuffix = 'so';
-{$endif}
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
 uses dl;
 
-Function DoLoadLibrary(const Name : RawByteString) : TLibHandle;
+Function SysLoadLibraryA(const Name : RawByteString) : TLibHandle;
 {$ifdef aix}
 var
   MemberName: RawByteString;
@@ -65,23 +38,41 @@ begin
 {$endif aix}
 end;
 
-Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+Function SysLoadLibraryU(const Name: UnicodeString) : TLibHandle;
+begin
+  Result := SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
+end;
+
+Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
 
 begin
   Result:=dlsym(lib,pchar(ProcName));
 end;
 
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
 
 begin
   Result:=dlClose(Lib)=0;
 end;
 
-Function GetLoadErrorStr: string;
+Function SysGetLoadErrorStr: string;
 
 begin
   Result:=dl.dlerror;
 end;
 
-{$endif}
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: Nil;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;
 

+ 30 - 0
rtl/unix/sysdlh.inc

@@ -0,0 +1,30 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implement OS-dependent part of dynamic library loading.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+Type
+  { using PtrInt here is compliant with the other platforms }
+  TLibHandle = PtrInt;
+
+Const
+  NilHandle = TLibHandle(0);
+// these are for easier crossplatform construction of dll names in dynloading libs.
+{$if defined(Darwin)}
+  SharedSuffix = 'dylib';
+{$elseif defined(aix)}
+  SharedSuffix = 'a';
+{$else}
+  SharedSuffix = 'so';
+{$endif}
+

+ 1 - 0
rtl/unix/sysunixh.inc

@@ -15,6 +15,7 @@
  **********************************************************************}
 
 {$define newsignal}
+{$define FPC_SYSTEM_HAS_SYSDLH}
 
 {$I systemh.inc}
 

+ 2 - 64
rtl/win/dynlibs.inc

@@ -13,69 +13,7 @@
 
  **********************************************************************}
 
-
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-{$define DYNLIBS_SUPPORTS_ORDINAL}
-
-Type
-  TLibHandle = System.THandle;
-  TOrdinalEntry = word;
-
-Const
-  NilHandle = 0;
-// these are for easier crossplatform construction of dll names in dynloading libs.
-  SharedSuffix = 'dll';
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
-Uses windows;
-
-Function DoLoadLibrary(const Name : UnicodeString) : TlibHandle;
-
-begin
-  Result:=Windows.LoadLibraryW(PWideChar(Name));
-end;
-
-Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
-
+procedure InitDynLibs;
 begin
-  Result:=Windows.GetProcAddress(Lib,PChar(ProcName));
+  { nothing to do here since Windows DynLibs is done in System }
 end;
-
-Function GetProcedureAddress(Lib : TLibHandle; Ordinal : Word) : Pointer;
-
-begin
-  Result:=Windows.GetProcAddress(Lib,PChar(Ordinal));
-end;
-
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
-
-begin
-  Result:=Windows.FreeLibrary(Lib);
-end;
-
-Function GetLoadErrorStr: string;
-
-Var
-  rc,c : integer;
-  
-begin  
-  rc := GetLastError;
-  SetLength(Result,255);
-  C:=FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,nil,rc,
-                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                 @Result[1], 255,nil);
-  SetLength(Result,c);
-end;
-
-{$endif}
-

+ 78 - 0
rtl/win/sysdl.inc

@@ -0,0 +1,78 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+
+Function SysLoadLibraryU(const Name : UnicodeString) : TlibHandle;
+
+begin
+  Result:=WinLoadLibraryW(PWideChar(Name));
+end;
+
+Function SysLoadLibraryA(const Name: RawByteString) : TLibHandle;
+
+begin
+  Result:=WinLoadLibraryW(PWideChar(UnicodeString(Name)));
+end;
+
+Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+
+begin
+  Result:=WinGetProcAddress(Lib,PChar(ProcName));
+end;
+
+{$push}
+{$warn 4056 off}
+Function SysGetProcedureAddressOrdinal(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
+
+begin
+  Result:=WinGetProcAddress(Lib,PChar(Ordinal));
+end;
+{$pop}
+
+Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
+
+begin
+  Result:=WinFreeLibrary(Lib);
+end;
+
+Function SysGetLoadErrorStr: string;
+
+Var
+  rc,c : integer;
+  temp: WideString;
+begin  
+  rc := GetLastError;
+  SetLength(temp,255);
+  C:=FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,nil,rc,
+                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                 @temp[1], 255,nil);
+  SetLength(temp,c);
+  Result:=AnsiString(temp);
+end;
+
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: @SysGetProcedureAddressOrdinal;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitSystemDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;

+ 24 - 0
rtl/win/sysdlh.inc

@@ -0,0 +1,24 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+type
+  TLibHandle = THandle;
+  TOrdinalEntry = word;
+
+const
+  NilHandle = 0;
+// these are for easier crossplatform construction of dll names in dynloading libs.
+  SharedSuffix = 'dll';
+

+ 18 - 3
rtl/win/sysos.inc

@@ -114,6 +114,10 @@ const
 
     EXCEPTION_MAXIMUM_PARAMETERS = 15;
 
+    FORMAT_MESSAGE_FROM_SYSTEM = 4096;
+    LANG_NEUTRAL = $00;
+    SUBLANG_DEFAULT = $00;
+
 type
    {UINT  = longint;
    BOOL  = longint; obsolete }
@@ -140,6 +144,13 @@ type
    PLPSTR  = ^LPSTR;
    PLPWSTR = ^LPWSTR;
 
+   { WARNING
+     the variable argument list
+     is not implemented for FPC
+     va_list is just a dummy record
+     MvdV: Nevertheless it should be a pointer type, not a record}
+   va_list = pchar;
+
   PSecurityAttributes = ^TSecurityAttributes;
   TSecurityAttributes = record
     nLength : DWORD;
@@ -288,15 +299,19 @@ type
      {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetFileSize';
    function SetEndOfFile(h : thandle) : longbool;
      {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetEndOfFile';
-   function FreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
 
    procedure GetSystemInfo(lpSystemInfo: PSystemInfo); {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetSystemInfo';
 
+   function WinLoadLibraryW(lpLibFileName:pwidechar):THandle; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'LoadLibraryW';
+   function WinLoadLibraryA(lpLibFileName:pwidechar):THandle; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'LoadLibraryA';
+   function WinGetProcAddress(hModule:THandle; lpProcName:pchar):pointer; {$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'GetProcAddress';
+   function WinFreeLibrary(hLibModule:THandle):ByteBool; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'FreeLibrary';
+
+   function FormatMessageW(dwFlags:DWORD; lpSource:LPCVOID; dwMessageId:DWORD; dwLanguageId:DWORD; lpBuffer:LPWSTR;nSize:DWORD; Arguments:va_list):DWORD;{$ifdef wince}cdecl{$else}stdcall{$endif}; external KernelDLL name 'FormatMessageW';
+
 {$ifndef WINCE}
-   function LoadLibrary(lpLibFileName:pchar):THandle; stdcall; external KernelDLL name 'LoadLibraryA';
    function GetFileType(Handle:thandle):DWord;
      stdcall;external KernelDLL name 'GetFileType';
-   function GetProcAddress(hModule:THandle; lpProcName:pchar):pointer; stdcall; external KernelDLL name 'GetProcAddress';
 
    { File }
    function DeleteFileW(p : punicodechar) : longint;

+ 3 - 0
rtl/win32/system.pp

@@ -28,6 +28,8 @@ interface
 
 {$define DISABLE_NO_THREAD_MANAGER}
 {$define HAS_WIDESTRINGMANAGER}
+{$define DISABLE_NO_DYNLIBS_MANAGER}
+{$define FPC_SYSTEM_HAS_SYSDLH}
 
 {$ifdef FPC_USE_WIN32_SEH}
   {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
@@ -676,6 +678,7 @@ begin
   SysInitStdIO;
   { Arguments }
   setup_arguments;
+  InitSystemDynLibs;
   { Reset IO Error }
   InOutRes:=0;
   ProcessID := GetCurrentProcessID;

+ 3 - 0
rtl/win64/system.pp

@@ -25,6 +25,8 @@ interface
 
 {$define DISABLE_NO_THREAD_MANAGER}
 {$define HAS_WIDESTRINGMANAGER}
+{$define DISABLE_NO_DYNLIBS_MANAGER}
+{$define FPC_SYSTEM_HAS_SYSDLH}
 
 {$ifdef FPC_USE_WIN64_SEH}
   {$define FPC_SYSTEM_HAS_RAISEEXCEPTION}
@@ -612,6 +614,7 @@ begin
   SysInitStdIO;
   { Arguments }
   setup_arguments;
+  InitSystemDynLibs;
   { Reset IO Error }
   InOutRes:=0;
   ProcessID := GetCurrentProcessID;

+ 2 - 63
rtl/wince/dynlibs.inc

@@ -14,69 +14,8 @@
  **********************************************************************}
 
 
-{$ifdef readinterface}
-
-{ ---------------------------------------------------------------------
-    Interface declarations
-  ---------------------------------------------------------------------}
-
-{$define DYNLIBS_SUPPORTS_ORDINAL}
-
-Type
-  TLibHandle = Longint;
-  TOrdinalEntry = word;
-
-Const
-  NilHandle = 0;
-// these are for easier crossplatform construction of dll names in dynloading libs.
-  SharedSuffix = 'dll';
-
-{$else}
-
-{ ---------------------------------------------------------------------
-    Implementation section
-  ---------------------------------------------------------------------}
-
-Uses windows;
-
-Function DoLoadLibrary(const Name : UnicodeString) : TlibHandle;
-begin
-  Result:=Windows.LoadLibrary(PWideChar(Name));
-end;
-
-Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
-var
-  ws: PWideChar;
-begin
-  ws:=StringToPWideChar(ProcName);
-  Result:=Windows.GetProcAddress(Lib, ws);
-  FreeMem(ws);
-end;
-
-Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
-begin
-  Result:=Windows.GetProcAddress(Lib, PWideChar(Ordinal));
-end;
-
-Function UnloadLibrary(Lib : TLibHandle) : Boolean;
+procedure InitDynLibs;
 begin
-  Result:=Windows.FreeLibrary(Lib);
-end;
-
-Function GetLoadErrorStr: string;
-
-Var
-  rc,c : integer;
-  w : widestring;
-  
-begin  
-  rc := GetLastError;
-  SetLength(w,255);
-  C:=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,rc,
-                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                 @W[1], 255,nil);
-  SetLength(w,c);
-  Result:=w;
+  { nothing to do here since Windows DynLibs is done in System }
 end;
 
-{$endif}

+ 78 - 0
rtl/wince/sysdl.inc

@@ -0,0 +1,78 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+
+Function SysLoadLibraryU(const Name : UnicodeString) : TlibHandle;
+
+begin
+  Result:=WinLoadLibraryW(PWideChar(Name));
+end;
+
+Function SysLoadLibraryA(const Name: RawByteString) : TLibHandle;
+
+begin
+  Result:=WinLoadLibraryW(PWideChar(UnicodeString(Name)));
+end;
+
+Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
+
+begin
+  Result:=WinGetProcAddress(Lib,PChar(ProcName));
+end;
+
+{$push}
+{$warn 4056 off}
+Function SysGetProcedureAddressOrdinal(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
+
+begin
+  Result:=WinGetProcAddress(Lib,PChar(Ordinal));
+end;
+{$pop}
+
+Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
+
+begin
+  Result:=WinFreeLibrary(Lib);
+end;
+
+Function SysGetLoadErrorStr: string;
+
+Var
+  rc,c : integer;
+  temp: WideString;
+begin  
+  rc := GetLastError;
+  SetLength(temp,255);
+  C:=FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,nil,rc,
+                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                 @temp[1], 255,nil);
+  SetLength(temp,c);
+  Result:=AnsiString(temp);
+end;
+
+const
+  SysDynLibsManager: TDynLibsManager = (
+    LoadLibraryU: @SysLoadLibraryU;
+    LoadLibraryA: @SysLoadLibraryA;
+    GetProcAddress: @SysGetProcedureAddress;
+    GetProcAddressOrdinal: @SysGetProcedureAddressOrdinal;
+    UnloadLibrary: @SysUnloadLibrary;
+    GetLoadErrorStr: @SysGetLoadErrorStr;
+  );
+
+procedure InitSystemDynLibs;
+begin
+  SetDynLibsManager(SysDynLibsManager);
+end;

+ 24 - 0
rtl/wince/sysdlh.inc

@@ -0,0 +1,24 @@
+{
+    This file is part of the Free Pascal run time library.
+    Copyright (c) 1999-2000 by the Free Pascal development team
+
+    Implements OS dependent part for loading of dynamic libraries.
+
+    See the file COPYING.FPC, included in this distribution,
+    for details about the copyright.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+type
+  TLibHandle = THandle;
+  TOrdinalEntry = word;
+
+const
+  NilHandle = 0;
+// these are for easier crossplatform construction of dll names in dynloading libs.
+  SharedSuffix = 'dll';
+

+ 1 - 0
rtl/wince/system.pp

@@ -1769,6 +1769,7 @@ initialization
   ProcessID := GetCurrentProcessID;
   { threading }
   InitSystemThreads;
+  InitSystemDynLibs;
   DispCallByIDProc:=@DoDispCallByIDError;
 
 finalization