|
|
1.1 root 1: // Simple driver that demonstrates dynamically loading and unloading
2:
3: #include "ntddk.h"
4:
5: #define NT_DEVICE_NAME L"\\Device\\Ldunld"
6: #define DOS_DEVICE_NAME L"\\DosDevices\\LOADTEST"
7:
8: NTSTATUS
9: LdUnldOpen(
10: IN PDEVICE_OBJECT DeviceObject,
11: IN PIRP Irp
12: );
13:
14: NTSTATUS
15: LdUnldClose(
16: IN PDEVICE_OBJECT DeviceObject,
17: IN PIRP Irp
18: );
19:
20: VOID
21: LdUnldUnload(
22: IN PDRIVER_OBJECT DriverObject
23: );
24:
25: NTSTATUS
26: DriverEntry(
27: IN PDRIVER_OBJECT DriverObject,
28: IN PUNICODE_STRING RegistryPath
29: )
30: {
31:
32: PDEVICE_OBJECT deviceObject = NULL;
33: NTSTATUS status;
34: UNICODE_STRING uniNtNameString;
35: UNICODE_STRING uniWin32NameString;
36:
37: KdPrint( ("LDUNLD: Entered the Load/Unload driver!\n") );
38:
39: //
40: // Create counted string version of our device name.
41: //
42:
43: RtlInitUnicodeString( &uniNtNameString, NT_DEVICE_NAME );
44:
45: //
46: // Create the device object
47: //
48:
49: status = IoCreateDevice(
50: DriverObject,
51: 0, // We don't use a device extension
52: &uniNtNameString,
53: FILE_DEVICE_UNKNOWN,
54: 0, // No standard device characteristics
55: FALSE, // This isn't an exclusive device
56: &deviceObject
57: );
58:
59: if ( NT_SUCCESS(status) )
60: {
61:
62: //
63: // Create dispatch points for create/open, close, unload.
64: //
65:
66: DriverObject->MajorFunction[IRP_MJ_CREATE] = LdUnldOpen;
67: DriverObject->MajorFunction[IRP_MJ_CLOSE] = LdUnldClose;
68: DriverObject->DriverUnload = LdUnldUnload;
69:
70: KdPrint( ("LDUNLD: just about ready!\n") );
71:
72: //
73: // Create counted string version of our Win32 device name.
74: //
75:
76: RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );
77:
78: //
79: // Create a link from our device name to a name in the Win32 namespace.
80: //
81:
82: status = IoCreateSymbolicLink( &uniWin32NameString, &uniNtNameString );
83:
84: if (!NT_SUCCESS(status))
85: {
86: KdPrint( ("LDUNLD: Couldn't create the symbolic link\n") );
87:
88: IoDeleteDevice( DriverObject->DeviceObject );
89: }
90: else
91: {
92: KdPrint( ("LDUNLD: All initialized!\n") );
93: }
94: }
95: else
96: {
97: KdPrint( ("LDUNLD: Couldn't create the device\n") );
98: }
99: return status;
100: }
101:
102: NTSTATUS
103: LdUnldOpen(
104: IN PDEVICE_OBJECT DeviceObject,
105: IN PIRP Irp
106: )
107: {
108:
109: //
110: // No need to do anything.
111: //
112:
113: //
114: // Fill these in before calling IoCompleteRequest.
115: //
116: // DON'T get cute and try to use the status field of
117: // the irp in the return status. That IRP IS GONE as
118: // soon as you call IoCompleteRequest.
119: //
120:
121: KdPrint( ("LDUNLD: Opened!!\n") );
122:
123: Irp->IoStatus.Status = STATUS_SUCCESS;
124: Irp->IoStatus.Information = 0;
125:
126: IoCompleteRequest( Irp, IO_NO_INCREMENT );
127:
128: return STATUS_SUCCESS;
129: }
130:
131: NTSTATUS
132: LdUnldClose(
133: IN PDEVICE_OBJECT DeviceObject,
134: IN PIRP Irp
135: )
136: {
137:
138: //
139: // No need to do anything.
140: //
141:
142: //
143: // Fill these in before calling IoCompleteRequest.
144: //
145: // DON'T get cute and try to use the status field of
146: // the irp in the return status. That IRP IS GONE as
147: // soon as you call IoCompleteRequest.
148: //
149:
150: Irp->IoStatus.Status = STATUS_SUCCESS;
151: Irp->IoStatus.Information = 0;
152:
153: KdPrint( ("LDUNLD: Closed!!\n") );
154:
155: IoCompleteRequest( Irp, IO_NO_INCREMENT );
156:
157: return STATUS_SUCCESS;
158: }
159:
160: VOID
161: LdUnldUnload(
162: IN PDRIVER_OBJECT DriverObject
163: )
164: {
165: UNICODE_STRING uniWin32NameString;
166:
167: //
168: // All *THIS* driver needs to do is to delete the device object and the
169: // symbolic link between our device name and the Win32 visible name.
170: //
171: // Almost every other driver ever witten would need to do a
172: // significant amount of work here deallocating stuff.
173: //
174:
175: KdPrint( ("LDUNLD: Unloading!!\n") );
176:
177: //
178: // Create counted string version of our Win32 device name.
179: //
180:
181: RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );
182:
183: //
184: // Delete the link from our device name to a name in the Win32 namespace.
185: //
186:
187: IoDeleteSymbolicLink( &uniWin32NameString );
188:
189: //
190: // Finally delete our device object
191: //
192:
193: IoDeleteDevice( DriverObject->DeviceObject );
194: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.