2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
5 * 2005 Hervé Poussineau (hpoussin@reactos.org)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "setupapi_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
26 /* info passed to callback functions dealing with files */
27 struct files_callback_info
35 /* info passed to callback functions dealing with the registry */
36 struct registry_callback_info
42 /* info passed to callback functions dealing with registering dlls */
43 struct register_dll_info
45 PSP_FILE_CALLBACK_W callback
;
46 PVOID callback_context
;
50 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
52 /* Unicode constants */
53 static const WCHAR CopyFiles
[] = {'C','o','p','y','F','i','l','e','s',0};
54 static const WCHAR DelFiles
[] = {'D','e','l','F','i','l','e','s',0};
55 static const WCHAR RenFiles
[] = {'R','e','n','F','i','l','e','s',0};
56 static const WCHAR Ini2Reg
[] = {'I','n','i','2','R','e','g',0};
57 static const WCHAR LogConf
[] = {'L','o','g','C','o','n','f',0};
58 static const WCHAR AddReg
[] = {'A','d','d','R','e','g',0};
59 static const WCHAR DelReg
[] = {'D','e','l','R','e','g',0};
60 static const WCHAR BitReg
[] = {'B','i','t','R','e','g',0};
61 static const WCHAR UpdateInis
[] = {'U','p','d','a','t','e','I','n','i','s',0};
62 static const WCHAR CopyINF
[] = {'C','o','p','y','I','N','F',0};
63 static const WCHAR UpdateIniFields
[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
64 static const WCHAR RegisterDlls
[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
65 static const WCHAR UnregisterDlls
[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
66 static const WCHAR ProfileItems
[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
69 /***********************************************************************
72 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
74 static WCHAR
*get_field_string( INFCONTEXT
*context
, DWORD index
, WCHAR
*buffer
,
75 WCHAR
*static_buffer
, DWORD
*size
)
79 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
80 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
82 /* now grow the buffer */
83 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
84 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
*sizeof(WCHAR
) ))) return NULL
;
86 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
88 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
93 /***********************************************************************
96 * Called once for each CopyFiles entry in a given section.
98 static BOOL
copy_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
100 struct files_callback_info
*info
= arg
;
102 if (field
[0] == '@') /* special case: copy single file */
103 SetupQueueDefaultCopyW( info
->queue
, info
->layout
, info
->src_root
, NULL
, field
, info
->copy_flags
);
105 SetupQueueCopySectionW( info
->queue
, info
->src_root
, info
->layout
, hinf
, field
, info
->copy_flags
);
110 /***********************************************************************
111 * delete_files_callback
113 * Called once for each DelFiles entry in a given section.
115 static BOOL
delete_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
117 struct files_callback_info
*info
= arg
;
118 SetupQueueDeleteSectionW( info
->queue
, hinf
, 0, field
);
123 /***********************************************************************
124 * rename_files_callback
126 * Called once for each RenFiles entry in a given section.
128 static BOOL
rename_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
130 struct files_callback_info
*info
= arg
;
131 SetupQueueRenameSectionW( info
->queue
, hinf
, 0, field
);
136 /***********************************************************************
139 * Retrieve the registry root key from its name.
141 static HKEY
get_root_key( const WCHAR
*name
, HKEY def_root
)
143 static const WCHAR HKCR
[] = {'H','K','C','R',0};
144 static const WCHAR HKCU
[] = {'H','K','C','U',0};
145 static const WCHAR HKLM
[] = {'H','K','L','M',0};
146 static const WCHAR HKU
[] = {'H','K','U',0};
147 static const WCHAR HKR
[] = {'H','K','R',0};
149 if (!strcmpiW( name
, HKCR
)) return HKEY_CLASSES_ROOT
;
150 if (!strcmpiW( name
, HKCU
)) return HKEY_CURRENT_USER
;
151 if (!strcmpiW( name
, HKLM
)) return HKEY_LOCAL_MACHINE
;
152 if (!strcmpiW( name
, HKU
)) return HKEY_USERS
;
153 if (!strcmpiW( name
, HKR
)) return def_root
;
158 /***********************************************************************
159 * append_multi_sz_value
161 * Append a multisz string to a multisz registry value.
163 static void append_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*strings
,
166 DWORD size
, type
, total
;
169 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
170 if (type
!= REG_MULTI_SZ
) return;
172 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
173 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
175 /* compare each string against all the existing ones */
179 int len
= strlenW(strings
) + 1;
181 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
182 if (!strcmpiW( p
, strings
)) break;
184 if (!*p
) /* not found, need to append it */
186 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
194 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
195 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
);
198 HeapFree( GetProcessHeap(), 0, buffer
);
202 /***********************************************************************
203 * delete_multi_sz_value
205 * Remove a string from a multisz registry value.
207 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
210 WCHAR
*buffer
, *src
, *dst
;
212 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
213 if (type
!= REG_MULTI_SZ
) return;
214 /* allocate double the size, one for value before and one for after */
215 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
216 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
221 int len
= strlenW(src
) + 1;
222 if (strcmpiW( src
, string
))
224 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
230 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
232 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
233 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
234 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
237 HeapFree( GetProcessHeap(), 0, buffer
);
241 /***********************************************************************
244 * Perform an add/delete registry operation depending on the flags.
246 static BOOL
do_reg_operation( HKEY hkey
, const WCHAR
*value
, INFCONTEXT
*context
, INT flags
)
250 if (flags
& (FLG_ADDREG_DELREG_BIT
| FLG_ADDREG_DELVAL
)) /* deletion */
252 if (*value
&& !(flags
& FLG_DELREG_KEYONLY_COMMON
))
254 if ((flags
& FLG_DELREG_MULTI_SZ_DELSTRING
) == FLG_DELREG_MULTI_SZ_DELSTRING
)
258 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
) || !size
) return TRUE
;
259 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
260 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
261 delete_multi_sz_value( hkey
, value
, str
);
262 HeapFree( GetProcessHeap(), 0, str
);
264 else RegDeleteValueW( hkey
, value
);
266 else RegDeleteKeyW( hkey
, NULL
);
270 if (flags
& (FLG_ADDREG_KEYONLY
|FLG_ADDREG_KEYONLY_COMMON
)) return TRUE
;
272 if (flags
& (FLG_ADDREG_NOCLOBBER
|FLG_ADDREG_OVERWRITEONLY
))
274 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
275 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
)) return TRUE
;
276 if (!exists
& (flags
& FLG_ADDREG_OVERWRITEONLY
)) return TRUE
;
279 switch(flags
& FLG_ADDREG_TYPE_MASK
)
281 case FLG_ADDREG_TYPE_SZ
: type
= REG_SZ
; break;
282 case FLG_ADDREG_TYPE_MULTI_SZ
: type
= REG_MULTI_SZ
; break;
283 case FLG_ADDREG_TYPE_EXPAND_SZ
: type
= REG_EXPAND_SZ
; break;
284 case FLG_ADDREG_TYPE_BINARY
: type
= REG_BINARY
; break;
285 case FLG_ADDREG_TYPE_DWORD
: type
= REG_DWORD
; break;
286 case FLG_ADDREG_TYPE_NONE
: type
= REG_NONE
; break;
287 default: type
= flags
>> 16; break;
290 if (!(flags
& FLG_ADDREG_BINVALUETYPE
) ||
291 (type
== REG_DWORD
&& SetupGetFieldCount(context
) == 5))
293 static const WCHAR empty
;
296 if (type
== REG_MULTI_SZ
)
298 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
301 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
302 SetupGetMultiSzFieldW( context
, 5, str
, size
, NULL
);
304 if (flags
& FLG_ADDREG_APPEND
)
306 if (!str
) return TRUE
;
307 append_multi_sz_value( hkey
, value
, str
, size
);
308 HeapFree( GetProcessHeap(), 0, str
);
311 /* else fall through to normal string handling */
315 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
318 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
319 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
323 if (type
== REG_DWORD
)
325 DWORD dw
= str
? strtoulW( str
, NULL
, 0 ) : 0;
326 TRACE( "setting dword %s to %lx\n", debugstr_w(value
), dw
);
327 RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&dw
, sizeof(dw
) );
331 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(str
) );
332 if (str
) RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)str
, size
* sizeof(WCHAR
) );
333 else RegSetValueExW( hkey
, value
, 0, type
, (const BYTE
*)&empty
, sizeof(WCHAR
) );
335 HeapFree( GetProcessHeap(), 0, str
);
338 else /* get the binary data */
342 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
345 if (!(data
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
346 TRACE( "setting binary data %s len %ld\n", debugstr_w(value
), size
);
347 SetupGetBinaryField( context
, 5, data
, size
, NULL
);
349 RegSetValueExW( hkey
, value
, 0, type
, data
, size
);
350 HeapFree( GetProcessHeap(), 0, data
);
356 /***********************************************************************
359 * Called once for each AddReg and DelReg entry in a given section.
361 static BOOL
registry_callback( HINF hinf
, PCWSTR field
, void *arg
)
363 struct registry_callback_info
*info
= arg
;
367 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
369 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
371 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
375 if (!SetupGetStringFieldW( &context
, 1, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
377 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
381 if (!SetupGetStringFieldW( &context
, 2, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
385 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
389 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
393 if (!flags
) flags
= FLG_ADDREG_DELREG_BIT
;
394 else if (!(flags
& FLG_ADDREG_DELREG_BIT
)) continue; /* ignore this entry */
397 if (info
->delete || (flags
& FLG_ADDREG_OVERWRITEONLY
))
399 if (RegOpenKeyW( root_key
, buffer
, &hkey
)) continue; /* ignore if it doesn't exist */
401 else if (RegCreateKeyW( root_key
, buffer
, &hkey
))
403 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
406 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
409 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
413 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
424 /***********************************************************************
427 * Register or unregister a dll.
429 static BOOL
do_register_dll( const struct register_dll_info
*info
, const WCHAR
*path
,
430 INT flags
, INT timeout
, const WCHAR
*args
)
434 SP_REGISTER_CONTROL_STATUSW status
;
436 status
.cbSize
= sizeof(status
);
437 status
.FileName
= path
;
438 status
.FailureCode
= SPREG_SUCCESS
;
439 status
.Win32Error
= ERROR_SUCCESS
;
443 switch(info
->callback( info
->callback_context
, SPFILENOTIFY_STARTREGISTRATION
,
444 (UINT_PTR
)&status
, !info
->unregister
))
447 SetLastError( ERROR_OPERATION_ABORTED
);
456 if (!(module
= LoadLibraryExW( path
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)))
458 WARN( "could not load %s\n", debugstr_w(path
) );
459 status
.FailureCode
= SPREG_LOADLIBRARY
;
460 status
.Win32Error
= GetLastError();
464 if (flags
& FLG_REGSVR_DLLREGISTER
)
466 const char *entry_point
= info
->unregister
? "DllUnregisterServer" : "DllRegisterServer";
467 HRESULT (WINAPI
*func
)(void) = (void *)GetProcAddress( module
, entry_point
);
471 status
.FailureCode
= SPREG_GETPROCADDR
;
472 status
.Win32Error
= GetLastError();
476 TRACE( "calling %s in %s\n", entry_point
, debugstr_w(path
) );
481 WARN( "calling %s in %s returned error %lx\n", entry_point
, debugstr_w(path
), res
);
482 status
.FailureCode
= SPREG_REGSVR
;
483 status
.Win32Error
= res
;
488 if (flags
& FLG_REGSVR_DLLINSTALL
)
490 HRESULT (WINAPI
*func
)(BOOL
,LPCWSTR
) = (void *)GetProcAddress( module
, "DllInstall" );
494 status
.FailureCode
= SPREG_GETPROCADDR
;
495 status
.Win32Error
= GetLastError();
499 TRACE( "calling DllInstall(%d,%s) in %s\n",
500 !info
->unregister
, debugstr_w(args
), debugstr_w(path
) );
501 res
= func( !info
->unregister
, args
);
505 WARN( "calling DllInstall in %s returned error %lx\n", debugstr_w(path
), res
);
506 status
.FailureCode
= SPREG_REGSVR
;
507 status
.Win32Error
= res
;
513 if (module
) FreeLibrary( module
);
514 if (info
->callback
) info
->callback( info
->callback_context
, SPFILENOTIFY_ENDREGISTRATION
,
515 (UINT_PTR
)&status
, !info
->unregister
);
520 /***********************************************************************
521 * register_dlls_callback
523 * Called once for each RegisterDlls entry in a given section.
525 static BOOL
register_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
527 struct register_dll_info
*info
= arg
;
530 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
532 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
534 WCHAR
*path
, *args
, *p
;
535 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
539 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
542 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
544 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
545 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
548 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
549 strcpyW( p
, buffer
);
552 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
555 if (!SetupGetIntField( &context
, 5, &timeout
)) timeout
= 60;
557 /* get command line */
559 if (SetupGetStringFieldW( &context
, 6, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
562 ret
= do_register_dll( info
, path
, flags
, timeout
, args
);
565 HeapFree( GetProcessHeap(), 0, path
);
571 /***********************************************************************
572 * update_ini_callback
574 * Called once for each UpdateInis entry in a given section.
576 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
580 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
582 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
584 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
585 WCHAR filename
[MAX_INF_STRING_LENGTH
];
586 WCHAR section
[MAX_INF_STRING_LENGTH
];
587 WCHAR entry
[MAX_INF_STRING_LENGTH
];
588 WCHAR string
[MAX_INF_STRING_LENGTH
];
591 if (!SetupGetStringFieldW( &context
, 1, filename
,
592 sizeof(filename
)/sizeof(WCHAR
), NULL
))
595 if (!SetupGetStringFieldW( &context
, 2, section
,
596 sizeof(section
)/sizeof(WCHAR
), NULL
))
599 if (!SetupGetStringFieldW( &context
, 4, buffer
,
600 sizeof(buffer
)/sizeof(WCHAR
), NULL
))
603 divider
= strchrW(buffer
,'=');
607 strcpyW(entry
,buffer
);
609 strcpyW(string
,divider
);
613 strcpyW(entry
,buffer
);
617 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
618 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
619 WritePrivateProfileStringW(section
,entry
,string
,filename
);
625 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
627 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
631 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
633 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
637 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
639 FIXME( "should do logconf %s\n", debugstr_w(field
) );
643 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
645 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
649 static BOOL
profile_items_callback( HINF hinf
, PCWSTR field
, void *arg
)
651 FIXME( "should do profile items %s\n", debugstr_w(field
) );
655 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
657 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
662 /***********************************************************************
663 * iterate_section_fields
665 * Iterate over all fields of a certain key of a certain section
667 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
668 iterate_fields_func callback
, void *arg
)
670 WCHAR static_buffer
[200];
671 WCHAR
*buffer
= static_buffer
;
672 DWORD size
= sizeof(static_buffer
)/sizeof(WCHAR
);
676 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
679 UINT i
, count
= SetupGetFieldCount( &context
);
680 for (i
= 1; i
<= count
; i
++)
682 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
684 if (!callback( hinf
, buffer
, arg
))
686 WARN("callback failed for %s %s err %ld\n",
687 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
691 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
695 if (buffer
&& buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
700 /***********************************************************************
701 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
703 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
704 PCSTR section
, PCSTR src_root
, UINT flags
)
706 UNICODE_STRING sectionW
;
709 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
711 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
715 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
720 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
722 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
723 srcW
.Buffer
, flags
);
724 RtlFreeUnicodeString( &srcW
);
726 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
728 RtlFreeUnicodeString( §ionW
);
733 /***********************************************************************
734 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
736 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
737 PCWSTR section
, PCWSTR src_root
, UINT flags
)
739 struct files_callback_info info
;
742 info
.src_root
= src_root
;
743 info
.copy_flags
= flags
;
744 info
.layout
= hlayout
;
745 return iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
);
749 /***********************************************************************
750 * SetupInstallFromInfSectionA (SETUPAPI.@)
752 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
753 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
754 PSP_FILE_CALLBACK_A callback
, PVOID context
,
755 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
757 UNICODE_STRING sectionW
, src_rootW
;
758 struct callback_WtoA_context ctx
;
761 src_rootW
.Buffer
= NULL
;
762 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
764 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
768 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
770 ctx
.orig_context
= context
;
771 ctx
.orig_handler
= callback
;
772 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
773 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
774 &ctx
, devinfo
, devinfo_data
);
775 RtlFreeUnicodeString( §ionW
);
777 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
779 RtlFreeUnicodeString( &src_rootW
);
784 /***********************************************************************
785 * SetupInstallFromInfSectionW (SETUPAPI.@)
787 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
788 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
789 PSP_FILE_CALLBACK_W callback
, PVOID context
,
790 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
792 if (flags
& SPINST_FILES
)
794 struct files_callback_info info
;
798 if (!(queue
= SetupOpenFileQueue())) return FALSE
;
800 info
.src_root
= src_root
;
801 info
.copy_flags
= copy_flags
;
803 ret
= (iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
) &&
804 iterate_section_fields( hinf
, section
, DelFiles
, delete_files_callback
, &info
) &&
805 iterate_section_fields( hinf
, section
, RenFiles
, rename_files_callback
, &info
) &&
806 SetupCommitFileQueueW( owner
, queue
, callback
, context
));
807 SetupCloseFileQueue( queue
);
808 if (!ret
) return FALSE
;
810 if (flags
& SPINST_INIFILES
)
812 if (!iterate_section_fields( hinf
, section
, UpdateInis
, update_ini_callback
, NULL
) ||
813 !iterate_section_fields( hinf
, section
, UpdateIniFields
,
814 update_ini_fields_callback
, NULL
))
817 if (flags
& SPINST_INI2REG
)
819 if (!iterate_section_fields( hinf
, section
, Ini2Reg
, ini2reg_callback
, NULL
))
822 if (flags
& SPINST_LOGCONFIG
)
824 if (!iterate_section_fields( hinf
, section
, LogConf
, logconf_callback
, NULL
))
827 if (flags
& SPINST_REGSVR
)
829 struct register_dll_info info
;
831 info
.unregister
= FALSE
;
832 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
834 info
.callback
= callback
;
835 info
.callback_context
= context
;
837 else info
.callback
= NULL
;
839 if (!iterate_section_fields( hinf
, section
, RegisterDlls
, register_dlls_callback
, &info
))
842 if (flags
& SPINST_UNREGSVR
)
844 struct register_dll_info info
;
846 info
.unregister
= TRUE
;
847 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
849 info
.callback
= callback
;
850 info
.callback_context
= context
;
852 else info
.callback
= NULL
;
854 if (!iterate_section_fields( hinf
, section
, UnregisterDlls
, register_dlls_callback
, &info
))
857 if (flags
& SPINST_REGISTRY
)
859 struct registry_callback_info info
;
861 info
.default_root
= key_root
;
863 if (!iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
))
866 if (!iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
))
869 if (flags
& SPINST_BITREG
)
871 if (!iterate_section_fields( hinf
, section
, BitReg
, bitreg_callback
, NULL
))
874 if (flags
& SPINST_PROFILEITEMS
)
876 if (!iterate_section_fields( hinf
, section
, ProfileItems
, profile_items_callback
, NULL
))
879 if (flags
& SPINST_COPYINF
)
881 if (!iterate_section_fields( hinf
, section
, CopyINF
, copy_inf_callback
, NULL
))
889 /***********************************************************************
890 * InstallHinfSectionW (SETUPAPI.@)
892 * NOTE: 'cmdline' is <section> <mode> <path> from
893 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
895 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
897 WCHAR
*p
, *path
, section
[MAX_PATH
];
898 void *callback_context
;
902 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
904 lstrcpynW( section
, cmdline
, sizeof(section
)/sizeof(WCHAR
) );
906 if (!(p
= strchrW( section
, ' ' ))) return;
908 while (*p
== ' ') p
++;
911 if (!(p
= strchrW( p
, ' ' ))) return;
913 while (*path
== ' ') path
++;
915 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
916 if (hinf
== INVALID_HANDLE_VALUE
) return;
918 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
919 SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
, SP_COPY_NEWER
,
920 SetupDefaultQueueCallbackW
, callback_context
,
922 SetupTermDefaultQueueCallback( callback_context
);
923 SetupCloseInfFile( hinf
);
925 /* FIXME: should check the mode and maybe reboot */
926 /* there isn't much point in doing that since we */
927 /* don't yet handle deferred file copies anyway. */
931 /***********************************************************************
932 * InstallHinfSectionA (SETUPAPI.@)
934 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
936 UNICODE_STRING cmdlineW
;
938 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
940 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
941 RtlFreeUnicodeString( &cmdlineW
);
946 /***********************************************************************
947 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
949 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF hinf
, PCSTR sectionname
, DWORD flags
)
951 return SetupInstallServicesFromInfSectionExA( hinf
, sectionname
, flags
,
952 NULL
, NULL
, NULL
, NULL
);
956 /***********************************************************************
957 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
959 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF hinf
, PCWSTR sectionname
, DWORD flags
)
961 return SetupInstallServicesFromInfSectionExW( hinf
, sectionname
, flags
,
962 NULL
, NULL
, NULL
, NULL
);
966 /***********************************************************************
967 * SetupInstallServicesFromInfSectionExA (SETUPAPI.@)
969 BOOL WINAPI
SetupInstallServicesFromInfSectionExA( HINF hinf
, PCSTR sectionname
, DWORD flags
, HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
, PVOID reserved1
, PVOID reserved2
)
971 UNICODE_STRING sectionnameW
;
974 if (RtlCreateUnicodeStringFromAsciiz( §ionnameW
, sectionname
))
976 ret
= SetupInstallServicesFromInfSectionExW( hinf
, sectionnameW
.Buffer
, flags
, devinfo
, devinfo_data
, reserved1
, reserved2
);
977 RtlFreeUnicodeString( §ionnameW
);
980 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
986 static BOOL
GetLineText( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, PWSTR
*value
)
993 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, NULL
, 0, &required
)
994 && GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
997 buf
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) );
1000 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1004 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, buf
, required
, &required
) )
1006 HeapFree( GetProcessHeap(), 0, buf
);
1015 static BOOL
GetIntField( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, INT
*value
)
1020 if (! GetLineText( hinf
, section_name
, key_name
, &buffer
) )
1023 res
= wcstol( buffer
, &end
, 0 );
1024 if (end
!= buffer
&& !*end
)
1026 HeapFree(GetProcessHeap(), 0, buffer
);
1032 HeapFree(GetProcessHeap(), 0, buffer
);
1033 SetLastError( ERROR_INVALID_DATA
);
1039 /***********************************************************************
1040 * SetupInstallServicesFromInfSectionExW (SETUPAPI.@)
1042 BOOL WINAPI
SetupInstallServicesFromInfSectionExW( HINF hinf
, PCWSTR sectionname
, DWORD flags
, HDEVINFO DeviceInfoSet
, PSP_DEVINFO_DATA DeviceInfoData
, PVOID reserved1
, PVOID reserved2
)
1044 SC_HANDLE hSCManager
= NULL
;
1045 SC_HANDLE hService
= NULL
;
1046 LPDWORD GroupOrder
= NULL
;
1047 LPQUERY_SERVICE_CONFIG ServiceConfig
= NULL
;
1048 struct DeviceInfoSet
*list
;
1051 TRACE("%p, %s, 0x%lx, %p, %p, %p, %p\n", hinf
, debugstr_w(sectionname
),
1052 flags
, DeviceInfoSet
, DeviceInfoData
, reserved1
, reserved2
);
1055 SetLastError(ERROR_INVALID_PARAMETER
);
1056 else if (DeviceInfoSet
== (HDEVINFO
)INVALID_HANDLE_VALUE
)
1057 SetLastError(ERROR_INVALID_HANDLE
);
1058 else if ((list
= (struct DeviceInfoSet
*)DeviceInfoSet
)->magic
!= SETUP_DEV_INFO_SET_MAGIC
)
1059 SetLastError(ERROR_INVALID_HANDLE
);
1060 else if (DeviceInfoData
&& DeviceInfoData
->cbSize
!= sizeof(SP_DEVINFO_DATA
))
1061 SetLastError(ERROR_INVALID_USER_BUFFER
);
1062 else if (!reserved1
)
1064 /* FIXME: I don't know how to get the service name. ATM, just fail the call */
1065 /* Maybe find it in DeviceInfoSet/DeviceInfoData? */
1066 FIXME("Service name not specified!\n");
1067 SetLastError(ERROR_GEN_FAILURE
);
1072 HKEY hGroupOrderListKey
= INVALID_HANDLE_VALUE
;
1073 LPWSTR ServiceBinary
= NULL
;
1074 LPWSTR LoadOrderGroup
= NULL
;
1075 LPWSTR DisplayName
= NULL
;
1076 LPWSTR Description
= NULL
;
1077 LPWSTR Dependencies
= NULL
;
1078 INT ServiceType
, StartType
, ErrorControl
;
1080 DWORD tagId
= (DWORD
)-1;
1083 if (!GetIntField(hinf
, sectionname
, L
"ServiceType", &ServiceType
))
1085 if (!GetIntField(hinf
, sectionname
, L
"StartType", &StartType
))
1087 if (!GetIntField(hinf
, sectionname
, L
"ErrorControl", &ErrorControl
))
1089 useTag
= (ServiceType
== SERVICE_BOOT_START
|| ServiceType
== SERVICE_SYSTEM_START
);
1091 hSCManager
= OpenSCManagerW(NULL
, SERVICES_ACTIVE_DATABASE
, SC_MANAGER_CREATE_SERVICE
);
1092 if (hSCManager
== NULL
)
1095 if (!GetLineText(hinf
, sectionname
, L
"ServiceBinary", &ServiceBinary
))
1098 /* Don't check return value, as these fields are optional and
1099 * GetLineText initialize output parameter even on failure */
1100 GetLineText(hinf
, sectionname
, L
"LoadOrderGroup", &LoadOrderGroup
);
1101 GetLineText(hinf
, sectionname
, L
"DisplayName", &DisplayName
);
1102 GetLineText(hinf
, sectionname
, L
"Description", &Description
);
1103 GetLineText(hinf
, sectionname
, L
"Dependencies", &Dependencies
);
1105 hService
= OpenServiceW(
1108 GENERIC_READ
| GENERIC_WRITE
);
1109 if (hService
== NULL
&& GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST
)
1112 if (flags
& (SPSVCINST_STOPSERVICE
| SPSVCINST_DELETEEVENTLOGENTRY
))
1114 if (hService
== NULL
)
1116 SetLastError(ERROR_SERVICE_DOES_NOT_EXIST
);
1119 if (flags
& SPSVCINST_STOPSERVICE
)
1121 SERVICE_STATUS ServiceStatus
;
1122 ret
= ControlService(hService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
1123 if (!ret
&& GetLastError() != ERROR_SERVICE_NOT_ACTIVE
)
1125 if (ServiceStatus
.dwCurrentState
!= SERVICE_STOP_PENDING
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
)
1127 SetLastError(ERROR_INSTALL_SERVICE_FAILURE
);
1131 if (flags
& SPSVCINST_DELETEEVENTLOGENTRY
)
1133 ret
= DeleteService(hService
);
1140 if (hService
== NULL
)
1142 /* Create new service */
1143 hService
= CreateServiceW(
1153 useTag
? &tagId
: NULL
,
1156 if (hService
== NULL
)
1162 /* Read current configuration */
1163 if (!QueryServiceConfigW(hService
, NULL
, 0, &bufferSize
))
1165 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1167 ServiceConfig
= HeapAlloc(GetProcessHeap(), 0, bufferSize
);
1170 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1173 if (!QueryServiceConfigW(hService
, ServiceConfig
, bufferSize
, &bufferSize
))
1176 tagId
= ServiceConfig
->dwTagId
;
1178 /* Update configuration */
1179 ret
= ChangeServiceConfigW(
1182 (flags
& SPSVCINST_NOCLOBBER_STARTTYPE
) ? SERVICE_NO_CHANGE
: StartType
,
1183 (flags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) ? SERVICE_NO_CHANGE
: ErrorControl
,
1185 (flags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
&& ServiceConfig
->lpLoadOrderGroup
) ? NULL
: LoadOrderGroup
,
1186 useTag
? &tagId
: NULL
,
1187 (flags
& SPSVCINST_NOCLOBBER_DEPENDENCIES
&& ServiceConfig
->lpDependencies
) ? NULL
: Dependencies
,
1189 (flags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
&& ServiceConfig
->lpDisplayName
) ? NULL
: DisplayName
);
1194 /* FIXME: use Description and SPSVCINST_NOCLOBBER_DESCRIPTION */
1198 /* Add the tag to SYSTEM\CurrentControlSet\Control\GroupOrderList key */
1200 LPCWSTR lpLoadOrderGroup
;
1203 lpLoadOrderGroup
= LoadOrderGroup
;
1204 if ((flags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
) && ServiceConfig
&& ServiceConfig
->lpLoadOrderGroup
)
1205 lpLoadOrderGroup
= ServiceConfig
->lpLoadOrderGroup
;
1207 rc
= RegOpenKey(list
->HKLM
, L
"SYSTEM\\CurrentControlSet\\Control\\GroupOrderList", &hGroupOrderListKey
);
1208 if (rc
!= ERROR_SUCCESS
)
1213 rc
= RegQueryValueExW(hGroupOrderListKey
, lpLoadOrderGroup
, NULL
, &dwRegType
, NULL
, &bufferSize
);
1214 if (rc
== ERROR_FILE_NOT_FOUND
)
1215 bufferSize
= sizeof(DWORD
);
1216 else if (rc
!= ERROR_SUCCESS
)
1221 else if (dwRegType
!= REG_BINARY
|| bufferSize
== 0 || bufferSize
% sizeof(DWORD
) != 0)
1223 SetLastError(ERROR_GEN_FAILURE
);
1226 /* Allocate buffer to store existing data + the new tag */
1227 GroupOrder
= HeapAlloc(GetProcessHeap(), 0, bufferSize
+ sizeof(DWORD
));
1230 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1233 if (rc
== ERROR_SUCCESS
)
1235 /* Read existing data */
1236 rc
= RegQueryValueExW(
1243 if (rc
!= ERROR_SUCCESS
)
1248 if (flags
& SPSVCINST_TAGTOFRONT
)
1249 memmove(&GroupOrder
[2], &GroupOrder
[1], bufferSize
- sizeof(DWORD
));
1256 if (flags
& SPSVCINST_TAGTOFRONT
)
1257 GroupOrder
[1] = tagId
;
1259 GroupOrder
[bufferSize
/ sizeof(DWORD
)] = tagId
;
1261 rc
= RegSetValueExW(
1267 bufferSize
+ sizeof(DWORD
));
1268 if (rc
!= ERROR_SUCCESS
)
1278 if (hSCManager
!= NULL
)
1279 CloseServiceHandle(hSCManager
);
1280 if (hService
!= NULL
)
1281 CloseServiceHandle(hService
);
1282 if (hGroupOrderListKey
!= INVALID_HANDLE_VALUE
)
1283 RegCloseKey(hGroupOrderListKey
);
1284 HeapFree(GetProcessHeap(), 0, ServiceConfig
);
1285 HeapFree(GetProcessHeap(), 0, ServiceBinary
);
1286 HeapFree(GetProcessHeap(), 0, LoadOrderGroup
);
1287 HeapFree(GetProcessHeap(), 0, DisplayName
);
1288 HeapFree(GetProcessHeap(), 0, Description
);
1289 HeapFree(GetProcessHeap(), 0, Dependencies
);
1290 HeapFree(GetProcessHeap(), 0, GroupOrder
);
1293 TRACE("Returning %d\n", ret
);