Release 940714
[wine/multimedia.git] / loader / library.c
blob2ceb59dd1f04fa8df142660b684b0c59a1eed1b7
1 /*
2 * Modules & Libraries functions
3 */
4 static char Copyright[] = "Copyright Martin Ayotte, 1994";
6 /*
7 #define DEBUG_MODULE
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include "wine.h"
17 #include "prototypes.h"
18 #include "windows.h"
19 #include "dlls.h"
20 #include "task.h"
21 #include "toolhelp.h"
23 extern struct w_files *wine_files;
24 extern struct dll_name_table_entry_s dll_builtin_table[];
26 struct w_files *GetFileInfo(HANDLE);
27 char *GetDosFileName(char *);
29 #define IS_BUILTIN_DLL(handle) ((handle >> 8) == 0xff)
31 /**********************************************************************/
33 void ExtractDLLName(char *libname, char *temp)
35 int i;
37 strcpy(temp, libname);
38 if (strchr(temp, '\\') || strchr(temp, '/'))
39 for (i = strlen(temp) - 1; i ; i--)
40 if (temp[i] == '\\' || temp[i] == '/') {
41 strcpy(temp, temp + i + 1);
42 break;
44 for (i = strlen(temp) - 1; i ; i--)
45 if (temp[i] == '.') {
46 temp[i] = 0;
47 break;
51 /**********************************************************************
52 * GetModuleHandle [KERNEL.47]
54 HANDLE GetModuleHandle(LPSTR lpModuleName)
56 register struct w_files *w = wine_files;
57 int i;
58 char dllname[256];
60 if ((int) lpModuleName & 0xffff0000)
61 ExtractDLLName(lpModuleName, dllname);
63 if ((int) lpModuleName & 0xffff0000)
64 printf("GetModuleHandle('%s');\n", lpModuleName);
65 else
66 printf("GetModuleHandle('%x');\n", lpModuleName);
68 /* printf("GetModuleHandle // searching in builtin libraries\n");*/
69 for (i = 0; i < N_BUILTINS; i++) {
70 if (dll_builtin_table[i].dll_name == NULL) break;
71 if (((int) lpModuleName & 0xffff0000) == 0) {
72 if (0xFF00 + i == (int) lpModuleName) {
73 printf("GetModuleHandle('%s') return %04X \n",
74 lpModuleName, 0xff00 + i);
75 return 0xFF00 + i;
78 else if (strcasecmp(dll_builtin_table[i].dll_name, dllname) == 0) {
79 printf("GetModuleHandle('%x') return %04X \n",
80 lpModuleName, 0xFF00 + i);
81 return (0xFF00 + i);
85 printf("GetModuleHandle // searching in loaded modules\n");
86 while (w) {
87 /* printf("GetModuleHandle // '%x' \n", w->name); */
88 if (((int) lpModuleName & 0xffff0000) == 0) {
89 if (w->hinstance == (int) lpModuleName) {
90 printf("GetModuleHandle('%x') return %04X \n",
91 lpModuleName, w->hinstance);
92 return w->hinstance;
95 else if (strcasecmp(w->name, dllname) == 0) {
96 printf("GetModuleHandle('%s') return %04X \n",
97 lpModuleName, w->hinstance);
98 return w->hinstance;
100 w = w->next;
102 printf("GetModuleHandle('%x') not found !\n", lpModuleName);
103 return 0;
107 /**********************************************************************
108 * GetModuleUsage [KERNEL.48]
110 int GetModuleUsage(HANDLE hModule)
112 struct w_files *w;
114 printf("GetModuleUsage(%04X);\n", hModule);
116 /* built-in dll ? */
117 if (IS_BUILTIN_DLL(hModule))
118 return 2;
120 w = GetFileInfo(hModule);
121 /* return w->Usage; */
122 return 1;
126 /**********************************************************************
127 * GetModuleFilename [KERNEL.49]
129 int GetModuleFileName(HANDLE hModule, LPSTR lpFileName, short nSize)
131 struct w_files *w;
132 LPSTR str;
133 char windir[256], temp[256];
135 printf("GetModuleFileName(%04X, %08X, %d);\n", hModule, lpFileName, nSize);
137 if (lpFileName == NULL) return 0;
138 if (nSize < 1) return 0;
140 /* built-in dll ? */
141 if (IS_BUILTIN_DLL(hModule)) {
142 GetWindowsDirectory(windir, sizeof(windir));
143 sprintf(temp, "%s\\%s.DLL", windir, dll_builtin_table[hModule & 0x00ff].dll_name);
144 ToDos(temp);
145 strncpy(lpFileName, temp, nSize);
146 printf("GetModuleFileName copied '%s' (internal dll) return %d \n", lpFileName, nSize);
147 return strlen(lpFileName);
150 /* check loaded dlls */
151 if ((w = GetFileInfo(hModule)) == NULL)
152 return 0;
153 str = GetDosFileName(w->filename);
154 if (nSize > strlen(str)) nSize = strlen(str) + 1;
155 strncpy(lpFileName, str, nSize);
156 printf("GetModuleFileName copied '%s' return %d \n", lpFileName, nSize);
157 return nSize - 1;
161 /**********************************************************************
162 * LoadLibrary [KERNEL.95]
164 HANDLE LoadLibrary(LPSTR libname)
166 HANDLE h;
168 if ((h = LoadImage(libname, DLL, 0)) < 32)
169 return h;
171 if (!IS_BUILTIN_DLL(h))
172 InitDLL(GetFileInfo(h));
174 return h;
178 /**********************************************************************
179 * FreeLibrary [KERNEL.96]
181 void FreeLibrary(HANDLE hLib)
183 printf("FreeLibrary(%04X);\n", hLib);
185 /* built-in dll ? */
186 if (IS_BUILTIN_DLL(hLib))
187 return;
190 while (lpMod != NULL) {
191 if (lpMod->hInst == hLib) {
192 if (lpMod->Count == 1) {
193 if (hLib != (HANDLE)NULL) GlobalFree(hLib);
194 if (lpMod->ModuleName != NULL) free(lpMod->ModuleName);
195 if (lpMod->FileName != NULL) free(lpMod->FileName);
196 GlobalFree(lpMod->hModule);
197 printf("FreeLibrary // freed !\n");
198 return;
200 lpMod->Count--;
201 printf("FreeLibrary // Count decremented !\n");
202 return;
204 lpMod = lpMod->lpNextModule;
210 /**********************************************************************
211 * GetProcAddress [KERNEL.50]
213 FARPROC GetProcAddress(HANDLE hModule, char *proc_name)
215 #ifdef WINELIB
216 WINELIB_UNIMP ("GetProcAddress");
217 #else
218 int i, sel, addr, ret;
219 register struct w_files *w = wine_files;
220 int ordinal, len;
221 char * cpnt;
222 char C[128];
223 HTASK hTask;
224 LPTASKENTRY lpTask;
226 /* built-in dll ? */
227 if (IS_BUILTIN_DLL(hModule))
229 if ((int) proc_name & 0xffff0000)
231 printf("GetProcAddress: builtin %#04X, '%s'\n",
232 hModule, proc_name);
233 if (GetEntryDLLName(dll_builtin_table[hModule - 0xFF00].dll_name,
234 proc_name, &sel, &addr))
236 printf("Address not found !\n");
239 else
241 printf("GetProcAddress: builtin %#04X, %d\n",
242 hModule, (int)proc_name);
243 if (GetEntryDLLOrdinal(dll_builtin_table[hModule-0xFF00].dll_name,
244 (int)proc_name & 0x0000FFFF, &sel, &addr))
246 printf("Address not found !\n");
249 ret = MAKELONG(addr, sel);
250 printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n",
251 ret, sel, addr);
252 return (FARPROC)ret;
254 if (hModule == 0)
256 hTask = GetCurrentTask();
257 printf("GetProcAddress // GetCurrentTask()=%04X\n", hTask);
258 lpTask = (LPTASKENTRY) GlobalLock(hTask);
259 if (lpTask == NULL)
261 printf("GetProcAddress: can't find current module handle !\n");
262 return NULL;
264 hModule = lpTask->hInst;
265 printf("GetProcAddress: current module=%04X instance=%04X!\n",
266 lpTask->hModule, lpTask->hInst);
267 GlobalUnlock(hTask);
269 while (w && w->hinstance != hModule)
270 w = w->next;
271 if (w == NULL)
272 return NULL;
273 printf("GetProcAddress // Module Found ! w->filename='%s'\n", w->filename);
274 if ((int)proc_name & 0xFFFF0000)
276 AnsiUpper(proc_name);
277 printf("GetProcAddress: %04X, '%s'\n", hModule, proc_name);
278 cpnt = w->nrname_table;
279 while(TRUE)
281 if (((int) cpnt) - ((int)w->nrname_table) >
282 w->ne_header->nrname_tab_length) return NULL;
283 len = *cpnt++;
284 strncpy(C, cpnt, len);
285 C[len] = '\0';
286 #ifdef DEBUG_MODULE
287 printf("pointing Function '%s' ordinal=%d !\n",
288 C, *((unsigned short *)(cpnt + len)));
289 #endif
290 if (strncmp(cpnt, proc_name, len) == 0)
292 ordinal = *((unsigned short *)(cpnt + len));
293 break;
295 cpnt += len + 2;
297 if (ordinal == 0)
299 printf("GetProcAddress // function '%s' not found !\n", proc_name);
300 return NULL;
303 else
305 printf("GetProcAddress: %#04x, %d\n", hModule, (int) proc_name);
306 ordinal = (int)proc_name;
308 ret = GetEntryPointFromOrdinal(w, ordinal);
309 if (ret == -1)
311 printf("GetProcAddress // Function #%d not found !\n", ordinal);
312 return NULL;
314 addr = ret & 0xffff;
315 sel = (ret >> 16);
316 printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret, sel, addr);
317 return (FARPROC) ret;
318 #endif /* WINELIB */
321 /* internal dlls */
322 static void
323 FillModStructBuiltIn(MODULEENTRY *lpModule, struct dll_name_table_entry_s *dll)
325 lpModule->dwSize = dll->dll_table_length * 1024;
326 strcpy(lpModule->szModule, dll->dll_name);
327 lpModule->hModule = 0xff00 + dll->dll_number;
328 lpModule->wcUsage = GetModuleUsage(lpModule->hModule);
329 GetModuleFileName(lpModule->hModule, lpModule->szExePath, MAX_PATH + 1);
330 lpModule->wNext = 0;
333 /* loaded dlls */
334 static void
335 FillModStructLoaded(MODULEENTRY *lpModule, struct w_files *dll)
337 lpModule->dwSize = 16384;
338 strcpy(lpModule->szModule, dll->name);
339 lpModule->hModule = dll->hinstance;
340 lpModule->wcUsage = GetModuleUsage(lpModule->hModule);
341 GetModuleFileName(lpModule->hModule, lpModule->szExePath, MAX_PATH + 1);
342 lpModule->wNext = 0;
345 /**********************************************************************
346 * ModuleFirst [TOOHELP.59]
348 BOOL ModuleFirst(MODULEENTRY *lpModule)
350 printf("ModuleFirst(%08X)\n", lpModule);
352 FillModStructBuiltIn(lpModule, &dll_builtin_table[0]);
353 return TRUE;
356 /**********************************************************************
357 * ModuleNext [TOOHELP.60]
359 BOOL ModuleNext(MODULEENTRY *lpModule)
361 struct w_files *w;
363 printf("ModuleNext(%08X)\n", lpModule);
365 if (IS_BUILTIN_DLL(lpModule->hModule)) {
366 /* last built-in ? */
367 if ((lpModule->hModule & 0xff) == (N_BUILTINS - 1) ) {
368 if (wine_files) {
369 FillModStructLoaded(lpModule, wine_files);
370 return TRUE;
371 } else
372 return FALSE;
374 FillModStructBuiltIn(lpModule, &dll_builtin_table[(lpModule->hModule & 0xff)+1]);
375 return TRUE;
377 w = GetFileInfo(lpModule->hModule);
378 if (w->next) {
379 FillModStructLoaded(lpModule, w->next);
380 return TRUE;
382 return FALSE;
385 /**********************************************************************
386 * ModuleFindHandle [TOOHELP.62]
388 HMODULE ModuleFindHandle(MODULEENTRY *lpModule, HMODULE hModule)
390 struct w_files *w;
392 printf("ModuleFindHandle(%08X, %04X)\n", lpModule, hModule);
394 /* built-in dll ? */
395 if (IS_BUILTIN_DLL(hModule)) {
396 FillModStructBuiltIn(lpModule, &dll_builtin_table[hModule & 0xff]);
397 return hModule;
400 /* check loaded dlls */
401 if ((w = GetFileInfo(hModule)) == NULL)
402 return (HMODULE) NULL;
404 FillModStructLoaded(lpModule, w);
405 return w->hinstance;
408 /**********************************************************************
409 * ModuleFindName [TOOHELP.61]
411 HMODULE ModuleFindName(MODULEENTRY *lpModule, LPCSTR lpstrName)
413 return (ModuleFindHandle(lpModule, GetModuleHandle((char*)lpstrName)));