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
24 #include "wine/port.h"
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.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 *);
61 struct vxdcall_service
69 #define MAX_VXD_MODULES 32
71 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
73 static struct vxdcall_service vxd_services[] =
75 { {'v','m','m','.','v','x','d',0}, 0x0001, NULL, NULL },
76 { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
79 #define NB_VXD_SERVICES (sizeof(vxd_services)/sizeof(vxd_services[0]))
81 static CRITICAL_SECTION vxd_section;
82 static CRITICAL_SECTION_DEBUG critsect_debug =
85 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
86 0, 0, { (DWORD_PTR)(__FILE__ ": vxd_section") }
88 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
91 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
92 static HANDLE open_vxd_handle( LPCWSTR name )
94 const char *dir = wine_get_server_dir();
98 OBJECT_ATTRIBUTES attr;
102 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
103 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
104 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
105 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
107 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
110 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
111 nameW.Buffer[len-1] = '/';
112 strcpyW( nameW.Buffer + len, name );
114 attr.Length = sizeof(attr);
115 attr.RootDirectory = 0;
117 attr.ObjectName = &nameW;
118 attr.SecurityDescriptor = NULL;
119 attr.SecurityQualityOfService = NULL;
121 status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0,
122 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
123 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
127 SetLastError( RtlNtStatusToDosError(status) );
129 RtlFreeUnicodeString( &nameW );
133 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
134 static DeviceIoProc get_vxd_proc( HANDLE handle )
136 DeviceIoProc ret = NULL;
139 FILE_INTERNAL_INFORMATION info;
141 status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
144 SetLastError( RtlNtStatusToDosError(status) );
148 RtlEnterCriticalSection( &vxd_section );
150 for (i = 0; i < MAX_VXD_MODULES; i++)
152 if (!vxd_modules[i].module) break;
153 if (vxd_modules[i].index.QuadPart == info.IndexNumber.QuadPart)
155 if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
159 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
160 /* Let's wait to find out if there are actually apps out there that try to share */
161 /* VxD handles between processes, before we go to the trouble of implementing it. */
162 ERR( "handle %p not found in module list, inherited from another process?\n", handle );
165 RtlLeaveCriticalSection( &vxd_section );
170 /* load a VxD and return a file handle to it */
171 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
173 static const WCHAR dotVxDW[] = {'.','v','x','d',0};
179 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
181 SetLastError( ERROR_FILE_NOT_FOUND );
185 /* normalize the filename */
187 if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
188 strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
190 SetLastError( ERROR_FILE_NOT_FOUND );
193 strcpyW( name, filenameW );
195 p = strchrW( name, '.' );
196 if (!p) strcatW( name, dotVxDW );
197 else if (strcmpiW( p, dotVxDW )) /* existing extension has to be .vxd */
199 SetLastError( ERROR_FILE_NOT_FOUND );
203 /* try to load the module first */
205 if (!(module = LoadLibraryW( name )))
207 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
209 SetLastError( ERROR_FILE_NOT_FOUND );
213 /* register the module in the global list if necessary */
215 RtlEnterCriticalSection( &vxd_section );
217 for (i = 0; i < MAX_VXD_MODULES; i++)
219 if (vxd_modules[i].module == module)
221 handle = vxd_modules[i].handle;
222 goto done; /* already registered */
224 if (!vxd_modules[i].module) /* new one, register it */
227 FILE_INTERNAL_INFORMATION info;
229 /* get a file handle to the dummy file */
230 if (!(handle = open_vxd_handle( name )))
232 FreeLibrary( module );
235 if (!NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation ))
236 vxd_modules[i].index = info.IndexNumber;
238 vxd_modules[i].module = module;
239 vxd_modules[i].handle = handle;
240 vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
245 ERR("too many open VxD modules, please report\n" );
246 CloseHandle( handle );
247 FreeLibrary( module );
251 RtlLeaveCriticalSection( &vxd_section );
252 if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
253 (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
254 DUP_HANDLE_SAME_ACCESS ))
260 /***********************************************************************
261 * VxDCall0 (KERNEL32.1)
262 * VxDCall1 (KERNEL32.2)
263 * VxDCall2 (KERNEL32.3)
264 * VxDCall3 (KERNEL32.4)
265 * VxDCall4 (KERNEL32.5)
266 * VxDCall5 (KERNEL32.6)
267 * VxDCall6 (KERNEL32.7)
268 * VxDCall7 (KERNEL32.8)
269 * VxDCall8 (KERNEL32.9)
271 void WINAPI __regs_VxDCall( DWORD service, CONTEXT86 *context )
274 VxDCallProc proc = NULL;
276 RtlEnterCriticalSection( &vxd_section );
277 for (i = 0; i < NB_VXD_SERVICES; i++)
279 if (HIWORD(service) != vxd_services[i].service) continue;
280 if (!vxd_services[i].module) /* need to load it */
282 if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
283 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
285 proc = vxd_services[i].proc;
288 RtlLeaveCriticalSection( &vxd_section );
290 if (proc) context->Eax = proc( service, context );
293 FIXME( "Unknown/unimplemented VxD (%08x)\n", service);
294 context->Eax = 0xffffffff; /* FIXME */
297 #ifdef DEFINE_REGS_ENTRYPOINT
298 DEFINE_REGS_ENTRYPOINT( VxDCall, 4, 4 );
302 /***********************************************************************
303 * OpenVxDHandle (KERNEL32.@)
305 * This function is supposed to return the corresponding Ring 0
306 * ("kernel") handle for a Ring 3 handle in Win9x.
307 * Evidently, Wine will have problems with this. But we try anyway,
310 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
312 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
317 /****************************************************************************
318 * DeviceIoControl (KERNEL32.@)
319 * This is one of those big ugly nasty procedure which can do
320 * a million and one things when it comes to devices. It can also be
321 * used for VxD communication.
323 * A return value of FALSE indicates that something has gone wrong which
324 * GetLastError can decipher.
326 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
327 LPVOID lpvInBuffer, DWORD cbInBuffer,
328 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
329 LPDWORD lpcbBytesReturned,
330 LPOVERLAPPED lpOverlapped)
334 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
335 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
336 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
338 /* Check if this is a user defined control code for a VxD */
340 if( HIWORD( dwIoControlCode ) == 0 )
342 DeviceIoProc proc = get_vxd_proc( hDevice );
343 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
344 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
348 /* Not a VxD, let ntdll handle it */
352 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
353 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
354 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
355 dwIoControlCode, lpvInBuffer, cbInBuffer,
356 lpvOutBuffer, cbOutBuffer);
358 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
359 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
360 dwIoControlCode, lpvInBuffer, cbInBuffer,
361 lpvOutBuffer, cbOutBuffer);
362 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
366 IO_STATUS_BLOCK iosb;
368 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
369 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
370 dwIoControlCode, lpvInBuffer, cbInBuffer,
371 lpvOutBuffer, cbOutBuffer);
373 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
374 dwIoControlCode, lpvInBuffer, cbInBuffer,
375 lpvOutBuffer, cbOutBuffer);
376 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
378 if (status) SetLastError( RtlNtStatusToDosError(status) );