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 *****************************************************************/
15 #define WIN32_NO_STATUS
27 /* GLOBALS ******************************************************************/
30 /* FUNCTIONS *****************************************************************/
32 void PrintString(char* fmt
,...)
38 vsprintf(buffer
, fmt
, ap
);
41 OutputDebugStringA(buffer
);
58 SetVDMCurrentDirectories
70 typedef struct tag_VDM_CONFIG
{
76 //device=%SystemRoot%\system32\himem.sys
78 } VDM_CONFIG
, *PVDM_CONFIG
;
80 typedef struct tag_VDM_AUTOEXEC
{
82 //lh %SystemRoot%\system32\mscdexnt.exe
83 //lh %SystemRoot%\system32\redir
84 //lh %SystemRoot%\system32\dosx
85 } VDM_AUTOEXEC
, *PVDM_AUTOEXEC
;
87 typedef struct tag_VDM_CONTROL_BLOCK
{
91 VDM_AUTOEXEC vdmAutoexec
;
92 PROCESS_INFORMATION ProcessInformation
;
93 CHAR CommandLine
[MAX_PATH
];
94 CHAR CurrentDirectory
[MAX_PATH
];
96 } VDM_CONTROL_BLOCK
, *PVDM_CONTROL_BLOCK
;
100 StartVDM(PVDM_CONTROL_BLOCK vdm
)
103 STARTUPINFOA StartupInfo
;
105 StartupInfo
.cb
= sizeof(StartupInfo
);
106 StartupInfo
.lpReserved
= NULL
;
107 StartupInfo
.lpDesktop
= NULL
;
108 StartupInfo
.lpTitle
= NULL
;
109 StartupInfo
.dwFlags
= 0;
110 StartupInfo
.cbReserved2
= 0;
111 StartupInfo
.lpReserved2
= 0;
113 Result
= CreateProcessA(vdm
->CommandLine
,
122 &vdm
->ProcessInformation
);
124 PrintString("VDM: Failed to execute target process\n");
127 WaitForSingleObject(vdm
->ProcessInformation
.hProcess
, INFINITE
);
128 CloseHandle(vdm
->ProcessInformation
.hProcess
);
129 CloseHandle(vdm
->ProcessInformation
.hThread
);
134 ShutdownVDM(PVDM_CONTROL_BLOCK vdm
)
141 BOOL
ReadConfigForVDM(PVDM_CONTROL_BLOCK vdm
)
147 hFile
= CreateFileW(L
"\\system32\\config.nt",
151 OPEN_ALWAYS
/*OPEN_EXISTING*/,
152 FILE_ATTRIBUTE_NORMAL
,
154 dwError
= GetLastError();
155 if (hFile
== INVALID_HANDLE_VALUE
) {
156 // error with file path or system problem?
159 // we just created a new file, perhaps we should set/write some defaults?
161 if (dwError
== ERROR_ALREADY_EXISTS
) {
162 // read the line entries and cache in some struct...
167 hFile
= CreateFileW(L
"\\system32\\autoexec.nt",
172 FILE_ATTRIBUTE_NORMAL
,
174 dwError
= GetLastError();
175 if (hFile
== INVALID_HANDLE_VALUE
) {
176 // error with file path or system problem?
179 // we just created a new file, perhaps we should set/write some defaults?
181 if (dwError
== ERROR_ALREADY_EXISTS
) {
182 // read the line entries and cache in some struct...
191 LoadConfigDriversForVDM(PVDM_CONFIG vdmConfig
)
199 SetConfigOptionsForVDM(PVDM_AUTOEXEC vdmAutoexec
)
207 CreateVDM(PVDM_CONTROL_BLOCK vdm
)
209 // BOOL result = TRUE;
214 GlobalMemoryStatus(&stat
);
215 if (stat
.dwLength
!= sizeof(MEMORYSTATUS
)) {
216 printf("WARNING: GlobalMemoryStatus() returned unknown structure version, size %ld, expected %d.\n", stat
.dwLength
, sizeof(stat
));
218 printf("Memory Load: %ld percent in use.\n", stat
.dwMemoryLoad
);
219 printf("\t%ld total bytes physical memory.\n", stat
.dwTotalPhys
);
220 printf("\t%ld available physical memory.\n", stat
.dwAvailPhys
);
221 printf("\t%ld total bytes paging file.\n", stat
.dwTotalPageFile
);
222 printf("\t%ld available paging file.\n", stat
.dwAvailPageFile
);
223 printf("\t%lx total bytes virtual memory.\n", stat
.dwTotalVirtual
);
224 printf("\t%lx available bytes virtual memory.\n", stat
.dwAvailVirtual
);
226 #define OUT_OF_HEADROOM 90
227 if (stat
.dwMemoryLoad
> OUT_OF_HEADROOM
) {
228 DPRINT("VDM: system resources deemed to low to start VDM.\n");
236 vdm
->hHeap
= HeapCreate(0, inf
.dwAllocationGranularity
, 0);
237 if (vdm
->hHeap
== NULL
) {
238 DPRINT("VDM: failed to create heap.\n");
242 #define DEFAULT_VDM_IMAGE_SIZE 2000000
243 vdm
->ImageMem
= HeapAlloc(vdm
->hHeap
, 0, DEFAULT_VDM_IMAGE_SIZE
);
244 if (vdm
->ImageMem
== NULL
) {
245 DPRINT("VDM: failed to allocate image memory from heap %x.\n", vdm
->hHeap
);
246 HeapDestroy(vdm
->hHeap
);
254 DestroyVDM(PVDM_CONTROL_BLOCK vdm
)
258 if (vdm
->ImageMem
!= NULL
) {
259 if (HeapFree(vdm
->hHeap
, 0, vdm
->ImageMem
) != FALSE
) {
260 DPRINT("VDM: failed to free memory from heap %x.\n", vdm
->hHeap
);
263 vdm
->ImageMem
= NULL
;
265 if (vdm
->hHeap
!= NULL
) {
266 if (!HeapDestroy(vdm
->hHeap
)) {
267 DPRINT("VDM: failed to destroy heap %x.\n", vdm
->hHeap
);
276 WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nShowCmd
)
278 VDM_CONTROL_BLOCK VdmCB
;
281 BOOL vdmStarted
= FALSE
;
283 WCHAR WelcomeMsg
[RC_STRING_MAX_SIZE
];
284 WCHAR PromptMsg
[RC_STRING_MAX_SIZE
];
285 CHAR InputBuffer
[255];
287 LoadStringW( GetModuleHandle(NULL
), STRING_WelcomeMsg
, WelcomeMsg
,sizeof(WelcomeMsg
) / sizeof(WelcomeMsg
[0]));
288 LoadStringW( GetModuleHandle(NULL
), STRING_PromptMsg
, PromptMsg
,sizeof(PromptMsg
) / sizeof(PromptMsg
[0]));
291 SetConsoleTitleW(L
"ntvdm");
293 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
294 WelcomeMsg
, lstrlenW(WelcomeMsg
), // wcslen(WelcomeMsg),
297 if (!CreateVDM(&VdmCB
)) {
298 DPRINT("VDM: failed to create VDM.\n");
303 ReadConfigForVDM(&VdmCB
);
305 if (!LoadConfigDriversForVDM(&(VdmCB
.vdmConfig
))) {
306 DPRINT("VDM: failed to load configuration drivers.\n");
310 if (!SetConfigOptionsForVDM(&(VdmCB
.vdmAutoexec
))) {
311 DPRINT("VDM: failed to set configuration options.\n");
316 GetSystemDirectoryA(VdmCB
.CommandLine
, MAX_PATH
);
317 strcat(VdmCB
.CommandLine
, "\\hello.exe");
318 GetWindowsDirectoryA(VdmCB
.CurrentDirectory
, MAX_PATH
);
321 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
322 PromptMsg
, lstrlenW(PromptMsg
), // wcslen(PromptMsg),
326 ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
329 if (++i
>= (sizeof(InputBuffer
) - 1)) {
332 } while (InputBuffer
[i
- 1] != '\n');
333 InputBuffer
[i
- 1] = '\0';
335 if (InputBuffer
[0] == 'r' || InputBuffer
[0] == 'R') {
337 if (StartVDM(&VdmCB
)) {
340 DPRINT("VDM: failed to start.\n");
343 DPRINT("VDM: already started.\n");
346 if (InputBuffer
[0] == 's' || InputBuffer
[0] == 'S') {
348 if (ShutdownVDM(&VdmCB
)) {
351 DPRINT("VDM: failed to shutdown.\n");
354 DPRINT("VDM: not started.\n");
357 if (InputBuffer
[0] == 'q' || InputBuffer
[0] == 'Q') {
362 if (!ShutdownVDM(&VdmCB
)) {
363 DPRINT("VDM: failed to cleanly shutdown VDM.\n");
368 if (!DestroyVDM(&VdmCB
)) {
369 DPRINT("VDM: failed to cleanly destroy VDM.\n");