Tests are regular Winelib applications and thus should not be compiled
[wine.git] / loader / task.c
blobce673e2eec7452c185beae1139fb8bda8bf7ec1f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winnt.h"
34 #include "winuser.h"
36 #include "wine/winbase16.h"
37 #include "drive.h"
38 #include "file.h"
39 #include "global.h"
40 #include "instance.h"
41 #include "miscemu.h"
42 #include "module.h"
43 #include "winternl.h"
44 #include "selectors.h"
45 #include "wine/server.h"
46 #include "syslevel.h"
47 #include "stackframe.h"
48 #include "task.h"
49 #include "thread.h"
50 #include "toolhelp.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(task);
55 WINE_DECLARE_DEBUG_CHANNEL(relay);
56 WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
58 /* Min. number of thunks allocated when creating a new segment */
59 #define MIN_THUNKS 32
62 static THHOOK DefaultThhook;
63 THHOOK *pThhook = &DefaultThhook;
65 #define hCurrentTask (pThhook->CurTDB)
66 #define hFirstTask (pThhook->HeadTDB)
67 #define hLockedTask (pThhook->LockTDB)
69 static UINT16 nTaskCount = 0;
71 static HTASK16 initial_task;
73 /***********************************************************************
74 * TASK_InstallTHHook
76 void TASK_InstallTHHook( THHOOK *pNewThhook )
78 THHOOK *pOldThhook = pThhook;
80 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
82 *pThhook = *pOldThhook;
85 /***********************************************************************
86 * TASK_GetNextTask
88 HTASK16 TASK_GetNextTask( HTASK16 hTask )
90 TDB* pTask = TASK_GetPtr( hTask );
92 if (pTask->hNext) return pTask->hNext;
93 return (hFirstTask != hTask) ? hFirstTask : 0;
97 /***********************************************************************
98 * TASK_GetPtr
100 TDB *TASK_GetPtr( HTASK16 hTask )
102 return GlobalLock16( hTask );
106 /***********************************************************************
107 * TASK_GetCurrent
109 TDB *TASK_GetCurrent(void)
111 return TASK_GetPtr( GetCurrentTask() );
115 /***********************************************************************
116 * TASK_LinkTask
118 static void TASK_LinkTask( HTASK16 hTask )
120 HTASK16 *prevTask;
121 TDB *pTask;
123 if (!(pTask = TASK_GetPtr( hTask ))) return;
124 prevTask = &hFirstTask;
125 while (*prevTask)
127 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
128 if (prevTaskPtr->priority >= pTask->priority) break;
129 prevTask = &prevTaskPtr->hNext;
131 pTask->hNext = *prevTask;
132 *prevTask = hTask;
133 nTaskCount++;
137 /***********************************************************************
138 * TASK_UnlinkTask
140 static void TASK_UnlinkTask( HTASK16 hTask )
142 HTASK16 *prevTask;
143 TDB *pTask;
145 prevTask = &hFirstTask;
146 while (*prevTask && (*prevTask != hTask))
148 pTask = TASK_GetPtr( *prevTask );
149 prevTask = &pTask->hNext;
151 if (*prevTask)
153 pTask = TASK_GetPtr( *prevTask );
154 *prevTask = pTask->hNext;
155 pTask->hNext = 0;
156 nTaskCount--;
161 /***********************************************************************
162 * TASK_CreateThunks
164 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
165 * and containing 'count' entries.
167 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
169 int i;
170 WORD free;
171 THUNKS *pThunk;
173 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
174 pThunk->next = 0;
175 pThunk->magic = THUNK_MAGIC;
176 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
177 free = pThunk->free;
178 for (i = 0; i < count-1; i++)
180 free += 8; /* Offset of next thunk */
181 pThunk->thunks[4*i] = free;
183 pThunk->thunks[4*i] = 0; /* Last thunk */
187 /***********************************************************************
188 * TASK_AllocThunk
190 * Allocate a thunk for MakeProcInstance().
192 static SEGPTR TASK_AllocThunk(void)
194 TDB *pTask;
195 THUNKS *pThunk;
196 WORD sel, base;
198 if (!(pTask = TASK_GetCurrent())) return 0;
199 sel = pTask->hCSAlias;
200 pThunk = &pTask->thunks;
201 base = (int)pThunk - (int)pTask;
202 while (!pThunk->free)
204 sel = pThunk->next;
205 if (!sel) /* Allocate a new segment */
207 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
208 pTask->hPDB, WINE_LDT_FLAGS_CODE );
209 if (!sel) return (SEGPTR)0;
210 TASK_CreateThunks( sel, 0, MIN_THUNKS );
211 pThunk->next = sel;
213 pThunk = (THUNKS *)GlobalLock16( sel );
214 base = 0;
216 base += pThunk->free;
217 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
218 return MAKESEGPTR( sel, base );
222 /***********************************************************************
223 * TASK_FreeThunk
225 * Free a MakeProcInstance() thunk.
227 static BOOL TASK_FreeThunk( SEGPTR thunk )
229 TDB *pTask;
230 THUNKS *pThunk;
231 WORD sel, base;
233 if (!(pTask = TASK_GetCurrent())) return 0;
234 sel = pTask->hCSAlias;
235 pThunk = &pTask->thunks;
236 base = (int)pThunk - (int)pTask;
237 while (sel && (sel != HIWORD(thunk)))
239 sel = pThunk->next;
240 pThunk = (THUNKS *)GlobalLock16( sel );
241 base = 0;
243 if (!sel) return FALSE;
244 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
245 pThunk->free = LOWORD(thunk) - base;
246 return TRUE;
250 /***********************************************************************
251 * TASK_Create
253 * NOTE: This routine might be called by a Win32 thread. Thus, we need
254 * to be careful to protect global data structures. We do this
255 * by entering the Win16Lock while linking the task into the
256 * global task list.
258 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, TEB *teb, LPCSTR cmdline, BYTE len )
260 HTASK16 hTask;
261 TDB *pTask;
262 char name[10];
263 FARPROC16 proc;
265 /* Allocate the task structure */
267 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
268 if (!hTask) return NULL;
269 pTask = TASK_GetPtr( hTask );
270 FarSetOwner16( hTask, pModule->self );
272 /* Fill the task structure */
274 pTask->hSelf = hTask;
276 if (teb && teb->tibflags & TEBF_WIN32)
278 pTask->flags |= TDBF_WIN32;
279 pTask->hInstance = pModule->self;
280 pTask->hPrevInstance = 0;
281 /* NOTE: for 16-bit tasks, the instance handles are updated later on
282 in NE_InitProcess */
285 pTask->version = pModule->expected_version;
286 pTask->hModule = pModule->self;
287 pTask->hParent = GetCurrentTask();
288 pTask->magic = TDB_MAGIC;
289 pTask->nCmdShow = cmdShow;
290 pTask->teb = teb;
291 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
292 strcpy( pTask->curdir, "\\" );
293 WideCharToMultiByte(CP_ACP, 0, DRIVE_GetDosCwd(DRIVE_GetCurrentDrive()), -1,
294 pTask->curdir + 1, sizeof(pTask->curdir) - 1, NULL, NULL);
295 pTask->curdir[sizeof(pTask->curdir) - 1] = 0; /* ensure 0 termination */
297 /* Create the thunks block */
299 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
301 /* Copy the module name */
303 GetModuleName16( pModule->self, name, sizeof(name) );
304 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
306 /* Allocate a selector for the PDB */
308 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
309 pModule->self, WINE_LDT_FLAGS_DATA );
311 /* Fill the PDB */
313 pTask->pdb.int20 = 0x20cd;
314 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
315 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
316 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
317 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
318 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
319 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
320 pTask->pdb.fileHandlesPtr =
321 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
322 pTask->pdb.hFileHandles = 0;
323 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
324 /* FIXME: should we make a copy of the environment? */
325 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
326 pTask->pdb.nbFiles = 20;
328 /* Fill the command line */
330 if (!cmdline)
332 cmdline = GetCommandLineA();
333 /* remove the first word (program name) */
334 if (*cmdline == '"')
335 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
336 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
337 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
338 len = strlen(cmdline);
340 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
341 pTask->pdb.cmdLine[0] = len;
342 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
343 /* pTask->pdb.cmdLine[len+1] = 0; */
345 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name, len, cmdline, hTask );
347 /* Get the compatibility flags */
349 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
351 /* Allocate a code segment alias for the TDB */
353 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
354 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
356 /* Default DTA overwrites command line */
358 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
360 /* Create scheduler event for 16-bit tasks */
362 if ( !(pTask->flags & TDBF_WIN32) )
363 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
365 /* Enter task handle into thread */
367 if (teb) teb->htask16 = hTask;
368 if (!initial_task) initial_task = hTask;
370 return pTask;
374 /***********************************************************************
375 * TASK_DeleteTask
377 static void TASK_DeleteTask( HTASK16 hTask )
379 TDB *pTask;
380 HGLOBAL16 hPDB;
382 if (!(pTask = TASK_GetPtr( hTask ))) return;
383 hPDB = pTask->hPDB;
385 pTask->magic = 0xdead; /* invalidate signature */
387 /* Free the selector aliases */
389 GLOBAL_FreeBlock( pTask->hCSAlias );
390 GLOBAL_FreeBlock( pTask->hPDB );
392 /* Free the task module */
394 FreeModule16( pTask->hModule );
396 /* Free the task structure itself */
398 GlobalFree16( hTask );
400 /* Free all memory used by this task (including the 32-bit stack, */
401 /* the environment block and the thunk segments). */
403 GlobalFreeAll16( hPDB );
407 /***********************************************************************
408 * TASK_CreateMainTask
410 * Create a task for the main (32-bit) process.
412 void TASK_CreateMainTask(void)
414 TDB *pTask;
415 STARTUPINFOA startup_info;
416 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
418 GetStartupInfoA( &startup_info );
419 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
420 pTask = TASK_Create( (NE_MODULE *)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
421 cmdShow, NtCurrentTeb(), NULL, 0 );
422 if (!pTask)
424 ERR("could not create task for main process\n");
425 ExitProcess(1);
428 /* Add the task to the linked list */
429 /* (no need to get the win16 lock, we are the only thread at this point) */
430 TASK_LinkTask( pTask->hSelf );
434 /* startup routine for a new 16-bit thread */
435 static DWORD CALLBACK task_start( TDB *pTask )
437 DWORD ret;
439 NtCurrentTeb()->tibflags &= ~TEBF_WIN32;
440 NtCurrentTeb()->htask16 = pTask->hSelf;
442 _EnterWin16Lock();
443 TASK_LinkTask( pTask->hSelf );
444 pTask->teb = NtCurrentTeb();
445 ret = NE_StartTask();
446 _LeaveWin16Lock();
447 return ret;
451 /***********************************************************************
452 * TASK_SpawnTask
454 * Spawn a new 16-bit task.
456 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
457 LPCSTR cmdline, BYTE len, HANDLE *hThread )
459 TDB *pTask;
461 if (!(pTask = TASK_Create( pModule, cmdShow, NULL, cmdline, len ))) return 0;
462 if (!(*hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)task_start, pTask, 0, NULL )))
464 TASK_DeleteTask( pTask->hSelf );
465 return 0;
467 return pTask->hSelf;
471 /***********************************************************************
472 * TASK_ExitTask
474 void TASK_ExitTask(void)
476 TDB *pTask;
477 DWORD lockCount;
479 /* Enter the Win16Lock to protect global data structures */
480 _EnterWin16Lock();
482 pTask = TASK_GetCurrent();
483 if ( !pTask )
485 _LeaveWin16Lock();
486 return;
489 TRACE("Killing task %04x\n", pTask->hSelf );
491 /* Perform USER cleanup */
493 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
494 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
495 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
496 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
498 /* Remove the task from the list to be sure we never switch back to it */
499 TASK_UnlinkTask( pTask->hSelf );
501 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
503 TRACE("this is the last task, exiting\n" );
504 ExitKernel16();
507 if( nTaskCount )
509 TDB* p = TASK_GetPtr( hFirstTask );
510 while( p )
512 if( p->hYieldTo == pTask->hSelf ) p->hYieldTo = 0;
513 p = TASK_GetPtr( p->hNext );
517 pTask->nEvents = 0;
519 if ( hLockedTask == pTask->hSelf )
520 hLockedTask = 0;
522 TASK_DeleteTask( pTask->hSelf );
524 /* ... and completely release the Win16Lock, just in case. */
525 ReleaseThunkLock( &lockCount );
529 /***********************************************************************
530 * InitTask (KERNEL.91)
532 * Called by the application startup code.
534 void WINAPI InitTask16( CONTEXT86 *context )
536 TDB *pTask;
537 INSTANCEDATA *pinstance;
538 SEGPTR ptr;
540 context->Eax = 0;
541 if (!(pTask = TASK_GetCurrent())) return;
543 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
544 as some apps, notably Visual Basic apps, *modify* the heap/stack
545 size of the instance data segment before calling InitTask() */
547 /* Initialize the INSTANCEDATA structure */
548 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
549 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
550 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
551 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
552 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
554 /* Initialize the local heap */
555 if (LOWORD(context->Ecx))
556 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
558 /* Initialize implicitly loaded DLLs */
559 NE_InitializeDLLs( pTask->hModule );
560 NE_DllProcessAttach( pTask->hModule );
562 /* Registers on return are:
563 * ax 1 if OK, 0 on error
564 * cx stack limit in bytes
565 * dx cmdShow parameter
566 * si instance handle of the previous instance
567 * di instance handle of the new task
568 * es:bx pointer to command line inside PSP
570 * 0 (=%bp) is pushed on the stack
572 ptr = stack16_push( sizeof(WORD) );
573 *(WORD *)MapSL(ptr) = 0;
574 context->Esp -= 2;
576 context->Eax = 1;
578 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
579 else
581 LPBYTE p = &pTask->pdb.cmdLine[1];
582 while ((*p == ' ') || (*p == '\t')) p++;
583 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
585 context->Ecx = pinstance->stacktop;
586 context->Edx = pTask->nCmdShow;
587 context->Esi = (DWORD)pTask->hPrevInstance;
588 context->Edi = (DWORD)pTask->hInstance;
589 context->SegEs = (WORD)pTask->hPDB;
593 /***********************************************************************
594 * WaitEvent (KERNEL.30)
596 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
598 TDB *pTask;
600 if (!hTask) hTask = GetCurrentTask();
601 pTask = TASK_GetPtr( hTask );
603 if (pTask->flags & TDBF_WIN32)
605 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
606 return TRUE;
609 if (pTask->nEvents > 0)
611 pTask->nEvents--;
612 return FALSE;
615 if (pTask->teb == NtCurrentTeb())
617 DWORD lockCount;
619 NtResetEvent( pTask->hEvent, NULL );
620 ReleaseThunkLock( &lockCount );
621 SYSLEVEL_CheckNotLevel( 1 );
622 WaitForSingleObject( pTask->hEvent, INFINITE );
623 RestoreThunkLock( lockCount );
624 if (pTask->nEvents > 0) pTask->nEvents--;
626 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
628 return TRUE;
632 /***********************************************************************
633 * PostEvent (KERNEL.31)
635 void WINAPI PostEvent16( HTASK16 hTask )
637 TDB *pTask;
639 if (!hTask) hTask = GetCurrentTask();
640 if (!(pTask = TASK_GetPtr( hTask ))) return;
642 if (pTask->flags & TDBF_WIN32)
644 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
645 return;
648 pTask->nEvents++;
650 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
654 /***********************************************************************
655 * SetPriority (KERNEL.32)
657 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
659 TDB *pTask;
660 INT16 newpriority;
662 if (!hTask) hTask = GetCurrentTask();
663 if (!(pTask = TASK_GetPtr( hTask ))) return;
664 newpriority = pTask->priority + delta;
665 if (newpriority < -32) newpriority = -32;
666 else if (newpriority > 15) newpriority = 15;
668 pTask->priority = newpriority + 1;
669 TASK_UnlinkTask( pTask->hSelf );
670 TASK_LinkTask( pTask->hSelf );
671 pTask->priority--;
675 /***********************************************************************
676 * LockCurrentTask (KERNEL.33)
678 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
680 if (bLock) hLockedTask = GetCurrentTask();
681 else hLockedTask = 0;
682 return hLockedTask;
686 /***********************************************************************
687 * IsTaskLocked (KERNEL.122)
689 HTASK16 WINAPI IsTaskLocked16(void)
691 return hLockedTask;
695 /***********************************************************************
696 * OldYield (KERNEL.117)
698 void WINAPI OldYield16(void)
700 DWORD count;
702 ReleaseThunkLock(&count);
703 RestoreThunkLock(count);
706 /***********************************************************************
707 * WIN32_OldYield (KERNEL.447)
709 void WINAPI WIN32_OldYield16(void)
711 DWORD count;
713 ReleaseThunkLock(&count);
714 RestoreThunkLock(count);
717 /***********************************************************************
718 * DirectedYield (KERNEL.150)
720 void WINAPI DirectedYield16( HTASK16 hTask )
722 TDB *pCurTask = TASK_GetCurrent();
724 if (pCurTask->flags & TDBF_WIN32)
726 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
727 return;
730 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
732 pCurTask->hYieldTo = hTask;
733 OldYield16();
735 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
738 /***********************************************************************
739 * Yield (KERNEL.29)
741 void WINAPI Yield16(void)
743 TDB *pCurTask = TASK_GetCurrent();
745 if (pCurTask) pCurTask->hYieldTo = 0;
746 if (pCurTask && pCurTask->hQueue)
748 HMODULE mod = GetModuleHandleA( "user32.dll" );
749 if (mod)
751 FARPROC proc = GetProcAddress( mod, "UserYield16" );
752 if (proc)
754 proc();
755 return;
759 OldYield16();
762 /***********************************************************************
763 * KERNEL_490 (KERNEL.490)
765 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
767 if ( !someTask ) return 0;
769 FIXME("(%04x): stub\n", someTask );
770 return 0;
773 /***********************************************************************
774 * MakeProcInstance (KERNEL.51)
776 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
778 BYTE *thunk,*lfunc;
779 SEGPTR thunkaddr;
780 WORD hInstanceSelector;
782 hInstanceSelector = GlobalHandleToSel16(hInstance);
784 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
786 if (!HIWORD(func)) {
787 /* Win95 actually protects via SEH, but this is better for debugging */
788 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
789 return (FARPROC16)0;
792 if (hInstance)
794 if ( (!(hInstance & 4)) ||
795 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
797 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
798 hInstance);
799 return 0;
803 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
804 && (hInstance != 0)
805 && (hInstance != 0xffff) )
807 /* calling MPI with a foreign DSEG is invalid ! */
808 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
809 hInstance,CURRENT_DS);
812 /* Always use the DSEG that MPI was entered with.
813 * We used to set hInstance to GetTaskDS16(), but this should be wrong
814 * as CURRENT_DS provides the DSEG value we need.
815 * ("calling" DS, *not* "task" DS !) */
816 hInstanceSelector = CURRENT_DS;
817 hInstance = GlobalHandle16(hInstanceSelector);
819 /* no thunking for DLLs */
820 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
821 return func;
823 thunkaddr = TASK_AllocThunk();
824 if (!thunkaddr) return (FARPROC16)0;
825 thunk = MapSL( thunkaddr );
826 lfunc = MapSL( (SEGPTR)func );
828 TRACE("(%08lx,%04x): got thunk %08lx\n",
829 (DWORD)func, hInstance, (DWORD)thunkaddr );
830 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
831 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
833 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
836 *thunk++ = 0xb8; /* movw instance, %ax */
837 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
838 *thunk++ = (BYTE)(hInstanceSelector >> 8);
839 *thunk++ = 0xea; /* ljmp func */
840 *(DWORD *)thunk = (DWORD)func;
841 return (FARPROC16)thunkaddr;
842 /* CX reg indicates if thunkaddr != NULL, implement if needed */
846 /***********************************************************************
847 * FreeProcInstance (KERNEL.52)
849 void WINAPI FreeProcInstance16( FARPROC16 func )
851 TRACE("(%08lx)\n", (DWORD)func );
852 TASK_FreeThunk( (SEGPTR)func );
855 /**********************************************************************
856 * TASK_GetCodeSegment
858 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
859 * and logical segment number of a given code segment.
861 * 'proc' either *is* already a pair of module handle and segment number,
862 * in which case there's nothing to do. Otherwise, it is a pointer to
863 * a function, and we need to retrieve the code segment. If the pointer
864 * happens to point to a thunk, we'll retrieve info about the code segment
865 * where the function pointed to by the thunk resides, not the thunk itself.
867 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
868 * the function the snoop code will return to ...
871 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
872 SEGTABLEENTRY **ppSeg, int *pSegNr )
874 NE_MODULE *pModule = NULL;
875 SEGTABLEENTRY *pSeg = NULL;
876 int segNr=0;
878 /* Try pair of module handle / segment number */
879 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
880 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
882 segNr = LOWORD( proc );
883 if ( segNr && segNr <= pModule->seg_count )
884 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
887 /* Try thunk or function */
888 else
890 BYTE *thunk = MapSL( (SEGPTR)proc );
891 WORD selector;
893 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
894 selector = thunk[6] + (thunk[7] << 8);
895 else
896 selector = HIWORD( proc );
898 pModule = NE_GetPtr( GlobalHandle16( selector ) );
899 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
901 if ( pModule )
902 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
903 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
904 break;
906 if ( pModule && segNr > pModule->seg_count )
907 pSeg = NULL;
910 /* Abort if segment not found */
912 if ( !pModule || !pSeg )
913 return FALSE;
915 /* Return segment data */
917 if ( ppModule ) *ppModule = pModule;
918 if ( ppSeg ) *ppSeg = pSeg;
919 if ( pSegNr ) *pSegNr = segNr;
921 return TRUE;
924 /**********************************************************************
925 * GetCodeHandle (KERNEL.93)
927 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
929 SEGTABLEENTRY *pSeg;
931 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
932 return (HANDLE16)0;
934 return pSeg->hSeg;
937 /**********************************************************************
938 * GetCodeInfo (KERNEL.104)
940 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
942 NE_MODULE *pModule;
943 SEGTABLEENTRY *pSeg;
944 int segNr;
946 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
947 return FALSE;
949 /* Fill in segment information */
951 segInfo->offSegment = pSeg->filepos;
952 segInfo->cbSegment = pSeg->size;
953 segInfo->flags = pSeg->flags;
954 segInfo->cbAlloc = pSeg->minsize;
955 segInfo->h = pSeg->hSeg;
956 segInfo->alignShift = pModule->alignment;
958 if ( segNr == pModule->dgroup )
959 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
961 /* Return module handle in %es */
963 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
965 return TRUE;
969 /**********************************************************************
970 * DefineHandleTable (KERNEL.94)
972 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
974 FIXME("(%04x): stub ?\n", wOffset);
975 return TRUE;
979 /***********************************************************************
980 * SetTaskQueue (KERNEL.34)
982 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
984 HQUEUE16 hPrev;
985 TDB *pTask;
987 if (!hTask) hTask = GetCurrentTask();
988 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
990 hPrev = pTask->hQueue;
991 pTask->hQueue = hQueue;
993 return hPrev;
997 /***********************************************************************
998 * GetTaskQueue (KERNEL.35)
1000 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1002 TDB *pTask;
1004 if (!hTask) hTask = GetCurrentTask();
1005 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
1006 return pTask->hQueue;
1009 /***********************************************************************
1010 * SetThreadQueue (KERNEL.463)
1012 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1014 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1015 HQUEUE16 oldQueue = teb? teb->queue : 0;
1017 if ( teb )
1019 teb->queue = hQueue;
1021 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1022 SetTaskQueue16( teb->htask16, hQueue );
1025 return oldQueue;
1028 /***********************************************************************
1029 * GetThreadQueue (KERNEL.464)
1031 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1033 TEB *teb = NULL;
1034 if ( !thread )
1035 teb = NtCurrentTeb();
1036 else if ( HIWORD(thread) )
1037 teb = THREAD_IdToTEB( thread );
1038 else if ( IsTask16( (HTASK16)thread ) )
1039 teb = (TASK_GetPtr( (HANDLE16)thread ))->teb;
1041 return (HQUEUE16)(teb? teb->queue : 0);
1044 /***********************************************************************
1045 * SetFastQueue (KERNEL.624)
1047 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1049 TEB *teb = NULL;
1050 if ( !thread )
1051 teb = NtCurrentTeb();
1052 else if ( HIWORD(thread) )
1053 teb = THREAD_IdToTEB( thread );
1054 else if ( IsTask16( (HTASK16)thread ) )
1055 teb = (TASK_GetPtr( (HANDLE16)thread ))->teb;
1057 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1060 /***********************************************************************
1061 * GetFastQueue (KERNEL.625)
1063 HANDLE WINAPI GetFastQueue16( void )
1065 HANDLE ret = (HANDLE)NtCurrentTeb()->queue;
1067 if (!ret) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1068 return ret;
1071 /***********************************************************************
1072 * SwitchStackTo (KERNEL.108)
1074 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1076 TDB *pTask;
1077 STACK16FRAME *oldFrame, *newFrame;
1078 INSTANCEDATA *pData;
1079 UINT16 copySize;
1081 if (!(pTask = TASK_GetCurrent())) return;
1082 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1083 TRACE("old=%04x:%04x new=%04x:%04x\n",
1084 SELECTOROF( pTask->teb->cur_stack ),
1085 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1087 /* Save the old stack */
1089 oldFrame = THREAD_STACK16( pTask->teb );
1090 /* pop frame + args and push bp */
1091 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1092 + 2 * sizeof(WORD);
1093 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1094 pData->stacktop = top;
1095 pData->stackmin = ptr;
1096 pData->stackbottom = ptr;
1098 /* Switch to the new stack */
1100 /* Note: we need to take the 3 arguments into account; otherwise,
1101 * the stack will underflow upon return from this function.
1103 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1104 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1105 pTask->teb->cur_stack = MAKESEGPTR( seg, ptr - copySize );
1106 newFrame = THREAD_STACK16( pTask->teb );
1108 /* Copy the stack frame and the local variables to the new stack */
1110 memmove( newFrame, oldFrame, copySize );
1111 newFrame->bp = ptr;
1112 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1116 /***********************************************************************
1117 * SwitchStackBack (KERNEL.109)
1119 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1121 STACK16FRAME *oldFrame, *newFrame;
1122 INSTANCEDATA *pData;
1124 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1125 return;
1126 if (!pData->old_ss_sp)
1128 WARN("No previous SwitchStackTo\n" );
1129 return;
1131 TRACE("restoring stack %04x:%04x\n",
1132 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1134 oldFrame = CURRENT_STACK16;
1136 /* Pop bp from the previous stack */
1138 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
1139 pData->old_ss_sp += sizeof(WORD);
1141 /* Switch back to the old stack */
1143 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1144 context->SegSs = SELECTOROF(pData->old_ss_sp);
1145 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1146 pData->old_ss_sp = 0;
1148 /* Build a stack frame for the return */
1150 newFrame = CURRENT_STACK16;
1151 newFrame->frame32 = oldFrame->frame32;
1152 newFrame->module_cs = oldFrame->module_cs;
1153 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1154 newFrame->entry_ip = oldFrame->entry_ip;
1158 /***********************************************************************
1159 * GetTaskQueueDS (KERNEL.118)
1161 void WINAPI GetTaskQueueDS16(void)
1163 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1167 /***********************************************************************
1168 * GetTaskQueueES (KERNEL.119)
1170 void WINAPI GetTaskQueueES16(void)
1172 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1176 /***********************************************************************
1177 * GetCurrentTask (KERNEL32.@)
1179 HTASK16 WINAPI GetCurrentTask(void)
1181 return NtCurrentTeb()->htask16;
1184 /***********************************************************************
1185 * GetCurrentTask (KERNEL.36)
1187 DWORD WINAPI WIN16_GetCurrentTask(void)
1189 /* This is the version used by relay code; the first task is */
1190 /* returned in the high word of the result */
1191 return MAKELONG( GetCurrentTask(), hFirstTask );
1195 /***********************************************************************
1196 * GetCurrentPDB (KERNEL.37)
1198 * UNDOC: returns PSP of KERNEL in high word
1200 DWORD WINAPI GetCurrentPDB16(void)
1202 TDB *pTask;
1204 if (!(pTask = TASK_GetCurrent())) return 0;
1205 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1209 /***********************************************************************
1210 * GetCurPID (KERNEL.157)
1212 DWORD WINAPI GetCurPID16( DWORD unused )
1214 return 0;
1218 /***********************************************************************
1219 * GetInstanceData (KERNEL.54)
1221 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1223 char *ptr = (char *)GlobalLock16( instance );
1224 if (!ptr || !len) return 0;
1225 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1226 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1227 return len;
1231 /***********************************************************************
1232 * GetExeVersion (KERNEL.105)
1234 WORD WINAPI GetExeVersion16(void)
1236 TDB *pTask;
1238 if (!(pTask = TASK_GetCurrent())) return 0;
1239 return pTask->version;
1243 /***********************************************************************
1244 * SetErrorMode (KERNEL.107)
1246 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1248 TDB *pTask;
1249 if (!(pTask = TASK_GetCurrent())) return 0;
1250 pTask->error_mode = mode;
1251 return SetErrorMode( mode );
1255 /***********************************************************************
1256 * GetNumTasks (KERNEL.152)
1258 UINT16 WINAPI GetNumTasks16(void)
1260 return nTaskCount;
1264 /***********************************************************************
1265 * GetTaskDS (KERNEL.155)
1267 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1268 * I don't think we need to bother with this.
1270 HINSTANCE16 WINAPI GetTaskDS16(void)
1272 TDB *pTask;
1274 if (!(pTask = TASK_GetCurrent())) return 0;
1275 return GlobalHandleToSel16(pTask->hInstance);
1278 /***********************************************************************
1279 * GetDummyModuleHandleDS (KERNEL.602)
1281 WORD WINAPI GetDummyModuleHandleDS16(void)
1283 TDB *pTask;
1284 WORD selector;
1286 if (!(pTask = TASK_GetCurrent())) return 0;
1287 if (!(pTask->flags & TDBF_WIN32)) return 0;
1288 selector = GlobalHandleToSel16( pTask->hModule );
1289 CURRENT_DS = selector;
1290 return selector;
1293 /***********************************************************************
1294 * IsTask (KERNEL.320)
1295 * IsTask16 (KERNEL32.@)
1297 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1299 TDB *pTask;
1301 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1302 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1303 return (pTask->magic == TDB_MAGIC);
1307 /***********************************************************************
1308 * IsWinOldApTask (KERNEL.158)
1310 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1312 /* should return bit 0 of byte 0x48 in PSP */
1313 return FALSE;
1316 /***********************************************************************
1317 * SetTaskSignalProc (KERNEL.38)
1319 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1321 TDB *pTask;
1322 FARPROC16 oldProc;
1324 if (!hTask) hTask = GetCurrentTask();
1325 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1326 oldProc = pTask->userhandler;
1327 pTask->userhandler = proc;
1328 return oldProc;
1331 /***********************************************************************
1332 * TASK_CallTaskSignalProc
1334 /* ### start build ### */
1335 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1336 /* ### stop build ### */
1337 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1339 TDB *pTask = TASK_GetCurrent();
1340 if ( !pTask || !pTask->userhandler ) return;
1342 TASK_CallTo16_word_wwwww( pTask->userhandler,
1343 hTaskOrModule, uCode, 0,
1344 pTask->hInstance, pTask->hQueue );
1347 /***********************************************************************
1348 * SetSigHandler (KERNEL.140)
1350 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1351 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1353 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1354 newhandler,oldhandler,oldmode,newmode,flag );
1356 if (flag != 1) return 0;
1357 if (!newmode) newhandler = NULL; /* Default handler */
1358 if (newmode != 4)
1360 TDB *pTask;
1362 if (!(pTask = TASK_GetCurrent())) return 0;
1363 if (oldmode) *oldmode = pTask->signal_flags;
1364 pTask->signal_flags = newmode;
1365 if (oldhandler) *oldhandler = pTask->sighandler;
1366 pTask->sighandler = newhandler;
1368 return 0;
1372 /***********************************************************************
1373 * GlobalNotify (KERNEL.154)
1375 * Note that GlobalNotify does _not_ return the old NotifyProc
1376 * -- contrary to LocalNotify !!
1378 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1380 TDB *pTask;
1382 if (!(pTask = TASK_GetCurrent())) return;
1383 pTask->discardhandler = proc;
1387 /***********************************************************************
1388 * GetExePtrHelper
1390 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1392 char *ptr;
1393 HANDLE16 owner;
1395 /* Check for module handle */
1397 if (!(ptr = GlobalLock16( handle ))) return 0;
1398 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1400 /* Search for this handle inside all tasks */
1402 *hTask = hFirstTask;
1403 while (*hTask)
1405 TDB *pTask = TASK_GetPtr( *hTask );
1406 if ((*hTask == handle) ||
1407 (pTask->hInstance == handle) ||
1408 (pTask->hQueue == handle) ||
1409 (pTask->hPDB == handle)) return pTask->hModule;
1410 *hTask = pTask->hNext;
1413 /* Check the owner for module handle */
1415 owner = FarGetOwner16( handle );
1416 if (!(ptr = GlobalLock16( owner ))) return 0;
1417 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1419 /* Search for the owner inside all tasks */
1421 *hTask = hFirstTask;
1422 while (*hTask)
1424 TDB *pTask = TASK_GetPtr( *hTask );
1425 if ((*hTask == owner) ||
1426 (pTask->hInstance == owner) ||
1427 (pTask->hQueue == owner) ||
1428 (pTask->hPDB == owner)) return pTask->hModule;
1429 *hTask = pTask->hNext;
1432 return 0;
1435 /***********************************************************************
1436 * GetExePtr (KERNEL.133)
1438 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1440 HTASK16 hTask = 0;
1441 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1442 STACK16FRAME *frame = CURRENT_STACK16;
1443 frame->ecx = hModule;
1444 if (hTask) frame->es = hTask;
1445 return hModule;
1449 /***********************************************************************
1450 * K228 (KERNEL.228)
1452 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1454 HTASK16 hTask = 0;
1455 return GetExePtrHelper( handle, &hTask );
1459 /***********************************************************************
1460 * TaskFirst (TOOLHELP.63)
1462 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1464 lpte->hNext = hFirstTask;
1465 return TaskNext16( lpte );
1469 /***********************************************************************
1470 * TaskNext (TOOLHELP.64)
1472 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1474 TDB *pTask;
1475 INSTANCEDATA *pInstData;
1477 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1478 if (!lpte->hNext) return FALSE;
1480 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1481 while (1) {
1482 pTask = TASK_GetPtr( lpte->hNext );
1483 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1484 if (pTask->hInstance)
1485 break;
1486 lpte->hNext = pTask->hNext;
1488 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1489 lpte->hTask = lpte->hNext;
1490 lpte->hTaskParent = pTask->hParent;
1491 lpte->hInst = pTask->hInstance;
1492 lpte->hModule = pTask->hModule;
1493 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1494 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1495 lpte->wStackTop = pInstData->stacktop;
1496 lpte->wStackMinimum = pInstData->stackmin;
1497 lpte->wStackBottom = pInstData->stackbottom;
1498 lpte->wcEvents = pTask->nEvents;
1499 lpte->hQueue = pTask->hQueue;
1500 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1501 lpte->wPSPOffset = 0x100; /*??*/
1502 lpte->hNext = pTask->hNext;
1503 return TRUE;
1507 /***********************************************************************
1508 * TaskFindHandle (TOOLHELP.65)
1510 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1512 lpte->hNext = hTask;
1513 return TaskNext16( lpte );
1517 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1519 /**************************************************************************
1520 * FatalAppExit (KERNEL.137)
1522 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1524 TDB *pTask = TASK_GetCurrent();
1526 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1528 HMODULE mod = GetModuleHandleA( "user32.dll" );
1529 if (mod)
1531 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1532 if (pMessageBoxA)
1534 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1535 goto done;
1538 ERR( "%s\n", debugstr_a(str) );
1540 done:
1541 ExitThread(0xff);
1545 /***********************************************************************
1546 * TerminateApp (TOOLHELP.77)
1548 * See "Undocumented Windows".
1550 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1552 if (hTask && hTask != GetCurrentTask())
1554 FIXME("cannot terminate task %x\n", hTask);
1555 return;
1558 if (wFlags & NO_UAE_BOX)
1560 UINT16 old_mode;
1561 old_mode = SetErrorMode16(0);
1562 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1564 FatalAppExit16( 0, NULL );
1566 /* hmm, we're still alive ?? */
1568 /* check undocumented flag */
1569 if (!(wFlags & 0x8000))
1570 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1572 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1573 but let's just call ExitThread */
1574 ExitThread(0xff);
1578 /***********************************************************************
1579 * GetAppCompatFlags (KERNEL.354)
1581 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1583 TDB *pTask;
1585 if (!hTask) hTask = GetCurrentTask();
1586 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1587 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1588 return pTask->compat_flags;