source: trunk/src/kernel32/wprocess.cpp@ 2076

Last change on this file since 2076 was 2053, checked in by sandervl, 26 years ago

Memory map fixes

File size: 27.6 KB
Line 
1/* $Id: wprocess.cpp,v 1.57 1999-12-10 14:06:12 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
42ODINDEBUGCHANNEL(KERNEL32-WPROCESS)
43
44
45//******************************************************************************
46//******************************************************************************
47BOOL fFreeLibrary = FALSE;
48BOOL fIsOS2Image = FALSE; //TRUE -> Odin32 OS/2 application (not converted!)
49 //FALSE -> otherwise
50//Process database
51PDB ProcessPDB = {0};
52USHORT ProcessTIBSel = 0;
53DWORD *TIBFlatPtr = 0;
54
55//******************************************************************************
56//******************************************************************************
57TEB *WIN32API GetThreadTEB()
58{
59 if(TIBFlatPtr == NULL)
60 return 0;
61
62 return (TEB *)*TIBFlatPtr;
63}
64//******************************************************************************
65//******************************************************************************
66THDB *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//******************************************************************************
85TEB *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//******************************************************************************
173void 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/******************************************************************************/
201void SetPDBInstance(HINSTANCE hInstance)
202{
203 ProcessPDB.hInstance = hInstance;
204}
205/******************************************************************************/
206/******************************************************************************/
207void 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/******************************************************************************/
230USHORT 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//******************************************************************************
271void _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//******************************************************************************
283VOID 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//******************************************************************************
310BOOL 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/******************************************************************************/
332static 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 dprintf(("iLoadLibrary: found %s -> handle %x", lpszLibFile, module->getInstanceHandle()));
342 return module->getInstanceHandle();
343 }
344
345 strcpy(modname, lpszLibFile);
346 strupr(modname);
347 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
348 Win32DllBase::renameDll(modname);
349
350 hDll = O32_LoadLibrary(modname);
351 dprintf(("KERNEL32: iLoadLibraryA %s returned %X (%d)\n",
352 lpszLibFile,
353 hDll,
354 GetLastError()));
355 if(hDll)
356 {
357 return hDll; //converted dll or win32k took care of it
358 }
359
360 if(!strstr(modname, ".")) {
361 strcat(modname,".DLL");
362 }
363
364 if(Win32ImageBase::isPEImage((char *)modname))
365 {
366 module = Win32DllBase::findModule((char *)modname);
367 if(module) {//don't load it again
368 module->AddRef();
369 return module->getInstanceHandle();
370 }
371
372 Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
373 if(peldrDll == NULL)
374 return(0);
375
376 peldrDll->init(0);
377 if(peldrDll->getError() != NO_ERROR) {
378 dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
379 delete(peldrDll);
380 return(0);
381 }
382 if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
383 peldrDll->setNoEntryCalls();
384 }
385
386 if(peldrDll->attachProcess() == FALSE) {
387 dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
388 delete(peldrDll);
389 return(0);
390 }
391 peldrDll->AddRef();
392 return peldrDll->getInstanceHandle();
393 }
394 else return(0);
395}
396//******************************************************************************
397//******************************************************************************
398HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
399{
400 HINSTANCE hDll;
401
402 dprintf(("KERNEL32: LoadLibraryA(%s)\n",
403 lpszLibFile));
404 dprintf(("KERNEL32: LoadLibrary %x FS = %x\n", GetCurrentThreadId(), GetFS()));
405
406 hDll = iLoadLibraryA(lpszLibFile, 0);
407 if (hDll == 0)
408 {
409 char * pszName;
410
411 // remove path from the image name
412 pszName = strrchr((char *)lpszLibFile,
413 '\\');
414 if (pszName != NULL)
415 {
416 pszName++; // skip backslash
417
418 // now try again without fully qualified path
419 hDll = iLoadLibraryA(pszName, 0);
420 }
421 }
422
423 return hDll;
424}
425//******************************************************************************
426//******************************************************************************
427HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
428{
429 HINSTANCE hDll;
430
431 dprintf(("KERNEL32: LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
432 hDll = iLoadLibraryA(lpszLibFile, dwFlags);
433 if (hDll == 0)
434 {
435 char * pszName;
436
437 // remove path from the image name
438 pszName = strrchr((char *)lpszLibFile,
439 '\\');
440 if (pszName != NULL)
441 {
442 pszName++; // skip backslash
443
444 // now try again without fully qualified path
445 hDll = iLoadLibraryA(pszName, dwFlags);
446 }
447 }
448
449 return hDll;
450}
451//******************************************************************************
452//******************************************************************************
453HINSTANCE WIN32API LoadLibraryW(LPCWSTR lpModule)
454{
455 char *asciimodule;
456 HINSTANCE rc;
457
458 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
459 dprintf(("KERNEL32: OS2LoadLibraryW %s\n", asciimodule));
460 rc = LoadLibraryA(asciimodule);
461 free(asciimodule);
462 return(rc);
463}
464//******************************************************************************
465//******************************************************************************
466HINSTANCE WIN32API LoadLibraryExW(LPCWSTR lpModule, HANDLE hFile, DWORD dwFlags)
467{
468 char *asciimodule;
469 HINSTANCE rc;
470
471 asciimodule = UnicodeToAsciiString((LPWSTR)lpModule);
472 dprintf(("KERNEL32: OS2LoadLibraryExW %s (%d)\n", asciimodule, dwFlags));
473 rc = LoadLibraryExA(asciimodule, hFile, dwFlags);
474 free(asciimodule);
475 return(rc);
476}
477//******************************************************************************
478//******************************************************************************
479LPCSTR WIN32API GetCommandLineA()
480{
481 LPTSTR cmdline = NULL;
482
483 if(WinExe) {
484 cmdline = WinExe->getCommandLine();
485 }
486 if(cmdline == NULL) //not used for converted exes
487 cmdline = O32_GetCommandLine();
488
489 dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
490 dprintf(("KERNEL32: FS = %x\n", GetFS()));
491 return(cmdline);
492}
493//******************************************************************************
494//******************************************************************************
495LPCWSTR WIN32API GetCommandLineW(void)
496{
497 static WCHAR *UnicodeCmdLine = NULL;
498 char *asciicmdline = NULL;
499
500 dprintf(("KERNEL32: FS = %x\n", GetFS()));
501
502 if(UnicodeCmdLine)
503 return(UnicodeCmdLine); //already called before
504
505 if(WinExe) {
506 asciicmdline = WinExe->getCommandLine();
507 }
508 if(asciicmdline == NULL) //not used for converted exes
509 asciicmdline = O32_GetCommandLine();
510
511 if(asciicmdline) {
512 UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
513 AsciiToUnicode(asciicmdline, UnicodeCmdLine);
514 dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
515 return(UnicodeCmdLine);
516 }
517 dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
518 return NULL;
519}
520//******************************************************************************
521//******************************************************************************
522DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
523{
524 DWORD rc;
525 Win32ImageBase *module;
526 char *fpath = NULL;
527
528 dprintf(("GetModuleFileName %X", hinstModule));
529 if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
530 module = (Win32ImageBase *)WinExe;
531 }
532 else {
533 module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
534 }
535
536 if(module) {
537 fpath = module->getFullPath();
538 }
539 if(fpath) {
540 //SvL: 13-9-98: +1
541 rc = min(strlen(fpath)+1, cchPath);
542 strncpy(lpszPath, fpath, rc);
543 }
544 else rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
545
546 if(rc) {
547 dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
548 }
549 return(rc);
550}
551//******************************************************************************
552//******************************************************************************
553DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
554{
555 char *asciifilename = (char *)malloc(nSize+1);
556 DWORD rc;
557
558 dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
559 rc = GetModuleFileNameA(hModule, asciifilename, nSize);
560 if(rc) AsciiToUnicode(asciifilename, lpFileName);
561 free(asciifilename);
562 return(rc);
563}
564//******************************************************************************
565//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
566// very.weird.exe)
567//******************************************************************************
568HANDLE WIN32API GetModuleHandleA(LPCTSTR lpszModule)
569{
570 HANDLE hMod;
571 Win32DllBase *windll;
572 char szModule[CCHMAXPATH];
573 BOOL fDllModule = FALSE;
574
575 if(lpszModule == NULL) {
576 if(WinExe)
577 hMod = WinExe->getInstanceHandle();
578 else hMod = -1;
579 }
580 else {
581 strcpy(szModule, OSLibStripPath((char *)lpszModule));
582 strupr(szModule);
583 if(strstr(szModule, ".DLL")) {
584 fDllModule = TRUE;
585 }
586 else {
587 if(!strstr(szModule, ".")) {
588 //if there's no extension or trainling dot, we
589 //assume it's a dll (see Win32 SDK docs)
590 fDllModule = TRUE;
591 }
592 }
593 char *dot = strstr(szModule, ".");
594 if(dot)
595 *dot = 0;
596
597 if(!fDllModule && WinExe && !strcmpi(szModule, WinExe->getModuleName())) {
598 hMod = WinExe->getInstanceHandle();
599 }
600 else {
601 windll = Win32DllBase::findModule(szModule);
602 if(windll) {
603 hMod = windll->getInstanceHandle();
604 }
605 else hMod = OSLibiGetModuleHandleA((char *)lpszModule);
606 }
607 }
608
609 dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
610 return(hMod);
611}
612//******************************************************************************
613//******************************************************************************
614HMODULE WIN32API GetModuleHandleW(LPCWSTR arg1)
615{
616 HMODULE rc;
617 char *astring;
618
619 astring = UnicodeToAsciiString((LPWSTR)arg1);
620 rc = GetModuleHandleA(astring);
621 dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
622 FreeAsciiString(astring);
623 return(rc);
624}
625//******************************************************************************
626//******************************************************************************
627BOOL WINAPI CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine,
628 LPSECURITY_ATTRIBUTES lpProcessAttributes,
629 LPSECURITY_ATTRIBUTES lpThreadAttributes,
630 BOOL bInheritHandles, DWORD dwCreationFlags,
631 LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
632 LPSTARTUPINFOA lpStartupInfo,
633 LPPROCESS_INFORMATION lpProcessInfo )
634{
635 BOOL rc;
636 char *cmdline;
637 BOOL fAllocStr = FALSE;
638
639 dprintf(("KERNEL32: CreateProcessA %s cline:%s inherit:%d cFlags:%x Env:%x CurDir:%s StartupFlags:%x\n",
640 lpApplicationName, lpCommandLine, bInheritHandles, dwCreationFlags,
641 lpEnvironment, lpCurrentDirectory, lpStartupInfo));
642
643 if(O32_CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
644 lpThreadAttributes, bInheritHandles, dwCreationFlags,
645 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
646 lpProcessInfo) == TRUE)
647 return(TRUE);
648
649 //probably a win32 exe, so run it in the pe loader
650 if(lpApplicationName) {
651 if(lpCommandLine) {
652 cmdline = (char *)malloc(strlen(lpApplicationName)+strlen(lpCommandLine) + 16);
653 sprintf(cmdline, "PE.EXE %s %s", lpApplicationName, lpCommandLine);
654 fAllocStr = TRUE;
655 }
656 else {
657 cmdline = (char *)malloc(strlen(lpApplicationName) + 16);
658 sprintf(cmdline, "PE.EXE %s", lpApplicationName);
659 fAllocStr = TRUE;
660 }
661 }
662 else {
663 cmdline = (char *)malloc(strlen(lpCommandLine) + 16);
664 sprintf(cmdline, "PE.EXE %s", lpCommandLine);
665 fAllocStr = TRUE;
666 }
667 dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
668 rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
669 lpThreadAttributes, bInheritHandles, dwCreationFlags,
670 lpEnvironment, lpCurrentDirectory, lpStartupInfo,
671 lpProcessInfo);
672 if(fAllocStr)
673 free(cmdline);
674
675 if(lpProcessInfo)
676 dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
677 rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
678 lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
679 else
680 dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
681 return(rc);
682}
683//******************************************************************************
684//******************************************************************************
685BOOL WIN32API CreateProcessW(LPCWSTR arg1, LPWSTR arg2,
686 PSECURITY_ATTRIBUTES arg3,
687 PSECURITY_ATTRIBUTES arg4,
688 BOOL arg5, DWORD arg6, PVOID arg7,
689 LPCWSTR arg8, LPSTARTUPINFOW arg9,
690 LPPROCESS_INFORMATION arg10)
691{
692 BOOL rc;
693 char *astring1, *astring2, *astring3;
694
695 dprintf(("KERNEL32: OS2CreateProcessW DOESN't WORK"));
696 astring1 = UnicodeToAsciiString((LPWSTR)arg1);
697 astring2 = UnicodeToAsciiString(arg2);
698 astring3 = UnicodeToAsciiString((LPWSTR)arg8);
699 // NOTE: This will not work as is (needs UNICODE support)
700 rc = CreateProcessA(astring1, astring2, arg3, arg4, arg5, arg6, arg7,
701 astring3, (LPSTARTUPINFOA)arg9, arg10);
702 FreeAsciiString(astring3);
703 FreeAsciiString(astring2);
704 FreeAsciiString(astring1);
705 return(rc);
706}
707//******************************************************************************
708//******************************************************************************
709HINSTANCE WIN32API WinExec(LPCSTR lpCmdLine, UINT nCmdShow)
710{
711 STARTUPINFOA startinfo = {0};
712 PROCESS_INFORMATION procinfo;
713
714 dprintf(("KERNEL32: WinExec %s\n", lpCmdLine));
715 startinfo.dwFlags = nCmdShow;
716 if(CreateProcessA(NULL, (LPSTR)lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
717 &startinfo, &procinfo) == FALSE)
718 {
719 return 0;
720 }
721 return procinfo.hProcess; //correct?
722}
723//******************************************************************************
724//******************************************************************************
725FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
726{
727 Win32ImageBase *winmod;
728 FARPROC proc;
729 ULONG ulAPIOrdinal;
730
731 if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
732 winmod = WinExe;
733 }
734 else winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
735
736 if(winmod) {
737 ulAPIOrdinal = (ULONG)lpszProc;
738 if (ulAPIOrdinal <= 0x0000FFFF) {
739 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
740 }
741 else proc = (FARPROC)winmod->getApi((char *)lpszProc);
742 if(proc == 0) {
743 SetLastError(ERROR_PROC_NOT_FOUND);
744 }
745 return proc;
746 }
747 proc = O32_GetProcAddress(hModule, lpszProc);
748 if(HIWORD(lpszProc))
749 dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
750 else dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
751 return(proc);
752}
753//******************************************************************************
754//Retrieve the version
755//******************************************************************************
756BOOL SYSTEM GetVersionStruct(char *lpszModName, char *verstruct, ULONG bufLength)
757{
758 Win32ImageBase *winimage;
759 Win32PeLdrRsrcImg *rsrcimg;
760
761 dprintf(("GetVersionStruct of module %s", lpszModName));
762 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
763 winimage = (Win32ImageBase *)WinExe;
764 }
765 else {
766 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
767 if(winimage == NULL)
768 {
769 char modname[CCHMAXPATH];
770
771 strcpy(modname, lpszModName);
772 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
773 Win32DllBase::renameDll(modname);
774
775 if(Win32ImageBase::isPEImage(modname) == FALSE)
776 {
777 HINSTANCE hInstance;
778
779 //must be an LX dll, just load it (app will probably load it anyway)
780 hInstance = LoadLibraryA(modname);
781 if(hInstance == 0)
782 return 0;
783 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
784 if(winimage) {
785 return winimage->getVersionStruct(verstruct, bufLength);
786 }
787 return 0;
788 }
789 //SvL: Try to load it
790 rsrcimg = new Win32PeLdrRsrcImg(modname);
791 if(rsrcimg == NULL)
792 return 0;
793
794 rsrcimg->init(0);
795 if(rsrcimg->getError() != NO_ERROR)
796 {
797 dprintf(("GetVersionStruct can't load %s\n", modname));
798 delete rsrcimg;
799 return(FALSE);
800 }
801 BOOL rc = rsrcimg->getVersionStruct(verstruct, bufLength);
802 delete rsrcimg;
803 return rc;
804 }
805 }
806 return winimage->getVersionStruct(verstruct, bufLength);
807}
808//******************************************************************************
809//******************************************************************************
810ULONG SYSTEM GetVersionSize(char *lpszModName)
811{
812 Win32ImageBase *winimage;
813 Win32PeLdrRsrcImg *rsrcimg;
814
815 dprintf(("GetVersionSize of %s\n", lpszModName));
816
817 if(WinExe && !stricmp(WinExe->getFullPath(), lpszModName)) {
818 winimage = (Win32ImageBase *)WinExe;
819 }
820 else {
821 winimage = (Win32ImageBase *)Win32DllBase::findModule(lpszModName);
822 if(winimage == NULL)
823 {
824 char modname[CCHMAXPATH];
825
826 strcpy(modname, lpszModName);
827 //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
828 Win32DllBase::renameDll(modname);
829
830 if(Win32ImageBase::isPEImage(modname) == FALSE)
831 {
832 HINSTANCE hInstance;
833
834 //must be an LX dll, just load it (app will probably load it anyway)
835 hInstance = LoadLibraryA(modname);
836 if(hInstance == 0)
837 return 0;
838 winimage = (Win32ImageBase *)Win32DllBase::findModule(hInstance);
839 if(winimage) {
840 return winimage->getVersionSize();
841 }
842 return 0;
843 }
844
845 //SvL: Try to load it
846 rsrcimg = new Win32PeLdrRsrcImg(modname);
847 if(rsrcimg == NULL)
848 return 0;
849
850 rsrcimg->init(0);
851 if(rsrcimg->getError() != NO_ERROR)
852 {
853 dprintf(("GetVersionSize can't load %s\n", modname));
854 delete rsrcimg;
855 return(FALSE);
856 }
857 int size = rsrcimg->getVersionSize();
858 delete rsrcimg;
859 return size;
860 }
861 }
862 return winimage->getVersionSize();
863}
864//******************************************************************************
865//TODO:What does this do exactly??
866//******************************************************************************
867ODINFUNCTION1(BOOL,DisableThreadLibraryCalls,HMODULE,hModule)
868{
869 Win32DllBase *winmod;
870 FARPROC proc;
871 ULONG ulAPIOrdinal;
872
873 winmod = Win32DllBase::findModule((HINSTANCE)hModule);
874 if(winmod)
875 {
876 // don't call ATTACH/DETACH thread functions in DLL
877 winmod->setThreadLibraryCalls(FALSE);
878 return TRUE;
879 }
880 else
881 {
882 // raise error condition
883 SetLastError(ERROR_INVALID_HANDLE);
884 return FALSE;
885 }
886}
887//******************************************************************************
888//******************************************************************************
Note: See TracBrowser for help on using the repository browser.