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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
30 #include <sys/types.h>
40 #include "kernel_private.h"
41 #include "wine/library.h"
42 #include "wine/unicode.h"
43 #include "wine/server.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
48 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
49 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
60 struct vxdcall_service
68 #define MAX_VXD_MODULES 32
70 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
72 static struct vxdcall_service vxd_services[] =
74 { {'v','m','m','.','v','x','d',0}, 0x0001, NULL, NULL },
75 { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
78 #define NB_VXD_SERVICES (sizeof(vxd_services)/sizeof(vxd_services[0]))
80 static CRITICAL_SECTION vxd_section;
81 static CRITICAL_SECTION_DEBUG critsect_debug =
84 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
85 0, 0, { 0, (DWORD)(__FILE__ ": vxd_section") }
87 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
90 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
91 static HANDLE open_vxd_handle( LPCWSTR name )
93 const char *dir = wine_get_server_dir();
99 len2 = WideCharToMultiByte( CP_UNIXCP, 0, name, -1, NULL, 0, NULL, NULL);
100 if (!(unix_name = HeapAlloc( GetProcessHeap(), 0, len1 + len2 + 1 )))
102 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
105 strcpy( unix_name, dir );
106 unix_name[len1] = '/';
107 WideCharToMultiByte( CP_UNIXCP, 0, name, -1, unix_name + len1 + 1, len2, NULL, NULL);
108 ret = FILE_CreateFile( unix_name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
109 OPEN_ALWAYS, 0, 0, TRUE, DRIVE_FIXED );
110 HeapFree( GetProcessHeap(), 0, unix_name );
114 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
115 static DeviceIoProc get_vxd_proc( HANDLE handle )
118 DeviceIoProc ret = NULL;
121 status = wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
124 SetLastError( RtlNtStatusToDosError(status) );
127 if (fstat( fd, &st ) == -1)
129 wine_server_release_fd( handle, fd );
130 SetLastError( ERROR_INVALID_HANDLE );
133 wine_server_release_fd( handle, fd );
135 RtlEnterCriticalSection( &vxd_section );
137 for (i = 0; i < MAX_VXD_MODULES; i++)
139 if (!vxd_modules[i].module) break;
140 if (vxd_modules[i].dev == st.st_dev && vxd_modules[i].ino == st.st_ino)
142 if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
146 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
147 /* Let's wait to find out if there are actually apps out there that try to share */
148 /* VxD handles between processes, before we go to the trouble of implementing it. */
149 ERR( "handle %p not found in module list, inherited from another process?\n", handle );
152 RtlLeaveCriticalSection( &vxd_section );
157 /* load a VxD and return a file handle to it */
158 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
160 static const WCHAR dotVxDW[] = {'.','v','x','d',0};
166 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
168 SetLastError( ERROR_INVALID_PARAMETER );
172 /* normalize the filename */
174 if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
175 strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
177 SetLastError( ERROR_FILE_NOT_FOUND );
180 strcpyW( name, filenameW );
182 p = strchrW( name, '.' );
183 if (!p) strcatW( name, dotVxDW );
184 else if (strcmpW( p, dotVxDW )) /* existing extension has to be .vxd */
186 SetLastError( ERROR_FILE_NOT_FOUND );
190 /* try to load the module first */
192 if (!(module = LoadLibraryW( name )))
194 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
196 SetLastError( ERROR_FILE_NOT_FOUND );
200 /* register the module in the global list if necessary */
202 RtlEnterCriticalSection( &vxd_section );
204 for (i = 0; i < MAX_VXD_MODULES; i++)
206 if (vxd_modules[i].module == module)
208 handle = vxd_modules[i].handle;
209 goto done; /* already registered */
211 if (!vxd_modules[i].module) /* new one, register it */
216 /* get a file handle to the dummy file */
217 if (!(handle = open_vxd_handle( name )))
219 FreeLibrary( module );
222 wine_server_handle_to_fd( handle, 0, &fd, NULL, NULL );
223 if (fstat( fd, &st ) != -1)
225 vxd_modules[i].dev = st.st_dev;
226 vxd_modules[i].ino = st.st_ino;
228 vxd_modules[i].module = module;
229 vxd_modules[i].handle = handle;
230 vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
231 wine_server_release_fd( handle, fd );
236 ERR("too many open VxD modules, please report\n" );
237 CloseHandle( handle );
238 FreeLibrary( module );
242 RtlLeaveCriticalSection( &vxd_section );
243 if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
244 (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
245 DUP_HANDLE_SAME_ACCESS ))
251 /***********************************************************************
252 * VxDCall0 (KERNEL32.1)
253 * VxDCall1 (KERNEL32.2)
254 * VxDCall2 (KERNEL32.3)
255 * VxDCall3 (KERNEL32.4)
256 * VxDCall4 (KERNEL32.5)
257 * VxDCall5 (KERNEL32.6)
258 * VxDCall6 (KERNEL32.7)
259 * VxDCall7 (KERNEL32.8)
260 * VxDCall8 (KERNEL32.9)
262 void VxDCall( DWORD service, CONTEXT86 *context )
265 VxDCallProc proc = NULL;
267 RtlEnterCriticalSection( &vxd_section );
268 for (i = 0; i < NB_VXD_SERVICES; i++)
270 if (HIWORD(service) != vxd_services[i].service) continue;
271 if (!vxd_services[i].module) /* need to load it */
273 if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
274 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
276 proc = vxd_services[i].proc;
279 RtlLeaveCriticalSection( &vxd_section );
281 if (proc) context->Eax = proc( service, context );
284 FIXME( "Unknown/unimplemented VxD (%08lx)\n", service);
285 context->Eax = 0xffffffff; /* FIXME */
290 /***********************************************************************
291 * OpenVxDHandle (KERNEL32.@)
293 * This function is supposed to return the corresponding Ring 0
294 * ("kernel") handle for a Ring 3 handle in Win9x.
295 * Evidently, Wine will have problems with this. But we try anyway,
298 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
300 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
305 /****************************************************************************
306 * DeviceIoControl (KERNEL32.@)
307 * This is one of those big ugly nasty procedure which can do
308 * a million and one things when it comes to devices. It can also be
309 * used for VxD communication.
311 * A return value of FALSE indicates that something has gone wrong which
312 * GetLastError can decipher.
314 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
315 LPVOID lpvInBuffer, DWORD cbInBuffer,
316 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
317 LPDWORD lpcbBytesReturned,
318 LPOVERLAPPED lpOverlapped)
322 TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
323 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
324 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
326 /* Check if this is a user defined control code for a VxD */
328 if( HIWORD( dwIoControlCode ) == 0 )
330 DeviceIoProc proc = get_vxd_proc( hDevice );
331 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
332 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
336 /* Not a VxD, let ntdll handle it */
340 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
341 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
342 dwIoControlCode, lpvInBuffer, cbInBuffer,
343 lpvOutBuffer, cbOutBuffer);
344 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
348 IO_STATUS_BLOCK iosb;
350 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
351 dwIoControlCode, lpvInBuffer, cbInBuffer,
352 lpvOutBuffer, cbOutBuffer);
353 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
355 if (status) SetLastError( RtlNtStatusToDosError(status) );