Skip to content
Tcl Source Code View Ticket
Login

View Ticket

DL enhancement for extensions (dl* compat functions)

Deleted219180ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 20 years ago
Descriptionplain2000-10-26 05:02:55
OriginalBugID: 2113 RFE
Version: 8.1
SubmitDate: '1999-05-20'
LastModified: '2000-04-03'
Severity: MED
Status: Assigned
Submitter: pat
ChangedBy: hobbs
OS: Windows 95
Machine: X86
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
Jan Nijtmans

Comments:
This feature is already supplied by the plus-patches for Tcl7.6 and
    higher. It is duplicated in Img and Trf, in order to make those two
    extensions as independant as possible from the plus-patches. If
    those functions were provided in the stub table that would make
    the build process of Img and Trf a lot easier, a valuable
    addition to the TEA-summit.

    I have compatibility functions for AIX, ULTRIX, Macintosh, OSF
    HP-UX and the other UNIX variants which support dynamical loading
    as well. For now, I restrict this submission for Windows only,
    in order to simplify the evaluation process by Scriptics. All
    other functions can be found in Img 1.2 and in Trf 1.6, in the
    /compat/ directory. For UNIX, things are a little bit more
    complicated, because the configure script has the task to find
    out which one should be used.

    Because system libraries normally only use ASCII characters in
    their name, there is no need for extra precautions regarding
    UTF support. Therefore this patch should work for Tcl8.0.6
    as well.

DesiredBehavior:
Currently, extensions like Img, Trf, dllhelper and possibly
    others, which need to load other system dll's or shared
    libraries, only have access to the dlopen(), dlclose(), dlsym()
    and dlerror() functions in UNIX.

    It would be a lot easier if Tcl provided those functions,
    as compatibility functions in the stub table. Then extensions
    don't have to duplicate the trouble of finding out how
    dynamical loading should be done on each platform.

    tclLoadWin.c should be compiled with -DUSE_TCL_STUBS and
    -DSTATIC_BUILD (in order to prevent that the symbols are
    exported), and added to the stub library (as done in
    makefile.vc).

Patch:
*** win/makefile.vc.origThu May 20 20:13:48 1999
--- win/makefile.vcThu May 20 20:16:44 1999
***************
*** 206,211 ****
--- 206,212 ----
  $(TMPDIR)\tclWinTime.obj 

  TCLSTUBOBJS = $(TMPDIR)\tclStubLib.obj  + $(TMPDIR)\tclLoadWin.obj

  cc32= "$(TOOLS32)\bin\cl.exe"
  link32= "$(TOOLS32)\bin\link.exe"
***************
*** 531,536 ****
--- 532,539 ----
  $(TMPDIR)\tclStubLib.obj : $(GENERICDIR)\tclStubLib.c
  $(cc32) $(TCL_CFLAGS) -DSTATIC_BUILD -Fo$@ $?

+ $(TMPDIR)\tclLoadWin.obj : $(WINDIR)\tclLoadWin.c
+ $(cc32) $(TCL_CFLAGS) -DSTATIC_BUILD -DUSE_TCL_STUBS -Fo$@ $?

  # Dedependency rules

