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 *****************************************************************/
23 /* GLOBALS ******************************************************************/
26 /* FUNCTIONS *****************************************************************/
28 void PrintString(char* fmt
,...)
34 vsprintf(buffer
, fmt
, ap
);
37 OutputDebugStringA(buffer
);
54 SetVDMCurrentDirectories
66 typedef struct tag_VDM_CONFIG
{
72 //device=%SystemRoot%\system32\himem.sys
74 } VDM_CONFIG
, *PVDM_CONFIG
;
76 typedef struct tag_VDM_AUTOEXEC
{
78 //lh %SystemRoot%\system32\mscdexnt.exe
79 //lh %SystemRoot%\system32\redir
80 //lh %SystemRoot%\system32\dosx
81 } VDM_AUTOEXEC
, *PVDM_AUTOEXEC
;
83 typedef struct tag_VDM_CONTROL_BLOCK
{
87 VDM_AUTOEXEC vdmAutoexec
;
88 PROCESS_INFORMATION ProcessInformation
;
89 CHAR CommandLine
[MAX_PATH
];
90 CHAR CurrentDirectory
[MAX_PATH
];
92 } VDM_CONTROL_BLOCK
, *PVDM_CONTROL_BLOCK
;
96 StartVDM(PVDM_CONTROL_BLOCK vdm
)
99 STARTUPINFOA StartupInfo
;
101 StartupInfo
.cb
= sizeof(StartupInfo
);
102 StartupInfo
.lpReserved
= NULL
;
103 StartupInfo
.lpDesktop
= NULL
;
104 StartupInfo
.lpTitle
= NULL
;
105 StartupInfo
.dwFlags
= 0;
106 StartupInfo
.cbReserved2
= 0;
107 StartupInfo
.lpReserved2
= 0;
109 Result
= CreateProcessA(vdm
->CommandLine
,
118 &vdm
->ProcessInformation
);
120 PrintString("VDM: Failed to execute target process\n");
123 WaitForSingleObject(vdm
->ProcessInformation
.hProcess
, INFINITE
);
124 CloseHandle(vdm
->ProcessInformation
.hProcess
);
125 CloseHandle(vdm
->ProcessInformation
.hThread
);
130 ShutdownVDM(PVDM_CONTROL_BLOCK vdm
)
137 BOOL
ReadConfigForVDM(PVDM_CONTROL_BLOCK vdm
)
143 hFile
= CreateFileW(L
"\\system32\\config.nt",
147 OPEN_ALWAYS
/*OPEN_EXISTING*/,
148 FILE_ATTRIBUTE_NORMAL
,
150 dwError
= GetLastError();
151 if (hFile
== INVALID_HANDLE_VALUE
) {
152 // error with file path or system problem?
155 // we just created a new file, perhaps we should set/write some defaults?
157 if (dwError
== ERROR_ALREADY_EXISTS
) {
158 // read the line entries and cache in some struct...
163 hFile
= CreateFileW(L
"\\system32\\autoexec.nt",
168 FILE_ATTRIBUTE_NORMAL
,
170 dwError
= GetLastError();
171 if (hFile
== INVALID_HANDLE_VALUE
) {
172 // error with file path or system problem?
175 // we just created a new file, perhaps we should set/write some defaults?
177 if (dwError
== ERROR_ALREADY_EXISTS
) {
178 // read the line entries and cache in some struct...
187 LoadConfigDriversForVDM(PVDM_CONFIG vdmConfig
)
195 SetConfigOptionsForVDM(PVDM_AUTOEXEC vdmAutoexec
)
203 CreateVDM(PVDM_CONTROL_BLOCK vdm
)
205 // BOOL result = TRUE;
210 GlobalMemoryStatus(&stat
);
211 if (stat
.dwLength
!= sizeof(MEMORYSTATUS
)) {
212 printf("WARNING: GlobalMemoryStatus() returned unknown structure version, size %ld, expected %d.\n", stat
.dwLength
, sizeof(stat
));
214 printf("Memory Load: %ld percent in use.\n", stat
.dwMemoryLoad
);
215 printf("\t%ld total bytes physical memory.\n", stat
.dwTotalPhys
);
216 printf("\t%ld available physical memory.\n", stat
.dwAvailPhys
);
217 printf("\t%ld total bytes paging file.\n", stat
.dwTotalPageFile
);
218 printf("\t%ld available paging file.\n", stat
.dwAvailPageFile
);
219 printf("\t%lx total bytes virtual memory.\n", stat
.dwTotalVirtual
);
220 printf("\t%lx available bytes virtual memory.\n", stat
.dwAvailVirtual
);
222 #define OUT_OF_HEADROOM 90
223 if (stat
.dwMemoryLoad
> OUT_OF_HEADROOM
) {
224 DPRINT("VDM: system resources deemed to low to start VDM.\n");
232 vdm
->hHeap
= HeapCreate(0, inf
.dwAllocationGranularity
, 0);
233 if (vdm
->hHeap
== NULL
) {
234 DPRINT("VDM: failed to create heap.\n");
238 #define DEFAULT_VDM_IMAGE_SIZE 2000000
239 vdm
->ImageMem
= HeapAlloc(vdm
->hHeap
, 0, DEFAULT_VDM_IMAGE_SIZE
);
240 if (vdm
->ImageMem
== NULL
) {
241 DPRINT("VDM: failed to allocate image memory from heap %x.\n", vdm
->hHeap
);
242 HeapDestroy(vdm
->hHeap
);
250 DestroyVDM(PVDM_CONTROL_BLOCK vdm
)
254 if (vdm
->ImageMem
!= NULL
) {
255 if (HeapFree(vdm
->hHeap
, 0, vdm
->ImageMem
) != FALSE
) {
256 DPRINT("VDM: failed to free memory from heap %x.\n", vdm
->hHeap
);
259 vdm
->ImageMem
= NULL
;
261 if (vdm
->hHeap
!= NULL
) {
262 if (!HeapDestroy(vdm
->hHeap
)) {
263 DPRINT("VDM: failed to destroy heap %x.\n", vdm
->hHeap
);
272 WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPSTR lpCmdLine
, int nShowCmd
)
274 VDM_CONTROL_BLOCK VdmCB
;
277 BOOL vdmStarted
= FALSE
;
279 WCHAR WelcomeMsg
[RC_STRING_MAX_SIZE
];
280 WCHAR PromptMsg
[RC_STRING_MAX_SIZE
];
281 CHAR InputBuffer
[255];
283 LoadString( GetModuleHandle(NULL
), STRING_WelcomeMsg
, (LPTSTR
) WelcomeMsg
,sizeof(WelcomeMsg
) / sizeof(WelcomeMsg
[0]));
284 LoadString( GetModuleHandle(NULL
), STRING_PromptMsg
, (LPTSTR
) PromptMsg
,sizeof(PromptMsg
) / sizeof(PromptMsg
[0]));
287 SetConsoleTitleW(L
"ntvdm");
289 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
290 WelcomeMsg
, lstrlenW(WelcomeMsg
), // wcslen(WelcomeMsg),
293 if (!CreateVDM(&VdmCB
)) {
294 DPRINT("VDM: failed to create VDM.\n");
299 ReadConfigForVDM(&VdmCB
);
301 if (!LoadConfigDriversForVDM(&(VdmCB
.vdmConfig
))) {
302 DPRINT("VDM: failed to load configuration drivers.\n");
306 if (!SetConfigOptionsForVDM(&(VdmCB
.vdmAutoexec
))) {
307 DPRINT("VDM: failed to set configuration options.\n");
312 GetSystemDirectoryA(VdmCB
.CommandLine
, MAX_PATH
);
313 strcat(VdmCB
.CommandLine
, "\\hello.exe");
314 GetWindowsDirectoryA(VdmCB
.CurrentDirectory
, MAX_PATH
);
317 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
318 PromptMsg
, lstrlenW(PromptMsg
), // wcslen(PromptMsg),
322 ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE
),
325 if (++i
>= (sizeof(InputBuffer
) - 1)) {
328 } while (InputBuffer
[i
- 1] != '\n');
329 InputBuffer
[i
- 1] = '\0';
331 if (InputBuffer
[0] == 'r' || InputBuffer
[0] == 'R') {
333 if (StartVDM(&VdmCB
)) {
336 DPRINT("VDM: failed to start.\n");
339 DPRINT("VDM: already started.\n");
342 if (InputBuffer
[0] == 's' || InputBuffer
[0] == 'S') {
344 if (ShutdownVDM(&VdmCB
)) {
347 DPRINT("VDM: failed to shutdown.\n");
350 DPRINT("VDM: not started.\n");
353 if (InputBuffer
[0] == 'q' || InputBuffer
[0] == 'Q') {
358 if (!ShutdownVDM(&VdmCB
)) {
359 DPRINT("VDM: failed to cleanly shutdown VDM.\n");
364 if (!DestroyVDM(&VdmCB
)) {
365 DPRINT("VDM: failed to cleanly destroy VDM.\n");