1 | /* $Id: wprocess.cpp,v 1.36 1999-10-09 13:33:25 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 process functions
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * NOTE: Even though Odin32 OS/2 apps don't switch FS selectors,
|
---|
10 | * we still allocate a TEB to store misc information.
|
---|
11 | *
|
---|
12 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
13 | *
|
---|
14 | */
|
---|
15 | #include <odin.h>
|
---|
16 | #include <odinwrap.h>
|
---|
17 | #include <os2win.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <stdlib.h>
|
---|
20 | #include <string.h>
|
---|
21 |
|
---|
22 | #include <unicode.h>
|
---|
23 | #include <windllbase.h>
|
---|
24 | #include <winexebase.h>
|
---|
25 | #include <windllpeldr.h>
|
---|
26 |
|
---|
27 | #ifdef __IBMCPP__
|
---|
28 | #include <builtin.h>
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #include "exceptutil.h"
|
---|
32 | #include "oslibmisc.h"
|
---|
33 |
|
---|
34 | #include "console.h"
|
---|
35 | #include "cio.h"
|
---|
36 | #include "versionos2.h" /*PLF Wed 98-03-18 02:36:51*/
|
---|
37 | #include <wprocess.h>
|
---|
38 | #include "mmap.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
|
---|
42 |
|
---|
43 |
|
---|
44 | //******************************************************************************
|
---|
45 | //******************************************************************************
|
---|
46 | BOOL fFreeLibrary = FALSE;
|
---|
47 | BOOL fIsOS2Image = FALSE; //TRUE -> Odin32 OS/2 application (not converted!)
|
---|
48 | //FALSE -> otherwise
|
---|
49 | //Process database
|
---|
50 | PDB ProcessPDB = {0};
|
---|
51 | USHORT ProcessTIBSel = 0;
|
---|
52 | DWORD *TIBFlatPtr = 0;
|
---|
53 |
|
---|
54 | extern "C" ULONG QueryExceptionChain();
|
---|
55 |
|
---|
56 |
|
---|
57 | //******************************************************************************
|
---|
58 | //******************************************************************************
|
---|
59 | TEB *WIN32API GetThreadTEB()
|
---|
60 | {
|
---|
61 | return (TEB *)*TIBFlatPtr;
|
---|
62 | }
|
---|
63 | //******************************************************************************
|
---|
64 | //******************************************************************************
|
---|
65 | THDB *WIN32API GetThreadTHDB()
|
---|
66 | {
|
---|
67 | TEB *winteb;
|
---|
68 | THDB *thdb;
|
---|
69 |
|
---|
70 | winteb = (TEB *)*TIBFlatPtr;
|
---|
71 | if(winteb == NULL) {
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 | thdb = (THDB *)(winteb+1);
|
---|
75 |
|
---|
76 | return thdb;
|
---|
77 | }
|
---|
78 | //******************************************************************************
|
---|
79 | // Set up the TIB selector and memory for the current thread
|
---|
80 | //******************************************************************************
|
---|
81 | TEB *InitializeTIB(BOOL fMainThread)
|
---|
82 | {
|
---|
83 | TEB *winteb;
|
---|
84 | THDB *thdb;
|
---|
85 |
|
---|
86 | USHORT tibsel;
|
---|
87 |
|
---|
88 | //Allocate one dword to store the flat address of our TEB
|
---|
89 | if(fMainThread) {
|
---|
90 | TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1);
|
---|
91 | if(TIBFlatPtr == 0) {
|
---|
92 | dprintf(("InitializeTIB: local thread memory alloc failed!!"));
|
---|
93 | DebugInt3();
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if(OSLibAllocSel(PAGE_SIZE, &tibsel) == FALSE)
|
---|
98 | {
|
---|
99 | dprintf(("InitializeTIB: selector alloc failed!!"));
|
---|
100 | DebugInt3();
|
---|
101 | return NULL;
|
---|
102 | }
|
---|
103 | winteb = (TEB *)OSLibSelToFlat(tibsel);
|
---|
104 | if(winteb == NULL)
|
---|
105 | {
|
---|
106 | dprintf(("InitializeTIB: DosSelToFlat failed!!"));
|
---|
107 | DebugInt3();
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 | memset(winteb, 0, PAGE_SIZE);
|
---|
111 | thdb = (THDB *)(winteb+1);
|
---|
112 | *TIBFlatPtr = (DWORD)winteb;
|
---|
113 |
|
---|
114 | winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */
|
---|
115 | winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */
|
---|
116 | winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */
|
---|
117 | winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */
|
---|
118 | winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */
|
---|
119 | winteb->self = winteb; /* 18 Pointer to this structure */
|
---|
120 | winteb->flags = TEBF_WIN32; /* 1c Flags */
|
---|
121 | winteb->queue = 0; /* 28 Message queue */
|
---|
122 | winteb->tls_ptr = &thdb->tls_array[0]; /* 2c Pointer to TLS array */
|
---|
123 | winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/
|
---|
124 |
|
---|
125 | memcpy(&thdb->teb, winteb, sizeof(TEB));
|
---|
126 | thdb->process = &ProcessPDB;
|
---|
127 | thdb->exit_code = 0x103; /* STILL_ACTIVE */
|
---|
128 | thdb->teb_sel = tibsel;
|
---|
129 | thdb->OrgTIBSel = GetFS();
|
---|
130 |
|
---|
131 | if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM)
|
---|
132 | {
|
---|
133 | thdb->flags = 0; //todo gui
|
---|
134 | }
|
---|
135 | else thdb->flags = 0; //todo textmode
|
---|
136 |
|
---|
137 | if(fMainThread)
|
---|
138 | {
|
---|
139 | //todo initialize PDB during process creation
|
---|
140 | //todo: initialize TLS array if required
|
---|
141 | //TLS in executable always TLS index 0?
|
---|
142 | ProcessTIBSel = tibsel;
|
---|
143 | }
|
---|
144 | dprintf(("InitializeTIB setup TEB with selector %x", tibsel));
|
---|
145 | dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
---|
146 | return winteb;
|
---|
147 | }
|
---|
148 | //******************************************************************************
|
---|
149 | // Destroy the TIB selector and memory for the current thread
|
---|
150 | //******************************************************************************
|
---|
151 | void DestroyTIB()
|
---|
152 | {
|
---|
153 | SHORT orgtibsel;
|
---|
154 | TEB *winteb;
|
---|
155 | THDB *thdb;
|
---|
156 |
|
---|
157 | dprintf(("DestroyTIB: FS = %x", GetFS()));
|
---|
158 | dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));
|
---|
159 |
|
---|
160 | winteb = (TEB *)*TIBFlatPtr;
|
---|
161 | if(winteb) {
|
---|
162 | thdb = (THDB *)(winteb+1);
|
---|
163 | orgtibsel = thdb->OrgTIBSel;
|
---|
164 |
|
---|
165 | //Restore our original FS selector
|
---|
166 | SetFS(orgtibsel);
|
---|
167 |
|
---|
168 | //And free our own
|
---|
169 | OSLibFreeSel(thdb->teb_sel);
|
---|
170 | }
|
---|
171 | else dprintf(("Already destroyed TIB"));
|
---|
172 |
|
---|
173 | dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));
|
---|
174 | *TIBFlatPtr = 0;
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | /******************************************************************************/
|
---|
178 | /******************************************************************************/
|
---|
179 | void WIN32API RestoreOS2TIB()
|
---|
180 | {
|
---|
181 | SHORT orgtibsel;
|
---|
182 | TEB *winteb;
|
---|
183 | THDB *thdb;
|
---|
184 |
|
---|
185 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
---|
186 | //we don't switch FS selectors
|
---|
187 | if(fIsOS2Image) {
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | winteb = (TEB *)*TIBFlatPtr;
|
---|
192 | if(winteb) {
|
---|
193 | thdb = (THDB *)(winteb+1);
|
---|
194 | orgtibsel = thdb->OrgTIBSel;
|
---|
195 |
|
---|
196 | //Restore our original FS selector
|
---|
197 | SetFS(orgtibsel);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | /******************************************************************************/
|
---|
201 | /******************************************************************************/
|
---|
202 | USHORT WIN32API SetWin32TIB()
|
---|
203 | {
|
---|
204 | SHORT win32tibsel;
|
---|
205 | TEB *winteb;
|
---|
206 | THDB *thdb;
|
---|
207 |
|
---|
208 | //If we're running an Odin32 OS/2 application (not converted!), then we
|
---|
209 | //we don't switch FS selectors
|
---|
210 | if(fIsOS2Image) {
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | winteb = (TEB *)*TIBFlatPtr;
|
---|
215 | if(winteb) {
|
---|
216 | thdb = (THDB *)(winteb+1);
|
---|
217 | win32tibsel = thdb->teb_sel;
|
---|
218 |
|
---|
219 | //Restore our win32 FS selector
|
---|
220 | return SetReturnFS(win32tibsel);
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | //we didn't create this thread, so allocate a selector now
|
---|
224 | //NOTE: Possible memory leak (i.e. DART threads in WINMM)
|
---|
225 | winteb = InitializeTIB();
|
---|
226 | if(winteb == NULL) {
|
---|
227 | DebugInt3();
|
---|
228 | return GetFS();
|
---|
229 | }
|
---|
230 | thdb = (THDB *)(winteb+1);
|
---|
231 | win32tibsel = thdb->teb_sel;
|
---|
232 |
|
---|
233 | //Restore our win32 FS selector
|
---|
234 | return SetReturnFS(win32tibsel);
|
---|
235 | }
|
---|
236 | // nested calls are OK, OS2ToWinCallback for instance
|
---|
237 | //else DebugInt3();
|
---|
238 |
|
---|
239 | return GetFS();
|
---|
240 | }
|
---|
241 | //******************************************************************************
|
---|
242 | //******************************************************************************
|
---|
243 | void _System Win32DllExitList(ULONG reason)
|
---|
244 | {
|
---|
245 | dprintf(("Win32DllExitList %d\n", reason));
|
---|
246 |
|
---|
247 | if(WinExe) {
|
---|
248 | delete(WinExe);
|
---|
249 | WinExe = NULL;
|
---|
250 | }
|
---|
251 | return;
|
---|
252 | }
|
---|
253 | //******************************************************************************
|
---|
254 | //******************************************************************************
|
---|
255 | VOID WIN32API ExitProcess(DWORD exitcode)
|
---|
256 | {
|
---|
257 | dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
|
---|
258 | dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
|
---|
259 |
|
---|
260 | SetOS2ExceptionChain(-1);
|
---|
261 |
|
---|
262 | //Flush and delete all open memory mapped files
|
---|
263 | Win32MemMap::deleteAll();
|
---|
264 |
|
---|
265 | Win32DllExitList(0);
|
---|
266 |
|
---|
267 | //Restore original OS/2 TIB selector
|
---|
268 | DestroyTIB();
|
---|
269 | SetExceptionChain((ULONG)-1);
|
---|
270 |
|
---|
271 | //avoid crashes since win32 & OS/2 exception handler aren't identical
|
---|
272 | //(terminate process generates two exceptions)
|
---|
273 | /* @@@PH 1998/02/12 Added Console Support */
|
---|
274 | if (iConsoleIsActive())
|
---|
275 | iConsoleWaitClose();
|
---|
276 |
|
---|
277 | O32_ExitProcess(exitcode);
|
---|
278 | }
|
---|
279 | //******************************************************************************
|
---|
280 | //******************************************************************************
|
---|
281 | BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
|
---|
282 | {
|
---|
283 | Win32DllBase *winmod;
|
---|
284 | BOOL rc;
|
---|
285 |
|
---|
286 | dprintf(("FreeLibrary"));
|
---|
287 | winmod = Win32DllBase::findModule(hinstance);
|
---|
288 | if(winmod) {
|
---|
289 | winmod->Release();
|
---|
290 | return(TRUE);
|
---|
291 | }
|
---|
292 | dprintf(("KERNEL32: FreeLibrary %s %X\n", OSLibGetDllName(hinstance), hinstance));
|
---|
293 |
|
---|
294 | //TODO: Not thread safe
|
---|
295 | fFreeLibrary = TRUE; //ditch dll
|
---|
296 | rc = O32_FreeLibrary(hinstance);
|
---|
297 | fFreeLibrary = FALSE;
|
---|
298 | dprintf(("FreeLibrary returned %X\n", rc));
|
---|
299 | return(TRUE);
|
---|
300 | }
|
---|
301 | /******************************************************************************/
|
---|
302 | /******************************************************************************/
|
---|
303 | static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile, DWORD dwFlags)
|
---|
304 | {
|
---|
305 | char modname[CCHMAXPATH];
|
---|
306 | HINSTANCE hDll;
|
---|
307 | Win32DllBase *module;
|
---|
308 |
|
---|
309 | hDll = O32_LoadLibrary(lpszLibFile);
|
---|
310 | dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
|
---|
311 | lpszLibFile,
|
---|
312 | hDll,
|
---|
313 | GetLastError()));
|
---|
314 | if(hDll)
|
---|
315 | {
|
---|
316 | return hDll; //converted dll or win32k took care of it
|
---|
317 | }
|
---|
318 |
|
---|
319 | strcpy(modname, lpszLibFile);
|
---|
320 | strupr(modname);
|
---|
321 | if(!strstr(modname, ".DLL")) {
|
---|
322 | strcat(modname,".DLL");
|
---|
323 | }
|
---|
324 |
|
---|
325 | if(Win32ImageBase::isPEImage((char *)modname))
|
---|
326 | {
|
---|
327 | module = Win32DllBase::findModule((char *)modname);
|
---|
328 | if(module) {//don't load it again
|
---|
329 | module->AddRef();
|
---|
330 | return module->getInstanceHandle();
|
---|
331 | }
|
---|
332 |
|
---|
333 | Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
|
---|
334 | if(peldrDll == NULL)
|
---|
335 | return(0);
|
---|
336 |
|
---|
337 | peldrDll->init(0);
|
---|
338 | if(peldrDll->getError() != NO_ERROR) {
|
---|
339 | dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
|
---|
340 | delete(peldrDll);
|
---|
341 | return(0);
|
---|
342 | }
|
---|
343 | if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
|
---|
344 | peldrDll->setNoEntryCalls();
|
---|
345 | }
|
---|
346 |
|
---|
347 | if(peldrDll->attachProcess() == FALSE) {
|
---|
348 | dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
|
---|
349 | delete(peldrDll);
|
---|
350 | return(0);
|
---|
351 | }
|
---|
352 | peldrDll->AddRef();
|
---|
353 | return peldrDll->getInstanceHandle();
|
---|
354 | }
|
---|
355 | else return(0);
|
---|
356 | }
|
---|
357 | //******************************************************************************
|
---|
358 | //******************************************************************************
|
---|
359 | HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
|
---|
360 | {
|
---|
361 | HINSTANCE hDll;
|
---|
362 |
|
---|
363 | dprintf(("KERNEL32: LoadLibraryA(%s)\n",
|
---|
364 | lpszLibFile));
|
---|
365 |
|
---|
366 | hDll = iLoadLibraryA(lpszLibFile, 0);
|
---|
367 | if (hDll == 0)
|
---|
368 | {
|
---|
369 | char * pszName;
|
---|
370 |
|
---|
371 | // remove path from the image name
|
---|
372 | pszName = strrchr((char *)lpszLibFile,
|
---|
373 | '\\');
|
---|
374 | if (pszName != NULL)
|
---|
375 | {
|
---|
376 | pszName++; // skip backslash
|
---|
377 |
|
---|
378 | // now try again without fully qualified path
|
---|
379 | hDll = iLoadLibraryA(pszName, 0);
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | return hDll;
|
---|
384 | }
|
---|
385 | //******************************************************************************
|
---|
386 | //******************************************************************************
|
---|
387 | HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
|
---|
388 | {
|
---|
389 | HINSTANCE hDll;
|
---|
390 |
|
---|
391 | dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
|
---|
392 | hDll = iLoadLibraryA(lpszLibFile, dwFlags);
|
---|
393 | if (hDll == 0)
|
---|
394 | {
|
---|
395 | char * pszName;
|
---|
396 |
|
---|
397 | // remove path from the image name
|
---|
398 | pszName = strrchr((char *)lpszLibFile,
|
---|
399 | '\\');
|
---|
400 | if (pszName != NULL)
|
---|
401 | {
|
---|
402 | pszName++; // skip backslash
|
---|
403 |
|
---|
404 | // now try again without fully qualified path
|
---|
405 | hDll = iLoadLibraryA(pszName, dwFlags);
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | return hDll;
|
---|
410 | }
|
---|
411 | //******************************************************************************
|
---|
412 | //******************************************************************************
|
---|
413 | HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
|
---|
414 | {
|
---|
415 | char *asciimodule;
|
---|
416 | HINSTANCE rc;
|
---|
417 |
|
---|
418 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
---|
419 | dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
|
---|
420 | rc = LoadLibraryA(asciimodule);
|
---|
421 | free(asciimodule);
|
---|
422 | return(rc);
|
---|
423 | }
|
---|
424 | //******************************************************************************
|
---|
425 | //******************************************************************************
|
---|
426 | HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
|
---|
427 | {
|
---|
428 | char *asciimodule;
|
---|
429 | HINSTANCE rc;
|
---|
430 |
|
---|
431 | asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
|
---|
432 | dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
|
---|
433 | rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
|
---|
434 | free(asciimodule);
|
---|
435 | return(rc);
|
---|
436 | }
|
---|
437 | //******************************************************************************
|
---|
438 | //******************************************************************************
|
---|
439 | LPCSTR WIN32API GetCommandLineA()
|
---|
440 | {
|
---|
441 | LPTSTR cmdline = NULL;
|
---|
442 |
|
---|
443 | if(WinExe) {
|
---|
444 | cmdline = WinExe->getCommandLine();
|
---|
445 | }
|
---|
446 | if(cmdline == NULL) //not used for converted exes
|
---|
447 | cmdline = O32_GetCommandLine();
|
---|
448 |
|
---|
449 | dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
|
---|
450 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
---|
451 | return(cmdline);
|
---|
452 | }
|
---|
453 | //******************************************************************************
|
---|
454 | //******************************************************************************
|
---|
455 | LPCWSTR WIN32API GetCommandLineW(void)
|
---|
456 | {
|
---|
457 | static WCHAR *UnicodeCmdLine = NULL;
|
---|
458 | char *asciicmdline = NULL;
|
---|
459 |
|
---|
460 | dprintf(("KERNEL32: FS = %x\n", GetFS()));
|
---|
461 |
|
---|
462 | if(UnicodeCmdLine)
|
---|
463 | return(UnicodeCmdLine); //already called before
|
---|
464 |
|
---|
465 | if(WinExe) {
|
---|
466 | asciicmdline = WinExe->getCommandLine();
|
---|
467 | }
|
---|
468 | if(asciicmdline == NULL) //not used for converted exes
|
---|
469 | asciicmdline = O32_GetCommandLine();
|
---|
470 |
|
---|
471 | if(asciicmdline) {
|
---|
472 | UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
|
---|
473 | AsciiToUnicode(asciicmdline, UnicodeCmdLine);
|
---|
474 | dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
|
---|
475 | return(UnicodeCmdLine);
|
---|
476 | }
|
---|
477 | dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
|
---|
478 | return NULL;
|
---|
479 | }
|
---|
480 | //******************************************************************************
|
---|
481 | //******************************************************************************
|
---|
482 | DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
|
---|
483 | {
|
---|
484 | DWORD rc;
|
---|
485 | Win32ImageBase *module;
|
---|
486 | char *fpath = NULL;
|
---|
487 |
|
---|
488 | dprintf(("GetModuleFileName %X", hinstModule));
|
---|
489 | if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
|
---|
490 | module = (Win32ImageBase *)WinExe;
|
---|
491 | }
|
---|
492 | else {
|
---|
493 | module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
|
---|
494 | }
|
---|
495 |
|
---|
496 | if(module) {
|
---|
497 | fpath = module->getFullPath();
|
---|
498 | }
|
---|
499 | if(fpath) {
|
---|
500 | //SvL: 13-9-98: +1
|
---|
501 | rc = min(strlen(fpath)+1, cchPath);
|
---|
502 | strncpy(lpszPath, fpath, rc);
|
---|
503 | }
|
---|
504 | else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
|
---|
505 |
|
---|
506 | if(rc) {
|
---|
507 | dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
|
---|
508 | }
|
---|
509 | return(rc);
|
---|
510 | }
|
---|
511 | //******************************************************************************
|
---|
512 | //******************************************************************************
|
---|
513 | DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
|
---|
514 | {
|
---|
515 | char *asciifilename = (char *)malloc(nSize+1);
|
---|
516 | DWORD rc;
|
---|
517 |
|
---|
518 | dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
|
---|
519 | rc = GetModuleFileNameA(hModule, asciifilename, nSize);
|
---|
520 | if(rc) AsciiToUnicode(asciifilename, lpFileName);
|
---|
521 | free(asciifilename);
|
---|
522 | return(rc);
|
---|
523 | }
|
---|
524 | //******************************************************************************
|
---|
525 | //NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
|
---|
526 | // very.weird.exe)
|
---|
527 | //******************************************************************************
|
---|
528 | HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
|
---|
529 | {
|
---|
530 | HANDLE hMod;
|
---|
531 | Win32DllBase *windll;
|
---|
532 | char szModule[CCHMAXPATH];
|
---|
533 | BOOL fDllModule = FALSE;
|
---|
534 |
|
---|
535 | if(lpszModule == NULL) {
|
---|
536 | if(WinExe)
|
---|
537 | hMod = WinExe->getInstanceHandle();
|
---|
538 | else hMod = -1;
|
---|
539 | }
|
---|
540 | else {
|
---|
541 | strcpy(szModule, OSLibStripPath((char *)lpszModule));
|
---|
542 | strupr(szModule);
|
---|
543 | if(strstr(szModule, ".DLL")) {
|
---|
544 | fDllModule = TRUE;
|
---|
545 | }
|
---|
546 | else {
|
---|
547 | if(!strstr(szModule, ".")) {
|
---|
548 | //if there's no extension or trainling dot, we
|
---|
549 | //assume it's a dll (see Win32 SDK docs)
|
---|
550 | fDllModule = TRUE;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | char *dot = strstr(szModule, ".");
|
---|
554 | if(dot)
|
---|
555 | *dot = 0;
|
---|
556 |
|
---|
557 | if(!fDllModule && WinExe && !strcmpi(lpszModule, WinExe->getModuleName())) {
|
---|
558 | hMod = WinExe->getInstanceHandle();
|
---|
559 | }
|
---|
560 | else {
|
---|
561 | windll = Win32DllBase::findModule(szModule);
|
---|
562 | if(windll) {
|
---|
563 | hMod = windll->getInstanceHandle();
|
---|
564 | }
|
---|
565 | else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | eprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
|
---|
570 | return(hMod);
|
---|
571 | }
|
---|
572 | //******************************************************************************
|
---|
573 | //******************************************************************************
|
---|
574 | HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
|
---|
575 | {
|
---|
576 | HMODULE rc;
|
---|
577 | char *astring;
|
---|
578 |
|
---|
579 | astring = UnicodeToAsciiString((LPWSTR)arg1);
|
---|
580 | rc = GetModuleHandleA(astring);
|
---|
581 | dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
|
---|
582 | FreeAsciiString(astring);
|
---|
583 | return(rc);
|
---|
584 | }
|
---|
585 | //******************************************************************************
|
---|
586 | //******************************************************************************
|
---|
587 | BOOL WIN32API CreateProcessA(LPCSTR lpszImageName, LPSTR lpszCommandLine,
|
---|
588 | PSECURITY_ATTRIBUTES arg3,
|
---|
589 | PSECURITY_ATTRIBUTES arg4, BOOL arg5, DWORD arg6,
|
---|
590 | PVOID arg7, LPCSTR arg8, LPSTARTUPINFOA arg9,
|
---|
591 | LPPROCESS_INFORMATION arg10)
|
---|
592 | {
|
---|
593 | BOOL rc;
|
---|
594 | char *cmdline;
|
---|
595 | BOOL fAllocStr = FALSE;
|
---|
596 |
|
---|
597 | if(O32_CreateProcess(lpszImageName, lpszCommandLine, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) == TRUE)
|
---|
598 | return(TRUE);
|
---|
599 |
|
---|
600 | //probably a win32 exe, so run it in the pe loader
|
---|
601 | if(lpszImageName) {
|
---|
602 | if(lpszCommandLine) {
|
---|
603 | cmdline = (char *)malloc(strlen(lpszImageName)+strlen(lpszCommandLine) + 16);
|
---|
604 | sprintf(cmdline, "%s %s", lpszImageName, lpszCommandLine);
|
---|
605 | fAllocStr = TRUE;
|
---|
606 | }
|
---|
607 | else cmdline = (char *)lpszImageName;
|
---|
608 | }
|
---|
609 | else cmdline = (char *)lpszCommandLine;
|
---|
610 |
|
---|
611 | dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
|
---|
612 | rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
---|
613 | if(fAllocStr)
|
---|
614 | free(cmdline);
|
---|
615 |
|
---|
616 | dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
|
---|
617 | return(rc);
|
---|
618 | }
|
---|
619 | //******************************************************************************
|
---|
620 | //******************************************************************************
|
---|
621 | BOOL WIN32API CreateProcessW(LPCWSTR arg1, LPWSTR arg2,
|
---|
622 | PSECURITY_ATTRIBUTES arg3,
|
---|
623 | PSECURITY_ATTRIBUTES arg4,
|
---|
624 | BOOL arg5, DWORD arg6, PVOID arg7,
|
---|
625 | LPCWSTR arg8, LPSTARTUPINFOW arg9,
|
---|
626 | LPPROCESS_INFORMATION arg10)
|
---|
627 | {
|
---|
628 | BOOL rc;
|
---|
629 | char *astring1, *astring2, *astring3;
|
---|
630 |
|
---|
631 | dprintf(("KERNEL32: OS2CreateProcessW DOESN't WORK"));
|
---|
632 | astring1 = UnicodeToAsciiString((LPWSTR)arg1);
|
---|
633 | astring2 = UnicodeToAsciiString(arg2);
|
---|
634 | astring3 = UnicodeToAsciiString((LPWSTR)arg8);
|
---|
635 | // NOTE: This will not work as is (needs UNICODE support)
|
---|
636 | rc = CreateProcessA(astring1, astring2, arg3, arg4, arg5, arg6, arg7,
|
---|
637 | astring3, (LPSTARTUPINFOA)arg9, arg10);
|
---|
638 | FreeAsciiString(astring3);
|
---|
639 | FreeAsciiString(astring2);
|
---|
640 | FreeAsciiString(astring1);
|
---|
641 | return(rc);
|
---|
642 | }
|
---|
643 | //******************************************************************************
|
---|
644 | //******************************************************************************
|
---|
645 | FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
|
---|
646 | {
|
---|
647 | Win32DllBase *winmod;
|
---|
648 | FARPROC proc;
|
---|
649 | ULONG ulAPIOrdinal;
|
---|
650 |
|
---|
651 | winmod = Win32DllBase::findModule((HINSTANCE)hModule);
|
---|
652 | if(winmod) {
|
---|
653 | ulAPIOrdinal = (ULONG)lpszProc;
|
---|
654 | if (ulAPIOrdinal <= 0x0000FFFF) {
|
---|
655 | proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
|
---|
656 | }
|
---|
657 | else proc = (FARPROC)winmod->getApi((char *)lpszProc);
|
---|
658 | if(proc == 0) {
|
---|
659 | SetLastError(ERROR_PROC_NOT_FOUND);
|
---|
660 | }
|
---|
661 | return proc;
|
---|
662 | }
|
---|
663 | proc = O32_GetProcAddress(hModule, lpszProc);
|
---|
664 | dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
|
---|
665 | return(proc);
|
---|
666 | }
|
---|
667 | //******************************************************************************
|
---|
668 | //Retrieve the version
|
---|
669 | //******************************************************************************
|
---|
670 | BOOL SYSTEM GetVersionStruct(char *modname, char *verstruct, ULONG bufLength)
|
---|
671 | {
|
---|
672 | Win32ImageBase *winimage;
|
---|
673 |
|
---|
674 | dprintf(("GetVersionStruct"));
|
---|
675 | if(WinExe && !stricmp(WinExe->getFullPath(), modname)) {
|
---|
676 | winimage = (Win32ImageBase *)WinExe;
|
---|
677 | }
|
---|
678 | else {
|
---|
679 | winimage = (Win32ImageBase *)Win32DllBase::findModule(modname);
|
---|
680 | if(winimage == NULL) {
|
---|
681 | dprintf(("GetVersionStruct can't find Win32Image for %s\n", modname));
|
---|
682 | return(FALSE);
|
---|
683 | }
|
---|
684 | }
|
---|
685 | return winimage->getVersionStruct(verstruct, bufLength);
|
---|
686 | }
|
---|
687 | //******************************************************************************
|
---|
688 | //******************************************************************************
|
---|
689 | ULONG SYSTEM GetVersionSize(char *modname)
|
---|
690 | {
|
---|
691 | Win32ImageBase *winimage;
|
---|
692 |
|
---|
693 | dprintf(("GetVersionSize of %s\n", modname));
|
---|
694 |
|
---|
695 | if(WinExe && !stricmp(WinExe->getFullPath(), modname)) {
|
---|
696 | winimage = (Win32ImageBase *)WinExe;
|
---|
697 | }
|
---|
698 | else {
|
---|
699 | winimage = (Win32ImageBase *)Win32DllBase::findModule(modname);
|
---|
700 | if(winimage == NULL) {
|
---|
701 | dprintf(("GetVersionSize can't find Win32Image for %s\n", modname));
|
---|
702 | return(FALSE);
|
---|
703 | }
|
---|
704 | }
|
---|
705 | return winimage->getVersionSize();
|
---|
706 | }
|
---|
707 | //******************************************************************************
|
---|
708 | //******************************************************************************
|
---|
709 |
|
---|
710 |
|
---|
711 | /***********************************************************************
|
---|
712 | * RegisterServiceProcess (KERNEL, KERNEL32)
|
---|
713 | *
|
---|
714 | * A service process calls this function to ensure that it continues to run
|
---|
715 | * even after a user logged off.
|
---|
716 | */
|
---|
717 | DWORD WIN32API RegisterServiceProcess(DWORD dwProcessId,
|
---|
718 | DWORD dwType)
|
---|
719 | {
|
---|
720 | dprintf(("KERNEL32: RegisterServiceProcess(%08xh,%08xh) not implemented.\n",
|
---|
721 | dwProcessId,
|
---|
722 | dwType));
|
---|
723 |
|
---|
724 | /* I don't think that Wine needs to do anything in that function */
|
---|
725 | return 1; /* success */
|
---|
726 | }
|
---|
727 |
|
---|
728 | //******************************************************************************
|
---|
729 | //TODO:What does this do exactly??
|
---|
730 | //******************************************************************************
|
---|
731 | ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
|
---|
732 | {
|
---|
733 | Win32DllBase *winmod;
|
---|
734 | FARPROC proc;
|
---|
735 | ULONG ulAPIOrdinal;
|
---|
736 |
|
---|
737 | winmod = Win32DllBase::findModule((HINSTANCE)hModule);
|
---|
738 | if(winmod)
|
---|
739 | {
|
---|
740 | // don't call ATTACH/DETACH thread functions in DLL
|
---|
741 | winmod->setThreadLibraryCalls(FALSE);
|
---|
742 | return TRUE;
|
---|
743 | }
|
---|
744 | else
|
---|
745 | {
|
---|
746 | // raise error condition
|
---|
747 | SetLastError(ERROR_INVALID_HANDLE);
|
---|
748 | return FALSE;
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|