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