push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / kernel32 / vxd.c
blob1ce1060a762cbf0ea429479a8ad7f6569e7b8ddb
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 LARGE_INTEGER index;
56 HANDLE handle;
57 HMODULE module;
58 DeviceIoProc proc;
61 struct vxdcall_service
63 WCHAR name[12];
64 DWORD service;
65 HMODULE module;
66 VxDCallProc proc;
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 =
84 0, 0, &vxd_section,
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 static const WCHAR prefixW[] = {'\\','?','?','\\','u','n','i','x'};
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 = sizeof(prefixW) + (len + strlenW( name )) * sizeof(WCHAR);
105 nameW.MaximumLength = nameW.Length + sizeof(WCHAR);
106 if (!(nameW.Buffer = HeapAlloc( GetProcessHeap(), 0, nameW.MaximumLength )))
108 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
109 return 0;
111 memcpy( nameW.Buffer, prefixW, sizeof(prefixW) );
112 MultiByteToWideChar( CP_UNIXCP, 0, dir, -1, nameW.Buffer + sizeof(prefixW)/sizeof(WCHAR), len );
113 len += sizeof(prefixW) / sizeof(WCHAR);
114 nameW.Buffer[len-1] = '/';
115 strcpyW( nameW.Buffer + len, name );
117 attr.Length = sizeof(attr);
118 attr.RootDirectory = 0;
119 attr.Attributes = 0;
120 attr.ObjectName = &nameW;
121 attr.SecurityDescriptor = NULL;
122 attr.SecurityQualityOfService = NULL;
124 status = NtCreateFile( &ret, 0, &attr, &io, NULL, 0,
125 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF,
126 FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
127 if (status)
129 ret = 0;
130 SetLastError( RtlNtStatusToDosError(status) );
132 RtlFreeUnicodeString( &nameW );
133 return ret;
136 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
137 static DeviceIoProc get_vxd_proc( HANDLE handle )
139 DeviceIoProc ret = NULL;
140 int status, i;
141 IO_STATUS_BLOCK io;
142 FILE_INTERNAL_INFORMATION info;
144 status = NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation );
145 if (status)
147 SetLastError( RtlNtStatusToDosError(status) );
148 return NULL;
151 RtlEnterCriticalSection( &vxd_section );
153 for (i = 0; i < MAX_VXD_MODULES; i++)
155 if (!vxd_modules[i].module) break;
156 if (vxd_modules[i].index.QuadPart == info.IndexNumber.QuadPart)
158 if (!(ret = vxd_modules[i].proc)) SetLastError( ERROR_INVALID_FUNCTION );
159 goto done;
162 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
163 /* Let's wait to find out if there are actually apps out there that try to share */
164 /* VxD handles between processes, before we go to the trouble of implementing it. */
165 ERR( "handle %p not found in module list, inherited from another process?\n", handle );
167 done:
168 RtlLeaveCriticalSection( &vxd_section );
169 return ret;
173 /* load a VxD and return a file handle to it */
174 HANDLE VXD_Open( LPCWSTR filenameW, DWORD access, SECURITY_ATTRIBUTES *sa )
176 static const WCHAR dotVxDW[] = {'.','v','x','d',0};
177 int i;
178 HANDLE handle;
179 HMODULE module;
180 WCHAR *p, name[16];
182 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
184 SetLastError( ERROR_FILE_NOT_FOUND );
185 return 0;
188 /* normalize the filename */
190 if (strlenW( filenameW ) >= sizeof(name)/sizeof(WCHAR) - 4 ||
191 strchrW( filenameW, '/' ) || strchrW( filenameW, '\\' ))
193 SetLastError( ERROR_FILE_NOT_FOUND );
194 return 0;
196 strcpyW( name, filenameW );
197 strlwrW( name );
198 p = strchrW( name, '.' );
199 if (!p) strcatW( name, dotVxDW );
200 else if (strcmpiW( p, dotVxDW )) /* existing extension has to be .vxd */
202 SetLastError( ERROR_FILE_NOT_FOUND );
203 return 0;
206 /* try to load the module first */
208 if (!(module = LoadLibraryW( name )))
210 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
211 debugstr_w(name) );
212 SetLastError( ERROR_FILE_NOT_FOUND );
213 return 0;
216 /* register the module in the global list if necessary */
218 RtlEnterCriticalSection( &vxd_section );
220 for (i = 0; i < MAX_VXD_MODULES; i++)
222 if (vxd_modules[i].module == module)
224 handle = vxd_modules[i].handle;
225 goto done; /* already registered */
227 if (!vxd_modules[i].module) /* new one, register it */
229 IO_STATUS_BLOCK io;
230 FILE_INTERNAL_INFORMATION info;
232 /* get a file handle to the dummy file */
233 if (!(handle = open_vxd_handle( name )))
235 FreeLibrary( module );
236 goto done;
238 if (!NtQueryInformationFile( handle, &io, &info, sizeof(info), FileInternalInformation ))
239 vxd_modules[i].index = info.IndexNumber;
241 vxd_modules[i].module = module;
242 vxd_modules[i].handle = handle;
243 vxd_modules[i].proc = (DeviceIoProc)GetProcAddress( module, "DeviceIoControl" );
244 goto done;
248 ERR("too many open VxD modules, please report\n" );
249 FreeLibrary( module );
250 handle = 0;
252 done:
253 RtlLeaveCriticalSection( &vxd_section );
254 if (!DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &handle, 0,
255 (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle),
256 DUP_HANDLE_SAME_ACCESS ))
257 handle = 0;
258 return handle;
262 /***********************************************************************
263 * VxDCall0 (KERNEL32.1)
264 * VxDCall1 (KERNEL32.2)
265 * VxDCall2 (KERNEL32.3)
266 * VxDCall3 (KERNEL32.4)
267 * VxDCall4 (KERNEL32.5)
268 * VxDCall5 (KERNEL32.6)
269 * VxDCall6 (KERNEL32.7)
270 * VxDCall7 (KERNEL32.8)
271 * VxDCall8 (KERNEL32.9)
273 void WINAPI __regs_VxDCall( DWORD service, CONTEXT86 *context )
275 unsigned int i;
276 VxDCallProc proc = NULL;
278 RtlEnterCriticalSection( &vxd_section );
279 for (i = 0; i < NB_VXD_SERVICES; i++)
281 if (HIWORD(service) != vxd_services[i].service) continue;
282 if (!vxd_services[i].module) /* need to load it */
284 if ((vxd_services[i].module = LoadLibraryW( vxd_services[i].name )))
285 vxd_services[i].proc = (VxDCallProc)GetProcAddress( vxd_services[i].module, "VxDCall" );
287 proc = vxd_services[i].proc;
288 break;
290 RtlLeaveCriticalSection( &vxd_section );
292 if (proc) context->Eax = proc( service, context );
293 else
295 FIXME( "Unknown/unimplemented VxD (%08x)\n", service);
296 context->Eax = 0xffffffff; /* FIXME */
299 #ifdef DEFINE_REGS_ENTRYPOINT
300 DEFINE_REGS_ENTRYPOINT( VxDCall, 4, 4 )
301 #endif
304 /***********************************************************************
305 * OpenVxDHandle (KERNEL32.@)
307 * This function is supposed to return the corresponding Ring 0
308 * ("kernel") handle for a Ring 3 handle in Win9x.
309 * Evidently, Wine will have problems with this. But we try anyway,
310 * maybe it helps...
312 HANDLE WINAPI OpenVxDHandle(HANDLE hHandleRing3)
314 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3);
315 return hHandleRing3;
319 /****************************************************************************
320 * DeviceIoControl (KERNEL32.@)
321 * This is one of those big ugly nasty procedure which can do
322 * a million and one things when it comes to devices. It can also be
323 * used for VxD communication.
325 * A return value of FALSE indicates that something has gone wrong which
326 * GetLastError can decipher.
328 BOOL WINAPI DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode,
329 LPVOID lpvInBuffer, DWORD cbInBuffer,
330 LPVOID lpvOutBuffer, DWORD cbOutBuffer,
331 LPDWORD lpcbBytesReturned,
332 LPOVERLAPPED lpOverlapped)
334 NTSTATUS status;
336 TRACE( "(%p,%x,%p,%d,%p,%d,%p,%p)\n",
337 hDevice,dwIoControlCode,lpvInBuffer,cbInBuffer,
338 lpvOutBuffer,cbOutBuffer,lpcbBytesReturned,lpOverlapped );
340 /* Check if this is a user defined control code for a VxD */
342 if (HIWORD( dwIoControlCode ) == 0 && (GetVersion() & 0x80000000))
344 DeviceIoProc proc = get_vxd_proc( hDevice );
345 if (proc) return proc( dwIoControlCode, lpvInBuffer, cbInBuffer,
346 lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped );
349 /* Not a VxD, let ntdll handle it */
351 if (lpOverlapped)
353 LPVOID cvalue = ((ULONG_PTR)lpOverlapped->hEvent & 1) ? NULL : lpOverlapped;
354 lpOverlapped->Internal = STATUS_PENDING;
355 lpOverlapped->InternalHigh = 0;
356 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
357 status = NtFsControlFile(hDevice, lpOverlapped->hEvent,
358 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
359 dwIoControlCode, lpvInBuffer, cbInBuffer,
360 lpvOutBuffer, cbOutBuffer);
361 else
362 status = NtDeviceIoControlFile(hDevice, lpOverlapped->hEvent,
363 NULL, cvalue, (PIO_STATUS_BLOCK)lpOverlapped,
364 dwIoControlCode, lpvInBuffer, cbInBuffer,
365 lpvOutBuffer, cbOutBuffer);
366 if (lpcbBytesReturned) *lpcbBytesReturned = lpOverlapped->InternalHigh;
368 else
370 IO_STATUS_BLOCK iosb;
372 if (HIWORD(dwIoControlCode) == FILE_DEVICE_FILE_SYSTEM)
373 status = NtFsControlFile(hDevice, NULL, NULL, NULL, &iosb,
374 dwIoControlCode, lpvInBuffer, cbInBuffer,
375 lpvOutBuffer, cbOutBuffer);
376 else
377 status = NtDeviceIoControlFile(hDevice, NULL, NULL, NULL, &iosb,
378 dwIoControlCode, lpvInBuffer, cbInBuffer,
379 lpvOutBuffer, cbOutBuffer);
380 if (lpcbBytesReturned) *lpcbBytesReturned = iosb.Information;
382 if (status) SetLastError( RtlNtStatusToDosError(status) );
383 return !status;