2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/unicode.h"
33 #include "setupapi_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
38 /* info passed to callback functions dealing with files */
39 struct files_callback_info
47 /* info passed to callback functions dealing with the registry */
48 struct registry_callback_info
54 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
56 /* Unicode constants */
57 static const WCHAR CopyFiles
[] = {'C','o','p','y','F','i','l','e','s',0};
58 static const WCHAR DelFiles
[] = {'D','e','l','F','i','l','e','s',0};
59 static const WCHAR RenFiles
[] = {'R','e','n','F','i','l','e','s',0};
60 static const WCHAR Ini2Reg
[] = {'I','n','i','2','R','e','g',0};
61 static const WCHAR LogConf
[] = {'L','o','g','C','o','n','f',0};
62 static const WCHAR AddReg
[] = {'A','d','d','R','e','g',0};
63 static const WCHAR DelReg
[] = {'D','e','l','R','e','g',0};
64 static const WCHAR UpdateInis
[] = {'U','p','d','a','t','e','I','n','i','s',0};
65 static const WCHAR UpdateIniFields
[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
68 /***********************************************************************
71 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
73 static WCHAR
*get_field_string( INFCONTEXT
*context
, DWORD index
, WCHAR
*buffer
,
74 WCHAR
*static_buffer
, DWORD
*size
)
78 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
79 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
81 /* now grow the buffer */
82 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
83 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
*sizeof(WCHAR
) ))) return NULL
;
85 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
87 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
92 /***********************************************************************
95 * Called once for each CopyFiles entry in a given section.
97 static BOOL
copy_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
99 struct files_callback_info
*info
= arg
;
101 if (field
[0] == '@') /* special case: copy single file */
102 SetupQueueDefaultCopyW( info
->queue
, info
->layout
, info
->src_root
, NULL
, field
, info
->copy_flags
);
104 SetupQueueCopySectionW( info
->queue
, info
->src_root
, info
->layout
, hinf
, field
, info
->copy_flags
);
109 /***********************************************************************
110 * delete_files_callback
112 * Called once for each DelFiles entry in a given section.
114 static BOOL
delete_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
116 struct files_callback_info
*info
= arg
;
117 SetupQueueDeleteSectionW( info
->queue
, hinf
, 0, field
);
122 /***********************************************************************
123 * rename_files_callback
125 * Called once for each RenFiles entry in a given section.
127 static BOOL
rename_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
129 struct files_callback_info
*info
= arg
;
130 SetupQueueRenameSectionW( info
->queue
, hinf
, 0, field
);
135 /***********************************************************************
138 * Retrieve the registry root key from its name.
140 static HKEY
get_root_key( const WCHAR
*name
, HKEY def_root
)
142 static const WCHAR HKCR
[] = {'H','K','C','R',0};
143 static const WCHAR HKCU
[] = {'H','K','C','U',0};
144 static const WCHAR HKLM
[] = {'H','K','L','M',0};
145 static const WCHAR HKU
[] = {'H','K','U',0};
146 static const WCHAR HKR
[] = {'H','K','R',0};
148 if (!strcmpiW( name
, HKCR
)) return HKEY_CLASSES_ROOT
;
149 if (!strcmpiW( name
, HKCU
)) return HKEY_CURRENT_USER
;
150 if (!strcmpiW( name
, HKLM
)) return HKEY_LOCAL_MACHINE
;
151 if (!strcmpiW( name
, HKU
)) return HKEY_USERS
;
152 if (!strcmpiW( name
, HKR
)) return def_root
;
157 /***********************************************************************
158 * append_multi_sz_value
160 * Append a multisz string to a multisz registry value.
162 static void append_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*strings
,
165 DWORD size
, type
, total
;
168 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
169 if (type
!= REG_MULTI_SZ
) return;
171 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
172 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
174 /* compare each string against all the existing ones */
178 int len
= strlenW(strings
) + 1;
180 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
181 if (!strcmpiW( p
, strings
)) break;
183 if (!*p
) /* not found, need to append it */
185 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
193 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
194 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
);
197 HeapFree( GetProcessHeap(), 0, buffer
);
201 /***********************************************************************
202 * delete_multi_sz_value
204 * Remove a string from a multisz registry value.
206 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
209 WCHAR
*buffer
, *src
, *dst
;
211 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
212 if (type
!= REG_MULTI_SZ
) return;
213 /* allocate double the size, one for value before and one for after */
214 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
215 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
220 int len
= strlenW(src
) + 1;
221 if (strcmpiW( src
, string
))
223 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
229 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
231 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
232 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
233 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
236 HeapFree( GetProcessHeap(), 0, buffer
);
240 /***********************************************************************
243 * Perform an add/delete registry operation depending on the flags.
245 static BOOL
do_reg_operation( HKEY hkey
, const WCHAR
*value
, INFCONTEXT
*context
, INT flags
)
249 if (flags
& (FLG_ADDREG_DELREG_BIT
| FLG_ADDREG_DELVAL
)) /* deletion */
251 if (*value
&& !(flags
& FLG_DELREG_KEYONLY_COMMON
))
253 if ((flags
& FLG_DELREG_MULTI_SZ_DELSTRING
) == FLG_DELREG_MULTI_SZ_DELSTRING
)
257 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
) || !size
) return TRUE
;
258 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
259 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
260 delete_multi_sz_value( hkey
, value
, str
);
261 HeapFree( GetProcessHeap(), 0, str
);
263 else RegDeleteValueW( hkey
, value
);
265 else RegDeleteKeyW( hkey
, NULL
);
269 if (flags
& (FLG_ADDREG_KEYONLY
|FLG_ADDREG_KEYONLY_COMMON
)) return TRUE
;
271 if (flags
& (FLG_ADDREG_NOCLOBBER
|FLG_ADDREG_OVERWRITEONLY
))
273 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
274 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
)) return TRUE
;
275 if (!exists
& (flags
& FLG_ADDREG_OVERWRITEONLY
)) return TRUE
;
278 switch(flags
& FLG_ADDREG_TYPE_MASK
)
280 case FLG_ADDREG_TYPE_SZ
: type
= REG_SZ
; break;
281 case FLG_ADDREG_TYPE_MULTI_SZ
: type
= REG_MULTI_SZ
; break;
282 case FLG_ADDREG_TYPE_EXPAND_SZ
: type
= REG_EXPAND_SZ
; break;
283 case FLG_ADDREG_TYPE_BINARY
: type
= REG_BINARY
; break;
284 case FLG_ADDREG_TYPE_DWORD
: type
= REG_DWORD
; break;
285 case FLG_ADDREG_TYPE_NONE
: type
= REG_NONE
; break;
286 default: type
= flags
>> 16; break;
289 if (!(flags
& FLG_ADDREG_BINVALUETYPE
) ||
290 (type
== REG_DWORD
&& SetupGetFieldCount(context
) == 5))
292 static const WCHAR empty
;
295 if (type
== REG_MULTI_SZ
)
297 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
300 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
301 SetupGetMultiSzFieldW( context
, 5, str
, size
, NULL
);
303 if (flags
& FLG_ADDREG_APPEND
)
305 if (!str
) return TRUE
;
306 append_multi_sz_value( hkey
, value
, str
, size
);
307 HeapFree( GetProcessHeap(), 0, str
);
310 /* else fall through to normal string handling */
314 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
317 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
318 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
322 if (type
== REG_DWORD
)
324 DWORD dw
= str
? strtoulW( str
, NULL
, 16 ) : 0;
325 TRACE( "setting dword %s to %lx\n", debugstr_w(value
), dw
);
326 RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&dw
, sizeof(dw
) );
330 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(str
) );
331 if (str
) RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)str
, size
* sizeof(WCHAR
) );
332 else RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&empty
, sizeof(WCHAR
) );
334 HeapFree( GetProcessHeap(), 0, str
);
337 else /* get the binary data */
341 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
344 if (!(data
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
345 TRACE( "setting binary data %s len %ld\n", debugstr_w(value
), size
);
346 SetupGetBinaryField( context
, 5, data
, size
, NULL
);
348 RegSetValueExW( hkey
, value
, 0, type
, data
, size
);
349 HeapFree( GetProcessHeap(), 0, data
);
355 /***********************************************************************
358 * Called once for each AddReg and DelReg entry in a given section.
360 static BOOL
registry_callback( HINF hinf
, PCWSTR field
, void *arg
)
362 struct registry_callback_info
*info
= arg
;
366 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
368 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
370 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
374 if (!SetupGetStringFieldW( &context
, 1, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
376 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
380 if (!SetupGetStringFieldW( &context
, 2, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
384 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
388 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
392 if (!flags
) flags
= FLG_ADDREG_DELREG_BIT
;
393 else if (!(flags
& FLG_ADDREG_DELREG_BIT
)) continue; /* ignore this entry */
396 if (info
->delete || (flags
& FLG_ADDREG_OVERWRITEONLY
))
398 if (RegOpenKeyW( root_key
, buffer
, &hkey
)) continue; /* ignore if it doesn't exist */
400 else if (RegCreateKeyW( root_key
, buffer
, &hkey
))
402 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
405 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
408 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
412 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
423 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
427 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
429 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
431 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
432 WCHAR filename
[MAX_INF_STRING_LENGTH
];
433 WCHAR section
[MAX_INF_STRING_LENGTH
];
434 WCHAR entry
[MAX_INF_STRING_LENGTH
];
435 WCHAR string
[MAX_INF_STRING_LENGTH
];
438 if (!SetupGetStringFieldW( &context
, 1, filename
,
439 sizeof(filename
)/sizeof(WCHAR
), NULL
))
442 if (!SetupGetStringFieldW( &context
, 2, section
,
443 sizeof(section
)/sizeof(WCHAR
), NULL
))
446 if (!SetupGetStringFieldW( &context
, 4, buffer
,
447 sizeof(buffer
)/sizeof(WCHAR
), NULL
))
450 divider
= strchrW(buffer
,'=');
454 strcpyW(entry
,buffer
);
456 strcpyW(string
,divider
);
460 strcpyW(entry
,buffer
);
464 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
465 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
466 WritePrivateProfileStringW(section
,entry
,string
,filename
);
472 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
474 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
478 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
480 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
484 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
486 FIXME( "should do logconf %s\n", debugstr_w(field
) );
491 /***********************************************************************
492 * iterate_section_fields
494 * Iterate over all fields of a certain key of a certain section
496 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
497 iterate_fields_func callback
, void *arg
)
499 WCHAR static_buffer
[200];
500 WCHAR
*buffer
= static_buffer
;
501 DWORD size
= sizeof(static_buffer
)/sizeof(WCHAR
);
505 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
508 UINT i
, count
= SetupGetFieldCount( &context
);
509 for (i
= 1; i
<= count
; i
++)
511 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
513 if (!callback( hinf
, buffer
, arg
))
515 ERR("callback failed for %s %s\n", debugstr_w(section
), debugstr_w(buffer
) );
519 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
523 if (buffer
&& buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
528 /***********************************************************************
529 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
531 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
532 PCSTR section
, PCSTR src_root
, UINT flags
)
534 UNICODE_STRING sectionW
;
537 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
539 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
543 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
548 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
550 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
551 srcW
.Buffer
, flags
);
552 RtlFreeUnicodeString( &srcW
);
554 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
556 RtlFreeUnicodeString( §ionW
);
561 /***********************************************************************
562 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
564 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
565 PCWSTR section
, PCWSTR src_root
, UINT flags
)
567 struct files_callback_info info
;
570 info
.src_root
= src_root
;
571 info
.copy_flags
= flags
;
572 info
.layout
= hlayout
;
573 return iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
);
577 /***********************************************************************
578 * SetupInstallFromInfSectionA (SETUPAPI.@)
580 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
581 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
582 PSP_FILE_CALLBACK_A callback
, PVOID context
,
583 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
585 UNICODE_STRING sectionW
, src_rootW
;
586 struct callback_WtoA_context ctx
;
589 src_rootW
.Buffer
= NULL
;
590 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
592 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
596 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
598 ctx
.orig_context
= context
;
599 ctx
.orig_handler
= callback
;
600 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
601 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
602 &ctx
, devinfo
, devinfo_data
);
603 RtlFreeUnicodeString( §ionW
);
605 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
607 RtlFreeUnicodeString( &src_rootW
);
612 /***********************************************************************
613 * SetupInstallFromInfSectionW (SETUPAPI.@)
615 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
616 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
617 PSP_FILE_CALLBACK_W callback
, PVOID context
,
618 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
620 if (flags
& SPINST_FILES
)
622 struct files_callback_info info
;
626 if (!(queue
= SetupOpenFileQueue())) return FALSE
;
628 info
.src_root
= src_root
;
629 info
.copy_flags
= copy_flags
;
631 ret
= (iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
) &&
632 iterate_section_fields( hinf
, section
, DelFiles
, delete_files_callback
, &info
) &&
633 iterate_section_fields( hinf
, section
, RenFiles
, rename_files_callback
, &info
) &&
634 SetupCommitFileQueueW( owner
, queue
, callback
, context
));
635 SetupCloseFileQueue( queue
);
636 if (!ret
) return FALSE
;
638 if (flags
& SPINST_INIFILES
)
640 if (!iterate_section_fields( hinf
, section
, UpdateInis
, update_ini_callback
, NULL
) ||
641 !iterate_section_fields( hinf
, section
, UpdateIniFields
,
642 update_ini_fields_callback
, NULL
))
645 if (flags
& SPINST_INI2REG
)
647 if (!iterate_section_fields( hinf
, section
, Ini2Reg
, ini2reg_callback
, NULL
))
651 if (flags
& SPINST_LOGCONFIG
)
653 if (!iterate_section_fields( hinf
, section
, LogConf
, logconf_callback
, NULL
))
657 if (flags
& SPINST_REGISTRY
)
659 struct registry_callback_info info
;
661 info
.default_root
= key_root
;
663 if (!iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
))
666 if (!iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
))
669 if (flags
& (SPINST_BITREG
|SPINST_REGSVR
|SPINST_UNREGSVR
|SPINST_PROFILEITEMS
|SPINST_COPYINF
))
670 FIXME( "unsupported flags %x\n", flags
);