ntoskrnl.exe: Implemented IoCreateDevice and IoDeleteDevice.
[wine/dibdrv.git] / dlls / ntoskrnl.exe / ntoskrnl.c
blobae786651a364576d230e47b9f311c0618ab9ca46
1 /*
2 * ntoskrnl.exe implementation
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "ddk/wdm.h"
34 #include "wine/unicode.h"
35 #include "wine/server.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
40 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
42 if (!us) return "<null>";
43 return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
46 static HANDLE get_device_manager(void)
48 static HANDLE device_manager;
49 HANDLE handle = 0, ret = device_manager;
51 if (!ret)
53 SERVER_START_REQ( create_device_manager )
55 req->access = SYNCHRONIZE;
56 req->attributes = 0;
57 if (!wine_server_call( req )) handle = reply->handle;
59 SERVER_END_REQ;
61 if (!handle)
63 ERR( "failed to create the device manager\n" );
64 return 0;
66 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
67 ret = handle;
68 else
69 NtClose( handle ); /* somebody beat us to it */
71 return ret;
75 /***********************************************************************
76 * IoCreateDevice (NTOSKRNL.EXE.@)
78 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
79 UNICODE_STRING *name, DEVICE_TYPE type,
80 ULONG characteristics, BOOLEAN exclusive,
81 DEVICE_OBJECT **ret_device )
83 NTSTATUS status;
84 DEVICE_OBJECT *device;
85 HANDLE handle = 0;
86 HANDLE manager = get_device_manager();
88 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
89 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
91 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
92 return STATUS_NO_MEMORY;
94 SERVER_START_REQ( create_device )
96 req->access = 0;
97 req->attributes = 0;
98 req->rootdir = 0;
99 req->manager = manager;
100 req->user_ptr = device;
101 if (name) wine_server_add_data( req, name->Buffer, name->Length );
102 if (!(status = wine_server_call( req ))) handle = reply->handle;
104 SERVER_END_REQ;
106 if (status == STATUS_SUCCESS)
108 device->DriverObject = driver;
109 device->DeviceExtension = device + 1;
110 device->DeviceType = type;
111 device->Reserved = handle;
113 device->NextDevice = driver->DeviceObject;
114 driver->DeviceObject = device;
116 *ret_device = device;
118 else HeapFree( GetProcessHeap(), 0, device );
120 return status;
124 /***********************************************************************
125 * IoDeleteDevice (NTOSKRNL.EXE.@)
127 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
129 NTSTATUS status;
131 TRACE( "%p\n", device );
133 SERVER_START_REQ( delete_device )
135 req->handle = device->Reserved;
136 status = wine_server_call( req );
138 SERVER_END_REQ;
140 if (status == STATUS_SUCCESS)
142 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
143 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
144 if (*prev) *prev = (*prev)->NextDevice;
145 NtClose( device->Reserved );
146 HeapFree( GetProcessHeap(), 0, device );
151 /*****************************************************
152 * DllMain
154 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
156 switch(reason)
158 case DLL_PROCESS_ATTACH:
159 DisableThreadLibraryCalls( inst );
160 break;
162 return TRUE;