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