wsock32: Assign to struct instead of using memcpy.
[wine/hacks.git] / dlls / kernel32 / task.c
blob03913d7c5e829c35e0c2955c182cc19c554f9ed8
1 /*
2 * Task functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winnt.h"
36 #include "wownt32.h"
37 #include "winuser.h"
39 #include "wine/winbase16.h"
40 #include "winternl.h"
41 #include "toolhelp.h"
42 #include "kernel_private.h"
43 #include "kernel16_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(task);
48 WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
50 #include "pshpack1.h"
52 /* Segment containing MakeProcInstance() thunks */
53 typedef struct
55 WORD next; /* Selector of next segment */
56 WORD magic; /* Thunks signature */
57 WORD unused;
58 WORD free; /* Head of the free list */
59 WORD thunks[4]; /* Each thunk is 4 words long */
60 } THUNKS;
62 #include "poppack.h"
64 #define THUNK_MAGIC ('P' | ('T' << 8))
66 /* Min. number of thunks allocated when creating a new segment */
67 #define MIN_THUNKS 32
69 #define TDB_MAGIC ('T' | ('D' << 8))
71 static THHOOK DefaultThhook;
72 THHOOK *pThhook = &DefaultThhook;
74 #define hFirstTask (pThhook->HeadTDB)
75 #define hLockedTask (pThhook->LockTDB)
77 static UINT16 nTaskCount = 0;
79 static HTASK16 initial_task, main_task;
81 /***********************************************************************
82 * TASK_InstallTHHook
84 void TASK_InstallTHHook( THHOOK *pNewThhook )
86 THHOOK *pOldThhook = pThhook;
88 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
90 *pThhook = *pOldThhook;
93 /***********************************************************************
94 * TASK_GetPtr
96 static TDB *TASK_GetPtr( HTASK16 hTask )
98 return GlobalLock16( hTask );
102 /***********************************************************************
103 * TASK_GetCurrent
105 TDB *TASK_GetCurrent(void)
107 return TASK_GetPtr( GetCurrentTask() );
111 /***********************************************************************
112 * TASK_LinkTask
114 static void TASK_LinkTask( HTASK16 hTask )
116 HTASK16 *prevTask;
117 TDB *pTask;
119 if (!(pTask = TASK_GetPtr( hTask ))) return;
120 prevTask = &hFirstTask;
121 while (*prevTask)
123 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
124 if (prevTaskPtr->priority >= pTask->priority) break;
125 prevTask = &prevTaskPtr->hNext;
127 pTask->hNext = *prevTask;
128 *prevTask = hTask;
129 nTaskCount++;
133 /***********************************************************************
134 * TASK_UnlinkTask
136 static void TASK_UnlinkTask( HTASK16 hTask )
138 HTASK16 *prevTask;
139 TDB *pTask;
141 prevTask = &hFirstTask;
142 while (*prevTask && (*prevTask != hTask))
144 pTask = TASK_GetPtr( *prevTask );
145 prevTask = &pTask->hNext;
147 if (*prevTask)
149 pTask = TASK_GetPtr( *prevTask );
150 *prevTask = pTask->hNext;
151 pTask->hNext = 0;
152 nTaskCount--;
157 /***********************************************************************
158 * TASK_CreateThunks
160 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
161 * and containing 'count' entries.
163 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
165 int i;
166 WORD free;
167 THUNKS *pThunk;
169 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
170 pThunk->next = 0;
171 pThunk->magic = THUNK_MAGIC;
172 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
173 free = pThunk->free;
174 for (i = 0; i < count-1; i++)
176 free += 8; /* Offset of next thunk */
177 pThunk->thunks[4*i] = free;
179 pThunk->thunks[4*i] = 0; /* Last thunk */
183 /***********************************************************************
184 * TASK_AllocThunk
186 * Allocate a thunk for MakeProcInstance().
188 static SEGPTR TASK_AllocThunk(void)
190 TDB *pTask;
191 THUNKS *pThunk;
192 WORD sel, base;
194 if (!(pTask = TASK_GetCurrent())) return 0;
195 sel = pTask->hCSAlias;
196 pThunk = (THUNKS *)&pTask->thunks;
197 base = (char *)pThunk - (char *)pTask;
198 while (!pThunk->free)
200 sel = pThunk->next;
201 if (!sel) /* Allocate a new segment */
203 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
204 pTask->hPDB, WINE_LDT_FLAGS_CODE );
205 if (!sel) return (SEGPTR)0;
206 TASK_CreateThunks( sel, 0, MIN_THUNKS );
207 pThunk->next = sel;
209 pThunk = (THUNKS *)GlobalLock16( sel );
210 base = 0;
212 base += pThunk->free;
213 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
214 return MAKESEGPTR( sel, base );
218 /***********************************************************************
219 * TASK_FreeThunk
221 * Free a MakeProcInstance() thunk.
223 static BOOL TASK_FreeThunk( SEGPTR thunk )
225 TDB *pTask;
226 THUNKS *pThunk;
227 WORD sel, base;
229 if (!(pTask = TASK_GetCurrent())) return 0;
230 sel = pTask->hCSAlias;
231 pThunk = (THUNKS *)&pTask->thunks;
232 base = (char *)pThunk - (char *)pTask;
233 while (sel && (sel != HIWORD(thunk)))
235 sel = pThunk->next;
236 pThunk = (THUNKS *)GlobalLock16( sel );
237 base = 0;
239 if (!sel) return FALSE;
240 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
241 pThunk->free = LOWORD(thunk) - base;
242 return TRUE;
246 /***********************************************************************
247 * TASK_Create
249 * NOTE: This routine might be called by a Win32 thread. Thus, we need
250 * to be careful to protect global data structures. We do this
251 * by entering the Win16Lock while linking the task into the
252 * global task list.
254 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, LPCSTR cmdline, BYTE len )
256 HTASK16 hTask;
257 TDB *pTask;
258 FARPROC16 proc;
259 char curdir[MAX_PATH];
260 HMODULE16 hModule = pModule ? pModule->self : 0;
262 /* Allocate the task structure */
264 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
265 if (!hTask) return NULL;
266 pTask = TASK_GetPtr( hTask );
267 FarSetOwner16( hTask, hModule );
269 /* Fill the task structure */
271 pTask->hSelf = hTask;
273 pTask->version = pModule ? pModule->ne_expver : 0x0400;
274 pTask->hModule = hModule;
275 pTask->hParent = GetCurrentTask();
276 pTask->magic = TDB_MAGIC;
277 pTask->nCmdShow = cmdShow;
279 GetCurrentDirectoryA( sizeof(curdir), curdir );
280 GetShortPathNameA( curdir, curdir, sizeof(curdir) );
281 pTask->curdrive = (curdir[0] - 'A') | 0x80;
282 lstrcpynA( pTask->curdir, curdir + 2, sizeof(pTask->curdir) );
284 /* Create the thunks block */
286 TASK_CreateThunks( hTask, (char *)&pTask->thunks - (char *)pTask, 7 );
288 /* Copy the module name */
290 if (hModule)
292 char name[sizeof(pTask->module_name)+1];
293 size_t len;
294 GetModuleName16( hModule, name, sizeof(name) );
295 len = strlen(name) + 1;
296 memcpy(pTask->module_name, name, min(len,sizeof(pTask->module_name)));
297 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
300 /* Allocate a selector for the PDB */
302 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
303 hModule, WINE_LDT_FLAGS_DATA );
305 /* Fill the PDB */
307 pTask->pdb.int20 = 0x20cd;
308 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
309 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
310 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
311 pTask->pdb.savedint22 = 0;
312 pTask->pdb.savedint23 = 0;
313 pTask->pdb.savedint24 = 0;
314 pTask->pdb.fileHandlesPtr =
315 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
316 pTask->pdb.hFileHandles = 0;
317 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
318 /* FIXME: should we make a copy of the environment? */
319 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
320 pTask->pdb.nbFiles = 20;
322 /* Fill the command line */
324 if (!cmdline)
326 cmdline = GetCommandLineA();
327 /* remove the first word (program name) */
328 if (*cmdline == '"')
329 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
330 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
331 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
332 len = strlen(cmdline);
334 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
335 pTask->pdb.cmdLine[0] = len;
336 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
337 /* pTask->pdb.cmdLine[len+1] = 0; */
339 TRACE("cmdline='%.*s' task=%04x\n", len, cmdline, hTask );
341 /* Allocate a code segment alias for the TDB */
343 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
344 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
346 /* Default DTA overwrites command line */
348 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
350 /* Create scheduler event for 16-bit tasks */
352 if ( !(pTask->flags & TDBF_WIN32) )
353 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
355 if (!initial_task) initial_task = hTask;
357 return pTask;
361 /***********************************************************************
362 * TASK_DeleteTask
364 static void TASK_DeleteTask( HTASK16 hTask )
366 TDB *pTask;
367 HGLOBAL16 hPDB;
369 if (!(pTask = TASK_GetPtr( hTask ))) return;
370 hPDB = pTask->hPDB;
372 pTask->magic = 0xdead; /* invalidate signature */
374 /* Free the selector aliases */
376 GLOBAL_FreeBlock( pTask->hCSAlias );
377 GLOBAL_FreeBlock( pTask->hPDB );
379 /* Free the task module */
381 FreeModule16( pTask->hModule );
383 /* Free the task structure itself */
385 GlobalFree16( hTask );
387 /* Free all memory used by this task (including the 32-bit stack, */
388 /* the environment block and the thunk segments). */
390 GlobalFreeAll16( hPDB );
394 /***********************************************************************
395 * TASK_CreateMainTask
397 * Create a task for the main (32-bit) process.
399 void TASK_CreateMainTask(void)
401 TDB *pTask;
402 STARTUPINFOA startup_info;
403 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
405 GetStartupInfoA( &startup_info );
406 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
407 pTask = TASK_Create( NULL, cmdShow, NULL, 0 );
408 if (!pTask)
410 ERR("could not create task for main process\n");
411 ExitProcess(1);
414 pTask->flags |= TDBF_WIN32;
415 pTask->hInstance = 0;
416 pTask->hPrevInstance = 0;
417 pTask->teb = NtCurrentTeb();
419 /* Add the task to the linked list */
420 /* (no need to get the win16 lock, we are the only thread at this point) */
421 TASK_LinkTask( pTask->hSelf );
422 main_task = pTask->hSelf;
426 struct create_data
428 TDB *task;
429 WIN16_SUBSYSTEM_TIB *tib;
432 /* allocate the win16 TIB for a new 16-bit task */
433 static WIN16_SUBSYSTEM_TIB *allocate_win16_tib( TDB *pTask )
435 WCHAR path[MAX_PATH];
436 WIN16_SUBSYSTEM_TIB *tib;
437 UNICODE_STRING *curdir;
438 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
440 if (!(tib = HeapAlloc( GetProcessHeap(), 0, sizeof(*tib) ))) return NULL;
441 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
442 GetLongPathNameW( path, path, MAX_PATH );
443 if (RtlCreateUnicodeString( &tib->exe_str, path )) tib->exe_name = &tib->exe_str;
444 else tib->exe_name = NULL;
446 RtlAcquirePebLock();
447 if (NtCurrentTeb()->Tib.SubSystemTib)
448 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
449 else
450 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
451 tib->curdir.DosPath.MaximumLength = sizeof(tib->curdir_buffer);
452 tib->curdir.DosPath.Length = min( curdir->Length, tib->curdir.DosPath.MaximumLength-sizeof(WCHAR) );
453 tib->curdir.DosPath.Buffer = tib->curdir_buffer;
454 tib->curdir.Handle = 0;
455 memcpy( tib->curdir_buffer, curdir->Buffer, tib->curdir.DosPath.Length );
456 tib->curdir_buffer[tib->curdir.DosPath.Length/sizeof(WCHAR)] = 0;
457 RtlReleasePebLock();
458 return tib;
461 static inline void free_win16_tib( WIN16_SUBSYSTEM_TIB *tib )
463 if (tib->exe_name) RtlFreeUnicodeString( tib->exe_name );
464 HeapFree( GetProcessHeap(), 0, tib );
467 /* startup routine for a new 16-bit thread */
468 static DWORD CALLBACK task_start( LPVOID p )
470 struct create_data *data = p;
471 TDB *pTask = data->task;
472 DWORD ret;
474 kernel_get_thread_data()->htask16 = pTask->hSelf;
475 NtCurrentTeb()->Tib.SubSystemTib = data->tib;
476 HeapFree( GetProcessHeap(), 0, data );
478 _EnterWin16Lock();
479 TASK_LinkTask( pTask->hSelf );
480 pTask->teb = NtCurrentTeb();
481 ret = NE_StartTask();
482 _LeaveWin16Lock();
483 return ret;
487 /***********************************************************************
488 * TASK_SpawnTask
490 * Spawn a new 16-bit task.
492 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
493 LPCSTR cmdline, BYTE len, HANDLE *hThread )
495 struct create_data *data = NULL;
496 WIN16_SUBSYSTEM_TIB *tib;
497 TDB *pTask;
499 if (!(pTask = TASK_Create( pModule, cmdShow, cmdline, len ))) return 0;
500 if (!(tib = allocate_win16_tib( pTask ))) goto failed;
501 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data)))) goto failed;
502 data->task = pTask;
503 data->tib = tib;
504 if (!(*hThread = CreateThread( NULL, 0, task_start, data, 0, NULL ))) goto failed;
505 return pTask->hSelf;
507 failed:
508 if (tib) free_win16_tib( tib );
509 HeapFree( GetProcessHeap(), 0, data );
510 TASK_DeleteTask( pTask->hSelf );
511 return 0;
515 /***********************************************************************
516 * TASK_GetTaskFromThread
518 HTASK16 TASK_GetTaskFromThread( DWORD thread )
520 TDB *p = TASK_GetPtr( hFirstTask );
521 while (p)
523 if (p->teb->ClientId.UniqueThread == (HANDLE)thread) return p->hSelf;
524 p = TASK_GetPtr( p->hNext );
526 return 0;
530 /***********************************************************************
531 * TASK_CallTaskSignalProc
533 static void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
535 WORD args[5];
536 TDB *pTask = TASK_GetCurrent();
538 if ( !pTask || !pTask->userhandler ) return;
540 args[4] = hTaskOrModule;
541 args[3] = uCode;
542 args[2] = 0;
543 args[1] = pTask->hInstance;
544 args[0] = pTask->hQueue;
545 WOWCallback16Ex( (DWORD)pTask->userhandler, WCB16_PASCAL, sizeof(args), args, NULL );
549 /***********************************************************************
550 * TASK_ExitTask
552 void TASK_ExitTask(void)
554 WIN16_SUBSYSTEM_TIB *tib;
555 TDB *pTask;
556 DWORD lockCount;
558 /* Enter the Win16Lock to protect global data structures */
559 _EnterWin16Lock();
561 pTask = TASK_GetCurrent();
562 if ( !pTask )
564 _LeaveWin16Lock();
565 return;
568 TRACE("Killing task %04x\n", pTask->hSelf );
570 /* Perform USER cleanup */
572 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
574 /* Remove the task from the list to be sure we never switch back to it */
575 TASK_UnlinkTask( pTask->hSelf );
577 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
579 TRACE("this is the last task, exiting\n" );
580 ExitKernel16();
583 pTask->nEvents = 0;
585 if ( hLockedTask == pTask->hSelf )
586 hLockedTask = 0;
588 TASK_DeleteTask( pTask->hSelf );
590 if ((tib = NtCurrentTeb()->Tib.SubSystemTib))
592 free_win16_tib( tib );
593 NtCurrentTeb()->Tib.SubSystemTib = NULL;
596 /* ... and completely release the Win16Lock, just in case. */
597 ReleaseThunkLock( &lockCount );
601 /***********************************************************************
602 * ExitKernel (KERNEL.2)
604 * Clean-up everything and exit the Wine process.
606 void WINAPI ExitKernel16(void)
608 WriteOutProfiles16();
609 TerminateProcess( GetCurrentProcess(), 0 );
613 /***********************************************************************
614 * InitTask (KERNEL.91)
616 * Called by the application startup code.
618 void WINAPI InitTask16( CONTEXT86 *context )
620 TDB *pTask;
621 INSTANCEDATA *pinstance;
622 SEGPTR ptr;
624 context->Eax = 0;
625 if (!(pTask = TASK_GetCurrent())) return;
627 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
628 as some apps, notably Visual Basic apps, *modify* the heap/stack
629 size of the instance data segment before calling InitTask() */
631 /* Initialize the INSTANCEDATA structure */
632 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
633 pinstance->stackmin = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + sizeof( STACK16FRAME );
634 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
635 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
636 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
638 /* Initialize the local heap */
639 if (LOWORD(context->Ecx))
640 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
642 /* Initialize implicitly loaded DLLs */
643 NE_InitializeDLLs( pTask->hModule );
644 NE_DllProcessAttach( pTask->hModule );
646 /* Registers on return are:
647 * ax 1 if OK, 0 on error
648 * cx stack limit in bytes
649 * dx cmdShow parameter
650 * si instance handle of the previous instance
651 * di instance handle of the new task
652 * es:bx pointer to command line inside PSP
654 * 0 (=%bp) is pushed on the stack
656 ptr = stack16_push( sizeof(WORD) );
657 *(WORD *)MapSL(ptr) = 0;
658 context->Esp -= 2;
660 context->Eax = 1;
662 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
663 else
665 LPBYTE p = &pTask->pdb.cmdLine[1];
666 while ((*p == ' ') || (*p == '\t')) p++;
667 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
669 context->Ecx = pinstance->stacktop;
670 context->Edx = pTask->nCmdShow;
671 context->Esi = (DWORD)pTask->hPrevInstance;
672 context->Edi = (DWORD)pTask->hInstance;
673 context->SegEs = (WORD)pTask->hPDB;
677 /***********************************************************************
678 * WaitEvent (KERNEL.30)
680 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
682 TDB *pTask;
684 if (!hTask) hTask = GetCurrentTask();
685 pTask = TASK_GetPtr( hTask );
687 if (pTask->flags & TDBF_WIN32)
689 FIXME("called for Win32 thread (%04x)!\n", GetCurrentThreadId());
690 return TRUE;
693 if (pTask->nEvents > 0)
695 pTask->nEvents--;
696 return FALSE;
699 if (pTask->teb == NtCurrentTeb())
701 DWORD lockCount;
703 NtResetEvent( pTask->hEvent, NULL );
704 ReleaseThunkLock( &lockCount );
705 SYSLEVEL_CheckNotLevel( 1 );
706 WaitForSingleObject( pTask->hEvent, INFINITE );
707 RestoreThunkLock( lockCount );
708 if (pTask->nEvents > 0) pTask->nEvents--;
710 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
712 return TRUE;
716 /***********************************************************************
717 * PostEvent (KERNEL.31)
719 void WINAPI PostEvent16( HTASK16 hTask )
721 TDB *pTask;
723 if (!hTask) hTask = GetCurrentTask();
724 if (!(pTask = TASK_GetPtr( hTask ))) return;
726 if (pTask->flags & TDBF_WIN32)
728 FIXME("called for Win32 thread (%04x)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
729 return;
732 pTask->nEvents++;
734 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
738 /***********************************************************************
739 * SetPriority (KERNEL.32)
741 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
743 TDB *pTask;
744 INT16 newpriority;
746 if (!hTask) hTask = GetCurrentTask();
747 if (!(pTask = TASK_GetPtr( hTask ))) return;
748 newpriority = pTask->priority + delta;
749 if (newpriority < -32) newpriority = -32;
750 else if (newpriority > 15) newpriority = 15;
752 pTask->priority = newpriority + 1;
753 TASK_UnlinkTask( pTask->hSelf );
754 TASK_LinkTask( pTask->hSelf );
755 pTask->priority--;
759 /***********************************************************************
760 * LockCurrentTask (KERNEL.33)
762 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
764 if (bLock) hLockedTask = GetCurrentTask();
765 else hLockedTask = 0;
766 return hLockedTask;
770 /***********************************************************************
771 * IsTaskLocked (KERNEL.122)
773 HTASK16 WINAPI IsTaskLocked16(void)
775 return hLockedTask;
779 /***********************************************************************
780 * OldYield (KERNEL.117)
782 void WINAPI OldYield16(void)
784 DWORD count;
786 ReleaseThunkLock(&count);
787 RestoreThunkLock(count);
790 /***********************************************************************
791 * WIN32_OldYield (KERNEL.447)
793 void WINAPI WIN32_OldYield16(void)
795 DWORD count;
797 ReleaseThunkLock(&count);
798 RestoreThunkLock(count);
801 /***********************************************************************
802 * DirectedYield (KERNEL.150)
804 void WINAPI DirectedYield16( HTASK16 hTask )
806 OldYield16();
809 /***********************************************************************
810 * Yield (KERNEL.29)
812 void WINAPI Yield16(void)
814 TDB *pCurTask = TASK_GetCurrent();
816 if (pCurTask && pCurTask->hQueue)
818 HMODULE mod = GetModuleHandleA( "user32.dll" );
819 if (mod)
821 FARPROC proc = GetProcAddress( mod, "UserYield16" );
822 if (proc)
824 proc();
825 return;
829 OldYield16();
832 /***********************************************************************
833 * KERNEL_490 (KERNEL.490)
835 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
837 if ( !someTask ) return 0;
839 FIXME("(%04x): stub\n", someTask );
840 return 0;
843 /***********************************************************************
844 * MakeProcInstance (KERNEL.51)
846 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
848 BYTE *thunk,*lfunc;
849 SEGPTR thunkaddr;
850 WORD hInstanceSelector;
852 hInstanceSelector = GlobalHandleToSel16(hInstance);
854 TRACE("(%p, %04x);\n", func, hInstance);
856 if (!HIWORD(func)) {
857 /* Win95 actually protects via SEH, but this is better for debugging */
858 WARN("Ouch ! Called with invalid func %p !\n", func);
859 return (FARPROC16)0;
862 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
863 && (hInstance != 0)
864 && (hInstance != 0xffff) )
866 /* calling MPI with a foreign DSEG is invalid ! */
867 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
868 hInstance,CURRENT_DS);
871 /* Always use the DSEG that MPI was entered with.
872 * We used to set hInstance to GetTaskDS16(), but this should be wrong
873 * as CURRENT_DS provides the DSEG value we need.
874 * ("calling" DS, *not* "task" DS !) */
875 hInstanceSelector = CURRENT_DS;
876 hInstance = GlobalHandle16(hInstanceSelector);
878 /* no thunking for DLLs */
879 if (NE_GetPtr(FarGetOwner16(hInstance))->ne_flags & NE_FFLAGS_LIBMODULE)
880 return func;
882 thunkaddr = TASK_AllocThunk();
883 if (!thunkaddr) return (FARPROC16)0;
884 thunk = MapSL( thunkaddr );
885 lfunc = MapSL( (SEGPTR)func );
887 TRACE("(%p,%04x): got thunk %08x\n", func, hInstance, thunkaddr );
888 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
889 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
891 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
894 *thunk++ = 0xb8; /* movw instance, %ax */
895 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
896 *thunk++ = (BYTE)(hInstanceSelector >> 8);
897 *thunk++ = 0xea; /* ljmp func */
898 *(DWORD *)thunk = (DWORD)func;
899 return (FARPROC16)thunkaddr;
900 /* CX reg indicates if thunkaddr != NULL, implement if needed */
904 /***********************************************************************
905 * FreeProcInstance (KERNEL.52)
907 void WINAPI FreeProcInstance16( FARPROC16 func )
909 TRACE("(%p)\n", func );
910 TASK_FreeThunk( (SEGPTR)func );
913 /**********************************************************************
914 * TASK_GetCodeSegment
916 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
917 * and logical segment number of a given code segment.
919 * 'proc' either *is* already a pair of module handle and segment number,
920 * in which case there's nothing to do. Otherwise, it is a pointer to
921 * a function, and we need to retrieve the code segment. If the pointer
922 * happens to point to a thunk, we'll retrieve info about the code segment
923 * where the function pointed to by the thunk resides, not the thunk itself.
925 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
926 * the function the snoop code will return to ...
929 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
930 SEGTABLEENTRY **ppSeg, int *pSegNr )
932 NE_MODULE *pModule = NULL;
933 SEGTABLEENTRY *pSeg = NULL;
934 int segNr=0;
936 /* Try pair of module handle / segment number */
937 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
938 if ( pModule && pModule->ne_magic == IMAGE_OS2_SIGNATURE )
940 segNr = LOWORD( proc );
941 if ( segNr && segNr <= pModule->ne_cseg )
942 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
945 /* Try thunk or function */
946 else
948 BYTE *thunk = MapSL( (SEGPTR)proc );
949 WORD selector;
951 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
952 selector = thunk[6] + (thunk[7] << 8);
953 else
954 selector = HIWORD( proc );
956 pModule = NE_GetPtr( GlobalHandle16( selector ) );
957 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
959 if ( pModule )
960 for ( segNr = 1; segNr <= pModule->ne_cseg; segNr++, pSeg++ )
961 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
962 break;
964 if ( pModule && segNr > pModule->ne_cseg )
965 pSeg = NULL;
968 /* Abort if segment not found */
970 if ( !pModule || !pSeg )
971 return FALSE;
973 /* Return segment data */
975 if ( ppModule ) *ppModule = pModule;
976 if ( ppSeg ) *ppSeg = pSeg;
977 if ( pSegNr ) *pSegNr = segNr;
979 return TRUE;
982 /**********************************************************************
983 * GetCodeHandle (KERNEL.93)
985 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
987 SEGTABLEENTRY *pSeg;
989 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
990 return (HANDLE16)0;
992 return pSeg->hSeg;
995 /**********************************************************************
996 * GetCodeInfo (KERNEL.104)
998 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1000 NE_MODULE *pModule;
1001 SEGTABLEENTRY *pSeg;
1002 int segNr;
1004 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1005 return FALSE;
1007 /* Fill in segment information */
1009 segInfo->offSegment = pSeg->filepos;
1010 segInfo->cbSegment = pSeg->size;
1011 segInfo->flags = pSeg->flags;
1012 segInfo->cbAlloc = pSeg->minsize;
1013 segInfo->h = pSeg->hSeg;
1014 segInfo->alignShift = pModule->ne_align;
1016 if ( segNr == pModule->ne_autodata )
1017 segInfo->cbAlloc += pModule->ne_heap + pModule->ne_stack;
1019 /* Return module handle in %es */
1021 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1023 return TRUE;
1027 /**********************************************************************
1028 * DefineHandleTable (KERNEL.94)
1030 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1032 FIXME("(%04x): stub ?\n", wOffset);
1033 return TRUE;
1037 /***********************************************************************
1038 * SetTaskQueue (KERNEL.34)
1040 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1042 FIXME( "stub, should not get called\n" );
1043 return 0xbeef;
1047 /***********************************************************************
1048 * GetTaskQueue (KERNEL.35)
1050 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1052 FIXME( "stub, should not get called\n" );
1053 return 0xbeef;
1056 /***********************************************************************
1057 * SetThreadQueue (KERNEL.463)
1059 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1061 FIXME( "stub, should not get called\n" );
1062 return 0xbeef;
1065 /***********************************************************************
1066 * GetThreadQueue (KERNEL.464)
1068 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1070 FIXME( "stub, should not get called\n" );
1071 return 0xbeef;
1074 /***********************************************************************
1075 * SetFastQueue (KERNEL.624)
1077 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1079 FIXME( "stub, should not get called\n" );
1082 /***********************************************************************
1083 * GetFastQueue (KERNEL.625)
1085 HQUEUE16 WINAPI GetFastQueue16( void )
1087 FIXME( "stub, should not get called\n" );
1088 return 0xbeef;
1091 /***********************************************************************
1092 * SwitchStackTo (KERNEL.108)
1094 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1096 STACK16FRAME *oldFrame, *newFrame;
1097 INSTANCEDATA *pData;
1098 UINT16 copySize;
1100 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1101 TRACE("old=%04x:%04x new=%04x:%04x\n",
1102 SELECTOROF( NtCurrentTeb()->WOW32Reserved ),
1103 OFFSETOF( NtCurrentTeb()->WOW32Reserved ), seg, ptr );
1105 /* Save the old stack */
1107 oldFrame = CURRENT_STACK16;
1108 /* pop frame + args and push bp */
1109 pData->old_ss_sp = (SEGPTR)NtCurrentTeb()->WOW32Reserved + sizeof(STACK16FRAME)
1110 + 2 * sizeof(WORD);
1111 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1112 pData->stacktop = top;
1113 pData->stackmin = ptr;
1114 pData->stackbottom = ptr;
1116 /* Switch to the new stack */
1118 /* Note: we need to take the 3 arguments into account; otherwise,
1119 * the stack will underflow upon return from this function.
1121 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1122 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1123 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( seg, ptr - copySize );
1124 newFrame = CURRENT_STACK16;
1126 /* Copy the stack frame and the local variables to the new stack */
1128 memmove( newFrame, oldFrame, copySize );
1129 newFrame->bp = ptr;
1130 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1134 /***********************************************************************
1135 * SwitchStackBack (KERNEL.109)
1137 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1139 STACK16FRAME *oldFrame, *newFrame;
1140 INSTANCEDATA *pData;
1142 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->WOW32Reserved))))
1143 return;
1144 if (!pData->old_ss_sp)
1146 WARN("No previous SwitchStackTo\n" );
1147 return;
1149 TRACE("restoring stack %04x:%04x\n",
1150 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1152 oldFrame = CURRENT_STACK16;
1154 /* Pop bp from the previous stack */
1156 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
1157 pData->old_ss_sp += sizeof(WORD);
1159 /* Switch back to the old stack */
1161 NtCurrentTeb()->WOW32Reserved = (void *)(pData->old_ss_sp - sizeof(STACK16FRAME));
1162 context->SegSs = SELECTOROF(pData->old_ss_sp);
1163 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1164 pData->old_ss_sp = 0;
1166 /* Build a stack frame for the return */
1168 newFrame = CURRENT_STACK16;
1169 newFrame->frame32 = oldFrame->frame32;
1170 newFrame->module_cs = oldFrame->module_cs;
1171 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1172 newFrame->entry_ip = oldFrame->entry_ip;
1176 /***********************************************************************
1177 * GetTaskQueueDS (KERNEL.118)
1179 void WINAPI GetTaskQueueDS16(void)
1181 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1185 /***********************************************************************
1186 * GetTaskQueueES (KERNEL.119)
1188 void WINAPI GetTaskQueueES16(void)
1190 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1194 /***********************************************************************
1195 * GetCurrentTask (KERNEL32.@)
1197 HTASK16 WINAPI GetCurrentTask(void)
1199 HTASK16 ret = kernel_get_thread_data()->htask16;
1200 if (!ret) ret = main_task;
1201 return ret;
1204 /***********************************************************************
1205 * GetCurrentTask (KERNEL.36)
1207 DWORD WINAPI WIN16_GetCurrentTask(void)
1209 /* This is the version used by relay code; the first task is */
1210 /* returned in the high word of the result */
1211 return MAKELONG( GetCurrentTask(), hFirstTask );
1215 /***********************************************************************
1216 * GetCurrentPDB (KERNEL.37)
1218 * UNDOC: returns PSP of KERNEL in high word
1220 DWORD WINAPI GetCurrentPDB16(void)
1222 TDB *pTask;
1224 if (!(pTask = TASK_GetCurrent())) return 0;
1225 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1229 /***********************************************************************
1230 * GetCurPID (KERNEL.157)
1232 DWORD WINAPI GetCurPID16( DWORD unused )
1234 return 0;
1238 /***********************************************************************
1239 * GetInstanceData (KERNEL.54)
1241 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1243 char *ptr = (char *)GlobalLock16( instance );
1244 if (!ptr || !len) return 0;
1245 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1246 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1247 return len;
1251 /***********************************************************************
1252 * GetExeVersion (KERNEL.105)
1254 WORD WINAPI GetExeVersion16(void)
1256 TDB *pTask;
1258 if (!(pTask = TASK_GetCurrent())) return 0;
1259 return pTask->version;
1263 /***********************************************************************
1264 * SetErrorMode (KERNEL.107)
1266 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1268 TDB *pTask;
1269 if (!(pTask = TASK_GetCurrent())) return 0;
1270 pTask->error_mode = mode;
1271 return SetErrorMode( mode );
1275 /***********************************************************************
1276 * GetNumTasks (KERNEL.152)
1278 UINT16 WINAPI GetNumTasks16(void)
1280 return nTaskCount;
1284 /***********************************************************************
1285 * GetTaskDS (KERNEL.155)
1287 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1288 * I don't think we need to bother with this.
1290 HINSTANCE16 WINAPI GetTaskDS16(void)
1292 TDB *pTask;
1294 if (!(pTask = TASK_GetCurrent())) return 0;
1295 return GlobalHandleToSel16(pTask->hInstance);
1298 /***********************************************************************
1299 * GetDummyModuleHandleDS (KERNEL.602)
1301 WORD WINAPI GetDummyModuleHandleDS16(void)
1303 TDB *pTask;
1304 WORD selector;
1306 if (!(pTask = TASK_GetCurrent())) return 0;
1307 if (!(pTask->flags & TDBF_WIN32)) return 0;
1308 selector = GlobalHandleToSel16( pTask->hModule );
1309 CURRENT_DS = selector;
1310 return selector;
1313 /***********************************************************************
1314 * IsTask (KERNEL.320)
1316 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1318 TDB *pTask;
1320 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1321 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1322 return (pTask->magic == TDB_MAGIC);
1326 /***********************************************************************
1327 * IsWinOldApTask (KERNEL.158)
1329 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1331 /* should return bit 0 of byte 0x48 in PSP */
1332 return FALSE;
1335 /***********************************************************************
1336 * SetTaskSignalProc (KERNEL.38)
1338 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1340 TDB *pTask;
1341 FARPROC16 oldProc;
1343 if (!hTask) hTask = GetCurrentTask();
1344 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1345 oldProc = pTask->userhandler;
1346 pTask->userhandler = proc;
1347 return oldProc;
1350 /***********************************************************************
1351 * SetSigHandler (KERNEL.140)
1353 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1354 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1356 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1357 newhandler,oldhandler,oldmode,newmode,flag );
1359 if (flag != 1) return 0;
1360 if (!newmode) newhandler = NULL; /* Default handler */
1361 if (newmode != 4)
1363 TDB *pTask;
1365 if (!(pTask = TASK_GetCurrent())) return 0;
1366 if (oldmode) *oldmode = pTask->signal_flags;
1367 pTask->signal_flags = newmode;
1368 if (oldhandler) *oldhandler = pTask->sighandler;
1369 pTask->sighandler = newhandler;
1371 return 0;
1375 /***********************************************************************
1376 * GlobalNotify (KERNEL.154)
1378 * Note that GlobalNotify does _not_ return the old NotifyProc
1379 * -- contrary to LocalNotify !!
1381 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1383 TDB *pTask;
1385 if (!(pTask = TASK_GetCurrent())) return;
1386 pTask->discardhandler = proc;
1390 /***********************************************************************
1391 * GetExePtrHelper
1393 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1395 char *ptr;
1396 HANDLE16 owner;
1398 /* Check for module handle */
1400 if (!(ptr = GlobalLock16( handle ))) return 0;
1401 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return handle;
1403 /* Search for this handle inside all tasks */
1405 *hTask = hFirstTask;
1406 while (*hTask)
1408 TDB *pTask = TASK_GetPtr( *hTask );
1409 if ((*hTask == handle) ||
1410 (pTask->hInstance == handle) ||
1411 (pTask->hQueue == handle) ||
1412 (pTask->hPDB == handle)) return pTask->hModule;
1413 *hTask = pTask->hNext;
1416 /* Check the owner for module handle */
1418 owner = FarGetOwner16( handle );
1419 if (!(ptr = GlobalLock16( owner ))) return 0;
1420 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return owner;
1422 /* Search for the owner inside all tasks */
1424 *hTask = hFirstTask;
1425 while (*hTask)
1427 TDB *pTask = TASK_GetPtr( *hTask );
1428 if ((*hTask == owner) ||
1429 (pTask->hInstance == owner) ||
1430 (pTask->hQueue == owner) ||
1431 (pTask->hPDB == owner)) return pTask->hModule;
1432 *hTask = pTask->hNext;
1435 return 0;
1438 /***********************************************************************
1439 * GetExePtr (KERNEL.133)
1441 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1443 HTASK16 hTask = 0;
1444 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1445 STACK16FRAME *frame = CURRENT_STACK16;
1446 frame->ecx = hModule;
1447 if (hTask) frame->es = hTask;
1448 return hModule;
1452 /***********************************************************************
1453 * K228 (KERNEL.228)
1455 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1457 HTASK16 hTask = 0;
1458 return GetExePtrHelper( handle, &hTask );
1462 /***********************************************************************
1463 * TaskFirst (TOOLHELP.63)
1465 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1467 lpte->hNext = hFirstTask;
1468 return TaskNext16( lpte );
1472 /***********************************************************************
1473 * TaskNext (TOOLHELP.64)
1475 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1477 TDB *pTask;
1478 INSTANCEDATA *pInstData;
1480 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1481 if (!lpte->hNext) return FALSE;
1483 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1484 while (1) {
1485 pTask = TASK_GetPtr( lpte->hNext );
1486 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1487 if (pTask->hInstance)
1488 break;
1489 lpte->hNext = pTask->hNext;
1491 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1492 lpte->hTask = lpte->hNext;
1493 lpte->hTaskParent = pTask->hParent;
1494 lpte->hInst = pTask->hInstance;
1495 lpte->hModule = pTask->hModule;
1496 lpte->wSS = SELECTOROF( pTask->teb->WOW32Reserved );
1497 lpte->wSP = OFFSETOF( pTask->teb->WOW32Reserved );
1498 lpte->wStackTop = pInstData->stacktop;
1499 lpte->wStackMinimum = pInstData->stackmin;
1500 lpte->wStackBottom = pInstData->stackbottom;
1501 lpte->wcEvents = pTask->nEvents;
1502 lpte->hQueue = pTask->hQueue;
1503 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1504 lpte->wPSPOffset = 0x100; /*??*/
1505 lpte->hNext = pTask->hNext;
1506 return TRUE;
1510 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1512 /**************************************************************************
1513 * FatalAppExit (KERNEL.137)
1515 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1517 TDB *pTask = TASK_GetCurrent();
1519 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1521 HMODULE mod = GetModuleHandleA( "user32.dll" );
1522 if (mod)
1524 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1525 if (pMessageBoxA)
1527 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1528 goto done;
1531 ERR( "%s\n", debugstr_a(str) );
1533 done:
1534 ExitThread(0xff);
1538 /***********************************************************************
1539 * TerminateApp (TOOLHELP.77)
1541 * See "Undocumented Windows".
1543 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1545 if (hTask && hTask != GetCurrentTask())
1547 FIXME("cannot terminate task %x\n", hTask);
1548 return;
1551 if (wFlags & NO_UAE_BOX)
1553 UINT16 old_mode;
1554 old_mode = SetErrorMode16(0);
1555 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1557 FatalAppExit16( 0, NULL );
1559 /* hmm, we're still alive ?? */
1561 /* check undocumented flag */
1562 if (!(wFlags & 0x8000))
1563 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1565 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1566 but let's just call ExitThread */
1567 ExitThread(0xff);
1571 /***********************************************************************
1572 * GetAppCompatFlags (KERNEL.354)
1574 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1576 TDB *pTask;
1578 if (!hTask) hTask = GetCurrentTask();
1579 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1580 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1581 return pTask->compat_flags;
1585 /***********************************************************************
1586 * GetDOSEnvironment (KERNEL.131)
1588 * Note: the environment is allocated once, it doesn't track changes
1589 * made using the Win32 API. This shouldn't matter.
1591 * Format of a 16-bit environment block:
1592 * ASCIIZ string 1 (xx=yy format)
1593 * ...
1594 * ASCIIZ string n
1595 * BYTE 0
1596 * WORD 1
1597 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1599 SEGPTR WINAPI GetDOSEnvironment16(void)
1601 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1602 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1604 if (!handle)
1606 DWORD size;
1607 LPSTR p, env;
1609 p = env = GetEnvironmentStringsA();
1610 while (*p) p += strlen(p) + 1;
1611 p++; /* skip last null */
1612 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1613 handle = GlobalAlloc16( GMEM_FIXED, size );
1614 if (handle)
1616 WORD one = 1;
1617 LPSTR env16 = GlobalLock16( handle );
1618 memcpy( env16, env, p - env );
1619 memcpy( env16 + (p - env), &one, sizeof(one));
1620 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1621 GlobalUnlock16( handle );
1623 FreeEnvironmentStringsA( env );
1625 return WOWGlobalLock16( handle );