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 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
25 static const WCHAR source_disks_names
[] =
26 {'S','o','u','r','c','e','D','i','s','k','s','N','a','m','e','s',0};
27 static const WCHAR source_disks_files
[] =
28 {'S','o','u','r','c','e','D','i','s','k','s','F','i','l','e','s',0};
30 /* fills the PSP_INF_INFORMATION struct fill_info is TRUE
31 * always returns the required size of the information
33 static BOOL
fill_inf_info(HINF inf
, PSP_INF_INFORMATION buffer
, DWORD size
, DWORD
*required
)
35 LPCWSTR filename
= PARSER_get_inf_filename(inf
);
36 DWORD total_size
= FIELD_OFFSET(SP_INF_INFORMATION
, VersionData
)
37 + (lstrlenW(filename
) + 1) * sizeof(WCHAR
);
39 if (required
) *required
= total_size
;
41 /* FIXME: we need to parse the INF file to find the correct version info */
44 if (size
< total_size
)
46 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
49 buffer
->InfStyle
= INF_STYLE_WIN4
;
51 /* put the filename in buffer->VersionData */
52 lstrcpyW((LPWSTR
)&buffer
->VersionData
[0], filename
);
57 static HINF
search_for_inf(LPCVOID InfSpec
, DWORD SearchControl
)
59 HINF hInf
= INVALID_HANDLE_VALUE
;
60 WCHAR inf_path
[MAX_PATH
];
62 static const WCHAR infW
[] = {'\\','i','n','f','\\',0};
63 static const WCHAR system32W
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
65 if (SearchControl
== INFINFO_REVERSE_DEFAULT_SEARCH
)
67 GetWindowsDirectoryW(inf_path
, MAX_PATH
);
68 lstrcatW(inf_path
, system32W
);
69 lstrcatW(inf_path
, InfSpec
);
71 hInf
= SetupOpenInfFileW(inf_path
, NULL
,
72 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
73 if (hInf
!= INVALID_HANDLE_VALUE
)
76 GetWindowsDirectoryW(inf_path
, MAX_PATH
);
77 lstrcpyW(inf_path
, infW
);
78 lstrcatW(inf_path
, InfSpec
);
80 return SetupOpenInfFileW(inf_path
, NULL
,
81 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
84 return INVALID_HANDLE_VALUE
;
87 /***********************************************************************
88 * SetupGetInfInformationA (SETUPAPI.@)
91 BOOL WINAPI
SetupGetInfInformationA(LPCVOID InfSpec
, DWORD SearchControl
,
92 PSP_INF_INFORMATION ReturnBuffer
,
93 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
95 LPWSTR inf
= (LPWSTR
)InfSpec
;
99 if (InfSpec
&& SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
101 len
= lstrlenA(InfSpec
) + 1;
102 inf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
103 MultiByteToWideChar(CP_ACP
, 0, InfSpec
, -1, inf
, len
);
106 ret
= SetupGetInfInformationW(inf
, SearchControl
, ReturnBuffer
,
107 ReturnBufferSize
, RequiredSize
);
109 if (SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
110 HeapFree(GetProcessHeap(), 0, inf
);
115 /***********************************************************************
116 * SetupGetInfInformationW (SETUPAPI.@)
119 * Only handles the case when InfSpec is an INF handle.
121 BOOL WINAPI
SetupGetInfInformationW(LPCVOID InfSpec
, DWORD SearchControl
,
122 PSP_INF_INFORMATION ReturnBuffer
,
123 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
129 TRACE("(%p, %d, %p, %d, %p)\n", InfSpec
, SearchControl
, ReturnBuffer
,
130 ReturnBufferSize
, RequiredSize
);
134 if (SearchControl
== INFINFO_INF_SPEC_IS_HINF
)
135 SetLastError(ERROR_INVALID_HANDLE
);
137 SetLastError(ERROR_INVALID_PARAMETER
);
142 switch (SearchControl
)
144 case INFINFO_INF_SPEC_IS_HINF
:
147 case INFINFO_INF_NAME_IS_ABSOLUTE
:
148 case INFINFO_DEFAULT_SEARCH
:
149 inf
= SetupOpenInfFileW(InfSpec
, NULL
,
150 INF_STYLE_OLDNT
| INF_STYLE_WIN4
, NULL
);
152 case INFINFO_REVERSE_DEFAULT_SEARCH
:
153 inf
= search_for_inf(InfSpec
, SearchControl
);
155 case INFINFO_INF_PATH_LIST_SEARCH
:
156 FIXME("Unhandled search control: %d\n", SearchControl
);
163 SetLastError(ERROR_INVALID_PARAMETER
);
167 if (inf
== INVALID_HANDLE_VALUE
)
169 SetLastError(ERROR_FILE_NOT_FOUND
);
173 ret
= fill_inf_info(inf
, ReturnBuffer
, ReturnBufferSize
, &infSize
);
174 if (!ReturnBuffer
&& (ReturnBufferSize
>= infSize
))
176 SetLastError(ERROR_INVALID_PARAMETER
);
179 if (RequiredSize
) *RequiredSize
= infSize
;
181 if (SearchControl
>= INFINFO_INF_NAME_IS_ABSOLUTE
)
182 SetupCloseInfFile(inf
);
187 /***********************************************************************
188 * SetupQueryInfFileInformationA (SETUPAPI.@)
190 BOOL WINAPI
SetupQueryInfFileInformationA(PSP_INF_INFORMATION InfInformation
,
191 UINT InfIndex
, PSTR ReturnBuffer
,
192 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
198 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
, NULL
, 0, &size
);
202 filenameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
204 ret
= SetupQueryInfFileInformationW(InfInformation
, InfIndex
,
205 filenameW
, size
, &size
);
208 HeapFree(GetProcessHeap(), 0, filenameW
);
213 *RequiredSize
= size
;
217 HeapFree(GetProcessHeap(), 0, filenameW
);
218 if (ReturnBufferSize
)
220 SetLastError(ERROR_INVALID_PARAMETER
);
227 if (size
> ReturnBufferSize
)
229 HeapFree(GetProcessHeap(), 0, filenameW
);
230 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
234 WideCharToMultiByte(CP_ACP
, 0, filenameW
, -1, ReturnBuffer
, size
, NULL
, NULL
);
235 HeapFree(GetProcessHeap(), 0, filenameW
);
240 /***********************************************************************
241 * SetupQueryInfFileInformationW (SETUPAPI.@)
243 BOOL WINAPI
SetupQueryInfFileInformationW(PSP_INF_INFORMATION InfInformation
,
244 UINT InfIndex
, PWSTR ReturnBuffer
,
245 DWORD ReturnBufferSize
, PDWORD RequiredSize
)
250 TRACE("(%p, %u, %p, %d, %p) Stub!\n", InfInformation
, InfIndex
,
251 ReturnBuffer
, ReturnBufferSize
, RequiredSize
);
255 SetLastError(ERROR_INVALID_PARAMETER
);
260 FIXME("Appended INF files are not handled\n");
262 ptr
= (LPWSTR
)&InfInformation
->VersionData
[0];
266 *RequiredSize
= len
+ 1;
271 if (ReturnBufferSize
< len
)
273 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
277 lstrcpyW(ReturnBuffer
, ptr
);
281 /***********************************************************************
282 * SetupGetSourceFileLocationA (SETUPAPI.@)
285 BOOL WINAPI
SetupGetSourceFileLocationA( HINF hinf
, PINFCONTEXT context
, PCSTR filename
,
286 PUINT source_id
, PSTR buffer
, DWORD buffer_size
,
287 PDWORD required_size
)
290 WCHAR
*filenameW
= NULL
, *bufferW
= NULL
;
294 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf
, context
, debugstr_a(filename
), source_id
,
295 buffer
, buffer_size
, required_size
);
297 if (filename
&& *filename
&& !(filenameW
= strdupAtoW( filename
)))
300 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, NULL
, 0, &required
))
303 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
306 if (!SetupGetSourceFileLocationW( hinf
, context
, filenameW
, source_id
, bufferW
, required
, NULL
))
309 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
310 if (required_size
) *required_size
= size
;
314 if (buffer_size
>= size
)
315 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
318 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
325 HeapFree( GetProcessHeap(), 0, filenameW
);
326 HeapFree( GetProcessHeap(), 0, bufferW
);
330 static LPWSTR
get_source_id( HINF hinf
, PINFCONTEXT context
, PCWSTR filename
)
332 WCHAR Section
[MAX_PATH
];
336 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_files
, Section
, MAX_PATH
, NULL
, NULL
))
339 if (!SetupFindFirstLineW( hinf
, Section
, filename
, context
) &&
340 !SetupFindFirstLineW( hinf
, source_disks_files
, filename
, context
))
343 if (!SetupGetStringFieldW( context
, 1, NULL
, 0, &size
))
346 if (!(source_id
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )))
349 if (!SetupGetStringFieldW( context
, 1, source_id
, size
, NULL
))
351 HeapFree( GetProcessHeap(), 0, source_id
);
355 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_names
, Section
, MAX_PATH
, NULL
, NULL
))
358 if (!SetupFindFirstLineW( hinf
, Section
, source_id
, context
) &&
359 !SetupFindFirstLineW( hinf
, source_disks_names
, source_id
, context
))
361 HeapFree( GetProcessHeap(), 0, source_id
);
367 /***********************************************************************
368 * SetupGetSourceFileLocationW (SETUPAPI.@)
371 BOOL WINAPI
SetupGetSourceFileLocationW( HINF hinf
, PINFCONTEXT context
, PCWSTR filename
,
372 PUINT source_id
, PWSTR buffer
, DWORD buffer_size
,
373 PDWORD required_size
)
376 WCHAR
*end
, *source_id_str
;
378 TRACE("%p, %p, %s, %p, %p, 0x%08x, %p\n", hinf
, context
, debugstr_w(filename
), source_id
,
379 buffer
, buffer_size
, required_size
);
381 if (!context
) context
= &ctx
;
383 if (!(source_id_str
= get_source_id( hinf
, context
, filename
)))
386 *source_id
= strtolW( source_id_str
, &end
, 10 );
387 if (end
== source_id_str
|| *end
)
389 HeapFree( GetProcessHeap(), 0, source_id_str
);
392 HeapFree( GetProcessHeap(), 0, source_id_str
);
394 if (SetupGetStringFieldW( context
, 4, buffer
, buffer_size
, required_size
))
397 if (required_size
) *required_size
= 1;
400 if (buffer_size
>= 1) buffer
[0] = 0;
403 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
410 /***********************************************************************
411 * SetupGetSourceInfoA (SETUPAPI.@)
414 BOOL WINAPI
SetupGetSourceInfoA( HINF hinf
, UINT source_id
, UINT info
,
415 PSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
418 WCHAR
*bufferW
= NULL
;
422 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_size
,
425 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, NULL
, 0, &required
))
428 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
431 if (!SetupGetSourceInfoW( hinf
, source_id
, info
, bufferW
, required
, NULL
))
434 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
435 if (required_size
) *required_size
= size
;
439 if (buffer_size
>= size
)
440 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
443 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
450 HeapFree( GetProcessHeap(), 0, bufferW
);
454 /***********************************************************************
455 * SetupGetSourceInfoW (SETUPAPI.@)
458 BOOL WINAPI
SetupGetSourceInfoW( HINF hinf
, UINT source_id
, UINT info
,
459 PWSTR buffer
, DWORD buffer_size
, LPDWORD required_size
)
461 WCHAR Section
[MAX_PATH
];
463 WCHAR source_id_str
[11];
464 static const WCHAR fmt
[] = {'%','d',0};
467 TRACE("%p, %d, %d, %p, %d, %p\n", hinf
, source_id
, info
, buffer
, buffer_size
,
470 sprintfW( source_id_str
, fmt
, source_id
);
472 if (!SetupDiGetActualSectionToInstallW(hinf
, source_disks_names
, Section
, MAX_PATH
, NULL
, NULL
))
475 if (!SetupFindFirstLineW( hinf
, Section
, source_id_str
, &ctx
) &&
476 !SetupFindFirstLineW( hinf
, source_disks_names
, source_id_str
, &ctx
))
481 case SRCINFO_PATH
: index
= 4; break;
482 case SRCINFO_TAGFILE
: index
= 2; break;
483 case SRCINFO_DESCRIPTION
: index
= 1; break;
485 WARN("unknown info level: %d\n", info
);
489 if (SetupGetStringFieldW( &ctx
, index
, buffer
, buffer_size
, required_size
))
492 if (required_size
) *required_size
= 1;
495 if (buffer_size
>= 1) buffer
[0] = 0;
498 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
505 /***********************************************************************
506 * SetupGetTargetPathA (SETUPAPI.@)
509 BOOL WINAPI
SetupGetTargetPathA( HINF hinf
, PINFCONTEXT context
, PCSTR section
, PSTR buffer
,
510 DWORD buffer_size
, PDWORD required_size
)
513 WCHAR
*sectionW
= NULL
, *bufferW
= NULL
;
517 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf
, context
, debugstr_a(section
), buffer
,
518 buffer_size
, required_size
);
520 if (section
&& !(sectionW
= strdupAtoW( section
)))
523 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, NULL
, 0, &required
))
526 if (!(bufferW
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) )))
529 if (!SetupGetTargetPathW( hinf
, context
, sectionW
, bufferW
, required
, NULL
))
532 size
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
533 if (required_size
) *required_size
= size
;
537 if (buffer_size
>= size
)
538 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, buffer_size
, NULL
, NULL
);
541 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
548 HeapFree( GetProcessHeap(), 0, sectionW
);
549 HeapFree( GetProcessHeap(), 0, bufferW
);
553 /***********************************************************************
554 * SetupGetTargetPathW (SETUPAPI.@)
557 BOOL WINAPI
SetupGetTargetPathW( HINF hinf
, PINFCONTEXT context
, PCWSTR section
, PWSTR buffer
,
558 DWORD buffer_size
, PDWORD required_size
)
560 static const WCHAR destination_dirs
[] =
561 {'D','e','s','t','i','n','a','t','i','o','n','D','i','r','s',0};
562 static const WCHAR default_dest_dir
[] =
563 {'D','e','f','a','u','l','t','D','e','s','t','D','i','r',0};
566 WCHAR
*dir
, systemdir
[MAX_PATH
];
570 TRACE("%p, %p, %s, %p, 0x%08x, %p\n", hinf
, context
, debugstr_w(section
), buffer
,
571 buffer_size
, required_size
);
573 if (context
) ret
= SetupFindFirstLineW( hinf
, destination_dirs
, NULL
, context
);
576 if (!(ret
= SetupFindFirstLineW( hinf
, destination_dirs
, section
, &ctx
)))
577 ret
= SetupFindFirstLineW( hinf
, destination_dirs
, default_dest_dir
, &ctx
);
579 if (!ret
|| !(dir
= PARSER_get_dest_dir( context
? context
: &ctx
)))
581 GetSystemDirectoryW( systemdir
, MAX_PATH
);
584 size
= strlenW( dir
) + 1;
585 if (required_size
) *required_size
= size
;
589 if (buffer_size
>= size
)
590 lstrcpyW( buffer
, dir
);
593 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
594 HeapFree( GetProcessHeap(), 0, dir
);
598 if (dir
!= systemdir
) HeapFree( GetProcessHeap(), 0, dir
);
602 /***********************************************************************
603 * SetupQueryInfOriginalFileInformationA (SETUPAPI.@)
605 BOOL WINAPI
SetupQueryInfOriginalFileInformationA(
606 PSP_INF_INFORMATION InfInformation
, UINT InfIndex
,
607 PSP_ALTPLATFORM_INFO AlternativePlatformInfo
,
608 PSP_ORIGINAL_FILE_INFO_A OriginalFileInfo
)
611 SP_ORIGINAL_FILE_INFO_W OriginalFileInfoW
;
613 TRACE("(%p, %d, %p, %p)\n", InfInformation
, InfIndex
,
614 AlternativePlatformInfo
, OriginalFileInfo
);
616 if (OriginalFileInfo
->cbSize
!= sizeof(*OriginalFileInfo
))
618 WARN("incorrect OriginalFileInfo->cbSize of %d\n", OriginalFileInfo
->cbSize
);
619 SetLastError( ERROR_INVALID_USER_BUFFER
);
623 OriginalFileInfoW
.cbSize
= sizeof(OriginalFileInfoW
);
624 ret
= SetupQueryInfOriginalFileInformationW(InfInformation
, InfIndex
,
625 AlternativePlatformInfo
, &OriginalFileInfoW
);
628 WideCharToMultiByte(CP_ACP
, 0, OriginalFileInfoW
.OriginalInfName
, -1,
629 OriginalFileInfo
->OriginalInfName
, MAX_PATH
, NULL
, NULL
);
630 WideCharToMultiByte(CP_ACP
, 0, OriginalFileInfoW
.OriginalCatalogName
, -1,
631 OriginalFileInfo
->OriginalCatalogName
, MAX_PATH
, NULL
, NULL
);
637 /***********************************************************************
638 * SetupQueryInfOriginalFileInformationW (SETUPAPI.@)
640 BOOL WINAPI
SetupQueryInfOriginalFileInformationW(
641 PSP_INF_INFORMATION InfInformation
, UINT InfIndex
,
642 PSP_ALTPLATFORM_INFO AlternativePlatformInfo
,
643 PSP_ORIGINAL_FILE_INFO_W OriginalFileInfo
)
648 static const WCHAR wszVersion
[] = { 'V','e','r','s','i','o','n',0 };
649 static const WCHAR wszCatalogFile
[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
651 FIXME("(%p, %d, %p, %p): semi-stub\n", InfInformation
, InfIndex
,
652 AlternativePlatformInfo
, OriginalFileInfo
);
654 if (OriginalFileInfo
->cbSize
!= sizeof(*OriginalFileInfo
))
656 WARN("incorrect OriginalFileInfo->cbSize of %d\n", OriginalFileInfo
->cbSize
);
657 SetLastError(ERROR_INVALID_USER_BUFFER
);
661 inf_path
= (LPWSTR
)&InfInformation
->VersionData
[0];
663 /* FIXME: we should get OriginalCatalogName from CatalogFile line in
664 * the original inf file and cache it, but that would require building a
666 hinf
= SetupOpenInfFileW(inf_path
, NULL
, INF_STYLE_WIN4
, NULL
);
667 if (hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
669 if (!SetupGetLineTextW(NULL
, hinf
, wszVersion
, wszCatalogFile
,
670 OriginalFileInfo
->OriginalCatalogName
,
671 sizeof(OriginalFileInfo
->OriginalCatalogName
)/sizeof(OriginalFileInfo
->OriginalCatalogName
[0]),
674 OriginalFileInfo
->OriginalCatalogName
[0] = '\0';
676 SetupCloseInfFile(hinf
);
678 /* FIXME: not quite correct as we just return the same file name as
679 * destination (copied) inf file, not the source (original) inf file.
680 * to fix it properly would require building a .pnf file */
681 /* file name is stored in VersionData field of InfInformation */
682 inf_name
= strrchrW(inf_path
, '\\');
683 if (inf_name
) inf_name
++;
684 else inf_name
= inf_path
;
686 strcpyW(OriginalFileInfo
->OriginalInfName
, inf_name
);