4 * Copyright 2006 Christian Gmeiner
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
32 #include "wine/test.h"
34 /* function pointers */
35 static HMODULE hSetupAPI
;
36 static HDEVINFO (WINAPI
*pSetupDiCreateDeviceInfoList
)(GUID
*,HWND
);
37 static HDEVINFO (WINAPI
*pSetupDiCreateDeviceInfoListExW
)(GUID
*,HWND
,PCWSTR
,PVOID
);
38 static BOOL (WINAPI
*pSetupDiDestroyDeviceInfoList
)(HDEVINFO
);
39 static BOOL (WINAPI
*pSetupDiEnumDeviceInfo
)(HDEVINFO
, DWORD
, PSP_DEVINFO_DATA
);
40 static HKEY (WINAPI
*pSetupDiOpenClassRegKeyExA
)(GUID
*,REGSAM
,DWORD
,PCSTR
,PVOID
);
41 static BOOL (WINAPI
*pSetupDiCreateDeviceInfoA
)(HDEVINFO
, PCSTR
, GUID
*, PCSTR
, HWND
, DWORD
, PSP_DEVINFO_DATA
);
42 static BOOL (WINAPI
*pSetupDiGetDeviceInstanceIdA
)(HDEVINFO
, PSP_DEVINFO_DATA
, PSTR
, DWORD
, PDWORD
);
44 static void init_function_pointers(void)
46 hSetupAPI
= GetModuleHandleA("setupapi.dll");
48 pSetupDiCreateDeviceInfoA
= (void *)GetProcAddress(hSetupAPI
, "SetupDiCreateDeviceInfoA");
49 pSetupDiCreateDeviceInfoList
= (void *)GetProcAddress(hSetupAPI
, "SetupDiCreateDeviceInfoList");
50 pSetupDiCreateDeviceInfoListExW
= (void *)GetProcAddress(hSetupAPI
, "SetupDiCreateDeviceInfoListExW");
51 pSetupDiDestroyDeviceInfoList
= (void *)GetProcAddress(hSetupAPI
, "SetupDiDestroyDeviceInfoList");
52 pSetupDiEnumDeviceInfo
= (void *)GetProcAddress(hSetupAPI
, "SetupDiEnumDeviceInfo");
53 pSetupDiGetDeviceInstanceIdA
= (void *)GetProcAddress(hSetupAPI
, "SetupDiGetDeviceInstanceIdA");
54 pSetupDiOpenClassRegKeyExA
= (void *)GetProcAddress(hSetupAPI
, "SetupDiOpenClassRegKeyExA");
57 static void test_SetupDiCreateDeviceInfoListEx(void)
62 static CHAR notnull
[] = "NotNull";
63 static const WCHAR machine
[] = { 'd','u','m','m','y',0 };
65 SetLastError(0xdeadbeef);
66 /* create empty DeviceInfoList, but set Reserved to a value, which is not NULL */
67 devlist
= pSetupDiCreateDeviceInfoListExW(NULL
, NULL
, NULL
, notnull
);
69 error
= GetLastError();
70 if (error
== ERROR_CALL_NOT_IMPLEMENTED
)
72 skip("SetupDiCreateDeviceInfoListExW is not implemented\n");
75 ok(devlist
== INVALID_HANDLE_VALUE
, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist
, error
, INVALID_HANDLE_VALUE
);
76 ok(error
== ERROR_INVALID_PARAMETER
, "GetLastError returned wrong value : %d, (expected %d)\n", error
, ERROR_INVALID_PARAMETER
);
78 SetLastError(0xdeadbeef);
79 /* create empty DeviceInfoList, but set MachineName to something */
80 devlist
= pSetupDiCreateDeviceInfoListExW(NULL
, NULL
, machine
, NULL
);
82 error
= GetLastError();
83 ok(devlist
== INVALID_HANDLE_VALUE
, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist
, error
, INVALID_HANDLE_VALUE
);
84 ok(error
== ERROR_INVALID_MACHINENAME
, "GetLastError returned wrong value : %d, (expected %d)\n", error
, ERROR_INVALID_MACHINENAME
);
86 /* create empty DeviceInfoList */
87 devlist
= pSetupDiCreateDeviceInfoListExW(NULL
, NULL
, NULL
, NULL
);
88 ok(devlist
&& devlist
!= INVALID_HANDLE_VALUE
, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist
, error
, INVALID_HANDLE_VALUE
);
90 /* destroy DeviceInfoList */
91 ret
= pSetupDiDestroyDeviceInfoList(devlist
);
92 ok(ret
, "SetupDiDestroyDeviceInfoList failed : %d\n", error
);
95 static void test_SetupDiOpenClassRegKeyExA(void)
97 /* This is a unique guid for testing purposes */
98 GUID guid
= {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
99 0x00,0x11,0x95,0x5c,0x2b,0xdb}};
100 static const CHAR guidString
[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
103 /* Check return value for nonexistent key */
104 hkey
= pSetupDiOpenClassRegKeyExA(&guid
, KEY_ALL_ACCESS
,
105 DIOCR_INSTALLER
, NULL
, NULL
);
106 ok(hkey
== INVALID_HANDLE_VALUE
,
107 "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey
);
109 /* Test it for a key that exists */
110 hkey
= SetupDiOpenClassRegKey(NULL
, KEY_ALL_ACCESS
);
111 if (hkey
!= INVALID_HANDLE_VALUE
)
114 if (RegCreateKeyA(hkey
, guidString
, &classKey
) == ERROR_SUCCESS
)
116 RegCloseKey(classKey
);
117 SetLastError(0xdeadbeef);
118 classKey
= pSetupDiOpenClassRegKeyExA(&guid
, KEY_ALL_ACCESS
,
119 DIOCR_INSTALLER
, NULL
, NULL
);
120 ok(classKey
!= INVALID_HANDLE_VALUE
,
121 "opening class registry key failed with error %d\n",
123 if (classKey
!= INVALID_HANDLE_VALUE
)
124 RegCloseKey(classKey
);
125 RegDeleteKeyA(hkey
, guidString
);
128 trace("failed to create registry key for test\n");
131 trace("failed to open classes key\n");
134 static void testCreateDeviceInfo(void)
138 GUID guid
= {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
139 0x00,0x11,0x95,0x5c,0x2b,0xdb}};
141 if (!pSetupDiCreateDeviceInfoList
|| !pSetupDiEnumDeviceInfo
||
142 !pSetupDiDestroyDeviceInfoList
|| !pSetupDiCreateDeviceInfoA
)
144 skip("No SetupDiCreateDeviceInfoA\n");
147 SetLastError(0xdeadbeef);
148 ret
= pSetupDiCreateDeviceInfoA(NULL
, NULL
, NULL
, NULL
, NULL
, 0, NULL
);
149 ok(!ret
&& GetLastError() == ERROR_INVALID_DEVINST_NAME
,
150 "Expected ERROR_INVALID_DEVINST_NAME, got %08x\n", GetLastError());
151 SetLastError(0xdeadbeef);
152 ret
= pSetupDiCreateDeviceInfoA(NULL
, "Root\\LEGACY_BOGUS\\0000", NULL
,
153 NULL
, NULL
, 0, NULL
);
154 ok(!ret
&& GetLastError() == ERROR_INVALID_HANDLE
,
155 "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
156 set
= pSetupDiCreateDeviceInfoList(&guid
, NULL
);
157 ok(set
!= NULL
, "SetupDiCreateDeviceInfoList failed: %08x\n",
161 SP_DEVINFO_DATA devInfo
= { 0 };
164 SetLastError(0xdeadbeef);
165 ret
= pSetupDiCreateDeviceInfoA(set
, "Root\\LEGACY_BOGUS\\0000", NULL
,
166 NULL
, NULL
, 0, NULL
);
167 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
168 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
169 /* Finally, with all three required parameters, this succeeds: */
170 ret
= pSetupDiCreateDeviceInfoA(set
, "Root\\LEGACY_BOGUS\\0000", &guid
,
171 NULL
, NULL
, 0, NULL
);
172 ok(ret
, "pSetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
173 /* This fails because the device ID already exists.. */
174 SetLastError(0xdeadbeef);
175 ret
= pSetupDiCreateDeviceInfoA(set
, "Root\\LEGACY_BOGUS\\0000", &guid
,
176 NULL
, NULL
, 0, &devInfo
);
177 ok(!ret
&& GetLastError() == ERROR_DEVINST_ALREADY_EXISTS
,
178 "Expected ERROR_DEVINST_ALREADY_EXISTS, got %08x\n", GetLastError());
179 /* whereas this "fails" because cbSize is wrong.. */
180 SetLastError(0xdeadbeef);
181 ret
= pSetupDiCreateDeviceInfoA(set
, "LEGACY_BOGUS", &guid
, NULL
, NULL
,
182 DICD_GENERATE_ID
, &devInfo
);
183 ok(!ret
&& GetLastError() == ERROR_INVALID_USER_BUFFER
,
184 "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
185 devInfo
.cbSize
= sizeof(devInfo
);
186 ret
= pSetupDiCreateDeviceInfoA(set
, "LEGACY_BOGUS", &guid
, NULL
, NULL
,
187 DICD_GENERATE_ID
, &devInfo
);
188 /* and this finally succeeds. */
189 ok(ret
, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
190 /* There were three devices added, however - the second failure just
191 * resulted in the SP_DEVINFO_DATA not getting copied.
193 SetLastError(0xdeadbeef);
195 while (pSetupDiEnumDeviceInfo(set
, i
, &devInfo
))
197 ok(i
== 3, "Expected 3 devices, got %d\n", i
);
198 ok(GetLastError() == ERROR_NO_MORE_ITEMS
,
199 "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
200 pSetupDiDestroyDeviceInfoList(set
);
204 static void testGetDeviceInstanceId(void)
208 GUID guid
= {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
209 0x00,0x11,0x95,0x5c,0x2b,0xdb}};
210 SP_DEVINFO_DATA devInfo
= { 0 };
212 if (!pSetupDiCreateDeviceInfoList
|| !pSetupDiDestroyDeviceInfoList
||
213 !pSetupDiCreateDeviceInfoA
|| !pSetupDiGetDeviceInstanceIdA
)
215 skip("No SetupDiGetDeviceInstanceIdA\n");
218 SetLastError(0xdeadbeef);
219 ret
= pSetupDiGetDeviceInstanceIdA(NULL
, NULL
, NULL
, 0, NULL
);
221 ok(!ret
&& GetLastError() == ERROR_INVALID_HANDLE
,
222 "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
223 SetLastError(0xdeadbeef);
224 ret
= pSetupDiGetDeviceInstanceIdA(NULL
, &devInfo
, NULL
, 0, NULL
);
226 ok(!ret
&& GetLastError() == ERROR_INVALID_HANDLE
,
227 "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
228 set
= pSetupDiCreateDeviceInfoList(&guid
, NULL
);
229 ok(set
!= NULL
, "SetupDiCreateDeviceInfoList failed: %08x\n",
233 char instanceID
[MAX_PATH
];
236 SetLastError(0xdeadbeef);
237 ret
= pSetupDiGetDeviceInstanceIdA(set
, NULL
, NULL
, 0, NULL
);
239 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
240 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
241 SetLastError(0xdeadbeef);
242 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, NULL
, 0, NULL
);
244 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
245 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
246 SetLastError(0xdeadbeef);
247 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, NULL
, 0, &size
);
249 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
250 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
251 devInfo
.cbSize
= sizeof(devInfo
);
252 SetLastError(0xdeadbeef);
253 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, NULL
, 0, &size
);
255 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
256 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
257 ret
= pSetupDiCreateDeviceInfoA(set
, "Root\\LEGACY_BOGUS\\0000", &guid
,
258 NULL
, NULL
, 0, &devInfo
);
259 ok(ret
, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
260 SetLastError(0xdeadbeef);
261 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, NULL
, 0, &size
);
263 ok(!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
264 "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
265 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, instanceID
,
266 sizeof(instanceID
), NULL
);
268 ok(ret
, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
270 ok(!lstrcmpA(instanceID
, "ROOT\\LEGACY_BOGUS\\0000"),
271 "Unexpected instance ID %s\n", instanceID
);
272 ret
= pSetupDiCreateDeviceInfoA(set
, "LEGACY_BOGUS", &guid
,
273 NULL
, NULL
, DICD_GENERATE_ID
, &devInfo
);
274 ok(ret
, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
275 ret
= pSetupDiGetDeviceInstanceIdA(set
, &devInfo
, instanceID
,
276 sizeof(instanceID
), NULL
);
278 ok(ret
, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
280 ok(!lstrcmpA(instanceID
, "ROOT\\LEGACY_BOGUS\\0001"),
281 "Unexpected instance ID %s\n", instanceID
);
282 pSetupDiDestroyDeviceInfoList(set
);
288 init_function_pointers();
290 if (pSetupDiCreateDeviceInfoListExW
&& pSetupDiDestroyDeviceInfoList
)
291 test_SetupDiCreateDeviceInfoListEx();
293 skip("SetupDiCreateDeviceInfoListExW and/or SetupDiDestroyDeviceInfoList not available\n");
295 if (pSetupDiOpenClassRegKeyExA
)
296 test_SetupDiOpenClassRegKeyExA();
298 skip("SetupDiOpenClassRegKeyExA is not available\n");
299 testCreateDeviceInfo();
300 testGetDeviceInstanceId();