winecoreaudio: Implement widOpen and widClose.
[wine/multimedia.git] / dlls / setupapi / query.c
blob82eb62d3ea8263cb8c7342d0c95383774dbee15e
1 /*
2 * setupapi query functions
4 * Copyright 2006 James Hawkins
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 <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "winver.h"
28 #include "setupapi.h"
29 #include "advpub.h"
30 #include "winnls.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "setupapi_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
37 #ifdef __i386__
38 static const WCHAR source_disks_names_platform[] =
39 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s','.','x','8','6',0};
40 static const WCHAR source_disks_files_platform[] =
41 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s','.','x','8','6',0};
42 #elif defined(__x86_64)
43 static const WCHAR source_disks_names_platform[] =
44 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s','.','a','m','d','6','4',0};
45 static const WCHAR source_disks_files_platform[] =
46 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s','.','a','m','d','6','4',0};
47 #else /* FIXME: other platforms */
48 static const WCHAR source_disks_names_platform[] =
49 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
50 static const WCHAR source_disks_files_platform[] =
51 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
52 #endif
53 static const WCHAR source_disks_names[] =
54 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
55 static const WCHAR source_disks_files[] =
56 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
58 /* fills the PSP_INF_INFORMATION struct fill_info is TRUE
59 * always returns the required size of the information
61 static BOOL fill_inf_info(HINF inf, PSP_INF_INFORMATION buffer, DWORD size, DWORD *required)
63 LPCWSTR filename = PARSER_get_inf_filename(inf);
64 DWORD total_size = FIELD_OFFSET(SP_INF_INFORMATION, VersionData)
65 + (lstrlenW(filename) + 1) * sizeof(WCHAR);
67 if (required) *required = total_size;
69 /* FIXME: we need to parse the INF file to find the correct version info */
70 if (buffer)
72 if (size < total_size)
74 SetLastError(ERROR_INSUFFICIENT_BUFFER);
75 return FALSE;
77 buffer->InfStyle = INF_STYLE_WIN4;
78 buffer->InfCount = 1;
79 /* put the filename in buffer->VersionData */
80 lstrcpyW((LPWSTR)&buffer->VersionData[0], filename);
82 return TRUE;
85 static HINF search_for_inf(LPCVOID InfSpec, DWORD SearchControl)
87 HINF hInf = INVALID_HANDLE_VALUE;
88 WCHAR inf_path[MAX_PATH];
90 static const WCHAR infW[] = {'\\','i','n','f','\\',0};
91 static const WCHAR system32W[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
93 if (SearchControl == INFINFO_REVERSE_DEFAULT_SEARCH)
95 GetWindowsDirectoryW(inf_path, MAX_PATH);
96 lstrcatW(inf_path, system32W);
97 lstrcatW(inf_path, InfSpec);
99 hInf = SetupOpenInfFileW(inf_path, NULL,
100 INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
101 if (hInf != INVALID_HANDLE_VALUE)
102 return hInf;
104 GetWindowsDirectoryW(inf_path, MAX_PATH);
105 lstrcpyW(inf_path, infW);
106 lstrcatW(inf_path, InfSpec);
108 return SetupOpenInfFileW(inf_path, NULL,
109 INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
112 return INVALID_HANDLE_VALUE;
115 /***********************************************************************
116 * SetupGetInfInformationA (SETUPAPI.@)
119 BOOL WINAPI SetupGetInfInformationA(LPCVOID InfSpec, DWORD SearchControl,
120 PSP_INF_INFORMATION ReturnBuffer,
121 DWORD ReturnBufferSize, PDWORD RequiredSize)
123 LPWSTR inf = (LPWSTR)InfSpec;
124 DWORD len;
125 BOOL ret;
127 if (InfSpec && SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
129 len = lstrlenA(InfSpec) + 1;
130 inf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
131 MultiByteToWideChar(CP_ACP, 0, InfSpec, -1, inf, len);
134 ret = SetupGetInfInformationW(inf, SearchControl, ReturnBuffer,
135 ReturnBufferSize, RequiredSize);
137 if (SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
138 HeapFree(GetProcessHeap(), 0, inf);
140 return ret;
143 /***********************************************************************
144 * SetupGetInfInformationW (SETUPAPI.@)
146 * BUGS
147 * Only handles the case when InfSpec is an INF handle.
149 BOOL WINAPI SetupGetInfInformationW(LPCVOID InfSpec, DWORD SearchControl,
150 PSP_INF_INFORMATION ReturnBuffer,
151 DWORD ReturnBufferSize, PDWORD RequiredSize)
153 HINF inf;
154 BOOL ret;
155 DWORD infSize;
157 TRACE("(%p, %d, %p, %d, %p)\n", InfSpec, SearchControl, ReturnBuffer,
158 ReturnBufferSize, RequiredSize);
160 if (!InfSpec)
162 if (SearchControl == INFINFO_INF_SPEC_IS_HINF)
163 SetLastError(ERROR_INVALID_HANDLE);
164 else
165 SetLastError(ERROR_INVALID_PARAMETER);
167 return FALSE;
170 switch (SearchControl)
172 case INFINFO_INF_SPEC_IS_HINF:
173 inf = (HINF)InfSpec;
174 break;
175 case INFINFO_INF_NAME_IS_ABSOLUTE:
176 case INFINFO_DEFAULT_SEARCH:
177 inf = SetupOpenInfFileW(InfSpec, NULL,
178 INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
179 break;
180 case INFINFO_REVERSE_DEFAULT_SEARCH:
181 inf = search_for_inf(InfSpec, SearchControl);
182 break;
183 case INFINFO_INF_PATH_LIST_SEARCH:
184 FIXME("Unhandled search control: %d\n", SearchControl);
186 if (RequiredSize)
187 *RequiredSize = 0;
189 return FALSE;
190 default:
191 SetLastError(ERROR_INVALID_PARAMETER);
192 return FALSE;
195 if (inf == INVALID_HANDLE_VALUE)
197 SetLastError(ERROR_FILE_NOT_FOUND);
198 return FALSE;
201 ret = fill_inf_info(inf, ReturnBuffer, ReturnBufferSize, &infSize);
202 if (!ReturnBuffer && (ReturnBufferSize >= infSize))
204 SetLastError(ERROR_INVALID_PARAMETER);
205 ret = FALSE;
207 if (RequiredSize) *RequiredSize = infSize;
209 if (SearchControl >= INFINFO_INF_NAME_IS_ABSOLUTE)
210 SetupCloseInfFile(inf);
212 return ret;
215 /***********************************************************************
216 * SetupQueryInfFileInformationA (SETUPAPI.@)
218 BOOL WINAPI SetupQueryInfFileInformationA(PSP_INF_INFORMATION InfInformation,
219 UINT InfIndex, PSTR ReturnBuffer,
220 DWORD ReturnBufferSize, PDWORD RequiredSize)
222 LPWSTR filenameW;
223 DWORD size;
224 BOOL ret;
226 ret = SetupQueryInfFileInformationW(InfInformation, InfIndex, NULL, 0, &size);
227 if (!ret)
228 return FALSE;
230 filenameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
232 ret = SetupQueryInfFileInformationW(InfInformation, InfIndex,
233 filenameW, size, &size);
234 if (!ret)
236 HeapFree(GetProcessHeap(), 0, filenameW);
237 return FALSE;
240 if (RequiredSize)
241 *RequiredSize = size;
243 if (!ReturnBuffer)
245 if (ReturnBufferSize)
247 SetLastError(ERROR_INVALID_PARAMETER);
248 return FALSE;
251 return TRUE;
254 if (size > ReturnBufferSize)
256 SetLastError(ERROR_INSUFFICIENT_BUFFER);
257 return FALSE;
260 WideCharToMultiByte(CP_ACP, 0, filenameW, -1, ReturnBuffer, size, NULL, NULL);
261 HeapFree(GetProcessHeap(), 0, filenameW);
263 return ret;
266 /***********************************************************************
267 * SetupQueryInfFileInformationW (SETUPAPI.@)
269 BOOL WINAPI SetupQueryInfFileInformationW(PSP_INF_INFORMATION InfInformation,
270 UINT InfIndex, PWSTR ReturnBuffer,
271 DWORD ReturnBufferSize, PDWORD RequiredSize)
273 DWORD len;
274 LPWSTR ptr;
276 TRACE("(%p, %u, %p, %d, %p) Stub!\n", InfInformation, InfIndex,
277 ReturnBuffer, ReturnBufferSize, RequiredSize);
279 if (!InfInformation)
281 SetLastError(ERROR_INVALID_PARAMETER);
282 return FALSE;
285 if (InfIndex != 0)
286 FIXME("Appended INF files are not handled\n");
288 ptr = (LPWSTR)&InfInformation->VersionData[0];
289 len = lstrlenW(ptr);
291 if (RequiredSize)
292 *RequiredSize = len + 1;
294 if (!ReturnBuffer)
295 return TRUE;
297 if (ReturnBufferSize < len)
299 SetLastError(ERROR_INSUFFICIENT_BUFFER);
300 return FALSE;
303 lstrcpyW(ReturnBuffer, ptr);
304 return TRUE;
307 /***********************************************************************
308 * SetupGetSourceFileLocationA (SETUPAPI.@)
311 BOOL WINAPI SetupGetSourceFileLocationA( HINF hinf, PINFCONTEXT context, PCSTR filename,
312 PUINT source_id, PSTR buffer, DWORD buffer_size,
313 PDWORD required_size )
315 BOOL ret = FALSE;
316 WCHAR *filenameW = NULL, *bufferW = NULL;
317 DWORD required;
318 INT size;
320 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf, context, debugstr_a(filename), source_id,
321 buffer, buffer_size, required_size);
323 if (filename && !(filenameW = strdupAtoW( filename )))
324 return FALSE;
326 if (!SetupGetSourceFileLocationW( hinf, context, filenameW, source_id, NULL, 0, &required ))
327 goto done;
329 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
330 goto done;
332 if (!SetupGetSourceFileLocationW( hinf, context, filenameW, source_id, bufferW, required, NULL ))
333 goto done;
335 size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
336 if (required_size) *required_size = size;
338 if (buffer)
340 if (buffer_size >= size)
341 WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
342 else
344 SetLastError( ERROR_INSUFFICIENT_BUFFER );
345 goto done;
348 ret = TRUE;
350 done:
351 HeapFree( GetProcessHeap(), 0, filenameW );
352 HeapFree( GetProcessHeap(), 0, bufferW );
353 return ret;
356 static LPWSTR get_source_id( HINF hinf, PINFCONTEXT context, PCWSTR filename )
358 DWORD size;
359 LPWSTR source_id;
361 if (!SetupFindFirstLineW( hinf, source_disks_files_platform, filename, context ) &&
362 !SetupFindFirstLineW( hinf, source_disks_files, filename, context ))
363 return NULL;
365 if (!SetupGetStringFieldW( context, 1, NULL, 0, &size ))
366 return NULL;
368 if (!(source_id = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )))
369 return NULL;
371 if (!SetupGetStringFieldW( context, 1, source_id, size, NULL ))
373 HeapFree( GetProcessHeap(), 0, source_id );
374 return NULL;
377 if (!SetupFindFirstLineW( hinf, source_disks_names_platform, source_id, context ) &&
378 !SetupFindFirstLineW( hinf, source_disks_names, source_id, context ))
380 HeapFree( GetProcessHeap(), 0, source_id );
381 return NULL;
383 return source_id;
386 /***********************************************************************
387 * SetupGetSourceFileLocationW (SETUPAPI.@)
390 BOOL WINAPI SetupGetSourceFileLocationW( HINF hinf, PINFCONTEXT context, PCWSTR filename,
391 PUINT source_id, PWSTR buffer, DWORD buffer_size,
392 PDWORD required_size )
394 INFCONTEXT ctx;
395 WCHAR *end, *source_id_str;
397 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf, context, debugstr_w(filename), source_id,
398 buffer, buffer_size, required_size);
400 if (!context) context = &ctx;
402 if (!(source_id_str = get_source_id( hinf, context, filename )))
403 return FALSE;
405 *source_id = strtolW( source_id_str, &end, 10 );
406 if (end == source_id_str || *end)
408 HeapFree( GetProcessHeap(), 0, source_id_str );
409 return FALSE;
411 HeapFree( GetProcessHeap(), 0, source_id_str );
413 if (SetupGetStringFieldW( context, 4, buffer, buffer_size, required_size ))
414 return TRUE;
416 if (required_size) *required_size = 1;
417 if (buffer)
419 if (buffer_size >= 1) buffer[0] = 0;
420 else
422 SetLastError( ERROR_INSUFFICIENT_BUFFER );
423 return FALSE;
426 return TRUE;
429 /***********************************************************************
430 * SetupGetSourceInfoA (SETUPAPI.@)
433 BOOL WINAPI SetupGetSourceInfoA( HINF hinf, UINT source_id, UINT info,
434 PSTR buffer, DWORD buffer_size, LPDWORD required_size )
436 BOOL ret = FALSE;
437 WCHAR *bufferW = NULL;
438 DWORD required;
439 INT size;
441 TRACE("%p, %d, %d, %p, %d, %p\n", hinf, source_id, info, buffer, buffer_size,
442 required_size);
444 if (!SetupGetSourceInfoW( hinf, source_id, info, NULL, 0, &required ))
445 return FALSE;
447 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
448 return FALSE;
450 if (!SetupGetSourceInfoW( hinf, source_id, info, bufferW, required, NULL ))
451 goto done;
453 size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
454 if (required_size) *required_size = size;
456 if (buffer)
458 if (buffer_size >= size)
459 WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
460 else
462 SetLastError( ERROR_INSUFFICIENT_BUFFER );
463 goto done;
466 ret = TRUE;
468 done:
469 HeapFree( GetProcessHeap(), 0, bufferW );
470 return ret;
473 /***********************************************************************
474 * SetupGetSourceInfoW (SETUPAPI.@)
477 BOOL WINAPI SetupGetSourceInfoW( HINF hinf, UINT source_id, UINT info,
478 PWSTR buffer, DWORD buffer_size, LPDWORD required_size )
480 INFCONTEXT ctx;
481 WCHAR source_id_str[11];
482 static const WCHAR fmt[] = {'%','d',0};
483 DWORD index;
485 TRACE("%p, %d, %d, %p, %d, %p\n", hinf, source_id, info, buffer, buffer_size,
486 required_size);
488 sprintfW( source_id_str, fmt, source_id );
490 if (!SetupFindFirstLineW( hinf, source_disks_names_platform, source_id_str, &ctx ) &&
491 !SetupFindFirstLineW( hinf, source_disks_names, source_id_str, &ctx ))
492 return FALSE;
494 switch (info)
496 case SRCINFO_PATH: index = 4; break;
497 case SRCINFO_TAGFILE: index = 2; break;
498 case SRCINFO_DESCRIPTION: index = 1; break;
499 default:
500 WARN("unknown info level: %d\n", info);
501 return FALSE;
504 if (SetupGetStringFieldW( &ctx, index, buffer, buffer_size, required_size ))
505 return TRUE;
507 if (required_size) *required_size = 1;
508 if (buffer)
510 if (buffer_size >= 1) buffer[0] = 0;
511 else
513 SetLastError( ERROR_INSUFFICIENT_BUFFER );
514 return FALSE;
517 return TRUE;
520 /***********************************************************************
521 * SetupGetTargetPathA (SETUPAPI.@)
524 BOOL WINAPI SetupGetTargetPathA( HINF hinf, PINFCONTEXT context, PCSTR section, PSTR buffer,
525 DWORD buffer_size, PDWORD required_size )
527 BOOL ret = FALSE;
528 WCHAR *sectionW = NULL, *bufferW = NULL;
529 DWORD required;
530 INT size;
532 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf, context, debugstr_a(section), buffer,
533 buffer_size, required_size);
535 if (section && !(sectionW = strdupAtoW( section )))
536 return FALSE;
538 if (!SetupGetTargetPathW( hinf, context, sectionW, NULL, 0, &required ))
539 goto done;
541 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, required * sizeof(WCHAR) )))
542 goto done;
544 if (!SetupGetTargetPathW( hinf, context, sectionW, bufferW, required, NULL ))
545 goto done;
547 size = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
548 if (required_size) *required_size = size;
550 if (buffer)
552 if (buffer_size >= size)
553 WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, buffer_size, NULL, NULL );
554 else
556 SetLastError( ERROR_INSUFFICIENT_BUFFER );
557 goto done;
560 ret = TRUE;
562 done:
563 HeapFree( GetProcessHeap(), 0, sectionW );
564 HeapFree( GetProcessHeap(), 0, bufferW );
565 return ret;
568 /***********************************************************************
569 * SetupGetTargetPathW (SETUPAPI.@)
572 BOOL WINAPI SetupGetTargetPathW( HINF hinf, PINFCONTEXT context, PCWSTR section, PWSTR buffer,
573 DWORD buffer_size, PDWORD required_size )
575 static const WCHAR destination_dirs[] =
576 {'D','e','s','t','i','n','a','t','i','o','n','D','i','r','s',0};
577 static const WCHAR default_dest_dir[] =
578 {'D','e','f','a','u','l','t','D','e','s','t','D','i','r',0};
580 INFCONTEXT ctx;
581 WCHAR *dir;
582 INT size;
584 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf, context, debugstr_w(section), buffer,
585 buffer_size, required_size);
587 if (context && !SetupFindFirstLineW( hinf, destination_dirs, NULL, context )) return FALSE;
588 else if (section && !SetupFindFirstLineW( hinf, section, NULL, &ctx )) return FALSE;
589 else if (!SetupFindFirstLineW( hinf, destination_dirs, default_dest_dir, &ctx )) return FALSE;
591 if (!(dir = PARSER_get_dest_dir( context ? context : &ctx ))) return FALSE;
593 size = lstrlenW( dir ) + 1;
594 if (required_size) *required_size = size;
596 if (buffer)
598 if (buffer_size >= size)
599 lstrcpyW( buffer, dir );
600 else
602 SetLastError( ERROR_INSUFFICIENT_BUFFER );
603 HeapFree( GetProcessHeap(), 0, dir );
604 return FALSE;
607 HeapFree( GetProcessHeap(), 0, dir );
608 return TRUE;