msxml: Allow the element implementation to be aggregatable.
[wine/multimedia.git] / dlls / kernel / vxd.c
blobf407e33cd3dd62141546268a176c3e4048ebe040
1 /*
2 * Win32 VxD functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
6 * Copyright 1998 Patrik Stridvall
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <string.h>
35 #include <stdarg.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winternl.h"
41 #include "winioctl.h"
42 #include "kernel_private.h"
43 #include "wine/library.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
50 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
51 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
53 struct vxd_module
55 dev_t dev;
56 ino_t ino;
57 HANDLE handle;
58 HMODULE module;
59 DeviceIoProc proc;
62 struct vxdcall_service
64 WCHAR name[12];
65 DWORD service;
66 HMODULE module;
67 VxDCallProc proc;
70 #define MAX_VXD_MODULES 32
72 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
74 static struct vxdcall_service vxd_services[] =
76 { {'v','m','m','.','v','x','d',0}, 0x0001, NULL, NULL },
77 { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
80 #define NB_VXD_SERVICES (sizeof(vxd_services)/sizeof(vxd_services[0]))
82 static CRITICAL_SECTION vxd_section;
83 static CRITICAL_SECTION_DEBUG critsect_debug =
85 0, 0, &vxd_section,
86 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": vxd_section") }
89 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
92 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
93 static HANDLE open_vxd_handle( LPCWSTR name )
95 const char *dir = wine_get_server_dir();
96 int len;
97 HANDLE ret;
98 NTSTATUS status;
99 OBJECT_ATTRIBUTES attr;
100 UNICODE_STRING nameW;
101 IO_STATUS_BLOCK io;
103 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
104 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
105 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
106 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
108 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
109 return 0;
111 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
112 nameW.Buffer[len-1] = '/';
113 strcpyW( nameW.Buffer + len, name );
115 attr.Length = sizeof(attr);
116 attr.RootDirectory = 0;
117 attr.Attributes = 0;
118 attr.ObjectName = &nameW;
119 attr.SecurityDescriptor = NULL;
120 attr.SecurityQualityOfService = NULL;
122 status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0,
123 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
124 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
125 if (status)
127 ret = 0;
128 SetLastError( RtlNtStatusToDosError(status) );
130 RtlFreeUnicodeString( &nameW );
131 return ret;
134 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
135 static DeviceIoProc get_vxd_proc( HANDLE handle )
137 struct stat st;
138 DeviceIoProc ret = NULL;
139 int status, i, fd;
141 status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
142 if (status)
144 SetLastError( RtlNtStatusToDosError(status) );
145 return NULL;
147 if (fstat( fd, &st ) == -1)
149 wine_server_release_fd( handle, fd );
150 SetLastError( ERROR_INVALID_HANDLE );
151 return NULL;
153 wine_server_release_fd( handle, fd );
155 RtlEnterCriticalSection( &vxd_section );
157 for (i = 0; i < MAX_VXD_MODULES; i++)
159 if (!vxd_modules[i].module) break;
160 if (vxd_modules[i].dev == st.st_dev && vxd_modules[i].ino == st.st_ino)
162 if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
163 goto done;
166 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
167 /* Let's wait to find out if there are actually apps out there that try to share */
168 /* VxD handles between processes, before we go to the trouble of implementing it. */
169 ERR( "handle %p not found in module list, inherited from another process?\n", handle );
171 done:
172 RtlLeaveCriticalSection( &vxd_section );
173 return ret;
177 /* load a VxD and return a file handle to it */
178 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
180 static const WCHAR dotVxDW[] = {'.','v','x','d',0};
181 int i;
182 HANDLE handle;
183 HMODULE module;
184 WCHAR *p, name[16];
186 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
188 SetLastError( ERROR_FILE_NOT_FOUND );
189 return 0;
192 /* normalize the filename */
194 if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
195 strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
197 SetLastError( ERROR_FILE_NOT_FOUND );
198 return 0;
200 strcpyW( name, filenameW );
201 strlwrW( name );
202 p = strchrW( name, '.' );
203 if (!p) strcatW( name, dotVxDW );
204 else if (strcmpiW( p, dotVxDW )) /* existing extension has to be .vxd */
206 SetLastError( ERROR_FILE_NOT_FOUND );
207 return 0;
210 /* try to load the module first */
212 if (!(module = LoadLibraryW( name )))
214 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
215 debugstr_w(name) );
216 SetLastError( ERROR_FILE_NOT_FOUND );
217 return 0;
220 /* register the module in the global list if necessary */
222 RtlEnterCriticalSection( &vxd_section );
224 for (i = 0; i < MAX_VXD_MODULES; i++)
226 if (vxd_modules[i].module == module)
228 handle = vxd_modules[i].handle;
229 goto done; /* already registered */
231 if (!vxd_modules[i].module) /* new one, register it */
233 struct stat st;
234 int fd;
236 /* get a file handle to the dummy file */
237 if (!(handle = open_vxd_handle( name )))
239 FreeLibrary( module );
240 goto done;
242 wine_server_handle_to_fd( handle, 0, &fd, NULL );
243 if (fstat( fd, &st ) != -1)
245 vxd_modules[i].dev = st.st_dev;
246 vxd_modules[i].ino = st.st_ino;
248 vxd_modules[i].module = module;
249 vxd_modules[i].handle = handle;
250 vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
251 wine_server_release_fd( handle, fd );
252 goto done;
256 ERR("too many open VxD modules, please report\n" );
257 CloseHandle( handle );
258 FreeLibrary( module );
259 handle = 0;
261 done:
262 RtlLeaveCriticalSection( &vxd_section );
263 if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
264 (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
265 DUP_HANDLE_SAME_ACCESS ))
266 handle = 0;
267 return handle;
271 /***********************************************************************
272 * VxDCall0 (KERNEL32.1)
273 * VxDCall1 (KERNEL32.2)
274 * VxDCall2 (KERNEL32.3)
275 * VxDCall3 (KERNEL32.4)
276 * VxDCall4 (KERNEL32.5)
277 * VxDCall5 (KERNEL32.6)
278 * VxDCall6 (KERNEL32.7)
279 * VxDCall7 (KERNEL32.8)
280 * VxDCall8 (KERNEL32.9)
282 void WINAPI __regs_VxDCall( DWORD service, CONTEXT86 *context )
284 int i;
285 VxDCallProc proc = NULL;
287 RtlEnterCriticalSection( &vxd_section );
288 for (i = 0; i < NB_VXD_SERVICES; i++)
290 if (HIWORD(service) != vxd_services[i].service) continue;
291 if (!vxd_services[i].module) /* need to load it */
293 if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
294 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
296 proc = vxd_services[i].proc;
297 break;
299 RtlLeaveCriticalSection( &vxd_section );
301 if (proc) context->Eax = proc( service, context );
302 else
304 FIXME( "Unknown/unimplemented VxD (%08lx)\n", service);
305 context->Eax = 0xffffffff; /* FIXME */
308 #ifdef DEFINE_REGS_ENTRYPOINT
309 DEFINE_REGS_ENTRYPOINT( VxDCall, 4, 4 );
310 #endif
313 /***********************************************************************
314 * OpenVxDHandle (KERNEL32.@)
316 * This function is supposed to return the corresponding Ring 0
317 * ("kernel") handle for a Ring 3 handle in Win9x.
318 * Evidently, Wine will have problems with this. But we try anyway,
319 * maybe it helps...
321 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
323 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
324 return hHandleRing3;
328 /****************************************************************************
329 * DeviceIoControl (KERNEL32.@)
330 * This is one of those big ugly nasty procedure which can do
331 * a million and one things when it comes to devices. It can also be
332 * used for VxD communication.
334 * A return value of FALSE indicates that something has gone wrong which
335 * GetLastError can decipher.
337 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
338 LPVOID lpvInBuffer, DWORD cbInBuffer,
339 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
340 LPDWORD lpcbBytesReturned,
341 LPOVERLAPPED lpOverlapped)
343 NTSTATUS status;
345 TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
346 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
347 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
349 /* Check if this is a user defined control code for a VxD */
351 if( HIWORD( dwIoControlCode ) == 0 )
353 DeviceIoProc proc = get_vxd_proc( hDevice );
354 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
355 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
356 return FALSE;
359 /* Not a VxD, let ntdll handle it */
361 if (lpOverlapped)
363 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
364 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
365 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
366 dwIoControlCode, lpvInBuffer, cbInBuffer,
367 lpvOutBuffer, cbOutBuffer);
368 else
369 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
370 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
371 dwIoControlCode, lpvInBuffer, cbInBuffer,
372 lpvOutBuffer, cbOutBuffer);
373 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
375 else
377 IO_STATUS_BLOCK iosb;
379 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
380 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
381 dwIoControlCode, lpvInBuffer, cbInBuffer,
382 lpvOutBuffer, cbOutBuffer);
383 else
384 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
385 dwIoControlCode, lpvInBuffer, cbInBuffer,
386 lpvOutBuffer, cbOutBuffer);
387 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
389 if (status) SetLastError( RtlNtStatusToDosError(status) );
390 return !status;