2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: Virtual Device Drivers (VDD) Support
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9 /* INCLUDES *******************************************************************/
20 typedef VOID (WINAPI
*VDD_PROC
)(VOID
);
22 typedef struct _VDD_MODULE
25 VDD_PROC DispatchRoutine
;
26 } VDD_MODULE
, *PVDD_MODULE
;
28 /* PRIVATE VARIABLES **********************************************************/
30 // TODO: Maybe use a linked list.
31 // But the number of elements must be <= MAXUSHORT (MAXWORD)
32 #define MAX_VDD_MODULES 0xFF + 1
33 VDD_MODULE VDDList
[MAX_VDD_MODULES
] = {{NULL
}};
35 // Valid handles of VDD DLLs start at 1 and finish at MAX_VDD_MODULES
36 #define ENTRY_TO_HANDLE(Entry) ((Entry) + 1)
37 #define HANDLE_TO_ENTRY(Handle) ((Handle) - 1)
38 #define IS_VALID_HANDLE(Handle) ((Handle) > 0 && (Handle) <= MAX_VDD_MODULES)
40 /* PRIVATE FUNCTIONS **********************************************************/
42 USHORT
GetNextFreeVDDEntry(VOID
)
44 USHORT Entry
= MAX_VDD_MODULES
;
45 for (Entry
= 0; Entry
< sizeof(VDDList
)/sizeof(VDDList
[0]); ++Entry
)
47 if (VDDList
[Entry
].hDll
== NULL
) break;
52 VOID WINAPI
ThirdPartyVDDBop(LPWORD Stack
)
54 /* Get the Function Number and skip it */
55 BYTE FuncNum
= *(PBYTE
)SEG_OFF_TO_PTR(getCS(), getIP());
66 LPCSTR DllName
= NULL
,
67 InitRoutineName
= NULL
,
68 DispatchRoutineName
= NULL
;
70 VDD_PROC InitRoutine
= NULL
,
71 DispatchRoutine
= NULL
;
73 DPRINT("RegisterModule() called\n");
75 /* Clear the Carry Flag (no error happened so far) */
78 /* Retrieve the next free entry in the table (used later on) */
79 Entry
= GetNextFreeVDDEntry();
80 if (Entry
>= MAX_VDD_MODULES
)
82 DPRINT1("Failed to create a new VDD module entry\n");
88 /* Retrieve the VDD name in DS:SI */
89 DllName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getSI());
91 /* Retrieve the initialization routine API name in ES:DI (optional --> ES=DI=0) */
92 if (TO_LINEAR(getES(), getDI()) != 0)
93 InitRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getES(), getDI());
95 /* Retrieve the dispatch routine API name in DS:BX */
96 DispatchRoutineName
= (LPCSTR
)SEG_OFF_TO_PTR(getDS(), getBX());
98 DPRINT1("DllName = '%s' - InitRoutineName = '%s' - DispatchRoutineName = '%s'\n",
99 (DllName
? DllName
: "n/a"),
100 (InitRoutineName
? InitRoutineName
: "n/a"),
101 (DispatchRoutineName
? DispatchRoutineName
: "n/a"));
103 /* Load the VDD DLL */
104 hDll
= LoadLibraryA(DllName
);
107 DWORD LastError
= GetLastError();
110 if (LastError
== ERROR_NOT_ENOUGH_MEMORY
)
112 DPRINT1("Not enough memory to load DLL '%s'\n", DllName
);
118 DPRINT1("Failed to load DLL '%s'; last error = %d\n", DllName
, LastError
);
124 /* Load the initialization routine if needed */
127 InitRoutine
= (VDD_PROC
)GetProcAddress(hDll
, InitRoutineName
);
128 if (InitRoutine
== NULL
)
130 DPRINT1("Failed to load the initialization routine '%s'\n", InitRoutineName
);
137 /* Load the dispatch routine */
138 DispatchRoutine
= (VDD_PROC
)GetProcAddress(hDll
, DispatchRoutineName
);
139 if (DispatchRoutine
== NULL
)
141 DPRINT1("Failed to load the dispatch routine '%s'\n", DispatchRoutineName
);
147 /* If we arrived there, that means everything is OK */
149 /* Register the VDD DLL */
150 VDDList
[Entry
].hDll
= hDll
;
151 VDDList
[Entry
].DispatchRoutine
= DispatchRoutine
;
153 /* Call the initialization routine if needed */
154 if (InitRoutine
) InitRoutine();
156 /* We succeeded. RetVal will contain a valid VDD DLL handle */
158 RetVal
= ENTRY_TO_HANDLE(Entry
); // Convert the entry to a valid handle
163 /* Unload the VDD DLL */
164 if (hDll
) FreeLibrary(hDll
);
166 /* Set the Carry Flag to indicate that an error happened */
171 // /* Clear the Carry Flag (success) */
178 /* UnRegisterModule */
181 WORD Handle
= getAX();
182 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
184 DPRINT("UnRegisterModule() called\n");
187 if (!IS_VALID_HANDLE(Handle
) || VDDList
[Entry
].hDll
== NULL
)
189 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
195 /* Unregister the VDD DLL */
196 FreeLibrary(VDDList
[Entry
].hDll
);
197 VDDList
[Entry
].hDll
= NULL
;
198 VDDList
[Entry
].DispatchRoutine
= NULL
;
205 WORD Handle
= getAX();
206 WORD Entry
= HANDLE_TO_ENTRY(Handle
); // Convert the handle to a valid entry
208 DPRINT("DispatchCall() called\n");
211 if (!IS_VALID_HANDLE(Handle
) ||
212 VDDList
[Entry
].hDll
== NULL
||
213 VDDList
[Entry
].DispatchRoutine
== NULL
)
215 DPRINT1("Invalid VDD DLL Handle: %d\n", Entry
);
221 /* Call the dispatch routine */
222 VDDList
[Entry
].DispatchRoutine();
228 DPRINT1("Unknown 3rd-party VDD BOP Function: 0x%02X\n", FuncNum
);
235 BOOL
LoadInstallableVDD(VOID
)
237 #define ERROR_MEMORYVDD L"Insufficient memory to load installable Virtual Device Drivers."
238 #define ERROR_REGVDD L"Virtual Device Driver format in the registry is invalid."
239 #define ERROR_LOADVDD L"An installable Virtual Device Driver failed Dll initialization."
247 LPCWSTR VDDKeyName
= L
"SYSTEM\\CurrentControlSet\\Control\\VirtualDeviceDrivers";
248 LPWSTR VDDValueName
= L
"VDD";
249 LPWSTR VDDList
= NULL
;
253 /* Try to open the VDD registry key */
254 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
259 if (Error
== ERROR_FILE_NOT_FOUND
)
261 /* If the key just doesn't exist, don't do anything else */
264 else if (Error
!= ERROR_SUCCESS
)
266 /* The key exists but there was an access error: display an error and quit */
267 DisplayMessage(ERROR_REGVDD
);
272 * Retrieve the size of the VDD registry value
273 * and check that it's of REG_MULTI_SZ type.
275 Error
= RegQueryValueExW(hVDDKey
,
281 if (Error
== ERROR_FILE_NOT_FOUND
)
283 /* If the value just doesn't exist, don't do anything else */
287 else if (Error
!= ERROR_SUCCESS
|| Type
!= REG_MULTI_SZ
)
290 * The value exists but there was an access error or
291 * is of the wrong type: display an error and quit.
293 DisplayMessage(ERROR_REGVDD
);
298 /* Allocate the buffer */
299 BufSize
= (BufSize
< 2*sizeof(WCHAR
) ? 2*sizeof(WCHAR
) : BufSize
);
300 VDDList
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, BufSize
);
303 DisplayMessage(ERROR_MEMORYVDD
);
308 /* Retrieve the list of VDDs to load */
309 if (RegQueryValueExW(hVDDKey
,
314 &BufSize
) != ERROR_SUCCESS
)
316 DisplayMessage(ERROR_REGVDD
);
322 VDDValueName
= VDDList
;
325 DPRINT1("Loading VDD '%S'...", VDDList
);
326 hVDD
= LoadLibraryW(VDDList
);
329 DbgPrint("Failed\n");
330 DisplayMessage(ERROR_LOADVDD
);
334 DbgPrint("Succeeded\n");
336 /* Go to next string */
337 VDDList
+= wcslen(VDDList
) + 1;
339 VDDList
= VDDValueName
;
342 if (VDDList
) HeapFree(GetProcessHeap(), 0, VDDList
);
343 RegCloseKey(hVDDKey
);
347 /* PUBLIC FUNCTIONS ***********************************************************/
349 VOID
VDDSupInitialize(VOID
)
351 /* Register the 3rd-party VDD BOP Handler */
352 RegisterBop(BOP_3RDPARTY
, ThirdPartyVDDBop
);
354 /* Load the installable VDDs from the registry */
355 LoadInstallableVDD();