Release 940524
[wine] / loader / library.c
1 /*
2  *        Modules & Libraries functions
3  */
4 static char Copyright[] = "Copyright  Martin Ayotte, 1994";
5
6 /*
7 #define DEBUG_MODULE
8 */
9
10 #ifndef WINELIB
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include "prototypes.h"
19 #include "windows.h"
20 #include "wine.h"
21 #include "dlls.h"
22 #include "task.h"
23
24 typedef struct {
25         LPSTR           ModuleName;
26         LPSTR           FileName;
27         WORD            Count;
28         HANDLE          hModule;
29         HINSTANCE       hInst;
30         void            *lpPrevModule;
31         void            *lpNextModule;
32 } MODULEENTRY;
33 typedef MODULEENTRY *LPMODULEENTRY;
34
35 static LPMODULEENTRY lpModList = NULL;
36
37 extern struct  w_files * wine_files;
38 extern struct dll_name_table_entry_s dll_builtin_table[];
39
40 #define IS_BUILTIN_DLL(handle) ((handle >> 16) == 0xff) 
41
42 /**********************************************************************
43  *                              GetModuleHandle [KERNEL.47]
44  */
45 HANDLE GetModuleHandle(LPSTR lpModuleName)
46 {
47         register struct w_files *w = wine_files;
48         int     i;
49         printf("GetModuleHandle('%s');\n", lpModuleName);
50         printf("GetModuleHandle // searching in loaded modules\n");
51         while (w) {
52 /*              printf("GetModuleHandle // '%s' \n", w->name);  */
53                 if (strcasecmp(w->name, lpModuleName) == 0) {
54                         printf("GetModuleHandle('%s') return %04X \n", 
55                                                         lpModuleName, w->hinstance);
56                         return w->hinstance;
57                         }
58                 w = w->next;
59                 }
60         printf("GetModuleHandle // searching in builtin libraries\n");
61     for (i = 0; i < N_BUILTINS; i++) {
62                 if (dll_builtin_table[i].dll_name == NULL) break;
63                 if (strcasecmp(dll_builtin_table[i].dll_name, lpModuleName) == 0) {
64                         printf("GetModuleHandle('%s') return %04X \n", 
65                                                         lpModuleName, 0xFF00 + i);
66                         return (0xFF00 + i);
67                         }
68                 }
69         printf("GetModuleHandle('%s') not found !\n", lpModuleName);
70         return 0;
71 }
72
73
74 /**********************************************************************
75  *                              GetModuleUsage  [KERNEL.48]
76  */
77 int GetModuleUsage(HANDLE hModule)
78 {
79         struct w_files *w;
80         printf("GetModuleUsage(%04X);\n", hModule);
81         w = GetFileInfo(hModule);
82 /*      return w->Usage; */
83         return 1;
84 }
85
86
87 /**********************************************************************
88  *                              GetModuleFilename [KERNEL.49]
89  */
90 int GetModuleFileName(HANDLE hModule, LPSTR lpFileName, short nSize)
91 {
92     struct w_files *w;
93     LPSTR str;
94     char windir[256], temp[256];
95
96     printf("GetModuleFileName(%04X, %08X, %d);\n", hModule, lpFileName, nSize);
97
98     if (lpFileName == NULL) return 0;
99     if (nSize < 1) return 0;
100
101     /* built-in dll ? */
102     if (IS_BUILTIN_DLL(hModule)) {
103         GetWindowsDirectory(windir, sizeof(windir));
104         sprintf(temp, "%s\\%s.DLL", windir, dll_builtin_table[hModule & 0x00ff].dll_name);
105         ToDos(temp);
106         strncpy(lpFileName, temp, nSize);
107         printf("GetModuleFileName copied '%s' (internal dll) return %d \n", lpFileName, nSize);
108         return strlen(lpFileName);
109     }
110
111     /* check loaded dlls */
112     if ((w = GetFileInfo(hModule)) == NULL)
113         return 0;
114     str = GetDosFileName(w->filename);
115     if (nSize > strlen(str)) nSize = strlen(str) + 1;
116     strncpy(lpFileName, str, nSize);
117     printf("GetModuleFileName copied '%s' return %d \n", lpFileName, nSize);
118     return nSize - 1;
119 }
120
121
122 /**********************************************************************
123  *                              LoadLibrary     [KERNEL.95]
124  */
125 HANDLE LoadLibrary(LPSTR libname)
126 {
127     HANDLE hModule;
128     LPMODULEENTRY lpMod = lpModList;
129     LPMODULEENTRY lpNewMod;
130     int i;
131     char temp[64];
132
133     printf("LoadLibrary '%s'\n", libname);
134
135     /* extract dllname */
136     strcpy(temp, libname);
137     if (strchr(temp, '\\') || strchr(temp, '/'))
138         for (i = strlen(temp) - 1; i ; i--) 
139                 if (temp[i] == '\\' || temp[i] == '/') {
140                         strcpy(temp, temp + i + 1);
141                         break;
142                 }
143     for (i = strlen(temp) - 1; i ; i--) 
144         if (temp[i] == '.') {
145                 temp[i] = 0;
146                 break;
147         }
148     
149     if (FindDLLTable(temp))
150         {
151         printf("Library was a builtin - \n");
152         return GetModuleHandle(temp);
153         }
154
155     if (lpMod != NULL) 
156     {
157         while (TRUE) 
158         {
159             if (strcmp(libname, lpMod->FileName) == 0) 
160             {
161                 lpMod->Count++;
162                 printf("LoadLibrary // already loaded hInst=%04X\n", 
163                        lpMod->hInst);
164                 return lpMod->hInst;
165             }
166             if (lpMod->lpNextModule == NULL) break;
167             lpMod = lpMod->lpNextModule;
168         }
169     }
170
171     hModule = GlobalAlloc(GMEM_MOVEABLE, sizeof(MODULEENTRY));
172     lpNewMod = (LPMODULEENTRY) GlobalLock(hModule);     
173 #ifdef DEBUG_LIBRARY
174     printf("LoadLibrary // creating new module entry %08X\n", lpNewMod);
175 #endif
176     if (lpNewMod == NULL) 
177         return 0;
178     if (lpModList == NULL) 
179     {
180         lpModList = lpNewMod;
181         lpNewMod->lpPrevModule = NULL;
182     }
183     else 
184     {
185         lpMod->lpNextModule = lpNewMod;
186         lpNewMod->lpPrevModule = lpMod;
187     }
188
189     lpNewMod->lpNextModule = NULL;
190     lpNewMod->hModule = hModule;
191     lpNewMod->ModuleName = NULL;
192     lpNewMod->FileName = (LPSTR) malloc(strlen(libname));
193     if (lpNewMod->FileName != NULL)     
194         strcpy(lpNewMod->FileName, libname);
195     lpNewMod->hInst = LoadImage(libname, DLL);
196     lpNewMod->Count = 1;
197     printf("LoadLibrary returned Library hInst=%04X\n", lpNewMod->hInst);
198     GlobalUnlock(hModule);      
199     return lpNewMod->hInst;
200 }
201
202
203 /**********************************************************************
204  *                              FreeLibrary     [KERNEL.96]
205  */
206 void FreeLibrary(HANDLE hLib)
207 {
208         LPMODULEENTRY lpMod = lpModList;
209
210     printf("FreeLibrary(%04X);\n", hLib);
211
212         /* built-in dll ? */
213         if (IS_BUILTIN_DLL(hLib)) 
214                 return;
215
216         while (lpMod != NULL) {
217                 if (lpMod->hInst == hLib) {
218                         if (lpMod->Count == 1) {
219                                 if (hLib != (HANDLE)NULL) GlobalFree(hLib);
220                                 if (lpMod->ModuleName != NULL) free(lpMod->ModuleName);
221                                 if (lpMod->FileName != NULL) free(lpMod->FileName);
222                                 GlobalFree(lpMod->hModule);
223                                 printf("FreeLibrary // freed !\n");
224                                 return;
225                                 }
226                         lpMod->Count--;
227                         printf("FreeLibrary // Count decremented !\n");
228                         return;
229                         }
230                 lpMod = lpMod->lpNextModule;
231                 }
232 }
233
234
235 /**********************************************************************
236  *                                      GetProcAddress  [KERNEL.50]
237  */
238 FARPROC GetProcAddress(HANDLE hModule, char *proc_name)
239 {
240     int         i, sel, addr, ret;
241     register struct w_files *w = wine_files;
242     int         ordinal, len;
243     char        * cpnt;
244     char        C[128];
245     HTASK       hTask;
246     LPTASKENTRY lpTask;
247
248     /* built-in dll ? */
249     if (IS_BUILTIN_DLL(hModule))
250     {
251         if ((int) proc_name & 0xffff0000) 
252         {
253             printf("GetProcAddress: builtin %#04X, '%s'\n", 
254                    hModule, proc_name);
255             if (GetEntryDLLName(dll_builtin_table[hModule - 0xFF00].dll_name,
256                                 proc_name, &sel, &addr)) 
257             {
258                 printf("Address not found !\n");
259             }
260         }
261         else 
262         {
263             printf("GetProcAddress: builtin %#04X, %d\n", 
264                    hModule, (int)proc_name);
265             if (GetEntryDLLOrdinal(dll_builtin_table[hModule-0xFF00].dll_name,
266                                    (int)proc_name & 0x0000FFFF, &sel, &addr)) 
267             {
268                 printf("Address not found !\n");
269             }
270         }
271         ret = MAKELONG(addr, sel);
272         printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", 
273                ret, sel, addr);
274         return (FARPROC)ret;
275     }
276     if (hModule == 0) 
277     {
278         hTask = GetCurrentTask();
279         printf("GetProcAddress // GetCurrentTask()=%04X\n", hTask);
280         lpTask = (LPTASKENTRY) GlobalLock(hTask);
281         if (lpTask == NULL) 
282         {
283             printf("GetProcAddress: can't find current module handle !\n");
284             return NULL;
285         }
286         hModule = lpTask->hInst;
287         printf("GetProcAddress: current module=%04X instance=%04X!\n", 
288                lpTask->hModule, lpTask->hInst);
289         GlobalUnlock(hTask);
290     }
291     while (w && w->hinstance != hModule) 
292         w = w->next;
293     if (w == NULL) 
294         return NULL;
295     printf("GetProcAddress // Module Found ! w->filename='%s'\n", w->filename);
296     if ((int)proc_name & 0xFFFF0000) 
297     {
298         AnsiUpper(proc_name);
299         printf("GetProcAddress: %04X, '%s'\n", hModule, proc_name);
300         cpnt = w->nrname_table;
301         while(TRUE) 
302         {
303             if (((int) cpnt)  - ((int)w->nrname_table) >  
304                 w->ne_header->nrname_tab_length)  return NULL;
305             len = *cpnt++;
306             strncpy(C, cpnt, len);
307             C[len] = '\0';
308 #ifdef DEBUG_MODULE
309             printf("pointing Function '%s' ordinal=%d !\n", 
310                    C, *((unsigned short *)(cpnt +  len)));
311 #endif
312             if (strncmp(cpnt, proc_name, len) ==  0) 
313             {
314                 ordinal =  *((unsigned short *)(cpnt +  len));
315                 break;
316             }
317             cpnt += len + 2;
318         };
319         if (ordinal == 0) 
320         {
321             printf("GetProcAddress // function '%s' not found !\n", proc_name);
322             return NULL;
323         }
324     }
325     else 
326     {
327         printf("GetProcAddress: %#04x, %d\n", hModule, (int) proc_name);
328         ordinal = (int)proc_name;
329     }
330     ret = GetEntryPointFromOrdinal(w, ordinal);
331     if (ret == -1) 
332     {
333         printf("GetProcAddress // Function #%d not found !\n", ordinal);
334         return NULL;
335     }
336     addr  = ret & 0xffff;
337     sel = (ret >> 16);
338     printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret, sel, addr);
339     return (FARPROC) ret;
340 }
341
342 #endif /* ifndef WINELIB */
343