os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/win/tclWinLoad.c
Update contrib.
4 * This procedure provides a version of the TclLoadFile that
5 * works with the Windows "LoadLibrary" and "GetProcAddress"
6 * API for dynamic loading.
8 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * RCS: @(#) $Id: tclWinLoad.c,v 1.15 2002/10/10 12:25:53 vincentdarley Exp $
16 #include "tclWinInt.h"
20 *----------------------------------------------------------------------
24 * Dynamically loads a binary code file into memory and returns
25 * a handle to the new code.
28 * A standard Tcl completion code. If an error occurs, an error
29 * message is left in the interp's result.
32 * New code suddenly appears in memory.
34 *----------------------------------------------------------------------
38 TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
39 Tcl_Interp *interp; /* Used for error reporting. */
40 Tcl_Obj *pathPtr; /* Name of the file containing the desired
42 Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded
43 * file which will be passed back to
44 * (*unloadProcPtr)() to unload the file. */
45 Tcl_FSUnloadFileProc **unloadProcPtr;
46 /* Filled with address of Tcl_FSUnloadFileProc
47 * function which should be used for
51 CONST TCHAR *nativeName;
54 * First try the full path the user gave us. This is particularly
55 * important if the cwd is inside a vfs, and we are trying to load
56 * using a relative path.
58 nativeName = Tcl_FSGetNativePath(pathPtr);
59 handle = (*tclWinProcs->loadLibraryProc)(nativeName);
62 * Let the OS loader examine the binary search path for
63 * whatever string the user gave us which hopefully refers
64 * to a file on the binary path
67 char *fileName = Tcl_GetString(pathPtr);
68 nativeName = Tcl_WinUtfToTChar(fileName, -1, &ds);
69 handle = (*tclWinProcs->loadLibraryProc)(nativeName);
73 *loadHandle = (Tcl_LoadHandle) handle;
76 DWORD lastError = GetLastError();
79 * It would be ideal if the FormatMessage stuff worked better,
80 * but unfortunately it doesn't seem to want to...
85 size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
86 FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, lastError, 0,
87 (LPTSTR) &lpMsgBuf, 0, NULL);
88 buf = (char *) ckalloc((unsigned) TCL_INTEGER_SPACE + size + 1);
89 sprintf(buf, "%d %s", lastError, (char *)lpMsgBuf);
91 Tcl_AppendResult(interp, "couldn't load library \"",
92 Tcl_GetString(pathPtr), "\": ", (char *) NULL);
94 * Check for possible DLL errors. This doesn't work quite right,
95 * because Windows seems to only return ERROR_MOD_NOT_FOUND for
96 * just about any problem, but it's better than nothing. It'd be
97 * even better if there was a way to get what DLLs
100 case ERROR_MOD_NOT_FOUND:
101 case ERROR_DLL_NOT_FOUND:
102 Tcl_AppendResult(interp, "this library or a dependent library",
103 " could not be found in library path",
106 case ERROR_PROC_NOT_FOUND:
107 Tcl_AppendResult(interp, "could not find specified procedure",
110 case ERROR_INVALID_DLL:
111 Tcl_AppendResult(interp, "this library or a dependent library",
112 " is damaged", (char *) NULL);
114 case ERROR_DLL_INIT_FAILED:
115 Tcl_AppendResult(interp, "the library initialization",
116 " routine failed", (char *) NULL);
119 TclWinConvertError(lastError);
120 Tcl_AppendResult(interp, Tcl_PosixError(interp),
125 *unloadProcPtr = &TclpUnloadFile;
131 *----------------------------------------------------------------------
135 * Looks up a symbol, by name, through a handle associated with
136 * a previously loaded piece of code (shared library).
139 * Returns a pointer to the function associated with 'symbol' if
140 * it is found. Otherwise returns NULL and may leave an error
141 * message in the interp's result.
143 *----------------------------------------------------------------------
146 TclpFindSymbol(interp, loadHandle, symbol)
148 Tcl_LoadHandle loadHandle;
151 Tcl_PackageInitProc *proc = NULL;
152 HINSTANCE handle = (HINSTANCE)loadHandle;
155 * For each symbol, check for both Symbol and _Symbol, since Borland
156 * generates C symbols with a leading '_' by default.
159 proc = (Tcl_PackageInitProc *) GetProcAddress(handle, symbol);
162 Tcl_DStringInit(&ds);
163 Tcl_DStringAppend(&ds, "_", 1);
164 symbol = Tcl_DStringAppend(&ds, symbol, -1);
165 proc = (Tcl_PackageInitProc *) GetProcAddress(handle, symbol);
166 Tcl_DStringFree(&ds);
172 *----------------------------------------------------------------------
176 * Unloads a dynamically loaded binary code file from memory.
177 * Code pointers in the formerly loaded file are no longer valid
178 * after calling this function.
184 * Code removed from memory.
186 *----------------------------------------------------------------------
190 TclpUnloadFile(loadHandle)
191 Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
192 * to TclpDlopen(). The loadHandle is
193 * a token that represents the loaded
198 handle = (HINSTANCE) loadHandle;
203 *----------------------------------------------------------------------
205 * TclGuessPackageName --
207 * If the "load" command is invoked without providing a package
208 * name, this procedure is invoked to try to figure it out.
211 * Always returns 0 to indicate that we couldn't figure out a
212 * package name; generic code will then try to guess the package
213 * from the file name. A return value of 1 would have meant that
214 * we figured out the package name and put it in bufPtr.
219 *----------------------------------------------------------------------
223 TclGuessPackageName(fileName, bufPtr)
224 CONST char *fileName; /* Name of file containing package (already
225 * translated to local form if needed). */
226 Tcl_DString *bufPtr; /* Initialized empty dstring. Append
227 * package name to this if possible. */