Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / task.c
blobc1c4e37fa40ca129f5b09acff3692d408f5f6290
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 <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 "thread.h"
41 #include "winternl.h"
42 #include "wine/server.h"
43 #include "toolhelp.h"
44 #include "kernel_private.h"
45 #include "kernel16_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(task);
50 WINE_DECLARE_DEBUG_CHANNEL(relay);
51 WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
53 #include "pshpack1.h"
55 /* Segment containing MakeProcInstance() thunks */
56 typedef struct
58 WORD next; /* Selector of next segment */
59 WORD magic; /* Thunks signature */
60 WORD unused;
61 WORD free; /* Head of the free list */
62 WORD thunks[4]; /* Each thunk is 4 words long */
63 } THUNKS;
65 #include "poppack.h"
67 #define THUNK_MAGIC ('P' | ('T' << 8))
69 /* Min. number of thunks allocated when creating a new segment */
70 #define MIN_THUNKS 32
72 #define TDB_MAGIC ('T' | ('D' << 8))
74 static THHOOK DefaultThhook;
75 THHOOK *pThhook = &DefaultThhook;
77 #define hFirstTask (pThhook->HeadTDB)
78 #define hLockedTask (pThhook->LockTDB)
80 static UINT16 nTaskCount = 0;
82 static HTASK16 initial_task;
84 /***********************************************************************
85 * TASK_InstallTHHook
87 void TASK_InstallTHHook( THHOOK *pNewThhook )
89 THHOOK *pOldThhook = pThhook;
91 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
93 *pThhook = *pOldThhook;
96 /***********************************************************************
97 * TASK_GetPtr
99 static TDB *TASK_GetPtr( HTASK16 hTask )
101 return GlobalLock16( hTask );
105 /***********************************************************************
106 * TASK_GetCurrent
108 TDB *TASK_GetCurrent(void)
110 return TASK_GetPtr( GetCurrentTask() );
114 /***********************************************************************
115 * TASK_LinkTask
117 static void TASK_LinkTask( HTASK16 hTask )
119 HTASK16 *prevTask;
120 TDB *pTask;
122 if (!(pTask = TASK_GetPtr( hTask ))) return;
123 prevTask = &hFirstTask;
124 while (*prevTask)
126 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
127 if (prevTaskPtr->priority >= pTask->priority) break;
128 prevTask = &prevTaskPtr->hNext;
130 pTask->hNext = *prevTask;
131 *prevTask = hTask;
132 nTaskCount++;
136 /***********************************************************************
137 * TASK_UnlinkTask
139 static void TASK_UnlinkTask( HTASK16 hTask )
141 HTASK16 *prevTask;
142 TDB *pTask;
144 prevTask = &hFirstTask;
145 while (*prevTask && (*prevTask != hTask))
147 pTask = TASK_GetPtr( *prevTask );
148 prevTask = &pTask->hNext;
150 if (*prevTask)
152 pTask = TASK_GetPtr( *prevTask );
153 *prevTask = pTask->hNext;
154 pTask->hNext = 0;
155 nTaskCount--;
160 /***********************************************************************
161 * TASK_CreateThunks
163 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
164 * and containing 'count' entries.
166 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
168 int i;
169 WORD free;
170 THUNKS *pThunk;
172 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
173 pThunk->next = 0;
174 pThunk->magic = THUNK_MAGIC;
175 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
176 free = pThunk->free;
177 for (i = 0; i < count-1; i++)
179 free += 8; /* Offset of next thunk */
180 pThunk->thunks[4*i] = free;
182 pThunk->thunks[4*i] = 0; /* Last thunk */
186 /***********************************************************************
187 * TASK_AllocThunk
189 * Allocate a thunk for MakeProcInstance().
191 static SEGPTR TASK_AllocThunk(void)
193 TDB *pTask;
194 THUNKS *pThunk;
195 WORD sel, base;
197 if (!(pTask = TASK_GetCurrent())) return 0;
198 sel = pTask->hCSAlias;
199 pThunk = (THUNKS *)&pTask->thunks;
200 base = (char *)pThunk - (char *)pTask;
201 while (!pThunk->free)
203 sel = pThunk->next;
204 if (!sel) /* Allocate a new segment */
206 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
207 pTask->hPDB, WINE_LDT_FLAGS_CODE );
208 if (!sel) return (SEGPTR)0;
209 TASK_CreateThunks( sel, 0, MIN_THUNKS );
210 pThunk->next = sel;
212 pThunk = (THUNKS *)GlobalLock16( sel );
213 base = 0;
215 base += pThunk->free;
216 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
217 return MAKESEGPTR( sel, base );
221 /***********************************************************************
222 * TASK_FreeThunk
224 * Free a MakeProcInstance() thunk.
226 static BOOL TASK_FreeThunk( SEGPTR thunk )
228 TDB *pTask;
229 THUNKS *pThunk;
230 WORD sel, base;
232 if (!(pTask = TASK_GetCurrent())) return 0;
233 sel = pTask->hCSAlias;
234 pThunk = (THUNKS *)&pTask->thunks;
235 base = (char *)pThunk - (char *)pTask;
236 while (sel && (sel != HIWORD(thunk)))
238 sel = pThunk->next;
239 pThunk = (THUNKS *)GlobalLock16( sel );
240 base = 0;
242 if (!sel) return FALSE;
243 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
244 pThunk->free = LOWORD(thunk) - base;
245 return TRUE;
249 /***********************************************************************
250 * TASK_Create
252 * NOTE: This routine might be called by a Win32 thread. Thus, we need
253 * to be careful to protect global data structures. We do this
254 * by entering the Win16Lock while linking the task into the
255 * global task list.
257 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, LPCSTR cmdline, BYTE len )
259 HTASK16 hTask;
260 TDB *pTask;
261 FARPROC16 proc;
262 char curdir[MAX_PATH];
263 HMODULE16 hModule = pModule ? pModule->self : 0;
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, hModule );
272 /* Fill the task structure */
274 pTask->hSelf = hTask;
276 pTask->version = pModule ? pModule->ne_expver : 0x0400;
277 pTask->hModule = hModule;
278 pTask->hParent = GetCurrentTask();
279 pTask->magic = TDB_MAGIC;
280 pTask->nCmdShow = cmdShow;
282 GetCurrentDirectoryA( sizeof(curdir), curdir );
283 GetShortPathNameA( curdir, curdir, sizeof(curdir) );
284 pTask->curdrive = (curdir[0] - 'A') | 0x80;
285 lstrcpynA( pTask->curdir, curdir + 2, sizeof(pTask->curdir) );
287 /* Create the thunks block */
289 TASK_CreateThunks( hTask, (char *)&pTask->thunks - (char *)pTask, 7 );
291 /* Copy the module name */
293 if (hModule)
295 char name[sizeof(pTask->module_name)+1];
296 size_t len;
297 GetModuleName16( hModule, name, sizeof(name) );
298 len = strlen(name) + 1;
299 memcpy(pTask->module_name, name, min(len,sizeof(pTask->module_name)));
300 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
303 /* Allocate a selector for the PDB */
305 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
306 hModule, WINE_LDT_FLAGS_DATA );
308 /* Fill the PDB */
310 pTask->pdb.int20 = 0x20cd;
311 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
312 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
313 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
314 pTask->pdb.savedint22 = 0;
315 pTask->pdb.savedint23 = 0;
316 pTask->pdb.savedint24 = 0;
317 pTask->pdb.fileHandlesPtr =
318 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
319 pTask->pdb.hFileHandles = 0;
320 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
321 /* FIXME: should we make a copy of the environment? */
322 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
323 pTask->pdb.nbFiles = 20;
325 /* Fill the command line */
327 if (!cmdline)
329 cmdline = GetCommandLineA();
330 /* remove the first word (program name) */
331 if (*cmdline == '"')
332 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
333 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
334 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
335 len = strlen(cmdline);
337 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
338 pTask->pdb.cmdLine[0] = len;
339 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
340 /* pTask->pdb.cmdLine[len+1] = 0; */
342 TRACE("cmdline='%.*s' task=%04x\n", len, cmdline, hTask );
344 /* Allocate a code segment alias for the TDB */
346 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
347 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
349 /* Default DTA overwrites command line */
351 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
353 /* Create scheduler event for 16-bit tasks */
355 if ( !(pTask->flags & TDBF_WIN32) )
356 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
358 if (!initial_task) initial_task = hTask;
360 return pTask;
364 /***********************************************************************
365 * TASK_DeleteTask
367 static void TASK_DeleteTask( HTASK16 hTask )
369 TDB *pTask;
370 HGLOBAL16 hPDB;
372 if (!(pTask = TASK_GetPtr( hTask ))) return;
373 hPDB = pTask->hPDB;
375 pTask->magic = 0xdead; /* invalidate signature */
377 /* Free the selector aliases */
379 GLOBAL_FreeBlock( pTask->hCSAlias );
380 GLOBAL_FreeBlock( pTask->hPDB );
382 /* Free the task module */
384 FreeModule16( pTask->hModule );
386 /* Free the task structure itself */
388 GlobalFree16( hTask );
390 /* Free all memory used by this task (including the 32-bit stack, */
391 /* the environment block and the thunk segments). */
393 GlobalFreeAll16( hPDB );
397 /***********************************************************************
398 * TASK_CreateMainTask
400 * Create a task for the main (32-bit) process.
402 void TASK_CreateMainTask(void)
404 TDB *pTask;
405 STARTUPINFOA startup_info;
406 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
408 GetStartupInfoA( &startup_info );
409 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
410 pTask = TASK_Create( NULL, cmdShow, NULL, 0 );
411 if (!pTask)
413 ERR("could not create task for main process\n");
414 ExitProcess(1);
417 pTask->flags |= TDBF_WIN32;
418 pTask->hInstance = 0;
419 pTask->hPrevInstance = 0;
420 pTask->teb = NtCurrentTeb();
422 NtCurrentTeb()->htask16 = pTask->hSelf;
424 /* Add the task to the linked list */
425 /* (no need to get the win16 lock, we are the only thread at this point) */
426 TASK_LinkTask( pTask->hSelf );
430 /* allocate the win16 TIB for a new 16-bit task */
431 static WIN16_SUBSYSTEM_TIB *allocate_win16_tib( TDB *pTask )
433 WCHAR path[MAX_PATH];
434 WIN16_SUBSYSTEM_TIB *tib;
435 UNICODE_STRING *curdir;
436 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
438 if (!(tib = HeapAlloc( GetProcessHeap(), 0, sizeof(*tib) ))) return NULL;
439 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
440 GetLongPathNameW( path, path, MAX_PATH );
441 if (RtlCreateUnicodeString( &tib->exe_str, path )) tib->exe_name = &tib->exe_str;
442 else tib->exe_name = NULL;
444 RtlAcquirePebLock();
445 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
446 tib->curdir.DosPath.MaximumLength = sizeof(tib->curdir_buffer);
447 tib->curdir.DosPath.Length = min( curdir->Length, tib->curdir.DosPath.MaximumLength-sizeof(WCHAR) );
448 tib->curdir.DosPath.Buffer = tib->curdir_buffer;
449 tib->curdir.Handle = 0;
450 memcpy( tib->curdir_buffer, curdir->Buffer, tib->curdir.DosPath.Length );
451 tib->curdir_buffer[tib->curdir.DosPath.Length/sizeof(WCHAR)] = 0;
452 RtlReleasePebLock();
453 return tib;
456 /* startup routine for a new 16-bit thread */
457 static DWORD CALLBACK task_start( LPVOID p )
459 TDB *pTask = (TDB *)p;
460 DWORD ret;
462 NtCurrentTeb()->htask16 = pTask->hSelf;
463 NtCurrentTeb()->Tib.SubSystemTib = allocate_win16_tib( pTask );
465 _EnterWin16Lock();
466 TASK_LinkTask( pTask->hSelf );
467 pTask->teb = NtCurrentTeb();
468 ret = NE_StartTask();
469 _LeaveWin16Lock();
470 return ret;
474 /***********************************************************************
475 * TASK_SpawnTask
477 * Spawn a new 16-bit task.
479 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
480 LPCSTR cmdline, BYTE len, HANDLE *hThread )
482 TDB *pTask;
484 if (!(pTask = TASK_Create( pModule, cmdShow, cmdline, len ))) return 0;
485 if (!(*hThread = CreateThread( NULL, 0, task_start, pTask, 0, NULL )))
487 TASK_DeleteTask( pTask->hSelf );
488 return 0;
490 return pTask->hSelf;
494 /***********************************************************************
495 * TASK_GetTaskFromThread
497 HTASK16 TASK_GetTaskFromThread( DWORD thread )
499 TDB *p = TASK_GetPtr( hFirstTask );
500 while (p)
502 if (p->teb->ClientId.UniqueThread == (HANDLE)thread) return p->hSelf;
503 p = TASK_GetPtr( p->hNext );
505 return 0;
509 /***********************************************************************
510 * TASK_CallTaskSignalProc
512 static void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
514 WORD args[5];
515 TDB *pTask = TASK_GetCurrent();
517 if ( !pTask || !pTask->userhandler ) return;
519 args[4] = hTaskOrModule;
520 args[3] = uCode;
521 args[2] = 0;
522 args[1] = pTask->hInstance;
523 args[0] = pTask->hQueue;
524 WOWCallback16Ex( (DWORD)pTask->userhandler, WCB16_PASCAL, sizeof(args), args, NULL );
528 /***********************************************************************
529 * TASK_ExitTask
531 void TASK_ExitTask(void)
533 WIN16_SUBSYSTEM_TIB *tib;
534 TDB *pTask;
535 DWORD lockCount;
537 /* Enter the Win16Lock to protect global data structures */
538 _EnterWin16Lock();
540 pTask = TASK_GetCurrent();
541 if ( !pTask )
543 _LeaveWin16Lock();
544 return;
547 TRACE("Killing task %04x\n", pTask->hSelf );
549 /* Perform USER cleanup */
551 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
553 /* Remove the task from the list to be sure we never switch back to it */
554 TASK_UnlinkTask( pTask->hSelf );
556 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
558 TRACE("this is the last task, exiting\n" );
559 ExitKernel16();
562 pTask->nEvents = 0;
564 if ( hLockedTask == pTask->hSelf )
565 hLockedTask = 0;
567 TASK_DeleteTask( pTask->hSelf );
569 if ((tib = NtCurrentTeb()->Tib.SubSystemTib))
571 if (tib->exe_name) RtlFreeUnicodeString( tib->exe_name );
572 HeapFree( GetProcessHeap(), 0, tib );
573 NtCurrentTeb()->Tib.SubSystemTib = NULL;
576 /* ... and completely release the Win16Lock, just in case. */
577 ReleaseThunkLock( &lockCount );
581 /***********************************************************************
582 * ExitKernel (KERNEL.2)
584 * Clean-up everything and exit the Wine process.
586 void WINAPI ExitKernel16(void)
588 WriteOutProfiles16();
589 TerminateProcess( GetCurrentProcess(), 0 );
593 /***********************************************************************
594 * InitTask (KERNEL.91)
596 * Called by the application startup code.
598 void WINAPI InitTask16( CONTEXT86 *context )
600 TDB *pTask;
601 INSTANCEDATA *pinstance;
602 SEGPTR ptr;
604 context->Eax = 0;
605 if (!(pTask = TASK_GetCurrent())) return;
607 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
608 as some apps, notably Visual Basic apps, *modify* the heap/stack
609 size of the instance data segment before calling InitTask() */
611 /* Initialize the INSTANCEDATA structure */
612 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
613 pinstance->stackmin = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + sizeof( STACK16FRAME );
614 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
615 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
616 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
618 /* Initialize the local heap */
619 if (LOWORD(context->Ecx))
620 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
622 /* Initialize implicitly loaded DLLs */
623 NE_InitializeDLLs( pTask->hModule );
624 NE_DllProcessAttach( pTask->hModule );
626 /* Registers on return are:
627 * ax 1 if OK, 0 on error
628 * cx stack limit in bytes
629 * dx cmdShow parameter
630 * si instance handle of the previous instance
631 * di instance handle of the new task
632 * es:bx pointer to command line inside PSP
634 * 0 (=%bp) is pushed on the stack
636 ptr = stack16_push( sizeof(WORD) );
637 *(WORD *)MapSL(ptr) = 0;
638 context->Esp -= 2;
640 context->Eax = 1;
642 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
643 else
645 LPBYTE p = &pTask->pdb.cmdLine[1];
646 while ((*p == ' ') || (*p == '\t')) p++;
647 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
649 context->Ecx = pinstance->stacktop;
650 context->Edx = pTask->nCmdShow;
651 context->Esi = (DWORD)pTask->hPrevInstance;
652 context->Edi = (DWORD)pTask->hInstance;
653 context->SegEs = (WORD)pTask->hPDB;
657 /***********************************************************************
658 * WaitEvent (KERNEL.30)
660 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
662 TDB *pTask;
664 if (!hTask) hTask = GetCurrentTask();
665 pTask = TASK_GetPtr( hTask );
667 if (pTask->flags & TDBF_WIN32)
669 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
670 return TRUE;
673 if (pTask->nEvents > 0)
675 pTask->nEvents--;
676 return FALSE;
679 if (pTask->teb == NtCurrentTeb())
681 DWORD lockCount;
683 NtResetEvent( pTask->hEvent, NULL );
684 ReleaseThunkLock( &lockCount );
685 SYSLEVEL_CheckNotLevel( 1 );
686 WaitForSingleObject( pTask->hEvent, INFINITE );
687 RestoreThunkLock( lockCount );
688 if (pTask->nEvents > 0) pTask->nEvents--;
690 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
692 return TRUE;
696 /***********************************************************************
697 * PostEvent (KERNEL.31)
699 void WINAPI PostEvent16( HTASK16 hTask )
701 TDB *pTask;
703 if (!hTask) hTask = GetCurrentTask();
704 if (!(pTask = TASK_GetPtr( hTask ))) return;
706 if (pTask->flags & TDBF_WIN32)
708 FIXME("called for Win32 thread (%04lx)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
709 return;
712 pTask->nEvents++;
714 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
718 /***********************************************************************
719 * SetPriority (KERNEL.32)
721 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
723 TDB *pTask;
724 INT16 newpriority;
726 if (!hTask) hTask = GetCurrentTask();
727 if (!(pTask = TASK_GetPtr( hTask ))) return;
728 newpriority = pTask->priority + delta;
729 if (newpriority < -32) newpriority = -32;
730 else if (newpriority > 15) newpriority = 15;
732 pTask->priority = newpriority + 1;
733 TASK_UnlinkTask( pTask->hSelf );
734 TASK_LinkTask( pTask->hSelf );
735 pTask->priority--;
739 /***********************************************************************
740 * LockCurrentTask (KERNEL.33)
742 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
744 if (bLock) hLockedTask = GetCurrentTask();
745 else hLockedTask = 0;
746 return hLockedTask;
750 /***********************************************************************
751 * IsTaskLocked (KERNEL.122)
753 HTASK16 WINAPI IsTaskLocked16(void)
755 return hLockedTask;
759 /***********************************************************************
760 * OldYield (KERNEL.117)
762 void WINAPI OldYield16(void)
764 DWORD count;
766 ReleaseThunkLock(&count);
767 RestoreThunkLock(count);
770 /***********************************************************************
771 * WIN32_OldYield (KERNEL.447)
773 void WINAPI WIN32_OldYield16(void)
775 DWORD count;
777 ReleaseThunkLock(&count);
778 RestoreThunkLock(count);
781 /***********************************************************************
782 * DirectedYield (KERNEL.150)
784 void WINAPI DirectedYield16( HTASK16 hTask )
786 OldYield16();
789 /***********************************************************************
790 * Yield (KERNEL.29)
792 void WINAPI Yield16(void)
794 TDB *pCurTask = TASK_GetCurrent();
796 if (pCurTask && pCurTask->hQueue)
798 HMODULE mod = GetModuleHandleA( "user32.dll" );
799 if (mod)
801 FARPROC proc = GetProcAddress( mod, "UserYield16" );
802 if (proc)
804 proc();
805 return;
809 OldYield16();
812 /***********************************************************************
813 * KERNEL_490 (KERNEL.490)
815 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
817 if ( !someTask ) return 0;
819 FIXME("(%04x): stub\n", someTask );
820 return 0;
823 /***********************************************************************
824 * MakeProcInstance (KERNEL.51)
826 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
828 BYTE *thunk,*lfunc;
829 SEGPTR thunkaddr;
830 WORD hInstanceSelector;
832 hInstanceSelector = GlobalHandleToSel16(hInstance);
834 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
836 if (!HIWORD(func)) {
837 /* Win95 actually protects via SEH, but this is better for debugging */
838 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
839 return (FARPROC16)0;
842 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
843 && (hInstance != 0)
844 && (hInstance != 0xffff) )
846 /* calling MPI with a foreign DSEG is invalid ! */
847 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
848 hInstance,CURRENT_DS);
851 /* Always use the DSEG that MPI was entered with.
852 * We used to set hInstance to GetTaskDS16(), but this should be wrong
853 * as CURRENT_DS provides the DSEG value we need.
854 * ("calling" DS, *not* "task" DS !) */
855 hInstanceSelector = CURRENT_DS;
856 hInstance = GlobalHandle16(hInstanceSelector);
858 /* no thunking for DLLs */
859 if (NE_GetPtr(FarGetOwner16(hInstance))->ne_flags & NE_FFLAGS_LIBMODULE)
860 return func;
862 thunkaddr = TASK_AllocThunk();
863 if (!thunkaddr) return (FARPROC16)0;
864 thunk = MapSL( thunkaddr );
865 lfunc = MapSL( (SEGPTR)func );
867 TRACE("(%08lx,%04x): got thunk %08lx\n",
868 (DWORD)func, hInstance, (DWORD)thunkaddr );
869 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
870 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
872 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
875 *thunk++ = 0xb8; /* movw instance, %ax */
876 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
877 *thunk++ = (BYTE)(hInstanceSelector >> 8);
878 *thunk++ = 0xea; /* ljmp func */
879 *(DWORD *)thunk = (DWORD)func;
880 return (FARPROC16)thunkaddr;
881 /* CX reg indicates if thunkaddr != NULL, implement if needed */
885 /***********************************************************************
886 * FreeProcInstance (KERNEL.52)
888 void WINAPI FreeProcInstance16( FARPROC16 func )
890 TRACE("(%08lx)\n", (DWORD)func );
891 TASK_FreeThunk( (SEGPTR)func );
894 /**********************************************************************
895 * TASK_GetCodeSegment
897 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
898 * and logical segment number of a given code segment.
900 * 'proc' either *is* already a pair of module handle and segment number,
901 * in which case there's nothing to do. Otherwise, it is a pointer to
902 * a function, and we need to retrieve the code segment. If the pointer
903 * happens to point to a thunk, we'll retrieve info about the code segment
904 * where the function pointed to by the thunk resides, not the thunk itself.
906 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
907 * the function the snoop code will return to ...
910 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
911 SEGTABLEENTRY **ppSeg, int *pSegNr )
913 NE_MODULE *pModule = NULL;
914 SEGTABLEENTRY *pSeg = NULL;
915 int segNr=0;
917 /* Try pair of module handle / segment number */
918 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
919 if ( pModule && pModule->ne_magic == IMAGE_OS2_SIGNATURE )
921 segNr = LOWORD( proc );
922 if ( segNr && segNr <= pModule->ne_cseg )
923 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
926 /* Try thunk or function */
927 else
929 BYTE *thunk = MapSL( (SEGPTR)proc );
930 WORD selector;
932 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
933 selector = thunk[6] + (thunk[7] << 8);
934 else
935 selector = HIWORD( proc );
937 pModule = NE_GetPtr( GlobalHandle16( selector ) );
938 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
940 if ( pModule )
941 for ( segNr = 1; segNr <= pModule->ne_cseg; segNr++, pSeg++ )
942 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
943 break;
945 if ( pModule && segNr > pModule->ne_cseg )
946 pSeg = NULL;
949 /* Abort if segment not found */
951 if ( !pModule || !pSeg )
952 return FALSE;
954 /* Return segment data */
956 if ( ppModule ) *ppModule = pModule;
957 if ( ppSeg ) *ppSeg = pSeg;
958 if ( pSegNr ) *pSegNr = segNr;
960 return TRUE;
963 /**********************************************************************
964 * GetCodeHandle (KERNEL.93)
966 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
968 SEGTABLEENTRY *pSeg;
970 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
971 return (HANDLE16)0;
973 return pSeg->hSeg;
976 /**********************************************************************
977 * GetCodeInfo (KERNEL.104)
979 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
981 NE_MODULE *pModule;
982 SEGTABLEENTRY *pSeg;
983 int segNr;
985 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
986 return FALSE;
988 /* Fill in segment information */
990 segInfo->offSegment = pSeg->filepos;
991 segInfo->cbSegment = pSeg->size;
992 segInfo->flags = pSeg->flags;
993 segInfo->cbAlloc = pSeg->minsize;
994 segInfo->h = pSeg->hSeg;
995 segInfo->alignShift = pModule->ne_align;
997 if ( segNr == pModule->ne_autodata )
998 segInfo->cbAlloc += pModule->ne_heap + pModule->ne_stack;
1000 /* Return module handle in %es */
1002 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1004 return TRUE;
1008 /**********************************************************************
1009 * DefineHandleTable (KERNEL.94)
1011 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1013 FIXME("(%04x): stub ?\n", wOffset);
1014 return TRUE;
1018 /***********************************************************************
1019 * SetTaskQueue (KERNEL.34)
1021 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1023 FIXME( "stub, should not get called\n" );
1024 return 0xbeef;
1028 /***********************************************************************
1029 * GetTaskQueue (KERNEL.35)
1031 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1033 FIXME( "stub, should not get called\n" );
1034 return 0xbeef;
1037 /***********************************************************************
1038 * SetThreadQueue (KERNEL.463)
1040 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1042 FIXME( "stub, should not get called\n" );
1043 return 0xbeef;
1046 /***********************************************************************
1047 * GetThreadQueue (KERNEL.464)
1049 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1051 FIXME( "stub, should not get called\n" );
1052 return 0xbeef;
1055 /***********************************************************************
1056 * SetFastQueue (KERNEL.624)
1058 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1060 FIXME( "stub, should not get called\n" );
1063 /***********************************************************************
1064 * GetFastQueue (KERNEL.625)
1066 HQUEUE16 WINAPI GetFastQueue16( void )
1068 FIXME( "stub, should not get called\n" );
1069 return 0xbeef;
1072 /***********************************************************************
1073 * SwitchStackTo (KERNEL.108)
1075 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1077 STACK16FRAME *oldFrame, *newFrame;
1078 INSTANCEDATA *pData;
1079 UINT16 copySize;
1081 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1082 TRACE("old=%04x:%04x new=%04x:%04x\n",
1083 SELECTOROF( NtCurrentTeb()->WOW32Reserved ),
1084 OFFSETOF( NtCurrentTeb()->WOW32Reserved ), seg, ptr );
1086 /* Save the old stack */
1088 oldFrame = CURRENT_STACK16;
1089 /* pop frame + args and push bp */
1090 pData->old_ss_sp = (SEGPTR)NtCurrentTeb()->WOW32Reserved + sizeof(STACK16FRAME)
1091 + 2 * sizeof(WORD);
1092 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1093 pData->stacktop = top;
1094 pData->stackmin = ptr;
1095 pData->stackbottom = ptr;
1097 /* Switch to the new stack */
1099 /* Note: we need to take the 3 arguments into account; otherwise,
1100 * the stack will underflow upon return from this function.
1102 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1103 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1104 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( seg, ptr - copySize );
1105 newFrame = CURRENT_STACK16;
1107 /* Copy the stack frame and the local variables to the new stack */
1109 memmove( newFrame, oldFrame, copySize );
1110 newFrame->bp = ptr;
1111 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1115 /***********************************************************************
1116 * SwitchStackBack (KERNEL.109)
1118 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1120 STACK16FRAME *oldFrame, *newFrame;
1121 INSTANCEDATA *pData;
1123 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->WOW32Reserved))))
1124 return;
1125 if (!pData->old_ss_sp)
1127 WARN("No previous SwitchStackTo\n" );
1128 return;
1130 TRACE("restoring stack %04x:%04x\n",
1131 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1133 oldFrame = CURRENT_STACK16;
1135 /* Pop bp from the previous stack */
1137 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
1138 pData->old_ss_sp += sizeof(WORD);
1140 /* Switch back to the old stack */
1142 NtCurrentTeb()->WOW32Reserved = (void *)(pData->old_ss_sp - sizeof(STACK16FRAME));
1143 context->SegSs = SELECTOROF(pData->old_ss_sp);
1144 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1145 pData->old_ss_sp = 0;
1147 /* Build a stack frame for the return */
1149 newFrame = CURRENT_STACK16;
1150 newFrame->frame32 = oldFrame->frame32;
1151 newFrame->module_cs = oldFrame->module_cs;
1152 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1153 newFrame->entry_ip = oldFrame->entry_ip;
1157 /***********************************************************************
1158 * GetTaskQueueDS (KERNEL.118)
1160 void WINAPI GetTaskQueueDS16(void)
1162 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1166 /***********************************************************************
1167 * GetTaskQueueES (KERNEL.119)
1169 void WINAPI GetTaskQueueES16(void)
1171 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1175 /***********************************************************************
1176 * GetCurrentTask (KERNEL32.@)
1178 HTASK16 WINAPI GetCurrentTask(void)
1180 return NtCurrentTeb()->htask16;
1183 /***********************************************************************
1184 * GetCurrentTask (KERNEL.36)
1186 DWORD WINAPI WIN16_GetCurrentTask(void)
1188 /* This is the version used by relay code; the first task is */
1189 /* returned in the high word of the result */
1190 return MAKELONG( GetCurrentTask(), hFirstTask );
1194 /***********************************************************************
1195 * GetCurrentPDB (KERNEL.37)
1197 * UNDOC: returns PSP of KERNEL in high word
1199 DWORD WINAPI GetCurrentPDB16(void)
1201 TDB *pTask;
1203 if (!(pTask = TASK_GetCurrent())) return 0;
1204 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1208 /***********************************************************************
1209 * GetCurPID (KERNEL.157)
1211 DWORD WINAPI GetCurPID16( DWORD unused )
1213 return 0;
1217 /***********************************************************************
1218 * GetInstanceData (KERNEL.54)
1220 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1222 char *ptr = (char *)GlobalLock16( instance );
1223 if (!ptr || !len) return 0;
1224 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1225 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1226 return len;
1230 /***********************************************************************
1231 * GetExeVersion (KERNEL.105)
1233 WORD WINAPI GetExeVersion16(void)
1235 TDB *pTask;
1237 if (!(pTask = TASK_GetCurrent())) return 0;
1238 return pTask->version;
1242 /***********************************************************************
1243 * SetErrorMode (KERNEL.107)
1245 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1247 TDB *pTask;
1248 if (!(pTask = TASK_GetCurrent())) return 0;
1249 pTask->error_mode = mode;
1250 return SetErrorMode( mode );
1254 /***********************************************************************
1255 * GetNumTasks (KERNEL.152)
1257 UINT16 WINAPI GetNumTasks16(void)
1259 return nTaskCount;
1263 /***********************************************************************
1264 * GetTaskDS (KERNEL.155)
1266 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1267 * I don't think we need to bother with this.
1269 HINSTANCE16 WINAPI GetTaskDS16(void)
1271 TDB *pTask;
1273 if (!(pTask = TASK_GetCurrent())) return 0;
1274 return GlobalHandleToSel16(pTask->hInstance);
1277 /***********************************************************************
1278 * GetDummyModuleHandleDS (KERNEL.602)
1280 WORD WINAPI GetDummyModuleHandleDS16(void)
1282 TDB *pTask;
1283 WORD selector;
1285 if (!(pTask = TASK_GetCurrent())) return 0;
1286 if (!(pTask->flags & TDBF_WIN32)) return 0;
1287 selector = GlobalHandleToSel16( pTask->hModule );
1288 CURRENT_DS = selector;
1289 return selector;
1292 /***********************************************************************
1293 * IsTask (KERNEL.320)
1295 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1297 TDB *pTask;
1299 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1300 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1301 return (pTask->magic == TDB_MAGIC);
1305 /***********************************************************************
1306 * IsWinOldApTask (KERNEL.158)
1308 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1310 /* should return bit 0 of byte 0x48 in PSP */
1311 return FALSE;
1314 /***********************************************************************
1315 * SetTaskSignalProc (KERNEL.38)
1317 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1319 TDB *pTask;
1320 FARPROC16 oldProc;
1322 if (!hTask) hTask = GetCurrentTask();
1323 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1324 oldProc = pTask->userhandler;
1325 pTask->userhandler = proc;
1326 return oldProc;
1329 /***********************************************************************
1330 * SetSigHandler (KERNEL.140)
1332 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1333 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1335 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1336 newhandler,oldhandler,oldmode,newmode,flag );
1338 if (flag != 1) return 0;
1339 if (!newmode) newhandler = NULL; /* Default handler */
1340 if (newmode != 4)
1342 TDB *pTask;
1344 if (!(pTask = TASK_GetCurrent())) return 0;
1345 if (oldmode) *oldmode = pTask->signal_flags;
1346 pTask->signal_flags = newmode;
1347 if (oldhandler) *oldhandler = pTask->sighandler;
1348 pTask->sighandler = newhandler;
1350 return 0;
1354 /***********************************************************************
1355 * GlobalNotify (KERNEL.154)
1357 * Note that GlobalNotify does _not_ return the old NotifyProc
1358 * -- contrary to LocalNotify !!
1360 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1362 TDB *pTask;
1364 if (!(pTask = TASK_GetCurrent())) return;
1365 pTask->discardhandler = proc;
1369 /***********************************************************************
1370 * GetExePtrHelper
1372 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1374 char *ptr;
1375 HANDLE16 owner;
1377 /* Check for module handle */
1379 if (!(ptr = GlobalLock16( handle ))) return 0;
1380 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return handle;
1382 /* Search for this handle inside all tasks */
1384 *hTask = hFirstTask;
1385 while (*hTask)
1387 TDB *pTask = TASK_GetPtr( *hTask );
1388 if ((*hTask == handle) ||
1389 (pTask->hInstance == handle) ||
1390 (pTask->hQueue == handle) ||
1391 (pTask->hPDB == handle)) return pTask->hModule;
1392 *hTask = pTask->hNext;
1395 /* Check the owner for module handle */
1397 owner = FarGetOwner16( handle );
1398 if (!(ptr = GlobalLock16( owner ))) return 0;
1399 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return owner;
1401 /* Search for the owner inside all tasks */
1403 *hTask = hFirstTask;
1404 while (*hTask)
1406 TDB *pTask = TASK_GetPtr( *hTask );
1407 if ((*hTask == owner) ||
1408 (pTask->hInstance == owner) ||
1409 (pTask->hQueue == owner) ||
1410 (pTask->hPDB == owner)) return pTask->hModule;
1411 *hTask = pTask->hNext;
1414 return 0;
1417 /***********************************************************************
1418 * GetExePtr (KERNEL.133)
1420 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1422 HTASK16 hTask = 0;
1423 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1424 STACK16FRAME *frame = CURRENT_STACK16;
1425 frame->ecx = hModule;
1426 if (hTask) frame->es = hTask;
1427 return hModule;
1431 /***********************************************************************
1432 * K228 (KERNEL.228)
1434 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1436 HTASK16 hTask = 0;
1437 return GetExePtrHelper( handle, &hTask );
1441 /***********************************************************************
1442 * TaskFirst (TOOLHELP.63)
1444 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1446 lpte->hNext = hFirstTask;
1447 return TaskNext16( lpte );
1451 /***********************************************************************
1452 * TaskNext (TOOLHELP.64)
1454 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1456 TDB *pTask;
1457 INSTANCEDATA *pInstData;
1459 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1460 if (!lpte->hNext) return FALSE;
1462 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1463 while (1) {
1464 pTask = TASK_GetPtr( lpte->hNext );
1465 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1466 if (pTask->hInstance)
1467 break;
1468 lpte->hNext = pTask->hNext;
1470 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1471 lpte->hTask = lpte->hNext;
1472 lpte->hTaskParent = pTask->hParent;
1473 lpte->hInst = pTask->hInstance;
1474 lpte->hModule = pTask->hModule;
1475 lpte->wSS = SELECTOROF( pTask->teb->WOW32Reserved );
1476 lpte->wSP = OFFSETOF( pTask->teb->WOW32Reserved );
1477 lpte->wStackTop = pInstData->stacktop;
1478 lpte->wStackMinimum = pInstData->stackmin;
1479 lpte->wStackBottom = pInstData->stackbottom;
1480 lpte->wcEvents = pTask->nEvents;
1481 lpte->hQueue = pTask->hQueue;
1482 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1483 lpte->wPSPOffset = 0x100; /*??*/
1484 lpte->hNext = pTask->hNext;
1485 return TRUE;
1489 /***********************************************************************
1490 * TaskFindHandle (TOOLHELP.65)
1492 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1494 lpte->hNext = hTask;
1495 return TaskNext16( lpte );
1499 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1501 /**************************************************************************
1502 * FatalAppExit (KERNEL.137)
1504 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1506 TDB *pTask = TASK_GetCurrent();
1508 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1510 HMODULE mod = GetModuleHandleA( "user32.dll" );
1511 if (mod)
1513 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1514 if (pMessageBoxA)
1516 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1517 goto done;
1520 ERR( "%s\n", debugstr_a(str) );
1522 done:
1523 ExitThread(0xff);
1527 /***********************************************************************
1528 * TerminateApp (TOOLHELP.77)
1530 * See "Undocumented Windows".
1532 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1534 if (hTask && hTask != GetCurrentTask())
1536 FIXME("cannot terminate task %x\n", hTask);
1537 return;
1540 if (wFlags & NO_UAE_BOX)
1542 UINT16 old_mode;
1543 old_mode = SetErrorMode16(0);
1544 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1546 FatalAppExit16( 0, NULL );
1548 /* hmm, we're still alive ?? */
1550 /* check undocumented flag */
1551 if (!(wFlags & 0x8000))
1552 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1554 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1555 but let's just call ExitThread */
1556 ExitThread(0xff);
1560 /***********************************************************************
1561 * GetAppCompatFlags (KERNEL.354)
1563 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1565 TDB *pTask;
1567 if (!hTask) hTask = GetCurrentTask();
1568 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1569 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1570 return pTask->compat_flags;
1574 /***********************************************************************
1575 * GetDOSEnvironment (KERNEL.131)
1577 * Note: the environment is allocated once, it doesn't track changes
1578 * made using the Win32 API. This shouldn't matter.
1580 * Format of a 16-bit environment block:
1581 * ASCIIZ string 1 (xx=yy format)
1582 * ...
1583 * ASCIIZ string n
1584 * BYTE 0
1585 * WORD 1
1586 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1588 SEGPTR WINAPI GetDOSEnvironment16(void)
1590 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1591 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1593 if (!handle)
1595 DWORD size;
1596 LPSTR p, env;
1598 p = env = GetEnvironmentStringsA();
1599 while (*p) p += strlen(p) + 1;
1600 p++; /* skip last null */
1601 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1602 handle = GlobalAlloc16( GMEM_FIXED, size );
1603 if (handle)
1605 WORD one = 1;
1606 LPSTR env16 = GlobalLock16( handle );
1607 memcpy( env16, env, p - env );
1608 memcpy( env16 + (p - env), &one, sizeof(one));
1609 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1610 GlobalUnlock16( handle );
1612 FreeEnvironmentStringsA( env );
1614 return K32WOWGlobalLock16( handle );