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 "setupapi_private.h"
23 static const WCHAR source_disks_names
[] =
24 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
25 static const WCHAR source_disks_files
[] =
26 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
28 /* fills the PSP_INF_INFORMATION struct fill_info is TRUE
29 * always returns the required size of the information
31 static BOOL
fill_inf_info(HINF inf
, PSP_INF_INFORMATION buffer
, DWORD size
, DWORD
*required
)
33 LPCWSTR filename
= PARSER_get_inf_filename(inf
);
34 DWORD total_size
= FIELD_OFFSET(SP_INF_INFORMATION
, VersionData
)
35 + (lstrlenW(filename
) + 1) * sizeof(WCHAR
);
37 if (required
) *required
= total_size
;
39 /* FIXME: we need to parse the INF file to find the correct version info */
42 if (size
< total_size
)
44 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
47 buffer
->InfStyle
= INF_STYLE_WIN4
;
49 /* put the filename in buffer->VersionData */
50 lstrcpyW((LPWSTR
)&buffer
->VersionData
[0], filename
);
55 static HINF
search_for_inf(LPCVOID InfSpec
, DWORD SearchControl
)
57 HINF hInf
= INVALID_HANDLE_VALUE
;
58 WCHAR inf_path
[MAX_PATH
];
60 static const WCHAR infW
[] = {'\\','i','n','f','\\',0};
61 static const WCHAR system32W
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
63 if (SearchControl
== INFINFO_REVERSE_DEFAULT_SEARCH
)
65 GetWindowsDirectoryW(inf_path
, MAX_PATH
);
66 lstrcatW(inf_path
, system32W
);
67 lstrcatW(inf_path
, InfSpec
);
69 hInf
= SetupOpenInfFileW(inf_path
, NULL
,
70 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
71 if (hInf
!= INVALID_HANDLE_VALUE
)
74 GetWindowsDirectoryW(inf_path
, MAX_PATH
);
75 lstrcpyW(inf_path
, infW
);
76 lstrcatW(inf_path
, InfSpec
);
78 return SetupOpenInfFileW(inf_path
, NULL
,
79 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
82 return INVALID_HANDLE_VALUE
;
85 /***********************************************************************
86 * SetupGetInfInformationA (SETUPAPI.@)
89 BOOL WINAPI
SetupGetInfInformationA(LPCVOID InfSpec
, DWORD SearchControl
,
90 PSP_INF_INFORMATION ReturnBuffer
,
91 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
93 LPWSTR inf
= (LPWSTR
)InfSpec
;
97 if (InfSpec
&& SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
99 len
= lstrlenA(InfSpec
) + 1;
100 inf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
101 MultiByteToWideChar(CP_ACP
, 0, InfSpec
, -1, inf
, len
);
104 ret
= SetupGetInfInformationW(inf
, SearchControl
, ReturnBuffer
,
105 ReturnBufferSize
, RequiredSize
);
107 if (SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
108 HeapFree(GetProcessHeap(), 0, inf
);
113 /***********************************************************************
114 * SetupGetInfInformationW (SETUPAPI.@)
117 * Only handles the case when InfSpec is an INF handle.
119 BOOL WINAPI
SetupGetInfInformationW(LPCVOID InfSpec
, DWORD SearchControl
,
120 PSP_INF_INFORMATION ReturnBuffer
,
121 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
127 TRACE("(%p, %d, %p, %d, %p)\n", InfSpec
, SearchControl
, ReturnBuffer
,
128 ReturnBufferSize
, RequiredSize
);
132 if (SearchControl
== INFINFO_INF_SPEC_IS_HINF
)
133 SetLastError(ERROR_INVALID_HANDLE
);
135 SetLastError(ERROR_INVALID_PARAMETER
);
140 switch (SearchControl
)
142 case INFINFO_INF_SPEC_IS_HINF
:
145 case INFINFO_INF_NAME_IS_ABSOLUTE
:
146 case INFINFO_DEFAULT_SEARCH
:
147 inf
= SetupOpenInfFileW(InfSpec
, NULL
,
148 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
150 case INFINFO_REVERSE_DEFAULT_SEARCH
:
151 inf
= search_for_inf(InfSpec
, SearchControl
);
153 case INFINFO_INF_PATH_LIST_SEARCH
:
154 FIXME("Unhandled search control: %d\n", SearchControl
);
161 SetLastError(ERROR_INVALID_PARAMETER
);
165 if (inf
== INVALID_HANDLE_VALUE
)
167 SetLastError(ERROR_FILE_NOT_FOUND
);
171 ret
= fill_inf_info(inf
, ReturnBuffer
, ReturnBufferSize
, &infSize
);
172 if (!ReturnBuffer
&& (ReturnBufferSize
>= infSize
))
174 SetLastError(ERROR_INVALID_PARAMETER
);
177 if (RequiredSize
) *RequiredSize
= infSize
;
179 if (SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
180 SetupCloseInfFile(inf
);
185 /***********************************************************************
186 * SetupQueryInfFileInformationA (SETUPAPI.@)
188 BOOL WINAPI
SetupQueryInfFileInformationA(PSP_INF_INFORMATION InfInformation
,
189 UINT InfIndex
, PSTR ReturnBuffer
,
190 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
196 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
, NULL
, 0, &size
);
200 filenameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
202 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
,
203 filenameW
, size
, &size
);
206 HeapFree(GetProcessHeap(), 0, filenameW
);
211 *RequiredSize
= size
;
215 HeapFree(GetProcessHeap(), 0, filenameW
);
216 if (ReturnBufferSize
)
218 SetLastError(ERROR_INVALID_PARAMETER
);
225 if (size
> ReturnBufferSize
)
227 HeapFree(GetProcessHeap(), 0, filenameW
);
228 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
232 WideCharToMultiByte(CP_ACP
, 0, filenameW
, -1, ReturnBuffer
, size
, NULL
, NULL
);
233 HeapFree(GetProcessHeap(), 0, filenameW
);
238 /***********************************************************************
239 * SetupQueryInfFileInformationW (SETUPAPI.@)
241 BOOL WINAPI
SetupQueryInfFileInformationW(PSP_INF_INFORMATION InfInformation
,
242 UINT InfIndex
, PWSTR ReturnBuffer
,
243 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
248 TRACE("(%p, %u, %p, %d, %p) Stub!\n", InfInformation
, InfIndex
,
249 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
253 SetLastError(ERROR_INVALID_PARAMETER
);
258 FIXME("Appended INF files are not handled\n");
260 ptr
= (LPWSTR
)&InfInformation
->VersionData
[0];
264 *RequiredSize
= len
+ 1;
269 if (ReturnBufferSize
< len
)
271 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
275 lstrcpyW(ReturnBuffer
, ptr
);
279 /***********************************************************************
280 * SetupGetSourceFileLocationA (SETUPAPI.@)
283 BOOL WINAPI
SetupGetSourceFileLocationA( HINF hinf
, PINFCONTEXT context
, PCSTR filename
,
284 PUINT source_id
, PSTR buffer
, DWORD buffer_size
,
285 PDWORD required_size
)
288 WCHAR
*filenameW
= NULL
, *bufferW
= NULL
;
292 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf
, context
, debugstr_a(filename
), source_id
,
293 buffer
, buffer_size
, required_size
);
295 if (filename
&& *filename
&& !(filenameW
= strdupAtoW( filename
)))
298 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, NULL
, 0, &required
))
301 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
304 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, bufferW
, required
, NULL
))
307 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
308 if (required_size
) *required_size
= size
;
312 if (buffer_size
>= size
)
313 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
316 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
323 HeapFree( GetProcessHeap(), 0, filenameW
);
324 HeapFree( GetProcessHeap(), 0, bufferW
);
328 static LPWSTR
get_source_id( HINF hinf
, PINFCONTEXT context
, PCWSTR filename
)
330 WCHAR Section
[MAX_PATH
];
334 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_files
, Section
, MAX_PATH
, NULL
, NULL
))
337 if (!SetupFindFirstLineW( hinf
, Section
, filename
, context
) &&
338 !SetupFindFirstLineW( hinf
, source_disks_files
, filename
, context
))
341 if (!SetupGetStringFieldW( context
, 1, NULL
, 0, &size
))
344 if (!(source_id
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
347 if (!SetupGetStringFieldW( context
, 1, source_id
, size
, NULL
))
349 HeapFree( GetProcessHeap(), 0, source_id
);
353 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_names
, Section
, MAX_PATH
, NULL
, NULL
))
356 if (!SetupFindFirstLineW( hinf
, Section
, source_id
, context
) &&
357 !SetupFindFirstLineW( hinf
, source_disks_names
, source_id
, context
))
359 HeapFree( GetProcessHeap(), 0, source_id
);
365 /***********************************************************************
366 * SetupGetSourceFileLocationW (SETUPAPI.@)
369 BOOL WINAPI
SetupGetSourceFileLocationW( HINF hinf
, PINFCONTEXT context
, PCWSTR filename
,
370 PUINT source_id
, PWSTR buffer
, DWORD buffer_size
,
371 PDWORD required_size
)
374 WCHAR
*end
, *source_id_str
;
376 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf
, context
, debugstr_w(filename
), source_id
,
377 buffer
, buffer_size
, required_size
);
379 if (!context
) context
= &ctx
;
381 if (!(source_id_str
= get_source_id( hinf
, context
, filename
)))
384 *source_id
= strtolW( source_id_str
, &end
, 10 );
385 if (end
== source_id_str
|| *end
)
387 HeapFree( GetProcessHeap(), 0, source_id_str
);
390 HeapFree( GetProcessHeap(), 0, source_id_str
);
392 if (SetupGetStringFieldW( context
, 4, buffer
, buffer_size
, required_size
))
395 if (required_size
) *required_size
= 1;
398 if (buffer_size
>= 1) buffer
[0] = 0;
401 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
408 /***********************************************************************
409 * SetupGetSourceInfoA (SETUPAPI.@)
412 BOOL WINAPI
SetupGetSourceInfoA( HINF hinf
, UINT source_id
, UINT info
,
413 PSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
416 WCHAR
*bufferW
= NULL
;
420 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_size
,
423 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, NULL
, 0, &required
))
426 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
429 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, bufferW
, required
, NULL
))
432 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
433 if (required_size
) *required_size
= size
;
437 if (buffer_size
>= size
)
438 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
441 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
448 HeapFree( GetProcessHeap(), 0, bufferW
);
452 /***********************************************************************
453 * SetupGetSourceInfoW (SETUPAPI.@)
456 BOOL WINAPI
SetupGetSourceInfoW( HINF hinf
, UINT source_id
, UINT info
,
457 PWSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
459 WCHAR Section
[MAX_PATH
];
461 WCHAR source_id_str
[11];
462 static const WCHAR fmt
[] = {'%','d',0};
465 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_size
,
468 sprintfW( source_id_str
, fmt
, source_id
);
470 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_names
, Section
, MAX_PATH
, NULL
, NULL
))
473 if (!SetupFindFirstLineW( hinf
, Section
, source_id_str
, &ctx
) &&
474 !SetupFindFirstLineW( hinf
, source_disks_names
, source_id_str
, &ctx
))
479 case SRCINFO_PATH
: index
= 4; break;
480 case SRCINFO_TAGFILE
: index
= 2; break;
481 case SRCINFO_DESCRIPTION
: index
= 1; break;
483 WARN("unknown info level: %d\n", info
);
487 if (SetupGetStringFieldW( &ctx
, index
, buffer
, buffer_size
, required_size
))
490 if (required_size
) *required_size
= 1;
493 if (buffer_size
>= 1) buffer
[0] = 0;
496 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
503 /***********************************************************************
504 * SetupGetTargetPathA (SETUPAPI.@)
507 BOOL WINAPI
SetupGetTargetPathA( HINF hinf
, PINFCONTEXT context
, PCSTR section
, PSTR buffer
,
508 DWORD buffer_size
, PDWORD required_size
)
511 WCHAR
*sectionW
= NULL
, *bufferW
= NULL
;
515 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf
, context
, debugstr_a(section
), buffer
,
516 buffer_size
, required_size
);
518 if (section
&& !(sectionW
= strdupAtoW( section
)))
521 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, NULL
, 0, &required
))
524 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
527 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, bufferW
, required
, NULL
))
530 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
531 if (required_size
) *required_size
= size
;
535 if (buffer_size
>= size
)
536 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
539 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
546 HeapFree( GetProcessHeap(), 0, sectionW
);
547 HeapFree( GetProcessHeap(), 0, bufferW
);
551 /***********************************************************************
552 * SetupGetTargetPathW (SETUPAPI.@)
555 BOOL WINAPI
SetupGetTargetPathW( HINF hinf
, PINFCONTEXT context
, PCWSTR section
, PWSTR buffer
,
556 DWORD buffer_size
, PDWORD required_size
)
558 static const WCHAR destination_dirs
[] =
559 {'D','e','s','t','i','n','a','t','i','o','n','D','i','r','s',0};
560 static const WCHAR default_dest_dir
[] =
561 {'D','e','f','a','u','l','t','D','e','s','t','D','i','r',0};
564 WCHAR
*dir
, systemdir
[MAX_PATH
];
568 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf
, context
, debugstr_w(section
), buffer
,
569 buffer_size
, required_size
);
571 if (context
) ret
= SetupFindFirstLineW( hinf
, destination_dirs
, NULL
, context
);
574 if (!(ret
= SetupFindFirstLineW( hinf
, destination_dirs
, section
, &ctx
)))
575 ret
= SetupFindFirstLineW( hinf
, destination_dirs
, default_dest_dir
, &ctx
);
577 if (!ret
|| !(dir
= PARSER_get_dest_dir( context
? context
: &ctx
)))
579 GetSystemDirectoryW( systemdir
, MAX_PATH
);
582 size
= strlenW( dir
) + 1;
583 if (required_size
) *required_size
= size
;
587 if (buffer_size
>= size
)
588 lstrcpyW( buffer
, dir
);
591 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
592 HeapFree( GetProcessHeap(), 0, dir
);
596 if (dir
!= systemdir
) HeapFree( GetProcessHeap(), 0, dir
);
600 /***********************************************************************
601 * SetupQueryInfOriginalFileInformationA (SETUPAPI.@)
603 BOOL WINAPI
SetupQueryInfOriginalFileInformationA(
604 PSP_INF_INFORMATION InfInformation
, UINT InfIndex
,
605 PSP_ALTPLATFORM_INFO AlternativePlatformInfo
,
606 PSP_ORIGINAL_FILE_INFO_A OriginalFileInfo
)
609 SP_ORIGINAL_FILE_INFO_W OriginalFileInfoW
;
611 TRACE("(%p, %d, %p, %p)\n", InfInformation
, InfIndex
,
612 AlternativePlatformInfo
, OriginalFileInfo
);
614 if (OriginalFileInfo
->cbSize
!= sizeof(*OriginalFileInfo
))
616 WARN("incorrect OriginalFileInfo->cbSize of %d\n", OriginalFileInfo
->cbSize
);
617 SetLastError( ERROR_INVALID_USER_BUFFER
);
621 OriginalFileInfoW
.cbSize
= sizeof(OriginalFileInfoW
);
622 ret
= SetupQueryInfOriginalFileInformationW(InfInformation
, InfIndex
,
623 AlternativePlatformInfo
, &OriginalFileInfoW
);
626 WideCharToMultiByte(CP_ACP
, 0, OriginalFileInfoW
.OriginalInfName
, -1,
627 OriginalFileInfo
->OriginalInfName
, MAX_PATH
, NULL
, NULL
);
628 WideCharToMultiByte(CP_ACP
, 0, OriginalFileInfoW
.OriginalCatalogName
, -1,
629 OriginalFileInfo
->OriginalCatalogName
, MAX_PATH
, NULL
, NULL
);
635 /***********************************************************************
636 * SetupQueryInfOriginalFileInformationW (SETUPAPI.@)
638 BOOL WINAPI
SetupQueryInfOriginalFileInformationW(
639 PSP_INF_INFORMATION InfInformation
, UINT InfIndex
,
640 PSP_ALTPLATFORM_INFO AlternativePlatformInfo
,
641 PSP_ORIGINAL_FILE_INFO_W OriginalFileInfo
)
646 static const WCHAR wszVersion
[] = { 'V','e','r','s','i','o','n',0 };
647 static const WCHAR wszCatalogFile
[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
649 FIXME("(%p, %d, %p, %p): semi-stub\n", InfInformation
, InfIndex
,
650 AlternativePlatformInfo
, OriginalFileInfo
);
652 if (OriginalFileInfo
->cbSize
!= sizeof(*OriginalFileInfo
))
654 WARN("incorrect OriginalFileInfo->cbSize of %d\n", OriginalFileInfo
->cbSize
);
655 SetLastError(ERROR_INVALID_USER_BUFFER
);
659 inf_path
= (LPWSTR
)&InfInformation
->VersionData
[0];
661 /* FIXME: we should get OriginalCatalogName from CatalogFile line in
662 * the original inf file and cache it, but that would require building a
664 hinf
= SetupOpenInfFileW(inf_path
, NULL
, INF_STYLE_WIN4
, NULL
);
665 if (hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
667 if (!SetupGetLineTextW(NULL
, hinf
, wszVersion
, wszCatalogFile
,
668 OriginalFileInfo
->OriginalCatalogName
,
669 sizeof(OriginalFileInfo
->OriginalCatalogName
)/sizeof(OriginalFileInfo
->OriginalCatalogName
[0]),
672 OriginalFileInfo
->OriginalCatalogName
[0] = '\0';
674 SetupCloseInfFile(hinf
);
676 /* FIXME: not quite correct as we just return the same file name as
677 * destination (copied) inf file, not the source (original) inf file.
678 * to fix it properly would require building a .pnf file */
679 /* file name is stored in VersionData field of InfInformation */
680 inf_name
= strrchrW(inf_path
, '\\');
681 if (inf_name
) inf_name
++;
682 else inf_name
= inf_path
;
684 strcpyW(OriginalFileInfo
->OriginalInfName
, inf_name
);