2 * Modules & Libraries functions
4 static char Copyright[] = "Copyright Martin Ayotte, 1994";
14 #include <sys/types.h>
18 #include "prototypes.h"
33 typedef MODULEENTRY *LPMODULEENTRY;
35 static LPMODULEENTRY lpModList = NULL;
37 extern struct w_files * wine_files;
38 extern struct dll_name_table_entry_s dll_builtin_table[];
40 #define IS_BUILTIN_DLL(handle) ((handle >> 16) == 0xff)
42 /**********************************************************************
43 * GetModuleHandle [KERNEL.47]
45 HANDLE GetModuleHandle(LPSTR lpModuleName)
47 register struct w_files *w = wine_files;
49 printf("GetModuleHandle('%s');\n", lpModuleName);
50 printf("GetModuleHandle // searching in loaded modules\n");
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);
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);
69 printf("GetModuleHandle('%s') not found !\n", lpModuleName);
74 /**********************************************************************
75 * GetModuleUsage [KERNEL.48]
77 int GetModuleUsage(HANDLE hModule)
80 printf("GetModuleUsage(%04X);\n", hModule);
81 w = GetFileInfo(hModule);
82 /* return w->Usage; */
87 /**********************************************************************
88 * GetModuleFilename [KERNEL.49]
90 int GetModuleFileName(HANDLE hModule, LPSTR lpFileName, short nSize)
94 char windir[256], temp[256];
96 printf("GetModuleFileName(%04X, %08X, %d);\n", hModule, lpFileName, nSize);
98 if (lpFileName == NULL) return 0;
99 if (nSize < 1) return 0;
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);
106 strncpy(lpFileName, temp, nSize);
107 printf("GetModuleFileName copied '%s' (internal dll) return %d \n", lpFileName, nSize);
108 return strlen(lpFileName);
111 /* check loaded dlls */
112 if ((w = GetFileInfo(hModule)) == NULL)
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);
122 /**********************************************************************
123 * LoadLibrary [KERNEL.95]
125 HANDLE LoadLibrary(LPSTR libname)
128 LPMODULEENTRY lpMod = lpModList;
129 LPMODULEENTRY lpNewMod;
133 printf("LoadLibrary '%s'\n", libname);
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);
143 for (i = strlen(temp) - 1; i ; i--)
144 if (temp[i] == '.') {
149 if (FindDLLTable(temp))
151 printf("Library was a builtin - \n");
152 return GetModuleHandle(temp);
159 if (strcmp(libname, lpMod->FileName) == 0)
162 printf("LoadLibrary // already loaded hInst=%04X\n",
166 if (lpMod->lpNextModule == NULL) break;
167 lpMod = lpMod->lpNextModule;
171 hModule = GlobalAlloc(GMEM_MOVEABLE, sizeof(MODULEENTRY));
172 lpNewMod = (LPMODULEENTRY) GlobalLock(hModule);
174 printf("LoadLibrary // creating new module entry %08X\n", lpNewMod);
176 if (lpNewMod == NULL)
178 if (lpModList == NULL)
180 lpModList = lpNewMod;
181 lpNewMod->lpPrevModule = NULL;
185 lpMod->lpNextModule = lpNewMod;
186 lpNewMod->lpPrevModule = lpMod;
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);
197 printf("LoadLibrary returned Library hInst=%04X\n", lpNewMod->hInst);
198 GlobalUnlock(hModule);
199 return lpNewMod->hInst;
203 /**********************************************************************
204 * FreeLibrary [KERNEL.96]
206 void FreeLibrary(HANDLE hLib)
208 LPMODULEENTRY lpMod = lpModList;
210 printf("FreeLibrary(%04X);\n", hLib);
213 if (IS_BUILTIN_DLL(hLib))
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");
227 printf("FreeLibrary // Count decremented !\n");
230 lpMod = lpMod->lpNextModule;
235 /**********************************************************************
236 * GetProcAddress [KERNEL.50]
238 FARPROC GetProcAddress(HANDLE hModule, char *proc_name)
240 int i, sel, addr, ret;
241 register struct w_files *w = wine_files;
249 if (IS_BUILTIN_DLL(hModule))
251 if ((int) proc_name & 0xffff0000)
253 printf("GetProcAddress: builtin %#04X, '%s'\n",
255 if (GetEntryDLLName(dll_builtin_table[hModule - 0xFF00].dll_name,
256 proc_name, &sel, &addr))
258 printf("Address not found !\n");
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))
268 printf("Address not found !\n");
271 ret = MAKELONG(addr, sel);
272 printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n",
278 hTask = GetCurrentTask();
279 printf("GetProcAddress // GetCurrentTask()=%04X\n", hTask);
280 lpTask = (LPTASKENTRY) GlobalLock(hTask);
283 printf("GetProcAddress: can't find current module handle !\n");
286 hModule = lpTask->hInst;
287 printf("GetProcAddress: current module=%04X instance=%04X!\n",
288 lpTask->hModule, lpTask->hInst);
291 while (w && w->hinstance != hModule)
295 printf("GetProcAddress // Module Found ! w->filename='%s'\n", w->filename);
296 if ((int)proc_name & 0xFFFF0000)
298 AnsiUpper(proc_name);
299 printf("GetProcAddress: %04X, '%s'\n", hModule, proc_name);
300 cpnt = w->nrname_table;
303 if (((int) cpnt) - ((int)w->nrname_table) >
304 w->ne_header->nrname_tab_length) return NULL;
306 strncpy(C, cpnt, len);
309 printf("pointing Function '%s' ordinal=%d !\n",
310 C, *((unsigned short *)(cpnt + len)));
312 if (strncmp(cpnt, proc_name, len) == 0)
314 ordinal = *((unsigned short *)(cpnt + len));
321 printf("GetProcAddress // function '%s' not found !\n", proc_name);
327 printf("GetProcAddress: %#04x, %d\n", hModule, (int) proc_name);
328 ordinal = (int)proc_name;
330 ret = GetEntryPointFromOrdinal(w, ordinal);
333 printf("GetProcAddress // Function #%d not found !\n", ordinal);
338 printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret, sel, addr);
339 return (FARPROC) ret;
342 #endif /* ifndef WINELIB */