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