*** win/tclLoadWin.c.origThu May 20 20:03:36 1999
--- win/tclLoadWin.cThu May 20 20:23:33 1999
***************
*** 0 ****
--- 1,195 ----
+ /* 
+  * tclLoadWin.c --
+  *
+  *This procedure provides a version of dlopen() that
+  *works with the Windows "LoadLibrary" and "GetProcAddress"
+  *API for dynamic loading.
+  *
+  */
+ 
+ #include <windows.h>
+ #include "tclWinInt.h"
+ #include "compat/dlfcn.h"
+ 
+ typedef struct LibraryList {
+     HINSTANCE handle;
+     struct LibraryList *nextPtr;
+ } LibraryList;
+ 
+ static LibraryList *libraryList = NULL;/* List of currently loaded DLL's.  */
+ 
+ /*
+  * Declarations for functions that are only used in this file.
+  */
+ 
+ static void UnloadLibraries _ANSI_ARGS_((ClientData clientData));
+ 
+ 
+ /*
+  *----------------------------------------------------------------------
+  *
+  * dlopen --
+  *
+  *This function is an alternative for the functions
+  *TclWinLoadLibrary and TclWinGetTclInstance.  It is
+  *responsible for adding library handles to the library list so
+  *the libraries can be freed when tcl.dll is unloaded.
+  *
+  * Results:
+  *Returns the handle of the newly loaded library, or NULL on
+  *failure. If path is NULL, the global library instance handle
+  *is returned.
+  *
+  * Side effects:
+  *Loads the specified library into the process.
+  *
+  *----------------------------------------------------------------------
+  */
+ 
+ VOID *dlopen(path, mode)
+     CONST char *path;
+     int mode;
+ {
+     VOID *handle;
+     LibraryList *ptr;
+     static int initialized = 0;
+ 
+     if (!initialized) {
+ initialized = 1;
+ Tcl_CreateExitHandler((Tcl_ExitProc *) UnloadLibraries,
+     (ClientData) &libraryList);
+     }
+     handle = (VOID *) LoadLibrary(path);
+     if (handle != NULL) {
+     ptr = (LibraryList*) ckalloc(sizeof(LibraryList));
+     ptr->handle = (HINSTANCE) handle;
+     ptr->nextPtr = libraryList;
+     libraryList = ptr;
+     }
+     return handle;
+ }
+ 
+ 
+ /*
+  *----------------------------------------------------------------------
+  *
+  * dlclose --
+  *
+  *This function is an alternative for the function
+  *FreeLibrary.  It is responsible for removing library
+  *handles from the library list and remove the dll
+  *from memory.
+  *
+  * Results:
+  *-1 on error, 0 on success.
+  *
+  * Side effects:
+  *Removes the specified library from the process.
+  *
+  *----------------------------------------------------------------------
+  */
+ 
+ int
+ dlclose(handle)
+     VOID *handle;
+ {
+     LibraryList *ptr, *prevPtr;
+ 
+     ptr = libraryList; prevPtr = NULL;
+     while (ptr != NULL) {
+ if (ptr->handle == (HINSTANCE) handle) {
+     FreeLibrary((HINSTANCE) handle);
+     if (prevPtr) {
+ prevPtr->nextPtr = ptr->nextPtr;
+     } else {
+ libraryList = ptr->nextPtr;
+     }
+     ckfree((char *) ptr);
+     return 0;
+ }
+ prevPtr = ptr;
+ ptr = ptr->nextPtr;
+     }
+     return -1;
+ }
+ 
+ /*
+  *----------------------------------------------------------------------
+  *
+  * dlsym --
+  *
+  *This function is an alternative for the system function
+  *GetProcAddress. It returns the address of a
+  *symbol, give the handle returned by dlopen().
+  *
+  * Results:
+  *Returns the address of the symbol in the dll.
+  *
+  * Side effects:
+  *None.
+  *
+  *----------------------------------------------------------------------
+  */
+ 
+ VOID *dlsym(handle, symbol)
+     VOID *handle;
+     CONST char *symbol;
+ {
+     return (VOID *) GetProcAddress((HINSTANCE) handle, symbol);
+ }
+ 
+ 
+ /*
+  *----------------------------------------------------------------------
+  *
+  * dlerror --
+  *
+  *This function returns a string describing the error which
+  *occurred in dlopen().
+  *
+  * Results:
+  *Returns an error message.
+  *
+  * Side effects:
+  *errno is set.
+  *
+  *----------------------------------------------------------------------
+  */
+ 
+ char *
+ dlerror()
+ {
+     TclWinConvertError(GetLastError());
+     return Tcl_ErrnoMsg(Tcl_GetErrno());
+ }
+ 
+ /*
+  *----------------------------------------------------------------------
+  *
+  * UnloadLibraries --
+  *
+  *Frees any dynamically allocated libraries loaded by Tcl.
+  *
+  * Results:
+  *None.
+  *
+  * Side effects:
+  *Frees the libraries on the library list as well as the list.
+  *
+  *----------------------------------------------------------------------
+  */
+ 
+ static void
+ UnloadLibraries(clientData)
+     ClientData clientData;
+ {
+     LibraryList *ptr;
+     LibraryList *list = *((LibraryList **) clientData);
+ 
+     while (list != NULL) {
+ FreeLibrary(list->handle);
+ ptr = list->nextPtr;
+ ckfree((char *) list);
+ list = ptr;
+     }
+ }

PatchFiles:
win/makefile.vc win/tclLoadWin.c (new file)



I guess the purpose is to allow extensions that want to do their
own dl'ing of libraries to just use dlopen... 
-- 04/03/2000 hobbs
dgp Ticket [219180ffff]: 1 change [History diff]2001-04-05 06:52:46
hobbs Ticket [219180ffff]: 2 changes [History diff]2001-08-02 08:46:30
andreas_kupriesplain2001-08-25 00:40:47
Logged In: YES 
user_id=75003

Yes, in a portable way ...
Something for 8.4, or 9.0 ?
TIP required ?
vincentdarleyplain2001-08-29 23:08:54
Logged In: YES 
user_id=32170

This looks rather like something which could be 
incorporated into the vfs overhaul.  Perhaps Jan would like 
to see how it would fit in?  Certainly it would be a shame 
if 'load' and Tcl_FSLoadFile work from a Vfs, but a 
new 'dlopen' doesn't.
dkf Ticket [219180ffff]: 2 changes [History diff]2007-03-19 01:28:39
hobbs Deleted ticket [219180ffff]. [History diff]2007-03-23 06:46:21
nobodyplain2007-03-23 06:47:31
Logged In: NO 

test of non logged in user modifying this bug
Log in (anonymous is fine) to add a comment.