3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: subsys/ntvdm/ntvdm->c
6 * PURPOSE: Virtual DOS Machine
7 * PROGRAMMER: Robert Dickenson (robd@mok.lvcm.com)
12 /* INCLUDES *****************************************************************/
13 #define WIN32_NO_STATUS
21 /* GLOBALS ******************************************************************/
24 /* FUNCTIONS *****************************************************************/
26 void PrintString(char* fmt
,...)
32 vsprintf(buffer
, fmt
, ap
);
35 OutputDebugStringA(buffer
);
52 SetVDMCurrentDirectories
64 typedef struct tag_VDM_CONFIG
{
70 //device=%SystemRoot%\system32\himem.sys
72 } VDM_CONFIG
, *PVDM_CONFIG
;
74 typedef struct tag_VDM_AUTOEXEC
{
76 //lh %SystemRoot%\system32\mscdexnt.exe
77 //lh %SystemRoot%\system32\redir
78 //lh %SystemRoot%\system32\dosx
79 } VDM_AUTOEXEC
, *PVDM_AUTOEXEC
;
81 typedef struct tag_VDM_CONTROL_BLOCK
{
85 VDM_AUTOEXEC vdmAutoexec
;
86 PROCESS_INFORMATION ProcessInformation
;
87 CHAR CommandLine
[MAX_PATH
];
88 CHAR CurrentDirectory
[MAX_PATH
];
90 } VDM_CONTROL_BLOCK
, *PVDM_CONTROL_BLOCK
;
94 StartVDM(PVDM_CONTROL_BLOCK vdm
)
97 STARTUPINFOA StartupInfo
;
99 StartupInfo
.cb
= sizeof(StartupInfo
);
100 StartupInfo
.lpReserved
= NULL
;
101 StartupInfo
.lpDesktop
= NULL
;
102 StartupInfo
.lpTitle
= NULL
;
103 StartupInfo
.dwFlags
= 0;
104 StartupInfo
.cbReserved2
= 0;
105 StartupInfo
.lpReserved2
= 0;
107 Result
= CreateProcessA(vdm
->CommandLine
,
116 &vdm
->ProcessInformation
);
118 PrintString("VDM: Failed to execute target process\n");
121 WaitForSingleObject(vdm
->ProcessInformation
.hProcess
, INFINITE
);
122 CloseHandle(vdm
->ProcessInformation
.hProcess
);
123 CloseHandle(vdm
->ProcessInformation
.hThread
);
128 ShutdownVDM(PVDM_CONTROL_BLOCK vdm
)
135 BOOL
ReadConfigForVDM(PVDM_CONTROL_BLOCK vdm
)
141 hFile
= CreateFileW(L
"\\system32\\config.nt",
145 OPEN_ALWAYS
/*OPEN_EXISTING*/,
146 FILE_ATTRIBUTE_NORMAL
,
148 dwError
= GetLastError();
149 if (hFile
== INVALID_HANDLE_VALUE
) {
150 // error with file path or system problem?
153 // we just created a new file, perhaps we should set/write some defaults?
155 if (dwError
== ERROR_ALREADY_EXISTS
) {
156 // read the line entries and cache in some struct...
161 hFile
= CreateFileW(L
"\\system32\\autoexec.nt",
166 FILE_ATTRIBUTE_NORMAL
,
168 dwError
= GetLastError();
169 if (hFile
== INVALID_HANDLE_VALUE
) {
170 // error with file path or system problem?
173 // we just created a new file, perhaps we should set/write some defaults?
175 if (dwError
== ERROR_ALREADY_EXISTS
) {
176 // read the line entries and cache in some struct...
185 LoadConfigDriversForVDM(PVDM_CONFIG vdmConfig
)
193 SetConfigOptionsForVDM(PVDM_AUTOEXEC vdmAutoexec
)
201 CreateVDM(PVDM_CONTROL_BLOCK vdm
)
203 // BOOL result = TRUE;
208 GlobalMemoryStatus(&stat
);
209 if (stat
.dwLength
!= sizeof(MEMORYSTATUS
)) {
210 printf("WARNING: GlobalMemoryStatus() returned unknown structure version, size %ld, expected %d.\n", stat
.dwLength
, sizeof(stat
));
212 printf("Memory Load: %ld percent in use.\n", stat
.dwMemoryLoad
);
213 printf("\t%ld total bytes physical memory.\n", stat
.dwTotalPhys
);
214 printf("\t%ld available physical memory.\n", stat
.dwAvailPhys
);
215 printf("\t%ld total bytes paging file.\n", stat
.dwTotalPageFile
);
216 printf("\t%ld available paging file.\n", stat
.dwAvailPageFile
);
217 printf("\t%lx total bytes virtual memory.\n", stat
.dwTotalVirtual
);
218 printf("\t%lx available bytes virtual memory.\n", stat
.dwAvailVirtual
);
220 #define OUT_OF_HEADROOM 90
221 if (stat
.dwMemoryLoad
> OUT_OF_HEADROOM
) {
222 DPRINT("VDM: system resources deemed to low to start VDM.\n");
230 vdm
->hHeap
= HeapCreate(0, inf
.dwAllocationGranularity
, 0);
231 if (vdm
->hHeap
== NULL
) {
232 DPRINT("VDM: failed to create heap.\n");
236 #define DEFAULT_VDM_IMAGE_SIZE 2000000
237 vdm
->ImageMem
= HeapAlloc(vdm
->hHeap
, 0, DEFAULT_VDM_IMAGE_SIZE
);
238 if (vdm
->ImageMem
== NULL
) {
239 DPRINT("VDM: failed to allocate image memory from heap %x.\n", vdm
->hHeap
);
240 HeapDestroy(vdm
->hHeap
);
248 DestroyVDM(PVDM_CONTROL_BLOCK vdm
)
252 if (vdm
->ImageMem
!= NULL
) {
253 if (HeapFree(vdm
->hHeap
, 0, vdm
->ImageMem
) != FALSE
) {
254 DPRINT("VDM: failed to free memory from heap %x.\n", vdm
->hHeap
);
257 vdm
->ImageMem
= NULL
;
259 if (vdm
->hHeap
!= NULL
) {
260 if (!HeapDestroy(vdm
->hHeap
)) {
261 DPRINT("VDM: failed to destroy heap %x.\n", vdm
->hHeap
);
270 WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nShowCmd
)
272 VDM_CONTROL_BLOCK VdmCB
;
275 BOOL vdmStarted
= FALSE
;
277 WCHAR WelcomeMsg
[RC_STRING_MAX_SIZE
];
278 WCHAR PromptMsg
[RC_STRING_MAX_SIZE
];
279 CHAR InputBuffer
[255];
281 LoadStringW( GetModuleHandle(NULL
), STRING_WelcomeMsg
, WelcomeMsg
,sizeof(WelcomeMsg
) / sizeof(WelcomeMsg
[0]));
282 LoadStringW( GetModuleHandle(NULL
), STRING_PromptMsg
, PromptMsg
,sizeof(PromptMsg
) / sizeof(PromptMsg
[0]));
285 SetConsoleTitleW(L
"ntvdm");
287 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
288 WelcomeMsg
, lstrlenW(WelcomeMsg
), // wcslen(WelcomeMsg),
291 if (!CreateVDM(&VdmCB
)) {
292 DPRINT("VDM: failed to create VDM.\n");
297 ReadConfigForVDM(&VdmCB
);
299 if (!LoadConfigDriversForVDM(&(VdmCB
.vdmConfig
))) {
300 DPRINT("VDM: failed to load configuration drivers.\n");
304 if (!SetConfigOptionsForVDM(&(VdmCB
.vdmAutoexec
))) {
305 DPRINT("VDM: failed to set configuration options.\n");
310 GetSystemDirectoryA(VdmCB
.CommandLine
, MAX_PATH
);
311 strcat(VdmCB
.CommandLine
, "\\hello.exe");
312 GetWindowsDirectoryA(VdmCB
.CurrentDirectory
, MAX_PATH
);
315 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
316 PromptMsg
, lstrlenW(PromptMsg
), // wcslen(PromptMsg),
320 ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
323 if (++i
>= (sizeof(InputBuffer
) - 1)) {
326 } while (InputBuffer
[i
- 1] != '\n');
327 InputBuffer
[i
- 1] = '\0';
329 if (InputBuffer
[0] == 'r' || InputBuffer
[0] == 'R') {
331 if (StartVDM(&VdmCB
)) {
334 DPRINT("VDM: failed to start.\n");
337 DPRINT("VDM: already started.\n");
340 if (InputBuffer
[0] == 's' || InputBuffer
[0] == 'S') {
342 if (ShutdownVDM(&VdmCB
)) {
345 DPRINT("VDM: failed to shutdown.\n");
348 DPRINT("VDM: not started.\n");
351 if (InputBuffer
[0] == 'q' || InputBuffer
[0] == 'Q') {
356 if (!ShutdownVDM(&VdmCB
)) {
357 DPRINT("VDM: failed to cleanly shutdown VDM.\n");
362 if (!DestroyVDM(&VdmCB
)) {
363 DPRINT("VDM: failed to cleanly destroy VDM.\n");