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();
97 OBJECT_ATTRIBUTES attr
;
101 len
= MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, NULL
, 0 );
102 nameW
.Length
= (len
+ 1 + strlenW( name
)) * sizeof(WCHAR
);
103 nameW
.MaximumLength
= nameW
.Length
+ sizeof(WCHAR
);
104 if (!(nameW
.Buffer
= HeapAlloc( GetProcessHeap(), 0, nameW
.Length
)))
106 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
109 MultiByteToWideChar( CP_UNIXCP
, 0, dir
, -1, nameW
.Buffer
, len
);
110 nameW
.Buffer
[len
-1] = '/';
111 strcpyW( nameW
.Buffer
+ len
, name
);
113 attr
.Length
= sizeof(attr
);
114 attr
.RootDirectory
= 0;
116 attr
.ObjectName
= &nameW
;
117 attr
.SecurityDescriptor
= NULL
;
118 attr
.SecurityQualityOfService
= NULL
;
120 status
= NtCreateFile( &ret
, 0, &attr
, &io
, NULL
, 0,
121 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN_IF
,
122 FILE_SYNCHRONOUS_IO_ALERT
, NULL
, 0 );
126 SetLastError( RtlNtStatusToDosError(status
) );
128 RtlFreeUnicodeString( &nameW
);
132 /* retrieve the DeviceIoControl function for a Vxd given a file handle */
133 static DeviceIoProc
get_vxd_proc( HANDLE handle
)
136 DeviceIoProc ret
= NULL
;
139 status
= wine_server_handle_to_fd( handle
, 0, &fd
, NULL
, NULL
);
142 SetLastError( RtlNtStatusToDosError(status
) );
145 if (fstat( fd
, &st
) == -1)
147 wine_server_release_fd( handle
, fd
);
148 SetLastError( ERROR_INVALID_HANDLE
);
151 wine_server_release_fd( handle
, fd
);
153 RtlEnterCriticalSection( &vxd_section
);
155 for (i
= 0; i
< MAX_VXD_MODULES
; i
++)
157 if (!vxd_modules
[i
].module
) break;
158 if (vxd_modules
[i
].dev
== st
.st_dev
&& vxd_modules
[i
].ino
== st
.st_ino
)
160 if (!(ret
= vxd_modules
[i
].proc
)) SetLastError( ERROR_INVALID_FUNCTION
);
164 /* FIXME: Here we could go through the directory to find the VxD name and load it. */
165 /* Let's wait to find out if there are actually apps out there that try to share */
166 /* VxD handles between processes, before we go to the trouble of implementing it. */
167 ERR( "handle %p not found in module list, inherited from another process?\n", handle
);
170 RtlLeaveCriticalSection( &vxd_section
);
175 /* load a VxD and return a file handle to it */
176 HANDLE
VXD_Open( LPCWSTR filenameW
, DWORD access
, SECURITY_ATTRIBUTES
*sa
)
178 static const WCHAR dotVxDW
[] = {'.','v','x','d',0};
184 if (!(GetVersion() & 0x80000000)) /* there are no VxDs on NT */
186 SetLastError( ERROR_INVALID_PARAMETER
);
190 /* normalize the filename */
192 if (strlenW( filenameW
) >= sizeof(name
)/sizeof(WCHAR
) - 4 ||
193 strchrW( filenameW
, '/' ) || strchrW( filenameW
, '\\' ))
195 SetLastError( ERROR_FILE_NOT_FOUND
);
198 strcpyW( name
, filenameW
);
200 p
= strchrW( name
, '.' );
201 if (!p
) strcatW( name
, dotVxDW
);
202 else if (strcmpW( p
, dotVxDW
)) /* existing extension has to be .vxd */
204 SetLastError( ERROR_FILE_NOT_FOUND
);
208 /* try to load the module first */
210 if (!(module
= LoadLibraryW( name
)))
212 FIXME( "Unknown/unsupported VxD %s. Try setting Windows version to 'nt40' or 'win31'.\n",
214 SetLastError( ERROR_FILE_NOT_FOUND
);
218 /* register the module in the global list if necessary */
220 RtlEnterCriticalSection( &vxd_section
);
222 for (i
= 0; i
< MAX_VXD_MODULES
; i
++)
224 if (vxd_modules
[i
].module
== module
)
226 handle
= vxd_modules
[i
].handle
;
227 goto done
; /* already registered */
229 if (!vxd_modules
[i
].module
) /* new one, register it */
234 /* get a file handle to the dummy file */
235 if (!(handle
= open_vxd_handle( name
)))
237 FreeLibrary( module
);
240 wine_server_handle_to_fd( handle
, 0, &fd
, NULL
, NULL
);
241 if (fstat( fd
, &st
) != -1)
243 vxd_modules
[i
].dev
= st
.st_dev
;
244 vxd_modules
[i
].ino
= st
.st_ino
;
246 vxd_modules
[i
].module
= module
;
247 vxd_modules
[i
].handle
= handle
;
248 vxd_modules
[i
].proc
= (DeviceIoProc
)GetProcAddress( module
, "DeviceIoControl" );
249 wine_server_release_fd( handle
, fd
);
254 ERR("too many open VxD modules, please report\n" );
255 CloseHandle( handle
);
256 FreeLibrary( module
);
260 RtlLeaveCriticalSection( &vxd_section
);
261 if (!DuplicateHandle( GetCurrentProcess(), handle
, GetCurrentProcess(), &handle
, 0,
262 (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
),
263 DUP_HANDLE_SAME_ACCESS
))
269 /***********************************************************************
270 * VxDCall0 (KERNEL32.1)
271 * VxDCall1 (KERNEL32.2)
272 * VxDCall2 (KERNEL32.3)
273 * VxDCall3 (KERNEL32.4)
274 * VxDCall4 (KERNEL32.5)
275 * VxDCall5 (KERNEL32.6)
276 * VxDCall6 (KERNEL32.7)
277 * VxDCall7 (KERNEL32.8)
278 * VxDCall8 (KERNEL32.9)
280 void VxDCall( DWORD service
, CONTEXT86
*context
)
283 VxDCallProc proc
= NULL
;
285 RtlEnterCriticalSection( &vxd_section
);
286 for (i
= 0; i
< NB_VXD_SERVICES
; i
++)
288 if (HIWORD(service
) != vxd_services
[i
].service
) continue;
289 if (!vxd_services
[i
].module
) /* need to load it */
291 if ((vxd_services
[i
].module
= LoadLibraryW( vxd_services
[i
].name
)))
292 vxd_services
[i
].proc
= (VxDCallProc
)GetProcAddress( vxd_services
[i
].module
, "VxDCall" );
294 proc
= vxd_services
[i
].proc
;
297 RtlLeaveCriticalSection( &vxd_section
);
299 if (proc
) context
->Eax
= proc( service
, context
);
302 FIXME( "Unknown/unimplemented VxD (%08lx)\n", service
);
303 context
->Eax
= 0xffffffff; /* FIXME */
308 /***********************************************************************
309 * OpenVxDHandle (KERNEL32.@)
311 * This function is supposed to return the corresponding Ring 0
312 * ("kernel") handle for a Ring 3 handle in Win9x.
313 * Evidently, Wine will have problems with this. But we try anyway,
316 HANDLE WINAPI
OpenVxDHandle(HANDLE hHandleRing3
)
318 FIXME( "(%p), stub! (returning Ring 3 handle instead of Ring 0)\n", hHandleRing3
);
323 /****************************************************************************
324 * DeviceIoControl (KERNEL32.@)
325 * This is one of those big ugly nasty procedure which can do
326 * a million and one things when it comes to devices. It can also be
327 * used for VxD communication.
329 * A return value of FALSE indicates that something has gone wrong which
330 * GetLastError can decipher.
332 BOOL WINAPI
DeviceIoControl(HANDLE hDevice
, DWORD dwIoControlCode
,
333 LPVOID lpvInBuffer
, DWORD cbInBuffer
,
334 LPVOID lpvOutBuffer
, DWORD cbOutBuffer
,
335 LPDWORD lpcbBytesReturned
,
336 LPOVERLAPPED lpOverlapped
)
340 TRACE( "(%p,%lx,%p,%ld,%p,%ld,%p,%p)\n",
341 hDevice
,dwIoControlCode
,lpvInBuffer
,cbInBuffer
,
342 lpvOutBuffer
,cbOutBuffer
,lpcbBytesReturned
,lpOverlapped
);
344 /* Check if this is a user defined control code for a VxD */
346 if( HIWORD( dwIoControlCode
) == 0 )
348 DeviceIoProc proc
= get_vxd_proc( hDevice
);
349 if (proc
) return proc( dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
350 lpvOutBuffer
, cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
);
354 /* Not a VxD, let ntdll handle it */
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 status
= NtDeviceIoControlFile(hDevice
, NULL
, NULL
, NULL
, &iosb
,
369 dwIoControlCode
, lpvInBuffer
, cbInBuffer
,
370 lpvOutBuffer
, cbOutBuffer
);
371 if (lpcbBytesReturned
) *lpcbBytesReturned
= iosb
.Information
;
373 if (status
) SetLastError( RtlNtStatusToDosError(status
) );