dplayx: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / krnl386.exe16 / task.c
blob54d7a4c64ee7fc356455f5e10c8f2922c51a6498
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 <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winnt.h"
30 #include "wownt32.h"
31 #include "winuser.h"
33 #include "wine/winbase16.h"
34 #include "winternl.h"
35 #include "kernel16_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(task);
41 #include "pshpack1.h"
43 struct thunk
45 BYTE movw;
46 HANDLE16 instance;
47 BYTE ljmp;
48 FARPROC16 func;
51 /* Segment containing MakeProcInstance() thunks */
52 typedef struct
54 WORD next; /* Selector of next segment */
55 WORD magic; /* Thunks signature */
56 WORD unused;
57 WORD free; /* Head of the free list */
58 struct thunk thunks[1];
59 } THUNKS;
61 #include "poppack.h"
63 #define THUNK_MAGIC ('P' | ('T' << 8))
65 /* Min. number of thunks allocated when creating a new segment */
66 #define MIN_THUNKS 32
68 #define TDB_MAGIC ('T' | ('D' << 8))
70 static THHOOK DefaultThhook;
71 THHOOK *pThhook = &DefaultThhook;
73 #define hFirstTask (pThhook->HeadTDB)
74 #define hLockedTask (pThhook->LockTDB)
76 static UINT16 nTaskCount = 0;
78 static HTASK16 initial_task, main_task;
80 /***********************************************************************
81 * TASK_InstallTHHook
83 void TASK_InstallTHHook( THHOOK *pNewThhook )
85 THHOOK *pOldThhook = pThhook;
87 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
89 *pThhook = *pOldThhook;
92 /***********************************************************************
93 * TASK_GetPtr
95 static TDB *TASK_GetPtr( HTASK16 hTask )
97 return GlobalLock16( hTask );
101 /***********************************************************************
102 * TASK_GetCurrent
104 TDB *TASK_GetCurrent(void)
106 return TASK_GetPtr( GetCurrentTask() );
110 /***********************************************************************
111 * TASK_LinkTask
113 static void TASK_LinkTask( HTASK16 hTask )
115 HTASK16 *prevTask;
116 TDB *pTask;
118 if (!(pTask = TASK_GetPtr( hTask ))) return;
119 prevTask = &hFirstTask;
120 while (*prevTask)
122 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
123 if (prevTaskPtr->priority >= pTask->priority) break;
124 prevTask = &prevTaskPtr->hNext;
126 pTask->hNext = *prevTask;
127 *prevTask = hTask;
128 nTaskCount++;
132 /***********************************************************************
133 * TASK_UnlinkTask
135 static void TASK_UnlinkTask( HTASK16 hTask )
137 HTASK16 *prevTask;
138 TDB *pTask;
140 prevTask = &hFirstTask;
141 while (*prevTask && (*prevTask != hTask))
143 pTask = TASK_GetPtr( *prevTask );
144 prevTask = &pTask->hNext;
146 if (*prevTask)
148 pTask = TASK_GetPtr( *prevTask );
149 *prevTask = pTask->hNext;
150 pTask->hNext = 0;
151 nTaskCount--;
156 /***********************************************************************
157 * TASK_CreateThunks
159 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
160 * and containing 'count' entries.
162 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
164 int i;
165 THUNKS *pThunk;
167 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
168 pThunk->next = 0;
169 pThunk->magic = THUNK_MAGIC;
170 pThunk->free = FIELD_OFFSET( THUNKS, thunks );
171 for (i = 0; i < count-1; i++)
172 *(WORD *)&pThunk->thunks[i] = FIELD_OFFSET( THUNKS, thunks[i+1] );
173 *(WORD *)&pThunk->thunks[i] = 0; /* Last thunk */
177 /***********************************************************************
178 * TASK_AllocThunk
180 * Allocate a thunk for MakeProcInstance().
182 static SEGPTR TASK_AllocThunk(void)
184 TDB *pTask;
185 THUNKS *pThunk;
186 WORD sel, base;
188 if (!(pTask = TASK_GetCurrent())) return 0;
189 sel = pTask->hCSAlias;
190 pThunk = (THUNKS *)pTask->thunks;
191 base = (char *)pThunk - (char *)pTask;
192 while (!pThunk->free)
194 sel = pThunk->next;
195 if (!sel) /* Allocate a new segment */
197 sel = GLOBAL_Alloc( GMEM_FIXED, FIELD_OFFSET( THUNKS, thunks[MIN_THUNKS] ),
198 pTask->hPDB, LDT_FLAGS_CODE );
199 if (!sel) return 0;
200 TASK_CreateThunks( sel, 0, MIN_THUNKS );
201 pThunk->next = sel;
203 pThunk = GlobalLock16( sel );
204 base = 0;
206 base += pThunk->free;
207 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
208 return MAKESEGPTR( sel, base );
212 /***********************************************************************
213 * TASK_FreeThunk
215 * Free a MakeProcInstance() thunk.
217 static BOOL TASK_FreeThunk( SEGPTR thunk )
219 TDB *pTask;
220 THUNKS *pThunk;
221 WORD sel, base;
223 if (!(pTask = TASK_GetCurrent())) return FALSE;
224 sel = pTask->hCSAlias;
225 pThunk = (THUNKS *)pTask->thunks;
226 base = (char *)pThunk - (char *)pTask;
227 while (sel && (sel != HIWORD(thunk)))
229 sel = pThunk->next;
230 pThunk = GlobalLock16( sel );
231 base = 0;
233 if (!sel) return FALSE;
234 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
235 pThunk->free = LOWORD(thunk) - base;
236 return TRUE;
240 /***********************************************************************
241 * TASK_Create
243 * NOTE: This routine might be called by a Win32 thread. Thus, we need
244 * to be careful to protect global data structures. We do this
245 * by entering the Win16Lock while linking the task into the
246 * global task list.
248 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, LPCSTR cmdline, BYTE len )
250 HTASK16 hTask;
251 TDB *pTask;
252 FARPROC16 proc;
253 char curdir[MAX_PATH];
254 HMODULE16 hModule = pModule ? pModule->self : 0;
256 /* Allocate the task structure */
258 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
259 if (!hTask) return NULL;
260 pTask = TASK_GetPtr( hTask );
261 FarSetOwner16( hTask, hModule );
263 /* Fill the task structure */
265 pTask->hSelf = hTask;
267 pTask->version = pModule ? pModule->ne_expver : 0x0400;
268 pTask->hModule = hModule;
269 pTask->hParent = GetCurrentTask();
270 pTask->magic = TDB_MAGIC;
271 pTask->nCmdShow = cmdShow;
273 GetCurrentDirectoryA( sizeof(curdir), curdir );
274 GetShortPathNameA( curdir, curdir, sizeof(curdir) );
275 pTask->curdrive = (curdir[0] - 'A') | 0x80;
276 lstrcpynA( pTask->curdir, curdir + 2, sizeof(pTask->curdir) );
278 /* Create the thunks block */
280 TASK_CreateThunks( hTask, (char *)pTask->thunks - (char *)pTask, 7 );
282 /* Copy the module name */
284 if (hModule)
286 char name[sizeof(pTask->module_name)+1];
287 size_t len;
288 GetModuleName16( hModule, name, sizeof(name) );
289 len = strlen(name) + 1;
290 memcpy(pTask->module_name, name, min(len,sizeof(pTask->module_name)));
291 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
294 /* Allocate a selector for the PDB */
296 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
297 hModule, LDT_FLAGS_DATA );
299 /* Fill the PDB */
301 pTask->pdb.int20 = 0x20cd;
302 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
303 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
304 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
305 pTask->pdb.savedint22 = 0;
306 pTask->pdb.savedint23 = 0;
307 pTask->pdb.savedint24 = 0;
308 pTask->pdb.fileHandlesPtr =
309 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), FIELD_OFFSET( PDB16, fileHandles ));
310 pTask->pdb.hFileHandles = 0;
311 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
312 /* FIXME: should we make a copy of the environment? */
313 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
314 pTask->pdb.nbFiles = 20;
316 /* Fill the command line */
318 if (!cmdline)
320 cmdline = GetCommandLineA();
321 /* remove the first word (program name) */
322 if (*cmdline == '"')
323 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
324 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
325 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
326 len = strlen(cmdline);
328 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
329 pTask->pdb.cmdLine[0] = len;
330 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
331 /* pTask->pdb.cmdLine[len+1] = 0; */
333 TRACE("cmdline='%.*s' task=%04x\n", len, cmdline, hTask );
335 /* Allocate a code segment alias for the TDB */
337 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, pTask, sizeof(TDB),
338 pTask->hPDB, LDT_FLAGS_CODE );
340 /* Default DTA overwrites command line */
342 pTask->dta = MAKESEGPTR( pTask->hPDB, FIELD_OFFSET( PDB16, cmdLine ));
344 /* Create scheduler event for 16-bit tasks */
346 if ( !(pTask->flags & TDBF_WIN32) )
347 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, NotificationEvent, FALSE );
349 if (!initial_task) initial_task = hTask;
351 return pTask;
355 /***********************************************************************
356 * TASK_DeleteTask
358 static void TASK_DeleteTask( HTASK16 hTask )
360 TDB *pTask;
361 HGLOBAL16 hPDB;
363 if (!(pTask = TASK_GetPtr( hTask ))) return;
364 hPDB = pTask->hPDB;
366 pTask->magic = 0xdead; /* invalidate signature */
368 /* Free the selector aliases */
370 GLOBAL_FreeBlock( pTask->hCSAlias );
371 GLOBAL_FreeBlock( pTask->hPDB );
373 /* Free the task module */
375 FreeModule16( pTask->hModule );
377 /* Free the task structure itself */
379 GlobalFree16( hTask );
381 /* Free all memory used by this task (including the 32-bit stack, */
382 /* the environment block and the thunk segments). */
384 GlobalFreeAll16( hPDB );
388 /***********************************************************************
389 * TASK_CreateMainTask
391 * Create a task for the main (32-bit) process.
393 void TASK_CreateMainTask(void)
395 TDB *pTask;
396 STARTUPINFOA startup_info;
397 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
399 GetStartupInfoA( &startup_info );
400 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
401 pTask = TASK_Create( NULL, cmdShow, NULL, 0 );
402 if (!pTask)
404 ERR("could not create task for main process\n");
405 ExitProcess(1);
408 pTask->flags |= TDBF_WIN32;
409 pTask->hInstance = 0;
410 pTask->hPrevInstance = 0;
411 pTask->teb = NtCurrentTeb();
413 /* Add the task to the linked list */
414 /* (no need to get the win16 lock, we are the only thread at this point) */
415 TASK_LinkTask( pTask->hSelf );
416 main_task = pTask->hSelf;
420 struct create_data
422 TDB *task;
423 WIN16_SUBSYSTEM_TIB *tib;
426 /* allocate the win16 TIB for a new 16-bit task */
427 static WIN16_SUBSYSTEM_TIB *allocate_win16_tib( TDB *pTask )
429 WCHAR path[MAX_PATH];
430 WIN16_SUBSYSTEM_TIB *tib;
431 UNICODE_STRING *curdir;
432 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
434 if (!(tib = HeapAlloc( GetProcessHeap(), 0, sizeof(*tib) ))) return NULL;
435 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
436 GetLongPathNameW( path, path, MAX_PATH );
437 if (RtlCreateUnicodeString( &tib->exe_str, path )) tib->exe_name = &tib->exe_str;
438 else tib->exe_name = NULL;
440 RtlAcquirePebLock();
441 if (NtCurrentTeb()->Tib.SubSystemTib)
442 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
443 else
444 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
445 tib->curdir.DosPath.MaximumLength = sizeof(tib->curdir_buffer);
446 tib->curdir.DosPath.Length = min( curdir->Length, tib->curdir.DosPath.MaximumLength-sizeof(WCHAR) );
447 tib->curdir.DosPath.Buffer = tib->curdir_buffer;
448 tib->curdir.Handle = 0;
449 memcpy( tib->curdir_buffer, curdir->Buffer, tib->curdir.DosPath.Length );
450 tib->curdir_buffer[tib->curdir.DosPath.Length/sizeof(WCHAR)] = 0;
451 RtlReleasePebLock();
452 return tib;
455 static inline void free_win16_tib( WIN16_SUBSYSTEM_TIB *tib )
457 if (tib->exe_name) RtlFreeUnicodeString( tib->exe_name );
458 HeapFree( GetProcessHeap(), 0, tib );
461 /* startup routine for a new 16-bit thread */
462 static DWORD CALLBACK task_start( LPVOID p )
464 struct create_data *data = p;
465 TDB *pTask = data->task;
466 DWORD ret;
468 kernel_get_thread_data()->htask16 = pTask->hSelf;
469 NtCurrentTeb()->Tib.SubSystemTib = data->tib;
470 HeapFree( GetProcessHeap(), 0, data );
472 _EnterWin16Lock();
473 TASK_LinkTask( pTask->hSelf );
474 pTask->teb = NtCurrentTeb();
475 ret = NE_StartTask();
476 _LeaveWin16Lock();
477 return ret;
481 /***********************************************************************
482 * TASK_SpawnTask
484 * Spawn a new 16-bit task.
486 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
487 LPCSTR cmdline, BYTE len, HANDLE *hThread )
489 struct create_data *data = NULL;
490 WIN16_SUBSYSTEM_TIB *tib;
491 TDB *pTask;
493 if (!(pTask = TASK_Create( pModule, cmdShow, cmdline, len ))) return 0;
494 if (!(tib = allocate_win16_tib( pTask ))) goto failed;
495 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data)))) goto failed;
496 data->task = pTask;
497 data->tib = tib;
498 if (!(*hThread = CreateThread( NULL, 0, task_start, data, 0, NULL ))) goto failed;
499 return pTask->hSelf;
501 failed:
502 if (tib) free_win16_tib( tib );
503 HeapFree( GetProcessHeap(), 0, data );
504 TASK_DeleteTask( pTask->hSelf );
505 return 0;
509 /***********************************************************************
510 * TASK_GetTaskFromThread
512 HTASK16 TASK_GetTaskFromThread( DWORD thread )
514 TDB *p = TASK_GetPtr( hFirstTask );
515 while (p)
517 if (p->teb->ClientId.UniqueThread == (HANDLE)thread) return p->hSelf;
518 p = TASK_GetPtr( p->hNext );
520 return 0;
524 /***********************************************************************
525 * TASK_CallTaskSignalProc
527 static void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
529 WORD args[5];
530 TDB *pTask = TASK_GetCurrent();
532 if ( !pTask || !pTask->userhandler ) return;
534 args[4] = hTaskOrModule;
535 args[3] = uCode;
536 args[2] = 0;
537 args[1] = pTask->hInstance;
538 args[0] = pTask->hQueue;
539 WOWCallback16Ex( (DWORD)pTask->userhandler, WCB16_PASCAL, sizeof(args), args, NULL );
543 /***********************************************************************
544 * TASK_ExitTask
546 void TASK_ExitTask(void)
548 WIN16_SUBSYSTEM_TIB *tib;
549 TDB *pTask;
550 DWORD lockCount;
552 /* Enter the Win16Lock to protect global data structures */
553 _EnterWin16Lock();
555 pTask = TASK_GetCurrent();
556 if ( !pTask )
558 _LeaveWin16Lock();
559 return;
562 TRACE("Killing task %04x\n", pTask->hSelf );
564 /* Perform USER cleanup */
566 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
568 /* Remove the task from the list to be sure we never switch back to it */
569 TASK_UnlinkTask( pTask->hSelf );
571 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
573 TRACE("this is the last task, exiting\n" );
574 ExitKernel16();
577 pTask->nEvents = 0;
579 if ( hLockedTask == pTask->hSelf )
580 hLockedTask = 0;
582 TASK_DeleteTask( pTask->hSelf );
584 if ((tib = NtCurrentTeb()->Tib.SubSystemTib))
586 free_win16_tib( tib );
587 NtCurrentTeb()->Tib.SubSystemTib = NULL;
590 /* ... and completely release the Win16Lock, just in case. */
591 ReleaseThunkLock( &lockCount );
595 /***********************************************************************
596 * ExitKernel (KERNEL.2)
598 * Clean-up everything and exit the Wine process.
600 void WINAPI ExitKernel16(void)
602 WriteOutProfiles16();
603 TerminateProcess( GetCurrentProcess(), 0 );
607 /***********************************************************************
608 * InitTask (KERNEL.91)
610 * Called by the application startup code.
612 void WINAPI InitTask16( CONTEXT *context )
614 TDB *pTask;
615 INSTANCEDATA *pinstance;
616 SEGPTR ptr;
618 context->Eax = 0;
619 if (!(pTask = TASK_GetCurrent())) return;
621 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
622 as some apps, notably Visual Basic apps, *modify* the heap/stack
623 size of the instance data segment before calling InitTask() */
625 /* Initialize the INSTANCEDATA structure */
626 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
627 pinstance->stackmin = CURRENT_SP + sizeof( STACK16FRAME );
628 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
629 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
630 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
632 /* Initialize the local heap */
633 if (LOWORD(context->Ecx))
634 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
636 /* Initialize implicitly loaded DLLs */
637 NE_InitializeDLLs( pTask->hModule );
638 NE_DllProcessAttach( pTask->hModule );
640 /* Registers on return are:
641 * ax 1 if OK, 0 on error
642 * cx stack limit in bytes
643 * dx cmdShow parameter
644 * si instance handle of the previous instance
645 * di instance handle of the new task
646 * es:bx pointer to command line inside PSP
648 * 0 (=%bp) is pushed on the stack
650 ptr = stack16_push( sizeof(WORD) );
651 *(WORD *)MapSL(ptr) = 0;
652 context->Esp -= 2;
654 context->Eax = 1;
656 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
657 else
659 LPBYTE p = &pTask->pdb.cmdLine[1];
660 while ((*p == ' ') || (*p == '\t')) p++;
661 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
663 context->Ecx = pinstance->stacktop;
664 context->Edx = pTask->nCmdShow;
665 context->Esi = (DWORD)pTask->hPrevInstance;
666 context->Edi = (DWORD)pTask->hInstance;
667 context->SegEs = (WORD)pTask->hPDB;
671 /***********************************************************************
672 * WaitEvent (KERNEL.30)
674 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
676 TDB *pTask;
678 if (!hTask) hTask = GetCurrentTask();
679 pTask = TASK_GetPtr( hTask );
681 if (pTask->flags & TDBF_WIN32)
683 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
684 return TRUE;
687 if (pTask->nEvents > 0)
689 pTask->nEvents--;
690 return FALSE;
693 if (pTask->teb == NtCurrentTeb())
695 DWORD lockCount;
697 NtResetEvent( pTask->hEvent, NULL );
698 ReleaseThunkLock( &lockCount );
699 SYSLEVEL_CheckNotLevel( 1 );
700 WaitForSingleObject( pTask->hEvent, INFINITE );
701 RestoreThunkLock( lockCount );
702 if (pTask->nEvents > 0) pTask->nEvents--;
704 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
706 return TRUE;
710 /***********************************************************************
711 * PostEvent (KERNEL.31)
713 void WINAPI PostEvent16( HTASK16 hTask )
715 TDB *pTask;
717 if (!hTask) hTask = GetCurrentTask();
718 if (!(pTask = TASK_GetPtr( hTask ))) return;
720 if (pTask->flags & TDBF_WIN32)
722 FIXME("called for Win32 thread (%04lx)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
723 return;
726 pTask->nEvents++;
728 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
732 /***********************************************************************
733 * SetPriority (KERNEL.32)
735 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
737 TDB *pTask;
738 INT16 newpriority;
740 if (!hTask) hTask = GetCurrentTask();
741 if (!(pTask = TASK_GetPtr( hTask ))) return;
742 newpriority = pTask->priority + delta;
743 if (newpriority < -32) newpriority = -32;
744 else if (newpriority > 15) newpriority = 15;
746 pTask->priority = newpriority + 1;
747 TASK_UnlinkTask( pTask->hSelf );
748 TASK_LinkTask( pTask->hSelf );
749 pTask->priority--;
753 /***********************************************************************
754 * LockCurrentTask (KERNEL.33)
756 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
758 if (bLock) hLockedTask = GetCurrentTask();
759 else hLockedTask = 0;
760 return hLockedTask;
764 /***********************************************************************
765 * IsTaskLocked (KERNEL.122)
767 HTASK16 WINAPI IsTaskLocked16(void)
769 return hLockedTask;
773 /***********************************************************************
774 * OldYield (KERNEL.117)
776 void WINAPI OldYield16(void)
778 DWORD count;
780 ReleaseThunkLock(&count);
781 RestoreThunkLock(count);
784 /***********************************************************************
785 * WIN32_OldYield (KERNEL.447)
787 void WINAPI WIN32_OldYield16(void)
789 DWORD count;
791 ReleaseThunkLock(&count);
792 RestoreThunkLock(count);
795 /***********************************************************************
796 * DirectedYield (KERNEL.150)
798 void WINAPI DirectedYield16( HTASK16 hTask )
800 OldYield16();
803 /***********************************************************************
804 * Yield (KERNEL.29)
806 void WINAPI Yield16(void)
808 TDB *pCurTask = TASK_GetCurrent();
810 if (pCurTask && pCurTask->hQueue)
812 HMODULE mod = GetModuleHandleA( "user32.dll" );
813 if (mod)
815 BOOL (WINAPI *pPeekMessageW)( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags );
816 pPeekMessageW = (void *)GetProcAddress( mod, "PeekMessageW" );
817 if (pPeekMessageW)
819 MSG msg;
820 pPeekMessageW( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
821 return;
825 OldYield16();
828 /***********************************************************************
829 * KERNEL_490 (KERNEL.490)
831 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
833 if ( !someTask ) return 0;
835 FIXME("(%04x): stub\n", someTask );
836 return 0;
839 /***********************************************************************
840 * MakeProcInstance (KERNEL.51)
842 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
844 struct thunk *thunk;
845 BYTE *lfunc;
846 SEGPTR thunkaddr;
847 WORD hInstanceSelector;
849 hInstanceSelector = GlobalHandleToSel16(hInstance);
851 TRACE("(%p, %04x);\n", func, hInstance);
853 if (!HIWORD(func)) {
854 /* Win95 actually protects via SEH, but this is better for debugging */
855 WARN("Ouch ! Called with invalid func %p !\n", func);
856 return NULL;
859 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
860 && (hInstance != 0)
861 && (hInstance != 0xffff) )
863 /* calling MPI with a foreign DSEG is invalid ! */
864 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
865 hInstance,CURRENT_DS);
868 /* Always use the DSEG that MPI was entered with.
869 * We used to set hInstance to GetTaskDS16(), but this should be wrong
870 * as CURRENT_DS provides the DSEG value we need.
871 * ("calling" DS, *not* "task" DS !) */
872 hInstanceSelector = CURRENT_DS;
873 hInstance = GlobalHandle16(hInstanceSelector);
875 /* no thunking for DLLs */
876 if (NE_GetPtr(FarGetOwner16(hInstance))->ne_flags & NE_FFLAGS_LIBMODULE)
877 return func;
879 thunkaddr = TASK_AllocThunk();
880 if (!thunkaddr) return NULL;
881 thunk = MapSL( thunkaddr );
882 lfunc = MapSL( (SEGPTR)func );
884 TRACE("(%p,%04x): got thunk %08lx\n", func, hInstance, thunkaddr );
885 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
886 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
888 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
891 thunk->movw = 0xb8; /* movw instance, %ax */
892 thunk->instance = hInstanceSelector;
893 thunk->ljmp = 0xea; /* ljmp func */
894 thunk->func = func;
895 return (FARPROC16)thunkaddr;
896 /* CX reg indicates if thunkaddr != NULL, implement if needed */
900 /***********************************************************************
901 * FreeProcInstance (KERNEL.52)
903 void WINAPI FreeProcInstance16( FARPROC16 func )
905 TRACE("(%p)\n", func );
906 TASK_FreeThunk( (SEGPTR)func );
909 /**********************************************************************
910 * TASK_GetCodeSegment
912 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
913 * and logical segment number of a given code segment.
915 * 'proc' either *is* already a pair of module handle and segment number,
916 * in which case there's nothing to do. Otherwise, it is a pointer to
917 * a function, and we need to retrieve the code segment. If the pointer
918 * happens to point to a thunk, we'll retrieve info about the code segment
919 * where the function pointed to by the thunk resides, not the thunk itself.
921 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
922 * the function the snoop code will return to ...
925 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
926 SEGTABLEENTRY **ppSeg, int *pSegNr )
928 NE_MODULE *pModule = NULL;
929 SEGTABLEENTRY *pSeg = NULL;
930 int segNr=0;
932 /* Try pair of module handle / segment number */
933 pModule = GlobalLock16( HIWORD( proc ) );
934 if ( pModule && pModule->ne_magic == IMAGE_OS2_SIGNATURE )
936 segNr = LOWORD( proc );
937 if ( segNr && segNr <= pModule->ne_cseg )
938 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
941 /* Try thunk or function */
942 else
944 BYTE *thunk = MapSL( (SEGPTR)proc );
945 WORD selector;
947 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
948 selector = thunk[6] + (thunk[7] << 8);
949 else
950 selector = HIWORD( proc );
952 pModule = NE_GetPtr( GlobalHandle16( selector ) );
953 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
955 if ( pModule )
956 for ( segNr = 1; segNr <= pModule->ne_cseg; segNr++, pSeg++ )
957 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
958 break;
960 if ( pModule && segNr > pModule->ne_cseg )
961 pSeg = NULL;
964 /* Abort if segment not found */
966 if ( !pModule || !pSeg )
967 return FALSE;
969 /* Return segment data */
971 if ( ppModule ) *ppModule = pModule;
972 if ( ppSeg ) *ppSeg = pSeg;
973 if ( pSegNr ) *pSegNr = segNr;
975 return TRUE;
978 /**********************************************************************
979 * GetCodeHandle (KERNEL.93)
981 DWORD WINAPI GetCodeHandle16( FARPROC16 proc )
983 SEGTABLEENTRY *pSeg;
985 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
986 return 0;
988 return MAKELONG( pSeg->hSeg, GlobalHandleToSel16(pSeg->hSeg) );
991 /**********************************************************************
992 * GetCodeInfo (KERNEL.104)
994 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
996 NE_MODULE *pModule;
997 SEGTABLEENTRY *pSeg;
998 int segNr;
1000 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1001 return FALSE;
1003 /* Fill in segment information */
1005 segInfo->offSegment = pSeg->filepos;
1006 segInfo->cbSegment = pSeg->size;
1007 segInfo->flags = pSeg->flags;
1008 segInfo->cbAlloc = pSeg->minsize;
1009 segInfo->h = pSeg->hSeg;
1010 segInfo->alignShift = pModule->ne_align;
1012 if ( segNr == pModule->ne_autodata )
1013 segInfo->cbAlloc += pModule->ne_heap + pModule->ne_stack;
1015 /* Return module handle in %es */
1017 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1019 return TRUE;
1023 /**********************************************************************
1024 * DefineHandleTable (KERNEL.94)
1026 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1028 FIXME("(%04x): stub ?\n", wOffset);
1029 return TRUE;
1033 /***********************************************************************
1034 * SetTaskQueue (KERNEL.34)
1036 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1038 FIXME( "stub, should not get called\n" );
1039 return 0xbeef;
1043 /***********************************************************************
1044 * GetTaskQueue (KERNEL.35)
1046 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1048 FIXME( "stub, should not get called\n" );
1049 return 0xbeef;
1052 /***********************************************************************
1053 * SetThreadQueue (KERNEL.463)
1055 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1057 FIXME( "stub, should not get called\n" );
1058 return 0xbeef;
1061 /***********************************************************************
1062 * GetThreadQueue (KERNEL.464)
1064 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1066 FIXME( "stub, should not get called\n" );
1067 return 0xbeef;
1070 /***********************************************************************
1071 * SetFastQueue (KERNEL.624)
1073 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1075 FIXME( "stub, should not get called\n" );
1078 /***********************************************************************
1079 * GetFastQueue (KERNEL.625)
1081 HQUEUE16 WINAPI GetFastQueue16( void )
1083 FIXME( "stub, should not get called\n" );
1084 return 0xbeef;
1087 /***********************************************************************
1088 * SwitchStackTo (KERNEL.108)
1090 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1092 STACK16FRAME *oldFrame, *newFrame;
1093 INSTANCEDATA *pData;
1094 UINT16 copySize;
1096 if (!(pData = GlobalLock16( seg ))) return;
1097 TRACE( "old=%04x:%04x new=%04x:%04x\n", CURRENT_SS, CURRENT_SP, seg, ptr );
1099 /* Save the old stack */
1101 oldFrame = CURRENT_STACK16;
1102 /* pop frame + args and push bp */
1103 pData->old_ss = CURRENT_SS;
1104 pData->old_sp = CURRENT_SP + sizeof(STACK16FRAME) + 2 * sizeof(WORD);
1105 *(WORD *)MapSL(MAKESEGPTR(pData->old_ss, pData->old_sp)) = oldFrame->bp;
1106 pData->stacktop = top;
1107 pData->stackmin = ptr;
1108 pData->stackbottom = ptr;
1110 /* Switch to the new stack */
1112 /* Note: we need to take the 3 arguments into account; otherwise,
1113 * the stack will underflow upon return from this function.
1115 copySize = oldFrame->bp - pData->old_sp;
1116 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1117 CURRENT_SS = seg;
1118 CURRENT_SP = ptr - copySize;
1119 newFrame = CURRENT_STACK16;
1121 /* Copy the stack frame and the local variables to the new stack */
1123 memmove( newFrame, oldFrame, copySize );
1124 newFrame->bp = ptr;
1125 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1129 /***********************************************************************
1130 * SwitchStackBack (KERNEL.109)
1132 void WINAPI SwitchStackBack16( CONTEXT *context )
1134 STACK16FRAME *oldFrame, *newFrame;
1135 INSTANCEDATA *pData;
1137 if (!(pData = GlobalLock16(CURRENT_SS)))
1138 return;
1139 if (!pData->old_ss)
1141 WARN("No previous SwitchStackTo\n" );
1142 return;
1144 TRACE( "restoring stack %04x:%04x\n", pData->old_ss, pData->old_sp );
1146 oldFrame = CURRENT_STACK16;
1148 /* Pop bp from the previous stack */
1150 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(MAKESEGPTR(pData->old_ss, pData->old_sp));
1151 pData->old_sp += sizeof(WORD);
1153 /* Switch back to the old stack */
1155 CURRENT_SS = pData->old_ss;
1156 CURRENT_SP = pData->old_sp - sizeof(STACK16FRAME);
1157 context->SegSs = pData->old_ss;
1158 context->Esp = pData->old_sp - sizeof(DWORD); /*ret addr*/
1159 pData->old_ss = pData->old_sp = 0;
1161 /* Build a stack frame for the return */
1163 newFrame = CURRENT_STACK16;
1164 newFrame->frame32 = oldFrame->frame32;
1165 newFrame->module_cs = oldFrame->module_cs;
1166 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1167 newFrame->entry_ip = oldFrame->entry_ip;
1171 /***********************************************************************
1172 * GetTaskQueueDS (KERNEL.118)
1174 void WINAPI GetTaskQueueDS16(void)
1176 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1180 /***********************************************************************
1181 * GetTaskQueueES (KERNEL.119)
1183 void WINAPI GetTaskQueueES16(void)
1185 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1189 /***********************************************************************
1190 * GetCurrentTask (KERNEL32.@)
1192 HTASK16 WINAPI GetCurrentTask(void)
1194 HTASK16 ret = kernel_get_thread_data()->htask16;
1195 if (!ret) ret = main_task;
1196 return ret;
1199 /***********************************************************************
1200 * GetCurrentTask (KERNEL.36)
1202 DWORD WINAPI WIN16_GetCurrentTask(void)
1204 /* This is the version used by relay code; the first task is */
1205 /* returned in the high word of the result */
1206 return MAKELONG( GetCurrentTask(), hFirstTask );
1210 /***********************************************************************
1211 * GetCurrentPDB (KERNEL.37)
1213 * UNDOC: returns PSP of KERNEL in high word
1215 DWORD WINAPI GetCurrentPDB16(void)
1217 TDB *pTask;
1219 if (!(pTask = TASK_GetCurrent())) return 0;
1220 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1224 /***********************************************************************
1225 * GetCurPID (KERNEL.157)
1227 DWORD WINAPI GetCurPID16( DWORD unused )
1229 return 0;
1233 /***********************************************************************
1234 * GetInstanceData (KERNEL.54)
1236 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1238 char *ptr = GlobalLock16( instance );
1239 if (!ptr || !len) return 0;
1240 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1241 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1242 return len;
1246 /***********************************************************************
1247 * GetExeVersion (KERNEL.105)
1249 WORD WINAPI GetExeVersion16(void)
1251 TDB *pTask;
1253 if (!(pTask = TASK_GetCurrent())) return 0;
1254 return pTask->version;
1258 /***********************************************************************
1259 * GetLPErrMode (KERNEL.99)
1261 SEGPTR WINAPI GetLPErrMode(void)
1263 FIXME("(): stub\n");
1264 return 0;
1268 /***********************************************************************
1269 * SetErrorMode (KERNEL.107)
1271 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1273 TDB *pTask;
1274 if (!(pTask = TASK_GetCurrent())) return 0;
1275 pTask->error_mode = mode;
1276 return SetErrorMode( mode );
1280 /***********************************************************************
1281 * GetNumTasks (KERNEL.152)
1283 UINT16 WINAPI GetNumTasks16(void)
1285 return nTaskCount;
1289 /***********************************************************************
1290 * GetTaskDS (KERNEL.155)
1292 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1293 * I don't think we need to bother with this.
1295 HINSTANCE16 WINAPI GetTaskDS16(void)
1297 TDB *pTask;
1299 if (!(pTask = TASK_GetCurrent())) return 0;
1300 return GlobalHandleToSel16(pTask->hInstance);
1303 /***********************************************************************
1304 * GetDummyModuleHandleDS (KERNEL.602)
1306 WORD WINAPI GetDummyModuleHandleDS16(void)
1308 TDB *pTask;
1309 WORD selector;
1311 if (!(pTask = TASK_GetCurrent())) return 0;
1312 if (!(pTask->flags & TDBF_WIN32)) return 0;
1313 selector = GlobalHandleToSel16( pTask->hModule );
1314 CURRENT_DS = selector;
1315 return selector;
1318 /***********************************************************************
1319 * IsTask (KERNEL.320)
1321 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1323 TDB *pTask;
1325 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1326 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1327 return (pTask->magic == TDB_MAGIC);
1331 /***********************************************************************
1332 * IsWinOldApTask (KERNEL.158)
1334 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1336 /* should return bit 0 of byte 0x48 in PSP */
1337 return FALSE;
1340 /***********************************************************************
1341 * SetTaskSignalProc (KERNEL.38)
1343 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1345 TDB *pTask;
1346 FARPROC16 oldProc;
1348 if (!hTask) hTask = GetCurrentTask();
1349 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1350 oldProc = pTask->userhandler;
1351 pTask->userhandler = proc;
1352 return oldProc;
1355 /***********************************************************************
1356 * SetSigHandler (KERNEL.140)
1358 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1359 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1361 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1362 newhandler,oldhandler,oldmode,newmode,flag );
1364 if (flag != 1) return 0;
1365 if (!newmode) newhandler = NULL; /* Default handler */
1366 if (newmode != 4)
1368 TDB *pTask;
1370 if (!(pTask = TASK_GetCurrent())) return 0;
1371 if (oldmode) *oldmode = pTask->signal_flags;
1372 pTask->signal_flags = newmode;
1373 if (oldhandler) *oldhandler = pTask->sighandler;
1374 pTask->sighandler = newhandler;
1376 return 0;
1380 /***********************************************************************
1381 * GlobalNotify (KERNEL.154)
1383 * Note that GlobalNotify does _not_ return the old NotifyProc
1384 * -- contrary to LocalNotify !!
1386 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1388 TDB *pTask;
1390 if (!(pTask = TASK_GetCurrent())) return;
1391 pTask->discardhandler = proc;
1395 /***********************************************************************
1396 * GetExePtrHelper
1398 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1400 char *ptr;
1401 HANDLE16 owner;
1403 /* Check for module handle */
1405 if (!(ptr = GlobalLock16( handle ))) return 0;
1406 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return handle;
1408 /* Search for this handle inside all tasks */
1410 *hTask = hFirstTask;
1411 while (*hTask)
1413 TDB *pTask = TASK_GetPtr( *hTask );
1414 if ((*hTask == handle) ||
1415 (pTask->hInstance == handle) ||
1416 (pTask->hQueue == handle) ||
1417 (pTask->hPDB == handle)) return pTask->hModule;
1418 *hTask = pTask->hNext;
1421 /* Check the owner for module handle */
1423 owner = FarGetOwner16( handle );
1424 if (!(ptr = GlobalLock16( owner ))) return 0;
1425 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return owner;
1427 /* Search for the owner inside all tasks */
1429 *hTask = hFirstTask;
1430 while (*hTask)
1432 TDB *pTask = TASK_GetPtr( *hTask );
1433 if ((*hTask == owner) ||
1434 (pTask->hInstance == owner) ||
1435 (pTask->hQueue == owner) ||
1436 (pTask->hPDB == owner)) return pTask->hModule;
1437 *hTask = pTask->hNext;
1440 return 0;
1443 /***********************************************************************
1444 * GetExePtr (KERNEL.133)
1446 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1448 HTASK16 hTask = 0;
1449 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1450 STACK16FRAME *frame = CURRENT_STACK16;
1451 frame->ecx = hModule;
1452 if (hTask) frame->es = hTask;
1453 return hModule;
1457 /***********************************************************************
1458 * K228 (KERNEL.228)
1460 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1462 HTASK16 hTask = 0;
1463 return GetExePtrHelper( handle, &hTask );
1467 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1469 /**************************************************************************
1470 * FatalAppExit (KERNEL.137)
1472 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1474 TDB *pTask = TASK_GetCurrent();
1476 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1478 HMODULE mod = GetModuleHandleA( "user32.dll" );
1479 if (mod)
1481 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1482 if (pMessageBoxA)
1484 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1485 goto done;
1488 ERR( "%s\n", debugstr_a(str) );
1490 done:
1491 ExitThread(0xff);
1495 /***********************************************************************
1496 * GetAppCompatFlags (KERNEL.354)
1498 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1500 TDB *pTask;
1502 if (!hTask) hTask = GetCurrentTask();
1503 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1504 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1505 return pTask->compat_flags;
1509 /***********************************************************************
1510 * GetDOSEnvironment (KERNEL.131)
1512 * Note: the environment is allocated once, it doesn't track changes
1513 * made using the Win32 API. This shouldn't matter.
1515 * Format of a 16-bit environment block:
1516 * ASCIIZ string 1 (xx=yy format)
1517 * ...
1518 * ASCIIZ string n
1519 * BYTE 0
1520 * WORD 1
1521 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1523 SEGPTR WINAPI GetDOSEnvironment16(void)
1525 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1526 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1528 if (!handle)
1530 DWORD size;
1531 LPSTR p, env;
1533 p = env = GetEnvironmentStringsA();
1534 while (*p) p += strlen(p) + 1;
1535 p++; /* skip last null */
1536 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1537 handle = GlobalAlloc16( GMEM_FIXED, size );
1538 if (handle)
1540 WORD one = 1;
1541 LPSTR env16 = GlobalLock16( handle );
1542 memcpy( env16, env, p - env );
1543 memcpy( env16 + (p - env), &one, sizeof(one));
1544 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1545 GlobalUnlock16( handle );
1547 FreeEnvironmentStringsA( env );
1549 return WOWGlobalLock16( handle );