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
19 #define NTOS_MODE_USER
20 #include <ndk/ntndk.h>
25 /* GLOBALS ******************************************************************/
28 /* FUNCTIONS *****************************************************************/
30 void PrintString(char* fmt
,...)
36 vsprintf(buffer
, fmt
, ap
);
39 OutputDebugStringA(buffer
);
56 SetVDMCurrentDirectories
68 typedef struct tag_VDM_CONFIG
{
74 //device=%SystemRoot%\system32\himem.sys
76 } VDM_CONFIG
, *PVDM_CONFIG
;
78 typedef struct tag_VDM_AUTOEXEC
{
80 //lh %SystemRoot%\system32\mscdexnt.exe
81 //lh %SystemRoot%\system32\redir
82 //lh %SystemRoot%\system32\dosx
83 } VDM_AUTOEXEC
, *PVDM_AUTOEXEC
;
85 typedef struct tag_VDM_CONTROL_BLOCK
{
89 VDM_AUTOEXEC vdmAutoexec
;
90 PROCESS_INFORMATION ProcessInformation
;
91 CHAR CommandLine
[MAX_PATH
];
92 CHAR CurrentDirectory
[MAX_PATH
];
94 } VDM_CONTROL_BLOCK
, *PVDM_CONTROL_BLOCK
;
98 StartVDM(PVDM_CONTROL_BLOCK vdm
)
101 STARTUPINFOA StartupInfo
;
103 StartupInfo
.cb
= sizeof(StartupInfo
);
104 StartupInfo
.lpReserved
= NULL
;
105 StartupInfo
.lpDesktop
= NULL
;
106 StartupInfo
.lpTitle
= NULL
;
107 StartupInfo
.dwFlags
= 0;
108 StartupInfo
.cbReserved2
= 0;
109 StartupInfo
.lpReserved2
= 0;
111 Result
= CreateProcessA(vdm
->CommandLine
,
120 &vdm
->ProcessInformation
);
122 PrintString("VDM: Failed to execute target process\n");
125 WaitForSingleObject(vdm
->ProcessInformation
.hProcess
, INFINITE
);
126 CloseHandle(vdm
->ProcessInformation
.hProcess
);
127 CloseHandle(vdm
->ProcessInformation
.hThread
);
132 ShutdownVDM(PVDM_CONTROL_BLOCK vdm
)
139 BOOL
ReadConfigForVDM(PVDM_CONTROL_BLOCK vdm
)
145 hFile
= CreateFileW(L
"\\system32\\config.nt",
149 OPEN_ALWAYS
/*OPEN_EXISTING*/,
150 FILE_ATTRIBUTE_NORMAL
,
152 dwError
= GetLastError();
153 if (hFile
== INVALID_HANDLE_VALUE
) {
154 // error with file path or system problem?
157 // we just created a new file, perhaps we should set/write some defaults?
159 if (dwError
== ERROR_ALREADY_EXISTS
) {
160 // read the line entries and cache in some struct...
165 hFile
= CreateFileW(L
"\\system32\\autoexec.nt",
170 FILE_ATTRIBUTE_NORMAL
,
172 dwError
= GetLastError();
173 if (hFile
== INVALID_HANDLE_VALUE
) {
174 // error with file path or system problem?
177 // we just created a new file, perhaps we should set/write some defaults?
179 if (dwError
== ERROR_ALREADY_EXISTS
) {
180 // read the line entries and cache in some struct...
189 LoadConfigDriversForVDM(PVDM_CONFIG vdmConfig
)
197 SetConfigOptionsForVDM(PVDM_AUTOEXEC vdmAutoexec
)
205 CreateVDM(PVDM_CONTROL_BLOCK vdm
)
207 // BOOL result = TRUE;
212 GlobalMemoryStatus(&stat
);
213 if (stat
.dwLength
!= sizeof(MEMORYSTATUS
)) {
214 printf("WARNING: GlobalMemoryStatus() returned unknown structure version, size %ld, expected %d.\n", stat
.dwLength
, sizeof(stat
));
216 printf("Memory Load: %ld percent in use.\n", stat
.dwMemoryLoad
);
217 printf("\t%ld total bytes physical memory.\n", stat
.dwTotalPhys
);
218 printf("\t%ld available physical memory.\n", stat
.dwAvailPhys
);
219 printf("\t%ld total bytes paging file.\n", stat
.dwTotalPageFile
);
220 printf("\t%ld available paging file.\n", stat
.dwAvailPageFile
);
221 printf("\t%lx total bytes virtual memory.\n", stat
.dwTotalVirtual
);
222 printf("\t%lx available bytes virtual memory.\n", stat
.dwAvailVirtual
);
224 #define OUT_OF_HEADROOM 90
225 if (stat
.dwMemoryLoad
> OUT_OF_HEADROOM
) {
226 DPRINT("VDM: system resources deemed to low to start VDM.\n");
234 vdm
->hHeap
= HeapCreate(0, inf
.dwAllocationGranularity
, 0);
235 if (vdm
->hHeap
== NULL
) {
236 DPRINT("VDM: failed to create heap.\n");
240 #define DEFAULT_VDM_IMAGE_SIZE 2000000
241 vdm
->ImageMem
= HeapAlloc(vdm
->hHeap
, 0, DEFAULT_VDM_IMAGE_SIZE
);
242 if (vdm
->ImageMem
== NULL
) {
243 DPRINT("VDM: failed to allocate image memory from heap %x.\n", vdm
->hHeap
);
244 HeapDestroy(vdm
->hHeap
);
252 DestroyVDM(PVDM_CONTROL_BLOCK vdm
)
256 if (vdm
->ImageMem
!= NULL
) {
257 if (HeapFree(vdm
->hHeap
, 0, vdm
->ImageMem
) != FALSE
) {
258 DPRINT("VDM: failed to free memory from heap %x.\n", vdm
->hHeap
);
261 vdm
->ImageMem
= NULL
;
263 if (vdm
->hHeap
!= NULL
) {
264 if (!HeapDestroy(vdm
->hHeap
)) {
265 DPRINT("VDM: failed to destroy heap %x.\n", vdm
->hHeap
);
274 WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nShowCmd
)
276 VDM_CONTROL_BLOCK VdmCB
;
279 BOOL vdmStarted
= FALSE
;
281 WCHAR WelcomeMsg
[RC_STRING_MAX_SIZE
];
282 WCHAR PromptMsg
[RC_STRING_MAX_SIZE
];
283 CHAR InputBuffer
[255];
285 LoadStringW( GetModuleHandle(NULL
), STRING_WelcomeMsg
, WelcomeMsg
,sizeof(WelcomeMsg
) / sizeof(WelcomeMsg
[0]));
286 LoadStringW( GetModuleHandle(NULL
), STRING_PromptMsg
, PromptMsg
,sizeof(PromptMsg
) / sizeof(PromptMsg
[0]));
289 SetConsoleTitleW(L
"ntvdm");
291 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
292 WelcomeMsg
, lstrlenW(WelcomeMsg
), // wcslen(WelcomeMsg),
295 if (!CreateVDM(&VdmCB
)) {
296 DPRINT("VDM: failed to create VDM.\n");
301 ReadConfigForVDM(&VdmCB
);
303 if (!LoadConfigDriversForVDM(&(VdmCB
.vdmConfig
))) {
304 DPRINT("VDM: failed to load configuration drivers.\n");
308 if (!SetConfigOptionsForVDM(&(VdmCB
.vdmAutoexec
))) {
309 DPRINT("VDM: failed to set configuration options.\n");
314 GetSystemDirectoryA(VdmCB
.CommandLine
, MAX_PATH
);
315 strcat(VdmCB
.CommandLine
, "\\hello.exe");
316 GetWindowsDirectoryA(VdmCB
.CurrentDirectory
, MAX_PATH
);
319 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
320 PromptMsg
, lstrlenW(PromptMsg
), // wcslen(PromptMsg),
324 ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
327 if (++i
>= (sizeof(InputBuffer
) - 1)) {
330 } while (InputBuffer
[i
- 1] != '\n');
331 InputBuffer
[i
- 1] = '\0';
333 if (InputBuffer
[0] == 'r' || InputBuffer
[0] == 'R') {
335 if (StartVDM(&VdmCB
)) {
338 DPRINT("VDM: failed to start.\n");
341 DPRINT("VDM: already started.\n");
344 if (InputBuffer
[0] == 's' || InputBuffer
[0] == 'S') {
346 if (ShutdownVDM(&VdmCB
)) {
349 DPRINT("VDM: failed to shutdown.\n");
352 DPRINT("VDM: not started.\n");
355 if (InputBuffer
[0] == 'q' || InputBuffer
[0] == 'Q') {
360 if (!ShutdownVDM(&VdmCB
)) {
361 DPRINT("VDM: failed to cleanly shutdown VDM.\n");
366 if (!DestroyVDM(&VdmCB
)) {
367 DPRINT("VDM: failed to cleanly destroy VDM.\n");