2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/client/vdm.c
5 * PURPOSE: Virtual DOS Machines (VDM) Support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *******************************************************************/
16 /* TYPES **********************************************************************/
18 typedef struct _ENV_INFO
23 } ENV_INFO
, *PENV_INFO
;
25 /* GLOBALS ********************************************************************/
27 ENV_INFO BasepEnvNameType
[] =
29 {3, sizeof(L
"PATH") , L
"PATH" },
30 {2, sizeof(L
"WINDIR") , L
"WINDIR" },
31 {2, sizeof(L
"SYSTEMROOT"), L
"SYSTEMROOT"},
32 {3, sizeof(L
"TEMP") , L
"TEMP" },
33 {3, sizeof(L
"TMP") , L
"TMP" },
36 UNICODE_STRING BaseDotComSuffixName
= RTL_CONSTANT_STRING(L
".com");
37 UNICODE_STRING BaseDotPifSuffixName
= RTL_CONSTANT_STRING(L
".pif");
38 UNICODE_STRING BaseDotExeSuffixName
= RTL_CONSTANT_STRING(L
".exe");
40 /* FUNCTIONS ******************************************************************/
44 BaseIsDosApplication(IN PUNICODE_STRING PathName
,
47 UNICODE_STRING String
;
50 String
.Length
= BaseDotComSuffixName
.Length
;
51 String
.Buffer
= &PathName
->Buffer
[(PathName
->Length
- String
.Length
) / sizeof(WCHAR
)];
52 if (RtlEqualUnicodeString(&String
, &BaseDotComSuffixName
, TRUE
)) return BINARY_TYPE_COM
;
55 String
.Length
= BaseDotPifSuffixName
.Length
;
56 String
.Buffer
= &PathName
->Buffer
[(PathName
->Length
- String
.Length
) / sizeof(WCHAR
)];
57 if (RtlEqualUnicodeString(&String
, &BaseDotPifSuffixName
, TRUE
)) return BINARY_TYPE_PIF
;
60 String
.Length
= BaseDotExeSuffixName
.Length
;
61 String
.Buffer
= &PathName
->Buffer
[(PathName
->Length
- String
.Length
) / sizeof(WCHAR
)];
62 if (RtlEqualUnicodeString(&String
, &BaseDotExeSuffixName
, TRUE
)) return BINARY_TYPE_EXE
;
69 BaseCheckVDM(IN ULONG BinaryType
,
70 IN PCWCH ApplicationName
,
72 IN PCWCH CurrentDirectory
,
73 IN PANSI_STRING AnsiEnvironment
,
74 IN PCSR_API_MESSAGE ApiMessage
,
76 IN DWORD CreationFlags
,
77 IN LPSTARTUPINFOW StartupInfo
,
78 IN HANDLE hUserToken OPTIONAL
)
80 /* This is not supported */
87 BaseUpdateVDMEntry(IN ULONG UpdateIndex
,
88 IN OUT PHANDLE WaitHandle
,
92 #if 0 // Unimplemented in BASESRV
94 BASE_API_MESSAGE ApiMessage
;
95 PBASE_UPDATE_VDM_ENTRY UpdateVdmEntry
= &ApiMessage
.Data
.UpdateVdmEntry
;
97 /* Check what update is being sent */
100 /* VDM is being undone */
103 /* Tell the server how far we had gotten along */
104 UpdateVdmEntry
->iTask
= HandleToUlong(*WaitHandle
);
105 UpdateVdmEntry
->VDMCreationState
= IndexInfo
;
109 /* VDM is ready with a new process handle */
110 case VdmEntryUpdateProcess
:
112 /* Send it the process handle */
113 UpdateVdmEntry
->VDMProcessHandle
= *WaitHandle
;
114 UpdateVdmEntry
->iTask
= IndexInfo
;
119 /* Also check what kind of binary this is for the console handle */
120 if (BinaryType
== BINARY_TYPE_WOW
)
122 /* Magic value for 16-bit apps */
123 UpdateVdmEntry
->ConsoleHandle
= (HANDLE
)-1;
125 else if (UpdateVdmEntry
->iTask
)
127 /* No handle for true VDM */
128 UpdateVdmEntry
->ConsoleHandle
= NULL
;
132 /* Otherwise, use the regular console handle */
133 UpdateVdmEntry
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
136 /* Finally write the index and binary type */
137 UpdateVdmEntry
->EntryIndex
= UpdateIndex
;
138 UpdateVdmEntry
->BinaryType
= BinaryType
;
140 /* Send the message to CSRSS */
141 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
143 CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX
, BasepUpdateVDMEntry
),
144 sizeof(BASE_UPDATE_VDM_ENTRY
));
145 if (!NT_SUCCESS(Status
))
148 BaseSetLastNTError(Status
);
152 /* If this was an update, CSRSS returns a new wait handle */
153 if (UpdateIndex
== VdmEntryUpdateProcess
)
155 /* Return it to the caller */
156 *WaitHandle
= UpdateVdmEntry
->WaitObjectForParent
;
165 BaseCheckForVDM(IN HANDLE ProcessHandle
,
166 OUT LPDWORD ExitCode
)
168 #if 0 // Unimplemented in BASESRV
170 EVENT_BASIC_INFORMATION EventBasicInfo
;
171 BASE_API_MESSAGE ApiMessage
;
172 PBASE_GET_VDM_EXIT_CODE GetVdmExitCode
= &ApiMessage
.Data
.GetVdmExitCode
;
174 /* It's VDM if the process is actually a wait handle (an event) */
175 Status
= NtQueryEvent(ProcessHandle
,
176 EventBasicInformation
,
178 sizeof(EventBasicInfo
),
180 if (!NT_SUCCESS(Status
)) return FALSE
;
182 /* Setup the input parameters */
183 GetVdmExitCode
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
184 GetVdmExitCode
->hParent
= ProcessHandle
;
187 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
189 CSR_CREATE_API_NUMBER(BASESRV_SERVERDLL_INDEX
, BasepGetVDMExitCode
),
190 sizeof(BASE_GET_VDM_EXIT_CODE
));
191 if (!NT_SUCCESS(Status
)) return FALSE
;
193 /* Get the exit code from the reply */
194 *ExitCode
= GetVdmExitCode
->ExitCode
;
201 BaseGetVdmConfigInfo(IN LPCWSTR CommandLineReserved
,
204 IN PUNICODE_STRING CmdLineString
,
207 WCHAR Buffer
[MAX_PATH
];
208 WCHAR CommandLine
[MAX_PATH
* 2];
211 /* Clear the buffer in case we fail */
212 CmdLineString
->Buffer
= 0;
214 /* Always return the same size: 16 Mb */
215 *VdmSize
= 0x1000000;
217 /* Get the system directory */
218 Length
= GetSystemDirectoryW(Buffer
, MAX_PATH
);
219 if (!(Length
) || (Length
>= MAX_PATH
))
221 /* Eliminate no path or path too big */
222 SetLastError(ERROR_INVALID_NAME
);
226 /* Check if this is VDM with a DOS Sequence ID */
230 * Build the VDM string for it:
231 * -i%lx : Gives the DOS Sequence ID;
232 * %s%c : Nothing if DOS VDM, -w if WoW VDM, -ws if separate WoW VDM.
234 _snwprintf(CommandLine
,
236 L
"\"%s\\ntvdm.exe\" -i%lx %s%c",
239 (BinaryType
== BINARY_TYPE_DOS
) ? L
" " : L
"-w",
240 (BinaryType
== BINARY_TYPE_SEPARATE_WOW
) ? L
's' : L
' ');
245 * Build the string for it without the DOS Sequence ID:
246 * %s%c : Nothing if DOS VDM, -w if WoW VDM, -ws if separate WoW VDM.
248 _snwprintf(CommandLine
,
250 L
"\"%s\\ntvdm.exe\" %s%c",
252 (BinaryType
== BINARY_TYPE_DOS
) ? L
" " : L
"-w",
253 (BinaryType
== BINARY_TYPE_SEPARATE_WOW
) ? L
's' : L
' ');
256 /* Create the actual string */
257 return RtlCreateUnicodeString(CmdLineString
, CommandLine
);
262 BaseGetEnvNameType_U(IN PWCHAR Name
,
268 /* Start by assuming unknown type */
271 /* Loop all the environment names */
272 for (i
= 0; i
< (sizeof(BasepEnvNameType
) / sizeof(ENV_INFO
)); i
++)
275 EnvInfo
= &BasepEnvNameType
[i
];
277 /* Check if it matches the name */
278 if ((EnvInfo
->NameLength
== NameLength
) &&
279 !(_wcsnicmp(EnvInfo
->Name
, Name
, NameLength
)))
281 /* It does, return the type */
282 NameType
= EnvInfo
->NameType
;
287 /* Return what we found, or unknown if nothing */
293 BaseDestroyVDMEnvironment(IN PANSI_STRING AnsiEnv
,
294 IN PUNICODE_STRING UnicodeEnv
)
298 /* Clear the ASCII buffer since Rtl creates this for us */
299 if (AnsiEnv
->Buffer
) RtlFreeAnsiString(AnsiEnv
);
301 /* The Unicode buffer is build by hand, though */
302 if (UnicodeEnv
->Buffer
)
304 /* So clear it through the API */
305 NtFreeVirtualMemory(NtCurrentProcess(),
306 (PVOID
*)&UnicodeEnv
->Buffer
,
317 BaseCreateVDMEnvironment(IN PWCHAR lpEnvironment
,
318 IN PANSI_STRING AnsiEnv
,
319 IN PUNICODE_STRING UnicodeEnv
)
322 ULONG RegionSize
, EnvironmentSize
= 0;
323 PWCHAR p
, Environment
, NewEnvironment
= NULL
;
326 /* Make sure we have both strings */
327 if (!(AnsiEnv
) || !(UnicodeEnv
))
330 SetLastError(ERROR_INVALID_PARAMETER
);
334 /* Check if an environment was passed in */
337 /* Nope, create one */
338 Status
= RtlCreateEnvironment(TRUE
, (PWCHAR
*)&Environment
);
339 if (!NT_SUCCESS(Status
)) goto Quickie
;
343 /* Use the one we got */
344 Environment
= lpEnvironment
;
347 /* Do we have something now ? */
350 /* Still not, fail out */
351 SetLastError(ERROR_BAD_ENVIRONMENT
);
355 /* Count how much space the whole environment takes */
357 while ((*p
++ != UNICODE_NULL
) && (*p
!= UNICODE_NULL
)) EnvironmentSize
++;
358 EnvironmentSize
+= sizeof(UNICODE_NULL
);
360 /* Allocate a new copy */
361 RegionSize
= (EnvironmentSize
+ MAX_PATH
) * sizeof(WCHAR
);
362 if (!NT_SUCCESS(NtAllocateVirtualMemory(NtCurrentProcess(),
363 (PVOID
*)&NewEnvironment
,
369 /* We failed, bail out */
370 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
371 NewEnvironment
= NULL
;
375 /* Begin parsing the new environment */
378 /* FIXME: Code here */
383 /* Initialize the unicode string to hold it */
384 EnvironmentSize
= (p
- NewEnvironment
) * sizeof(WCHAR
);
385 RtlInitEmptyUnicodeString(UnicodeEnv
, NewEnvironment
, (USHORT
)EnvironmentSize
);
386 UnicodeEnv
->Length
= (USHORT
)EnvironmentSize
;
388 /* Create the ASCII version of it */
389 Status
= RtlUnicodeStringToAnsiString(AnsiEnv
, UnicodeEnv
, TRUE
);
390 if (!NT_SUCCESS(Status
))
392 /* Set last error if conversion failure */
393 BaseSetLastNTError(Status
);
397 /* Everything went okay, so return success */
399 NewEnvironment
= NULL
;
403 /* Cleanup path starts here, start by destroying the envrionment copy */
404 if (!(lpEnvironment
) && (Environment
)) RtlDestroyEnvironment(Environment
);
406 /* See if we are here due to failure */
409 /* Initialize the paths to be empty */
410 RtlInitEmptyUnicodeString(UnicodeEnv
, NULL
, 0);
411 RtlInitEmptyAnsiString(AnsiEnv
, NULL
, 0);
413 /* Free the environment copy */
415 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
416 (PVOID
*)&NewEnvironment
,
419 ASSERT(NT_SUCCESS(Status
));
422 /* Return the result */
427 /* Check whether a file is an OS/2 or a very old Windows executable
428 * by testing on import of KERNEL.
430 * FIXME: is reading the module imports the only way of discerning
431 * old Windows binaries from OS/2 ones ? At least it seems so...
434 InternalIsOS2OrOldWin(HANDLE hFile
, IMAGE_DOS_HEADER
*mz
, IMAGE_OS2_HEADER
*ne
)
437 LPWORD modtab
= NULL
;
438 LPSTR nametab
= NULL
;
443 CurPos
= SetFilePointer(hFile
, 0, NULL
, FILE_CURRENT
);
445 /* read modref table */
446 if((SetFilePointer(hFile
, mz
->e_lfanew
+ ne
->ne_modtab
, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
) ||
447 (!(modtab
= HeapAlloc(GetProcessHeap(), 0, ne
->ne_cmod
* sizeof(WORD
)))) ||
448 (!(ReadFile(hFile
, modtab
, ne
->ne_cmod
* sizeof(WORD
), &Read
, NULL
))) ||
449 (Read
!= (DWORD
)ne
->ne_cmod
* sizeof(WORD
)))
454 /* read imported names table */
455 if((SetFilePointer(hFile
, mz
->e_lfanew
+ ne
->ne_imptab
, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
) ||
456 (!(nametab
= HeapAlloc(GetProcessHeap(), 0, ne
->ne_enttab
- ne
->ne_imptab
))) ||
457 (!(ReadFile(hFile
, nametab
, ne
->ne_enttab
- ne
->ne_imptab
, &Read
, NULL
))) ||
458 (Read
!= (DWORD
)ne
->ne_enttab
- ne
->ne_imptab
))
463 for(i
= 0; i
< ne
->ne_cmod
; i
++)
466 module
= &nametab
[modtab
[i
]];
467 if(!strncmp(&module
[1], "KERNEL", module
[0]))
469 /* very old windows file */
476 DPRINT1("InternalIsOS2OrOldWin(): Binary file seems to be broken\n");
479 HeapFree(GetProcessHeap(), 0, modtab
);
480 HeapFree(GetProcessHeap(), 0, nametab
);
481 SetFilePointer(hFile
, CurPos
, NULL
, FILE_BEGIN
);
486 InternalGetBinaryType(HANDLE hFile
)
492 unsigned char magic
[4];
493 unsigned char ignored
[12];
499 unsigned long cputype
;
500 unsigned long cpusubtype
;
501 unsigned long filetype
;
508 if((SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
) ||
509 (!ReadFile(hFile
, &Header
, sizeof(Header
), &Read
, NULL
) ||
510 (Read
!= sizeof(Header
))))
512 return BINARY_UNKNOWN
;
515 if(!memcmp(Header
.elf
.magic
, "\177ELF", sizeof(Header
.elf
.magic
)))
517 /* FIXME: we don't bother to check byte order, architecture, etc. */
518 switch(Header
.elf
.type
)
521 return BINARY_UNIX_EXE
;
523 return BINARY_UNIX_LIB
;
525 return BINARY_UNKNOWN
;
528 /* Mach-o File with Endian set to Big Endian or Little Endian*/
529 if(Header
.macho
.magic
== 0xFEEDFACE ||
530 Header
.macho
.magic
== 0xCEFAEDFE)
532 switch(Header
.macho
.filetype
)
536 return BINARY_UNIX_LIB
;
538 return BINARY_UNKNOWN
;
541 /* Not ELF, try DOS */
542 if(Header
.mz
.e_magic
== IMAGE_DOS_SIGNATURE
)
544 /* We do have a DOS image so we will now try to seek into
545 * the file by the amount indicated by the field
546 * "Offset to extended header" and read in the
547 * "magic" field information at that location.
548 * This will tell us if there is more header information
551 if((SetFilePointer(hFile
, Header
.mz
.e_lfanew
, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
) ||
552 (!ReadFile(hFile
, magic
, sizeof(magic
), &Read
, NULL
) ||
553 (Read
!= sizeof(magic
))))
558 /* Reading the magic field succeeded so
559 * we will try to determine what type it is.
561 if(!memcmp(magic
, "PE\0\0", sizeof(magic
)))
563 IMAGE_FILE_HEADER FileHeader
;
564 if(!ReadFile(hFile
, &FileHeader
, sizeof(IMAGE_FILE_HEADER
), &Read
, NULL
) ||
565 (Read
!= sizeof(IMAGE_FILE_HEADER
)))
570 /* FIXME - detect 32/64 bit */
572 if(FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
573 return BINARY_PE_DLL32
;
574 return BINARY_PE_EXE32
;
577 if(!memcmp(magic
, "NE", 1))
579 /* This is a Windows executable (NE) header. This can
580 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
581 * DOS program (running under a DOS extender). To decide
582 * which, we'll have to read the NE header.
585 if((SetFilePointer(hFile
, Header
.mz
.e_lfanew
, NULL
, FILE_BEGIN
) == 1) ||
586 !ReadFile(hFile
, &ne
, sizeof(IMAGE_OS2_HEADER
), &Read
, NULL
) ||
587 (Read
!= sizeof(IMAGE_OS2_HEADER
)))
589 /* Couldn't read header, so abort. */
600 return InternalIsOS2OrOldWin(hFile
, &Header
.mz
, &ne
);
605 return BINARY_UNKNOWN
;
614 LPCWSTR lpApplicationName
,
621 if(!lpApplicationName
|| !lpBinaryType
)
623 SetLastError(ERROR_INVALID_PARAMETER
);
627 hFile
= CreateFileW(lpApplicationName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
628 OPEN_EXISTING
, 0, 0);
629 if(hFile
== INVALID_HANDLE_VALUE
)
634 BinType
= InternalGetBinaryType(hFile
);
644 * guess from filename
646 if(!(dot
= wcsrchr(lpApplicationName
, L
'.')))
650 if(!lstrcmpiW(dot
, L
".COM"))
652 *lpBinaryType
= SCS_DOS_BINARY
;
655 if(!lstrcmpiW(dot
, L
".PIF"))
657 *lpBinaryType
= SCS_PIF_BINARY
;
662 case BINARY_PE_EXE32
:
663 case BINARY_PE_DLL32
:
665 *lpBinaryType
= SCS_32BIT_BINARY
;
668 case BINARY_PE_EXE64
:
669 case BINARY_PE_DLL64
:
671 *lpBinaryType
= SCS_64BIT_BINARY
;
676 *lpBinaryType
= SCS_WOW_BINARY
;
681 *lpBinaryType
= SCS_OS216_BINARY
;
686 *lpBinaryType
= SCS_DOS_BINARY
;
689 case BINARY_UNIX_EXE
:
690 case BINARY_UNIX_LIB
:
696 DPRINT1("Invalid binary type %lu returned!\n", BinType
);
705 GetBinaryTypeA(IN LPCSTR lpApplicationName
,
706 OUT LPDWORD lpBinaryType
)
708 ANSI_STRING ApplicationNameString
;
709 UNICODE_STRING ApplicationNameW
;
710 BOOL StringAllocated
= FALSE
, Result
;
713 RtlInitAnsiString(&ApplicationNameString
, lpApplicationName
);
715 if (ApplicationNameString
.Length
* sizeof(WCHAR
) >= NtCurrentTeb()->StaticUnicodeString
.MaximumLength
)
717 StringAllocated
= TRUE
;
718 Status
= RtlAnsiStringToUnicodeString(&ApplicationNameW
, &ApplicationNameString
, TRUE
);
722 Status
= RtlAnsiStringToUnicodeString(&(NtCurrentTeb()->StaticUnicodeString
), &ApplicationNameString
, FALSE
);
725 if (!NT_SUCCESS(Status
))
727 BaseSetLastNTError(Status
);
733 Result
= GetBinaryTypeW(ApplicationNameW
.Buffer
, lpBinaryType
);
734 RtlFreeUnicodeString(&ApplicationNameW
);
738 Result
= GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString
.Buffer
, lpBinaryType
);
777 GetNextVDMCommand(PGET_NEXT_VDM_COMMAND_DATA CommandData
)
789 GetVDMCurrentDirectories (
828 RegisterWowBaseHandlers (
856 SetVDMCurrentDirectories (
870 VDMConsoleOperation (
885 VDMOperationStarted (