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