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
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "setupapi_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
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};
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 */
72 if (size
< total_size
)
74 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
77 buffer
->InfStyle
= INF_STYLE_WIN4
;
79 /* put the filename in buffer->VersionData */
80 lstrcpyW((LPWSTR
)&buffer
->VersionData
[0], filename
);
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
)
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
;
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
);
143 /***********************************************************************
144 * SetupGetInfInformationW (SETUPAPI.@)
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
)
157 TRACE("(%p, %d, %p, %d, %p)\n", InfSpec
, SearchControl
, ReturnBuffer
,
158 ReturnBufferSize
, RequiredSize
);
162 if (SearchControl
== INFINFO_INF_SPEC_IS_HINF
)
163 SetLastError(ERROR_INVALID_HANDLE
);
165 SetLastError(ERROR_INVALID_PARAMETER
);
170 switch (SearchControl
)
172 case INFINFO_INF_SPEC_IS_HINF
:
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
);
180 case INFINFO_REVERSE_DEFAULT_SEARCH
:
181 inf
= search_for_inf(InfSpec
, SearchControl
);
183 case INFINFO_INF_PATH_LIST_SEARCH
:
184 FIXME("Unhandled search control: %d\n", SearchControl
);
191 SetLastError(ERROR_INVALID_PARAMETER
);
195 if (inf
== INVALID_HANDLE_VALUE
)
197 SetLastError(ERROR_FILE_NOT_FOUND
);
201 ret
= fill_inf_info(inf
, ReturnBuffer
, ReturnBufferSize
, &infSize
);
202 if (!ReturnBuffer
&& (ReturnBufferSize
>= infSize
))
204 SetLastError(ERROR_INVALID_PARAMETER
);
207 if (RequiredSize
) *RequiredSize
= infSize
;
209 if (SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
210 SetupCloseInfFile(inf
);
215 /***********************************************************************
216 * SetupQueryInfFileInformationA (SETUPAPI.@)
218 BOOL WINAPI
SetupQueryInfFileInformationA(PSP_INF_INFORMATION InfInformation
,
219 UINT InfIndex
, PSTR ReturnBuffer
,
220 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
226 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
, NULL
, 0, &size
);
230 filenameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
232 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
,
233 filenameW
, size
, &size
);
236 HeapFree(GetProcessHeap(), 0, filenameW
);
241 *RequiredSize
= size
;
245 if (ReturnBufferSize
)
247 SetLastError(ERROR_INVALID_PARAMETER
);
254 if (size
> ReturnBufferSize
)
256 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
260 WideCharToMultiByte(CP_ACP
, 0, filenameW
, -1, ReturnBuffer
, size
, NULL
, NULL
);
261 HeapFree(GetProcessHeap(), 0, filenameW
);
266 /***********************************************************************
267 * SetupQueryInfFileInformationW (SETUPAPI.@)
269 BOOL WINAPI
SetupQueryInfFileInformationW(PSP_INF_INFORMATION InfInformation
,
270 UINT InfIndex
, PWSTR ReturnBuffer
,
271 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
276 TRACE("(%p, %u, %p, %d, %p) Stub!\n", InfInformation
, InfIndex
,
277 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
281 SetLastError(ERROR_INVALID_PARAMETER
);
286 FIXME("Appended INF files are not handled\n");
288 ptr
= (LPWSTR
)&InfInformation
->VersionData
[0];
292 *RequiredSize
= len
+ 1;
297 if (ReturnBufferSize
< len
)
299 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
303 lstrcpyW(ReturnBuffer
, ptr
);
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
)
316 WCHAR
*filenameW
= NULL
, *bufferW
= NULL
;
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
)))
326 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, NULL
, 0, &required
))
329 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
332 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, bufferW
, required
, NULL
))
335 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
336 if (required_size
) *required_size
= size
;
340 if (buffer_size
>= size
)
341 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
344 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
351 HeapFree( GetProcessHeap(), 0, filenameW
);
352 HeapFree( GetProcessHeap(), 0, bufferW
);
356 static LPWSTR
get_source_id( HINF hinf
, PINFCONTEXT context
, PCWSTR filename
)
361 if (!SetupFindFirstLineW( hinf
, source_disks_files_platform
, filename
, context
) &&
362 !SetupFindFirstLineW( hinf
, source_disks_files
, filename
, context
))
365 if (!SetupGetStringFieldW( context
, 1, NULL
, 0, &size
))
368 if (!(source_id
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
371 if (!SetupGetStringFieldW( context
, 1, source_id
, size
, NULL
))
373 HeapFree( GetProcessHeap(), 0, source_id
);
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
);
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
)
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
)))
405 *source_id
= strtolW( source_id_str
, &end
, 10 );
406 if (end
== source_id_str
|| *end
)
408 HeapFree( GetProcessHeap(), 0, source_id_str
);
411 HeapFree( GetProcessHeap(), 0, source_id_str
);
413 if (SetupGetStringFieldW( context
, 4, buffer
, buffer_size
, required_size
))
416 if (required_size
) *required_size
= 1;
419 if (buffer_size
>= 1) buffer
[0] = 0;
422 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
429 /***********************************************************************
430 * SetupGetSourceInfoA (SETUPAPI.@)
433 BOOL WINAPI
SetupGetSourceInfoA( HINF hinf
, UINT source_id
, UINT info
,
434 PSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
437 WCHAR
*bufferW
= NULL
;
441 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_size
,
444 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, NULL
, 0, &required
))
447 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
450 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, bufferW
, required
, NULL
))
453 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
454 if (required_size
) *required_size
= size
;
458 if (buffer_size
>= size
)
459 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
462 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
469 HeapFree( GetProcessHeap(), 0, bufferW
);
473 /***********************************************************************
474 * SetupGetSourceInfoW (SETUPAPI.@)
477 BOOL WINAPI
SetupGetSourceInfoW( HINF hinf
, UINT source_id
, UINT info
,
478 PWSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
481 WCHAR source_id_str
[11];
482 static const WCHAR fmt
[] = {'%','d',0};
485 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_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
))
496 case SRCINFO_PATH
: index
= 4; break;
497 case SRCINFO_TAGFILE
: index
= 2; break;
498 case SRCINFO_DESCRIPTION
: index
= 1; break;
500 WARN("unknown info level: %d\n", info
);
504 if (SetupGetStringFieldW( &ctx
, index
, buffer
, buffer_size
, required_size
))
507 if (required_size
) *required_size
= 1;
510 if (buffer_size
>= 1) buffer
[0] = 0;
513 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
520 /***********************************************************************
521 * SetupGetTargetPathA (SETUPAPI.@)
524 BOOL WINAPI
SetupGetTargetPathA( HINF hinf
, PINFCONTEXT context
, PCSTR section
, PSTR buffer
,
525 DWORD buffer_size
, PDWORD required_size
)
528 WCHAR
*sectionW
= NULL
, *bufferW
= NULL
;
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
)))
538 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, NULL
, 0, &required
))
541 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
544 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, bufferW
, required
, NULL
))
547 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
548 if (required_size
) *required_size
= size
;
552 if (buffer_size
>= size
)
553 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
556 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
563 HeapFree( GetProcessHeap(), 0, sectionW
);
564 HeapFree( GetProcessHeap(), 0, bufferW
);
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};
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
;
598 if (buffer_size
>= size
)
599 lstrcpyW( buffer
, dir
);
602 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
603 HeapFree( GetProcessHeap(), 0, dir
);
607 HeapFree( GetProcessHeap(), 0, dir
);