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