summaryrefslogtreecommitdiffstats
path: root/tkimg/compat/tclLoadWin.c
blob: dcfb8f17a2cd8dc2f7b07ad54f9e0aaa8ed847fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*

 * tclLoadWin.c --

 *

 *	This procedure provides a version of dlopen() that

 *	works with the Windows "LoadLibrary" and "GetProcAddress"

 *	API for dynamic loading.

 *

 */

#include "imgInt.h"

#ifdef USE_TCL_STUBS

#   include "tclWinInt.h"

#else

#   include <windows.h>

#endif

#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(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(
    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(
    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(
    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 (char *) 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
) {
    LibraryList *ptr;
    LibraryList *list = *((LibraryList **) clientData);

    while (list != NULL) {
	FreeLibrary(list->handle);
	ptr = list->nextPtr;
	ckfree((char *) list);
	list = ptr;
    }
}