2 * Modules & Libraries functions
4 static char Copyright
[] = "Copyright Martin Ayotte, 1994";
12 #include <sys/types.h>
17 #include "prototypes.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
)
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);
44 for (i
= strlen(temp
) - 1; i
; i
--)
51 /**********************************************************************
52 * GetModuleHandle [KERNEL.47]
54 HANDLE
GetModuleHandle(LPSTR lpModuleName
)
56 register struct w_files
*w
= wine_files
;
60 if ((int) lpModuleName
& 0xffff0000)
61 ExtractDLLName(lpModuleName
, dllname
);
63 if ((int) lpModuleName
& 0xffff0000)
64 printf("GetModuleHandle('%s');\n", lpModuleName
);
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
);
78 else if (strcasecmp(dll_builtin_table
[i
].dll_name
, dllname
) == 0) {
79 printf("GetModuleHandle('%x') return %04X \n",
80 lpModuleName
, 0xFF00 + i
);
85 printf("GetModuleHandle // searching in loaded modules\n");
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
);
95 else if (strcasecmp(w
->name
, dllname
) == 0) {
96 printf("GetModuleHandle('%s') return %04X \n",
97 lpModuleName
, w
->hinstance
);
102 printf("GetModuleHandle('%x') not found !\n", lpModuleName
);
107 /**********************************************************************
108 * GetModuleUsage [KERNEL.48]
110 int GetModuleUsage(HANDLE hModule
)
114 printf("GetModuleUsage(%04X);\n", hModule
);
117 if (IS_BUILTIN_DLL(hModule
))
120 w
= GetFileInfo(hModule
);
121 /* return w->Usage; */
126 /**********************************************************************
127 * GetModuleFilename [KERNEL.49]
129 int GetModuleFileName(HANDLE hModule
, LPSTR lpFileName
, short nSize
)
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;
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
);
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
)
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
);
161 /**********************************************************************
162 * LoadLibrary [KERNEL.95]
164 HANDLE
LoadLibrary(LPSTR libname
)
168 if ((h
= LoadImage(libname
, DLL
, 0)) < 32)
171 if (!IS_BUILTIN_DLL(h
))
172 InitDLL(GetFileInfo(h
));
178 /**********************************************************************
179 * FreeLibrary [KERNEL.96]
181 void FreeLibrary(HANDLE hLib
)
183 printf("FreeLibrary(%04X);\n", hLib
);
186 if (IS_BUILTIN_DLL(hLib
))
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");
201 printf("FreeLibrary // Count decremented !\n");
204 lpMod = lpMod->lpNextModule;
210 /**********************************************************************
211 * GetProcAddress [KERNEL.50]
213 FARPROC
GetProcAddress(HANDLE hModule
, char *proc_name
)
216 WINELIB_UNIMP ("GetProcAddress");
218 int i
, sel
, addr
, ret
;
219 register struct w_files
*w
= wine_files
;
227 if (IS_BUILTIN_DLL(hModule
))
229 if ((int) proc_name
& 0xffff0000)
231 printf("GetProcAddress: builtin %#04X, '%s'\n",
233 if (GetEntryDLLName(dll_builtin_table
[hModule
- 0xFF00].dll_name
,
234 proc_name
, &sel
, &addr
))
236 printf("Address not found !\n");
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",
256 hTask
= GetCurrentTask();
257 printf("GetProcAddress // GetCurrentTask()=%04X\n", hTask
);
258 lpTask
= (LPTASKENTRY
) GlobalLock(hTask
);
261 printf("GetProcAddress: can't find current module handle !\n");
264 hModule
= lpTask
->hInst
;
265 printf("GetProcAddress: current module=%04X instance=%04X!\n",
266 lpTask
->hModule
, lpTask
->hInst
);
269 while (w
&& w
->hinstance
!= hModule
)
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
;
281 if (((int) cpnt
) - ((int)w
->nrname_table
) >
282 w
->ne_header
->nrname_tab_length
) return NULL
;
284 strncpy(C
, cpnt
, len
);
287 printf("pointing Function '%s' ordinal=%d !\n",
288 C
, *((unsigned short *)(cpnt
+ len
)));
290 if (strncmp(cpnt
, proc_name
, len
) == 0)
292 ordinal
= *((unsigned short *)(cpnt
+ len
));
299 printf("GetProcAddress // function '%s' not found !\n", proc_name
);
305 printf("GetProcAddress: %#04x, %d\n", hModule
, (int) proc_name
);
306 ordinal
= (int)proc_name
;
308 ret
= GetEntryPointFromOrdinal(w
, ordinal
);
311 printf("GetProcAddress // Function #%d not found !\n", ordinal
);
316 printf("GetProcAddress // ret=%08X sel=%04X addr=%04X\n", ret
, sel
, addr
);
317 return (FARPROC
) ret
;
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);
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);
345 /**********************************************************************
346 * ModuleFirst [TOOHELP.59]
348 BOOL
ModuleFirst(MODULEENTRY
*lpModule
)
350 printf("ModuleFirst(%08X)\n", lpModule
);
352 FillModStructBuiltIn(lpModule
, &dll_builtin_table
[0]);
356 /**********************************************************************
357 * ModuleNext [TOOHELP.60]
359 BOOL
ModuleNext(MODULEENTRY
*lpModule
)
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) ) {
369 FillModStructLoaded(lpModule
, wine_files
);
374 FillModStructBuiltIn(lpModule
, &dll_builtin_table
[(lpModule
->hModule
& 0xff)+1]);
377 w
= GetFileInfo(lpModule
->hModule
);
379 FillModStructLoaded(lpModule
, w
->next
);
385 /**********************************************************************
386 * ModuleFindHandle [TOOHELP.62]
388 HMODULE
ModuleFindHandle(MODULEENTRY
*lpModule
, HMODULE hModule
)
392 printf("ModuleFindHandle(%08X, %04X)\n", lpModule
, hModule
);
395 if (IS_BUILTIN_DLL(hModule
)) {
396 FillModStructBuiltIn(lpModule
, &dll_builtin_table
[hModule
& 0xff]);
400 /* check loaded dlls */
401 if ((w
= GetFileInfo(hModule
)) == NULL
)
402 return (HMODULE
) NULL
;
404 FillModStructLoaded(lpModule
, w
);
408 /**********************************************************************
409 * ModuleFindName [TOOHELP.61]
411 HMODULE
ModuleFindName(MODULEENTRY
*lpModule
, LPCSTR lpstrName
)
413 return (ModuleFindHandle(lpModule
, GetModuleHandle((char*)lpstrName
)));