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>
39 #include "kernel_private.h"
40 #include "wine/library.h"
41 #include "wine/unicode.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(vxd);
47 typedef BOOL (WINAPI *DeviceIoProc)(DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
48 typedef DWORD (WINAPI *VxDCallProc)(DWORD, CONTEXT86 *);
59 struct vxdcall_service
67 #define MAX_VXD_MODULES 32
69 static struct vxd_module vxd_modules[MAX_VXD_MODULES];
71 static struct vxdcall_service vxd_services[] =
73 { {'v','m','m','.','v','x','d',0}, 0x0001, NULL, NULL },
74 { {'v','w','i','n','3','2','.','v','x','d',0}, 0x002a, NULL, NULL }
77 #define NB_VXD_SERVICES (sizeof(vxd_services)/sizeof(vxd_services[0]))
79 static CRITICAL_SECTION vxd_section;
80 static CRITICAL_SECTION_DEBUG critsect_debug =
83 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
84 0, 0, { 0, (DWORD)(__FILE__ ": vxd_section") }
86 static CRITICAL_SECTION vxd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
89 /* create a file handle to represent a VxD, by opening a dummy file in the wineserver directory */
90 static HANDLE open_vxd_handle( LPCWSTR name )
92 const char *dir = wine_get_server_dir();
96 OBJECT_ATTRIBUTES attr;
100 len = MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, NULL, 0 );
101 nameW.Length = (len + 1 + strlenW( name )) * sizeof(WCHAR);
102 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
103 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.Length )))
105 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
108 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer, len );
109 nameW.Buffer[len-1] = '/';
110 strcpyW( nameW.Buffer + len, name );
112 attr.Length = sizeof(attr);
113 attr.RootDirectory = 0;
115 attr.ObjectName = &nameW;
116 attr.SecurityDescriptor = NULL;
117 attr.SecurityQualityOfService = NULL;
119 status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0,
120 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
121 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
125 SetLastError( RtlNtStatusToDosError(status) );
127 RtlFreeUnicodeString( &nameW );
131 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
132 static DeviceIoProc get_vxd_proc( HANDLE handle )
135 DeviceIoProc ret = NULL;
138 status = wine_server_handle_to_fd( handle, 0, &fd, NULL );
141 SetLastError( RtlNtStatusToDosError(status) );
144 if (fstat( fd, &st ) == -1)
146 wine_server_release_fd( handle, fd );
147 SetLastError( ERROR_INVALID_HANDLE );
150 wine_server_release_fd( handle, fd );
152 RtlEnterCriticalSection( &vxd_section );
154 for (i = 0; i < MAX_VXD_MODULES; i++)
156 if (!vxd_modules[i].module) break;
157 if (vxd_modules[i].dev == st.st_dev && vxd_modules[i].ino == st.st_ino)
159 if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
163 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
164 /* Let's wait to find out if there are actually apps out there that try to share */
165 /* VxD handles between processes, before we go to the trouble of implementing it. */
166 ERR( "handle %p not found in module list, inherited from another process?\n", handle );
169 RtlLeaveCriticalSection( &vxd_section );
174 /* load a VxD and return a file handle to it */
175 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
177 static const WCHAR dotVxDW[] = {'.','v','x','d',0};
183 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
185 SetLastError( ERROR_FILE_NOT_FOUND );
189 /* normalize the filename */
191 if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
192 strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
194 SetLastError( ERROR_FILE_NOT_FOUND );
197 strcpyW( name, filenameW );
199 p = strchrW( name, '.' );
200 if (!p) strcatW( name, dotVxDW );
201 else if (strcmpW( p, dotVxDW )) /* existing extension has to be .vxd */
203 SetLastError( ERROR_FILE_NOT_FOUND );
207 /* try to load the module first */
209 if (!(module = LoadLibraryW( name )))
211 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
213 SetLastError( ERROR_FILE_NOT_FOUND );
217 /* register the module in the global list if necessary */
219 RtlEnterCriticalSection( &vxd_section );
221 for (i = 0; i < MAX_VXD_MODULES; i++)
223 if (vxd_modules[i].module == module)
225 handle = vxd_modules[i].handle;
226 goto done; /* already registered */
228 if (!vxd_modules[i].module) /* new one, register it */
233 /* get a file handle to the dummy file */
234 if (!(handle = open_vxd_handle( name )))
236 FreeLibrary( module );
239 wine_server_handle_to_fd( handle, 0, &fd, NULL );
240 if (fstat( fd, &st ) != -1)
242 vxd_modules[i].dev = st.st_dev;
243 vxd_modules[i].ino = st.st_ino;
245 vxd_modules[i].module = module;
246 vxd_modules[i].handle = handle;
247 vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
248 wine_server_release_fd( handle, fd );
253 ERR("too many open VxD modules, please report\n" );
254 CloseHandle( handle );
255 FreeLibrary( module );
259 RtlLeaveCriticalSection( &vxd_section );
260 if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
261 (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
262 DUP_HANDLE_SAME_ACCESS ))
268 /***********************************************************************
269 * VxDCall0 (KERNEL32.1)
270 * VxDCall1 (KERNEL32.2)
271 * VxDCall2 (KERNEL32.3)
272 * VxDCall3 (KERNEL32.4)
273 * VxDCall4 (KERNEL32.5)
274 * VxDCall5 (KERNEL32.6)
275 * VxDCall6 (KERNEL32.7)
276 * VxDCall7 (KERNEL32.8)
277 * VxDCall8 (KERNEL32.9)
279 void VxDCall( DWORD service, CONTEXT86 *context )
282 VxDCallProc proc = NULL;
284 RtlEnterCriticalSection( &vxd_section );
285 for (i = 0; i < NB_VXD_SERVICES; i++)
287 if (HIWORD(service) != vxd_services[i].service) continue;
288 if (!vxd_services[i].module) /* need to load it */
290 if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
291 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
293 proc = vxd_services[i].proc;
296 RtlLeaveCriticalSection( &vxd_section );
298 if (proc) context->Eax = proc( service, context );
301 FIXME( "Unknown/unimplemented VxD (%08lx)\n", service);
302 context->Eax = 0xffffffff; /* FIXME */
307 /***********************************************************************
308 * OpenVxDHandle (KERNEL32.@)
310 * This function is supposed to return the corresponding Ring 0
311 * ("kernel") handle for a Ring 3 handle in Win9x.
312 * Evidently, Wine will have problems with this. But we try anyway,
315 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
317 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
322 /****************************************************************************
323 * DeviceIoControl (KERNEL32.@)
324 * This is one of those big ugly nasty procedure which can do
325 * a million and one things when it comes to devices. It can also be
326 * used for VxD communication.
328 * A return value of FALSE indicates that something has gone wrong which
329 * GetLastError can decipher.
331 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
332 LPVOID lpvInBuffer, DWORD cbInBuffer,
333 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
334 LPDWORD lpcbBytesReturned,
335 LPOVERLAPPED lpOverlapped)
339 TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
340 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
341 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
343 /* Check if this is a user defined control code for a VxD */
345 if( HIWORD( dwIoControlCode ) == 0 )
347 DeviceIoProc proc = get_vxd_proc( hDevice );
348 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
349 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
353 /* Not a VxD, let ntdll handle it */
357 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
358 NULL, NULL, (PIO_STATUS_BLOCK)lpOverlapped,
359 dwIoControlCode, lpvInBuffer, cbInBuffer,
360 lpvOutBuffer, cbOutBuffer);
361 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
365 IO_STATUS_BLOCK iosb;
367 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
368 dwIoControlCode, lpvInBuffer, cbInBuffer,
369 lpvOutBuffer, cbOutBuffer);
370 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
372 if (status) SetLastError( RtlNtStatusToDosError(status) );