include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / krnl386.exe16 / task.c
blob534b912e884180123fdced87dd7ceb643fe5b7af
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 pTask = TASK_GetCurrent();
553 if (!pTask) return;
555 TRACE("Killing task %04x\n", pTask->hSelf );
557 /* Perform USER cleanup */
559 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
561 /* Remove the task from the list to be sure we never switch back to it */
562 TASK_UnlinkTask( pTask->hSelf );
564 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
566 TRACE("this is the last task, exiting\n" );
567 ExitKernel16();
570 pTask->nEvents = 0;
572 if ( hLockedTask == pTask->hSelf )
573 hLockedTask = 0;
575 TASK_DeleteTask( pTask->hSelf );
577 if ((tib = NtCurrentTeb()->Tib.SubSystemTib))
579 free_win16_tib( tib );
580 NtCurrentTeb()->Tib.SubSystemTib = NULL;
583 /* ... and completely release the Win16Lock, just in case. */
584 ReleaseThunkLock( &lockCount );
588 /***********************************************************************
589 * ExitKernel (KERNEL.2)
591 * Clean-up everything and exit the Wine process.
593 void WINAPI ExitKernel16(void)
595 WriteOutProfiles16();
596 TerminateProcess( GetCurrentProcess(), 0 );
600 /***********************************************************************
601 * InitTask (KERNEL.91)
603 * Called by the application startup code.
605 void WINAPI InitTask16( CONTEXT *context )
607 TDB *pTask;
608 INSTANCEDATA *pinstance;
609 SEGPTR ptr;
611 context->Eax = 0;
612 if (!(pTask = TASK_GetCurrent())) return;
614 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
615 as some apps, notably Visual Basic apps, *modify* the heap/stack
616 size of the instance data segment before calling InitTask() */
618 /* Initialize the INSTANCEDATA structure */
619 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
620 pinstance->stackmin = CURRENT_SP + sizeof( STACK16FRAME );
621 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
622 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
623 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
625 /* Initialize the local heap */
626 if (LOWORD(context->Ecx))
627 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
629 /* Initialize implicitly loaded DLLs */
630 NE_InitializeDLLs( pTask->hModule );
631 NE_DllProcessAttach( pTask->hModule );
633 /* Registers on return are:
634 * ax 1 if OK, 0 on error
635 * cx stack limit in bytes
636 * dx cmdShow parameter
637 * si instance handle of the previous instance
638 * di instance handle of the new task
639 * es:bx pointer to command line inside PSP
641 * 0 (=%bp) is pushed on the stack
643 ptr = stack16_push( sizeof(WORD) );
644 *(WORD *)MapSL(ptr) = 0;
645 context->Esp -= 2;
647 context->Eax = 1;
649 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
650 else
652 LPBYTE p = &pTask->pdb.cmdLine[1];
653 while ((*p == ' ') || (*p == '\t')) p++;
654 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
656 context->Ecx = pinstance->stacktop;
657 context->Edx = pTask->nCmdShow;
658 context->Esi = (DWORD)pTask->hPrevInstance;
659 context->Edi = (DWORD)pTask->hInstance;
660 context->SegEs = (WORD)pTask->hPDB;
664 /***********************************************************************
665 * WaitEvent (KERNEL.30)
667 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
669 TDB *pTask;
671 if (!hTask) hTask = GetCurrentTask();
672 pTask = TASK_GetPtr( hTask );
674 if (pTask->flags & TDBF_WIN32)
676 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
677 return TRUE;
680 if (pTask->nEvents > 0)
682 pTask->nEvents--;
683 return FALSE;
686 if (pTask->teb == NtCurrentTeb())
688 DWORD lockCount;
690 NtResetEvent( pTask->hEvent, NULL );
691 ReleaseThunkLock( &lockCount );
692 SYSLEVEL_CheckNotLevel( 1 );
693 WaitForSingleObject( pTask->hEvent, INFINITE );
694 RestoreThunkLock( lockCount );
695 if (pTask->nEvents > 0) pTask->nEvents--;
697 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
699 return TRUE;
703 /***********************************************************************
704 * PostEvent (KERNEL.31)
706 void WINAPI PostEvent16( HTASK16 hTask )
708 TDB *pTask;
710 if (!hTask) hTask = GetCurrentTask();
711 if (!(pTask = TASK_GetPtr( hTask ))) return;
713 if (pTask->flags & TDBF_WIN32)
715 FIXME("called for Win32 thread (%04lx)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
716 return;
719 pTask->nEvents++;
721 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
725 /***********************************************************************
726 * SetPriority (KERNEL.32)
728 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
730 TDB *pTask;
731 INT16 newpriority;
733 if (!hTask) hTask = GetCurrentTask();
734 if (!(pTask = TASK_GetPtr( hTask ))) return;
735 newpriority = pTask->priority + delta;
736 if (newpriority < -32) newpriority = -32;
737 else if (newpriority > 15) newpriority = 15;
739 pTask->priority = newpriority + 1;
740 TASK_UnlinkTask( pTask->hSelf );
741 TASK_LinkTask( pTask->hSelf );
742 pTask->priority--;
746 /***********************************************************************
747 * LockCurrentTask (KERNEL.33)
749 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
751 if (bLock) hLockedTask = GetCurrentTask();
752 else hLockedTask = 0;
753 return hLockedTask;
757 /***********************************************************************
758 * IsTaskLocked (KERNEL.122)
760 HTASK16 WINAPI IsTaskLocked16(void)
762 return hLockedTask;
766 /***********************************************************************
767 * OldYield (KERNEL.117)
769 void WINAPI OldYield16(void)
771 DWORD count;
773 ReleaseThunkLock(&count);
774 RestoreThunkLock(count);
777 /***********************************************************************
778 * WIN32_OldYield (KERNEL.447)
780 void WINAPI WIN32_OldYield16(void)
782 DWORD count;
784 ReleaseThunkLock(&count);
785 RestoreThunkLock(count);
788 /***********************************************************************
789 * DirectedYield (KERNEL.150)
791 void WINAPI DirectedYield16( HTASK16 hTask )
793 OldYield16();
796 /***********************************************************************
797 * Yield (KERNEL.29)
799 void WINAPI Yield16(void)
801 TDB *pCurTask = TASK_GetCurrent();
803 if (pCurTask && pCurTask->hQueue)
805 HMODULE mod = GetModuleHandleA( "user32.dll" );
806 if (mod)
808 BOOL (WINAPI *pPeekMessageW)( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags );
809 pPeekMessageW = (void *)GetProcAddress( mod, "PeekMessageW" );
810 if (pPeekMessageW)
812 MSG msg;
813 pPeekMessageW( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
814 return;
818 OldYield16();
821 /***********************************************************************
822 * KERNEL_490 (KERNEL.490)
824 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
826 if ( !someTask ) return 0;
828 FIXME("(%04x): stub\n", someTask );
829 return 0;
832 /***********************************************************************
833 * MakeProcInstance (KERNEL.51)
835 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
837 struct thunk *thunk;
838 BYTE *lfunc;
839 SEGPTR thunkaddr;
840 WORD hInstanceSelector;
842 hInstanceSelector = GlobalHandleToSel16(hInstance);
844 TRACE("(%p, %04x);\n", func, hInstance);
846 if (!HIWORD(func)) {
847 /* Win95 actually protects via SEH, but this is better for debugging */
848 WARN("Ouch ! Called with invalid func %p !\n", func);
849 return NULL;
852 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
853 && (hInstance != 0)
854 && (hInstance != 0xffff) )
856 /* calling MPI with a foreign DSEG is invalid ! */
857 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
858 hInstance,CURRENT_DS);
861 /* Always use the DSEG that MPI was entered with.
862 * We used to set hInstance to GetTaskDS16(), but this should be wrong
863 * as CURRENT_DS provides the DSEG value we need.
864 * ("calling" DS, *not* "task" DS !) */
865 hInstanceSelector = CURRENT_DS;
866 hInstance = GlobalHandle16(hInstanceSelector);
868 /* no thunking for DLLs */
869 if (NE_GetPtr(FarGetOwner16(hInstance))->ne_flags & NE_FFLAGS_LIBMODULE)
870 return func;
872 thunkaddr = TASK_AllocThunk();
873 if (!thunkaddr) return NULL;
874 thunk = MapSL( thunkaddr );
875 lfunc = MapSL( (SEGPTR)func );
877 TRACE("(%p,%04x): got thunk %08lx\n", func, hInstance, thunkaddr );
878 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
879 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
881 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
884 thunk->movw = 0xb8; /* movw instance, %ax */
885 thunk->instance = hInstanceSelector;
886 thunk->ljmp = 0xea; /* ljmp func */
887 thunk->func = func;
888 return (FARPROC16)thunkaddr;
889 /* CX reg indicates if thunkaddr != NULL, implement if needed */
893 /***********************************************************************
894 * FreeProcInstance (KERNEL.52)
896 void WINAPI FreeProcInstance16( FARPROC16 func )
898 TRACE("(%p)\n", func );
899 TASK_FreeThunk( (SEGPTR)func );
902 /**********************************************************************
903 * TASK_GetCodeSegment
905 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
906 * and logical segment number of a given code segment.
908 * 'proc' either *is* already a pair of module handle and segment number,
909 * in which case there's nothing to do. Otherwise, it is a pointer to
910 * a function, and we need to retrieve the code segment. If the pointer
911 * happens to point to a thunk, we'll retrieve info about the code segment
912 * where the function pointed to by the thunk resides, not the thunk itself.
914 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
915 * the function the snoop code will return to ...
918 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
919 SEGTABLEENTRY **ppSeg, int *pSegNr )
921 NE_MODULE *pModule = NULL;
922 SEGTABLEENTRY *pSeg = NULL;
923 int segNr=0;
925 /* Try pair of module handle / segment number */
926 pModule = GlobalLock16( HIWORD( proc ) );
927 if ( pModule && pModule->ne_magic == IMAGE_OS2_SIGNATURE )
929 segNr = LOWORD( proc );
930 if ( segNr && segNr <= pModule->ne_cseg )
931 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
934 /* Try thunk or function */
935 else
937 BYTE *thunk = MapSL( (SEGPTR)proc );
938 WORD selector;
940 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
941 selector = thunk[6] + (thunk[7] << 8);
942 else
943 selector = HIWORD( proc );
945 pModule = NE_GetPtr( GlobalHandle16( selector ) );
946 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
948 if ( pModule )
949 for ( segNr = 1; segNr <= pModule->ne_cseg; segNr++, pSeg++ )
950 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
951 break;
953 if ( pModule && segNr > pModule->ne_cseg )
954 pSeg = NULL;
957 /* Abort if segment not found */
959 if ( !pModule || !pSeg )
960 return FALSE;
962 /* Return segment data */
964 if ( ppModule ) *ppModule = pModule;
965 if ( ppSeg ) *ppSeg = pSeg;
966 if ( pSegNr ) *pSegNr = segNr;
968 return TRUE;
971 /**********************************************************************
972 * GetCodeHandle (KERNEL.93)
974 DWORD WINAPI GetCodeHandle16( FARPROC16 proc )
976 SEGTABLEENTRY *pSeg;
978 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
979 return 0;
981 return MAKELONG( pSeg->hSeg, GlobalHandleToSel16(pSeg->hSeg) );
984 /**********************************************************************
985 * GetCodeInfo (KERNEL.104)
987 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
989 NE_MODULE *pModule;
990 SEGTABLEENTRY *pSeg;
991 int segNr;
993 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
994 return FALSE;
996 /* Fill in segment information */
998 segInfo->offSegment = pSeg->filepos;
999 segInfo->cbSegment = pSeg->size;
1000 segInfo->flags = pSeg->flags;
1001 segInfo->cbAlloc = pSeg->minsize;
1002 segInfo->h = pSeg->hSeg;
1003 segInfo->alignShift = pModule->ne_align;
1005 if ( segNr == pModule->ne_autodata )
1006 segInfo->cbAlloc += pModule->ne_heap + pModule->ne_stack;
1008 /* Return module handle in %es */
1010 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1012 return TRUE;
1016 /**********************************************************************
1017 * DefineHandleTable (KERNEL.94)
1019 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1021 FIXME("(%04x): stub ?\n", wOffset);
1022 return TRUE;
1026 /***********************************************************************
1027 * SetTaskQueue (KERNEL.34)
1029 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1031 FIXME( "stub, should not get called\n" );
1032 return 0xbeef;
1036 /***********************************************************************
1037 * GetTaskQueue (KERNEL.35)
1039 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1041 FIXME( "stub, should not get called\n" );
1042 return 0xbeef;
1045 /***********************************************************************
1046 * SetThreadQueue (KERNEL.463)
1048 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1050 FIXME( "stub, should not get called\n" );
1051 return 0xbeef;
1054 /***********************************************************************
1055 * GetThreadQueue (KERNEL.464)
1057 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1059 FIXME( "stub, should not get called\n" );
1060 return 0xbeef;
1063 /***********************************************************************
1064 * SetFastQueue (KERNEL.624)
1066 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1068 FIXME( "stub, should not get called\n" );
1071 /***********************************************************************
1072 * GetFastQueue (KERNEL.625)
1074 HQUEUE16 WINAPI GetFastQueue16( void )
1076 FIXME( "stub, should not get called\n" );
1077 return 0xbeef;
1080 /***********************************************************************
1081 * SwitchStackTo (KERNEL.108)
1083 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1085 STACK16FRAME *oldFrame, *newFrame;
1086 INSTANCEDATA *pData;
1087 UINT16 copySize;
1089 if (!(pData = GlobalLock16( seg ))) return;
1090 TRACE( "old=%04x:%04x new=%04x:%04x\n", CURRENT_SS, CURRENT_SP, seg, ptr );
1092 /* Save the old stack */
1094 oldFrame = CURRENT_STACK16;
1095 /* pop frame + args and push bp */
1096 pData->old_ss = CURRENT_SS;
1097 pData->old_sp = CURRENT_SP + sizeof(STACK16FRAME) + 2 * sizeof(WORD);
1098 *(WORD *)MapSL(MAKESEGPTR(pData->old_ss, pData->old_sp)) = oldFrame->bp;
1099 pData->stacktop = top;
1100 pData->stackmin = ptr;
1101 pData->stackbottom = ptr;
1103 /* Switch to the new stack */
1105 /* Note: we need to take the 3 arguments into account; otherwise,
1106 * the stack will underflow upon return from this function.
1108 copySize = oldFrame->bp - pData->old_sp;
1109 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1110 CURRENT_SS = seg;
1111 CURRENT_SP = ptr - copySize;
1112 newFrame = CURRENT_STACK16;
1114 /* Copy the stack frame and the local variables to the new stack */
1116 memmove( newFrame, oldFrame, copySize );
1117 newFrame->bp = ptr;
1118 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1122 /***********************************************************************
1123 * SwitchStackBack (KERNEL.109)
1125 void WINAPI SwitchStackBack16( CONTEXT *context )
1127 STACK16FRAME *oldFrame, *newFrame;
1128 INSTANCEDATA *pData;
1130 if (!(pData = GlobalLock16(CURRENT_SS)))
1131 return;
1132 if (!pData->old_ss)
1134 WARN("No previous SwitchStackTo\n" );
1135 return;
1137 TRACE( "restoring stack %04x:%04x\n", pData->old_ss, pData->old_sp );
1139 oldFrame = CURRENT_STACK16;
1141 /* Pop bp from the previous stack */
1143 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(MAKESEGPTR(pData->old_ss, pData->old_sp));
1144 pData->old_sp += sizeof(WORD);
1146 /* Switch back to the old stack */
1148 CURRENT_SS = pData->old_ss;
1149 CURRENT_SP = pData->old_sp - sizeof(STACK16FRAME);
1150 context->SegSs = pData->old_ss;
1151 context->Esp = pData->old_sp - sizeof(DWORD); /*ret addr*/
1152 pData->old_ss = pData->old_sp = 0;
1154 /* Build a stack frame for the return */
1156 newFrame = CURRENT_STACK16;
1157 newFrame->frame32 = oldFrame->frame32;
1158 newFrame->module_cs = oldFrame->module_cs;
1159 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1160 newFrame->entry_ip = oldFrame->entry_ip;
1164 /***********************************************************************
1165 * GetTaskQueueDS (KERNEL.118)
1167 void WINAPI GetTaskQueueDS16(void)
1169 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1173 /***********************************************************************
1174 * GetTaskQueueES (KERNEL.119)
1176 void WINAPI GetTaskQueueES16(void)
1178 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1182 /***********************************************************************
1183 * GetCurrentTask (KERNEL32.@)
1185 HTASK16 WINAPI GetCurrentTask(void)
1187 HTASK16 ret = kernel_get_thread_data()->htask16;
1188 if (!ret) ret = main_task;
1189 return ret;
1192 /***********************************************************************
1193 * GetCurrentTask (KERNEL.36)
1195 DWORD WINAPI WIN16_GetCurrentTask(void)
1197 /* This is the version used by relay code; the first task is */
1198 /* returned in the high word of the result */
1199 return MAKELONG( GetCurrentTask(), hFirstTask );
1203 /***********************************************************************
1204 * GetCurrentPDB (KERNEL.37)
1206 * UNDOC: returns PSP of KERNEL in high word
1208 DWORD WINAPI GetCurrentPDB16(void)
1210 TDB *pTask;
1212 if (!(pTask = TASK_GetCurrent())) return 0;
1213 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1217 /***********************************************************************
1218 * GetCurPID (KERNEL.157)
1220 DWORD WINAPI GetCurPID16( DWORD unused )
1222 return 0;
1226 /***********************************************************************
1227 * GetInstanceData (KERNEL.54)
1229 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1231 char *ptr = GlobalLock16( instance );
1232 if (!ptr || !len) return 0;
1233 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1234 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1235 return len;
1239 /***********************************************************************
1240 * GetExeVersion (KERNEL.105)
1242 WORD WINAPI GetExeVersion16(void)
1244 TDB *pTask;
1246 if (!(pTask = TASK_GetCurrent())) return 0;
1247 return pTask->version;
1251 /***********************************************************************
1252 * GetLPErrMode (KERNEL.99)
1254 SEGPTR WINAPI GetLPErrMode(void)
1256 FIXME("(): stub\n");
1257 return 0;
1261 /***********************************************************************
1262 * SetErrorMode (KERNEL.107)
1264 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1266 TDB *pTask;
1267 if (!(pTask = TASK_GetCurrent())) return 0;
1268 pTask->error_mode = mode;
1269 return SetErrorMode( mode );
1273 /***********************************************************************
1274 * GetNumTasks (KERNEL.152)
1276 UINT16 WINAPI GetNumTasks16(void)
1278 return nTaskCount;
1282 /***********************************************************************
1283 * GetTaskDS (KERNEL.155)
1285 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1286 * I don't think we need to bother with this.
1288 HINSTANCE16 WINAPI GetTaskDS16(void)
1290 TDB *pTask;
1292 if (!(pTask = TASK_GetCurrent())) return 0;
1293 return GlobalHandleToSel16(pTask->hInstance);
1296 /***********************************************************************
1297 * GetDummyModuleHandleDS (KERNEL.602)
1299 WORD WINAPI GetDummyModuleHandleDS16(void)
1301 TDB *pTask;
1302 WORD selector;
1304 if (!(pTask = TASK_GetCurrent())) return 0;
1305 if (!(pTask->flags & TDBF_WIN32)) return 0;
1306 selector = GlobalHandleToSel16( pTask->hModule );
1307 CURRENT_DS = selector;
1308 return selector;
1311 /***********************************************************************
1312 * IsTask (KERNEL.320)
1314 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1316 TDB *pTask;
1318 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1319 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1320 return (pTask->magic == TDB_MAGIC);
1324 /***********************************************************************
1325 * IsWinOldApTask (KERNEL.158)
1327 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1329 /* should return bit 0 of byte 0x48 in PSP */
1330 return FALSE;
1333 /***********************************************************************
1334 * SetTaskSignalProc (KERNEL.38)
1336 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1338 TDB *pTask;
1339 FARPROC16 oldProc;
1341 if (!hTask) hTask = GetCurrentTask();
1342 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1343 oldProc = pTask->userhandler;
1344 pTask->userhandler = proc;
1345 return oldProc;
1348 /***********************************************************************
1349 * SetSigHandler (KERNEL.140)
1351 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1352 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1354 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1355 newhandler,oldhandler,oldmode,newmode,flag );
1357 if (flag != 1) return 0;
1358 if (!newmode) newhandler = NULL; /* Default handler */
1359 if (newmode != 4)
1361 TDB *pTask;
1363 if (!(pTask = TASK_GetCurrent())) return 0;
1364 if (oldmode) *oldmode = pTask->signal_flags;
1365 pTask->signal_flags = newmode;
1366 if (oldhandler) *oldhandler = pTask->sighandler;
1367 pTask->sighandler = newhandler;
1369 return 0;
1373 /***********************************************************************
1374 * GlobalNotify (KERNEL.154)
1376 * Note that GlobalNotify does _not_ return the old NotifyProc
1377 * -- contrary to LocalNotify !!
1379 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1381 TDB *pTask;
1383 if (!(pTask = TASK_GetCurrent())) return;
1384 pTask->discardhandler = proc;
1388 /***********************************************************************
1389 * GetExePtrHelper
1391 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1393 char *ptr;
1394 HANDLE16 owner;
1396 /* Check for module handle */
1398 if (!(ptr = GlobalLock16( handle ))) return 0;
1399 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return handle;
1401 /* Search for this handle inside all tasks */
1403 *hTask = hFirstTask;
1404 while (*hTask)
1406 TDB *pTask = TASK_GetPtr( *hTask );
1407 if ((*hTask == handle) ||
1408 (pTask->hInstance == handle) ||
1409 (pTask->hQueue == handle) ||
1410 (pTask->hPDB == handle)) return pTask->hModule;
1411 *hTask = pTask->hNext;
1414 /* Check the owner for module handle */
1416 owner = FarGetOwner16( handle );
1417 if (!(ptr = GlobalLock16( owner ))) return 0;
1418 if (((NE_MODULE *)ptr)->ne_magic == IMAGE_OS2_SIGNATURE) return owner;
1420 /* Search for the owner inside all tasks */
1422 *hTask = hFirstTask;
1423 while (*hTask)
1425 TDB *pTask = TASK_GetPtr( *hTask );
1426 if ((*hTask == owner) ||
1427 (pTask->hInstance == owner) ||
1428 (pTask->hQueue == owner) ||
1429 (pTask->hPDB == owner)) return pTask->hModule;
1430 *hTask = pTask->hNext;
1433 return 0;
1436 /***********************************************************************
1437 * GetExePtr (KERNEL.133)
1439 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1441 HTASK16 hTask = 0;
1442 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1443 STACK16FRAME *frame = CURRENT_STACK16;
1444 frame->ecx = hModule;
1445 if (hTask) frame->es = hTask;
1446 return hModule;
1450 /***********************************************************************
1451 * K228 (KERNEL.228)
1453 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1455 HTASK16 hTask = 0;
1456 return GetExePtrHelper( handle, &hTask );
1460 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1462 /**************************************************************************
1463 * FatalAppExit (KERNEL.137)
1465 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1467 TDB *pTask = TASK_GetCurrent();
1469 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1471 HMODULE mod = GetModuleHandleA( "user32.dll" );
1472 if (mod)
1474 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1475 if (pMessageBoxA)
1477 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1478 goto done;
1481 ERR( "%s\n", debugstr_a(str) );
1483 done:
1484 ExitThread(0xff);
1488 /***********************************************************************
1489 * GetAppCompatFlags (KERNEL.354)
1491 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1493 TDB *pTask;
1495 if (!hTask) hTask = GetCurrentTask();
1496 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1497 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1498 return pTask->compat_flags;
1502 /***********************************************************************
1503 * GetDOSEnvironment (KERNEL.131)
1505 * Note: the environment is allocated once, it doesn't track changes
1506 * made using the Win32 API. This shouldn't matter.
1508 * Format of a 16-bit environment block:
1509 * ASCIIZ string 1 (xx=yy format)
1510 * ...
1511 * ASCIIZ string n
1512 * BYTE 0
1513 * WORD 1
1514 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1516 SEGPTR WINAPI GetDOSEnvironment16(void)
1518 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1519 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1521 if (!handle)
1523 DWORD size;
1524 LPSTR p, env;
1526 p = env = GetEnvironmentStringsA();
1527 while (*p) p += strlen(p) + 1;
1528 p++; /* skip last null */
1529 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1530 handle = GlobalAlloc16( GMEM_FIXED, size );
1531 if (handle)
1533 WORD one = 1;
1534 LPSTR env16 = GlobalLock16( handle );
1535 memcpy( env16, env, p - env );
1536 memcpy( env16 + (p - env), &one, sizeof(one));
1537 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1538 GlobalUnlock16( handle );
1540 FreeEnvironmentStringsA( env );
1542 return WOWGlobalLock16( handle );