2 * Driver Regression Tests
4 * Copyright 2009 Michael Martin <martinmnet@hotmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; see the file COPYING.LIB.
18 * If not, write to the Free Software Foundation,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /* INCLUDES *******************************************************************/
27 Adds a service registry entry for a driver
28 The driver must reside in the same path as this loaded driver
29 The caller is resposible for releasing memory
31 PWCHAR
CreateLowerDeviceRegistryKey(PUNICODE_STRING RegistryPath
, PWCHAR NewDriver
)
33 OBJECT_ATTRIBUTES ObjectAttributes
;
36 UNICODE_STRING NewDriverRegPath
;
37 PKEY_VALUE_PARTIAL_INFORMATION ValuePartialInfo
= NULL
;
41 ULONG ServiceDWordValue
;
42 ULONG ResultLength
= 0;
44 PWCHAR ReturnPath
= NULL
;
45 /* Now lets find out where we were loaded from by using registry */
46 InitializeObjectAttributes(&ObjectAttributes
, RegistryPath
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
47 Status
= ZwOpenKey(&ServiceKey
, KEY_READ
, &ObjectAttributes
);
48 if (!NT_SUCCESS(Status
))
50 DbgPrint("ZwOpenKey () failed (Status %x)\n", Status
);
54 RtlInitUnicodeString(&Name
, L
"ImagePath");
56 /* First query how much memory we need */
57 Status
= ZwQueryValueKey(ServiceKey
, &Name
, KeyValuePartialInformation
, 0, 0, &ResultLength
);
59 ResultLength
+= sizeof(KEY_VALUE_PARTIAL_INFORMATION
);
60 ValuePartialInfo
= ExAllocatePool(PagedPool
, ResultLength
);
61 if (!ValuePartialInfo
)
63 DbgPrint("Out of memory!\n");
67 Length
= ResultLength
;
68 Status
= ZwQueryValueKey(ServiceKey
, &Name
, KeyValuePartialInformation
, (PVOID
)ValuePartialInfo
, Length
, &ResultLength
);
69 if (!NT_SUCCESS(Status
))
71 DbgPrint("ZwQueryValueKey() failed (Status %lx)\n", Status
);
75 /* Remove the current driver name from the string */
76 /* FIXME: Dont use hard coded driver name, determine it from the string returned from the above Query */
77 Length
= (wcslen((PWCHAR
)ValuePartialInfo
->Data
) * 2) - (wcslen(L
"kmtest.sys") * 2);
78 RtlZeroMemory((PVOID
)((ULONG_PTR
)ValuePartialInfo
->Data
+ Length
),
79 wcslen(L
"drvtests.sys") * 2);
82 /* Now add a registry entry for the driver */
84 NewDriverRegPath
.Length
= 0;
85 NewDriverRegPath
.MaximumLength
= (wcslen(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\") +
86 wcslen(NewDriver
) + 1) * sizeof(WCHAR
);
87 NewDriverRegPath
.Buffer
= ExAllocatePool(PagedPool
, NewDriverRegPath
.MaximumLength
);
88 if (!NewDriverRegPath
.Buffer
)
90 DbgPrint("Out of memory!\n");
91 ExFreePool(NewDriverRegPath
.Buffer
);
95 RtlAppendUnicodeToString(&NewDriverRegPath
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
96 RtlAppendUnicodeToString(&NewDriverRegPath
, NewDriver
);
98 InitializeObjectAttributes(&ObjectAttributes
,
100 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
104 Status
= ZwCreateKey(&ServiceKey
,
112 if (!NT_SUCCESS(Status
))
114 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
115 ExFreePool(NewDriverRegPath
.Buffer
);
119 ReturnPath
= NewDriverRegPath
.Buffer
;
120 RtlInitUnicodeString(&Name
, L
"ImagePath");
123 Value
.MaximumLength
= (wcslen((PWCHAR
)ValuePartialInfo
->Data
) +
124 wcslen(NewDriver
) + 5) * sizeof(WCHAR
);
125 Value
.Buffer
= ExAllocatePool(PagedPool
, Value
.MaximumLength
);
129 DbgPrint("Out of memory!\n");
130 ExFreePool(Value
.Buffer
);
134 RtlAppendUnicodeToString(&Value
, (PWCHAR
)ValuePartialInfo
->Data
);
135 RtlAppendUnicodeToString(&Value
, NewDriver
);
136 RtlAppendUnicodeToString(&Value
, L
".sys");
138 Status
= ZwSetValueKey(ServiceKey
,
143 (wcslen(Value
.Buffer
)+1) * sizeof(WCHAR
));
144 ExFreePool(Value
.Buffer
);
145 if (!NT_SUCCESS(Status
))
147 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
151 RtlInitUnicodeString(&Name
, L
"DisplayName");
152 RtlInitUnicodeString(&Value
, NewDriver
);
154 Status
= ZwSetValueKey(ServiceKey
,
159 (wcslen(Value
.Buffer
)+1) * sizeof(WCHAR
));
161 if (!NT_SUCCESS(Status
))
163 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
167 RtlInitUnicodeString(&Name
, L
"ErrorControl");
168 ServiceDWordValue
= 0;
170 Status
= ZwSetValueKey(ServiceKey
,
177 if (!NT_SUCCESS(Status
))
179 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
183 RtlInitUnicodeString(&Name
, L
"Start");
184 ServiceDWordValue
= 3;
185 Status
= ZwSetValueKey(ServiceKey
,
192 if (!NT_SUCCESS(Status
))
194 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
198 RtlInitUnicodeString(&Name
, L
"Type");
199 ServiceDWordValue
= 0;
200 Status
= ZwSetValueKey(ServiceKey
,
207 if (!NT_SUCCESS(Status
))
209 DbgPrint("ZwCreateKey() failed (Status %lx)\n", Status
);
215 if (ValuePartialInfo
) ExFreePool(ValuePartialInfo
);