Release 940518
[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 #define N_BUILTINS      11
39 extern struct dll_name_table_entry_s dll_builtin_table[N_BUILTINS];
40
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 (strcmp(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 (strcmp(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     printf("GetModuleFileName(%04X, %08X, %d);\n", hModule, lpFileName, nSize);
95     if (lpFileName == NULL) return 0;
96         if (nSize < 1) return 0;
97     w = GetFileInfo(hModule);
98     if (w == NULL) return 0;
99         str = w->filename;
100         if (str[0] == '/') str++;
101     if (nSize > strlen(str)) nSize = strlen(str) + 1;
102     strncpy(lpFileName, str, nSize);
103         ToDos(lpFileName);
104     printf("GetModuleFileName copied '%s' return %d \n", lpFileName, nSize);
105     return nSize - 1;
106 }
107
108
109 /**********************************************************************
110  *                              LoadLibrary     [KERNEL.95]
111  */
112 HANDLE LoadLibrary(LPSTR libname)
113 {
114     HANDLE hModule;
115     LPMODULEENTRY lpMod = lpModList;
116     LPMODULEENTRY lpNewMod;
117
118     if (FindDLLTable(libname))
119     {
120     printf("Library was a builtin - returning 0x23\n");
121         return WINE_CODE_SELECTOR;
122     }
123
124     printf("LoadLibrary '%s'\n", libname);
125     if (lpMod != NULL) 
126     {
127         while (TRUE) 
128         {
129             if (strcmp(libname, lpMod->FileName) == 0) 
130             {
131                 lpMod->Count++;
132                 printf("LoadLibrary // already loaded hInst=%04X\n", 
133                        lpMod->hInst);
134                 return lpMod->hInst;
135             }
136             if (lpMod->lpNextModule == NULL) break;
137             lpMod = lpMod->lpNextModule;
138         }
139     }
140
141     hModule = GlobalAlloc(GMEM_MOVEABLE, sizeof(MODULEENTRY));
142     lpNewMod = (LPMODULEENTRY) GlobalLock(hModule);     
143 #ifdef DEBUG_LIBRARY
144     printf("LoadLibrary // creating new module entry %08X\n", lpNewMod);
145 #endif
146     if (lpNewMod == NULL) 
147         return 0;
148     if (lpModList == NULL) 
149     {
150         lpModList = lpNewMod;
151         lpNewMod->lpPrevModule = NULL;
152     }
153     else 
154     {
155         lpMod->lpNextModule = lpNewMod;
156         lpNewMod->lpPrevModule = lpMod;
157     }
158
159     lpNewMod->lpNextModule = NULL;
160     lpNewMod->hModule = hModule;
161     lpNewMod->ModuleName = NULL;
162     lpNewMod->FileName = (LPSTR) malloc(strlen(libname));
163     if (lpNewMod->FileName != NULL)     
164         strcpy(lpNewMod->FileName, libname);
165     lpNewMod->hInst = LoadImage(libname, DLL);
166     lpNewMod->Count = 1;
167     printf("LoadLibrary returned Library hInst=%04X\n", lpNewMod->hInst);
168     GlobalUnlock(hModule);      
169     return lpNewMod->hInst;
170 }
171
172
173 /**********************************************************************
174  *                              FreeLibrary     [KERNEL.96]
175  */
176 void FreeLibrary(HANDLE hLib)
177 {
178         LPMODULEENTRY lpMod = lpModList;
179     printf("FreeLibrary(%04X);\n", hLib);
180         while (lpMod != NULL) {
181                 if (lpMod->hInst == hLib) {
182                         if (lpMod->Count == 1) {
183                                 if (hLib != (HANDLE)NULL) GlobalFree(hLib);
184                                 if (lpMod->ModuleName != NULL) free(lpMod->ModuleName);
185                                 if (lpMod->FileName != NULL) free(lpMod->FileName);
186                                 GlobalFree(lpMod->hModule);
187                                 printf("FreeLibrary // freed !\n");
188                                 return;
189                                 }
190                         lpMod->Count--;
191                         printf("FreeLibrary // Count decremented !\n");
192                         return;
193                         }
194                 lpMod = lpMod->lpNextModule;
195                 }
196 }
197
198
199 /**********************************************************************
200  *                                      GetProcAddress  [KERNEL.50]
201  */
202 FARPROC GetProcAddress(HANDLE hModule, char *proc_name)
203 {
204         WORD    wOrdin;
205         int             sel, addr, ret;
206     register struct w_files *w = wine_files;
207         int     ordinal, len;
208         char    * cpnt;
209         char    C[128];
210         HTASK   hTask;
211         LPTASKENTRY lpTask;
212         if (hModule >= 0xF000) {
213                 if ((int) proc_name & 0xffff0000) {
214                         printf("GetProcAddress: builtin %#04X, '%s'\n", 
215                                                                                 hModule, proc_name);
216 /*                      wOrdin = FindOrdinalFromName(struct dll_table_entry_s *dll_table, proc_name); */
217                         }
218                 else {
219                         printf("GetProcAddress: builtin %#04X, %d\n", 
220                                                                 hModule, (int)proc_name);
221                         }
222                 return NULL;
223                 }
224         if (hModule == 0) {
225                 hTask = GetCurrentTask();
226                 printf("GetProcAddress // GetCurrentTask()=%04X\n", hTask);
227                 lpTask = (LPTASKENTRY) GlobalLock(hTask);
228                 if (lpTask == NULL) {
229                         printf("GetProcAddress: can't find current module handle !\n");
230                         return NULL;
231                         }
232                 hModule = lpTask->hInst;
233                 printf("GetProcAddress: current module=%04X instance=%04X!\n", 
234                         lpTask->hModule, lpTask->hInst);
235                 GlobalUnlock(hTask);
236                 }
237         while (w && w->hinstance != hModule) w = w->next;
238         if (w == NULL) return NULL;
239         printf("GetProcAddress // Module Found ! w->filename='%s'\n", w->filename);
240         if ((int)proc_name & 0xFFFF0000) {
241                 AnsiUpper(proc_name);
242                 printf("GetProcAddress: %04X, '%s'\n", hModule, proc_name);
243                 cpnt = w->nrname_table;
244                 while(TRUE) {
245                         if (((int) cpnt)  - ((int)w->nrname_table) >  
246                            w->ne_header->nrname_tab_length)  return NULL;
247                         len = *cpnt++;
248                         strncpy(C, cpnt, len);
249                         C[len] = '\0';
250 #ifdef DEBUG_MODULE
251                         printf("pointing Function '%s' ordinal=%d !\n", 
252                                 C, *((unsigned short *)(cpnt +  len)));
253 #endif
254                         if (strncmp(cpnt, proc_name, len) ==  0) {
255                                 ordinal =  *((unsigned short *)(cpnt +  len));
256                                 break;
257                                 }
258                         cpnt += len + 2;
259                         };
260                 if (ordinal == 0) {
261                         printf("GetProcAddress // function '%s' not found !\n", proc_name);
262                         return NULL;
263                         }
264                 }
265         else {
266                 printf("GetProcAddress: %#04x, %d\n", hModule, (int) proc_name);
267                 ordinal = (int)proc_name;
268                 }
269         ret = GetEntryPointFromOrdinal(w, ordinal);
270         if (ret == -1) {
271                 printf("GetProcAddress // Function #%d not found !\n", ordinal);
272                 return NULL;
273                 }
274         addr  = ret & 0xffff;
275         sel = (ret >> 16);
276         printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret, sel, addr);
277         return (FARPROC) ret;
278 }
279
280 #endif /* ifndef WINELIB */
281