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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "setupapi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
39 /* info passed to callback functions dealing with files */
40 struct files_callback_info
48 /* info passed to callback functions dealing with the registry */
49 struct registry_callback_info
55 /* info passed to callback functions dealing with registering dlls */
56 struct register_dll_info
58 PSP_FILE_CALLBACK_W callback;
59 PVOID callback_context;
63 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
65 /* Unicode constants */
66 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
67 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
68 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
69 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
70 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
71 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
72 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
73 static const WCHAR BitReg[] = {'B','i','t','R','e','g',0};
74 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
75 static const WCHAR CopyINF[] = {'C','o','p','y','I','N','F',0};
76 static const WCHAR AddService[] = {'A','d','d','S','e','r','v','i','c','e',0};
77 static const WCHAR DelService[] = {'D','e','l','S','e','r','v','i','c','e',0};
78 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
79 static const WCHAR RegisterDlls[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
80 static const WCHAR UnregisterDlls[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
81 static const WCHAR ProfileItems[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
82 static const WCHAR WineFakeDlls[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
83 static const WCHAR DisplayName[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
84 static const WCHAR Description[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
85 static const WCHAR ServiceBinary[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
86 static const WCHAR StartName[] = {'S','t','a','r','t','N','a','m','e',0};
87 static const WCHAR LoadOrderGroup[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
88 static const WCHAR ServiceType[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
89 static const WCHAR StartType[] = {'S','t','a','r','t','T','y','p','e',0};
90 static const WCHAR ErrorControl[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
92 static const WCHAR ServicesKey[] = {'S','y','s','t','e','m','\\',
93 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
94 'S','e','r','v','i','c','e','s',0};
96 /***********************************************************************
99 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
101 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
102 WCHAR *static_buffer, DWORD *size )
106 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
107 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
109 /* now grow the buffer */
110 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
111 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
113 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
115 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
120 /***********************************************************************
121 * dup_section_line_field
123 * Retrieve the contents of a field in a newly-allocated buffer.
125 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
131 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
132 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
133 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
134 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
138 /***********************************************************************
139 * copy_files_callback
141 * Called once for each CopyFiles entry in a given section.
143 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
145 struct files_callback_info *info = arg;
147 if (field[0] == '@') /* special case: copy single file */
148 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf, info->src_root, NULL, field+1, info->copy_flags );
150 SetupQueueCopySectionW( info->queue, info->src_root, info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
155 /***********************************************************************
156 * delete_files_callback
158 * Called once for each DelFiles entry in a given section.
160 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
162 struct files_callback_info *info = arg;
163 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
168 /***********************************************************************
169 * rename_files_callback
171 * Called once for each RenFiles entry in a given section.
173 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
175 struct files_callback_info *info = arg;
176 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
181 /***********************************************************************
184 * Retrieve the registry root key from its name.
186 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
188 static const WCHAR HKCR[] = {'H','K','C','R',0};
189 static const WCHAR HKCU[] = {'H','K','C','U',0};
190 static const WCHAR HKLM[] = {'H','K','L','M',0};
191 static const WCHAR HKU[] = {'H','K','U',0};
192 static const WCHAR HKR[] = {'H','K','R',0};
194 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
195 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
196 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
197 if (!strcmpiW( name, HKU )) return HKEY_USERS;
198 if (!strcmpiW( name, HKR )) return def_root;
203 /***********************************************************************
204 * append_multi_sz_value
206 * Append a multisz string to a multisz registry value.
208 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
211 DWORD size, type, total;
214 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
215 if (type != REG_MULTI_SZ) return;
217 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
218 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
220 /* compare each string against all the existing ones */
224 int len = strlenW(strings) + 1;
226 for (p = buffer; *p; p += strlenW(p) + 1)
227 if (!strcmpiW( p, strings )) break;
229 if (!*p) /* not found, need to append it */
231 memcpy( p, strings, len * sizeof(WCHAR) );
239 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
240 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
243 HeapFree( GetProcessHeap(), 0, buffer );
247 /***********************************************************************
248 * delete_multi_sz_value
250 * Remove a string from a multisz registry value.
252 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
255 WCHAR *buffer, *src, *dst;
257 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
258 if (type != REG_MULTI_SZ) return;
259 /* allocate double the size, one for value before and one for after */
260 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
261 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
266 int len = strlenW(src) + 1;
267 if (strcmpiW( src, string ))
269 memcpy( dst, src, len * sizeof(WCHAR) );
275 if (dst != buffer + 2*size) /* did we remove something? */
277 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
278 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
279 (BYTE *)(buffer + size), dst - (buffer + size) );
282 HeapFree( GetProcessHeap(), 0, buffer );
286 /***********************************************************************
289 * Perform an add/delete registry operation depending on the flags.
291 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
295 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
297 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
299 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
303 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
304 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
305 SetupGetStringFieldW( context, 5, str, size, NULL );
306 delete_multi_sz_value( hkey, value, str );
307 HeapFree( GetProcessHeap(), 0, str );
309 else RegDeleteValueW( hkey, value );
311 else NtDeleteKey( hkey );
315 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
317 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
319 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
320 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
321 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
324 switch(flags & FLG_ADDREG_TYPE_MASK)
326 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
327 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
328 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
329 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
330 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
331 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
332 default: type = flags >> 16; break;
335 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
336 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
338 static const WCHAR empty;
341 if (type == REG_MULTI_SZ)
343 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
346 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
347 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
349 if (flags & FLG_ADDREG_APPEND)
351 if (!str) return TRUE;
352 append_multi_sz_value( hkey, value, str, size );
353 HeapFree( GetProcessHeap(), 0, str );
356 /* else fall through to normal string handling */
360 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
363 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
364 SetupGetStringFieldW( context, 5, str, size, NULL );
368 if (type == REG_DWORD)
370 DWORD dw = str ? strtoulW( str, NULL, 0 ) : 0;
371 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
372 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
376 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
377 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
378 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)&empty, sizeof(WCHAR) );
380 HeapFree( GetProcessHeap(), 0, str );
383 else /* get the binary data */
387 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
390 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
391 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
392 SetupGetBinaryField( context, 5, data, size, NULL );
394 RegSetValueExW( hkey, value, 0, type, data, size );
395 HeapFree( GetProcessHeap(), 0, data );
401 /***********************************************************************
404 * Called once for each AddReg and DelReg entry in a given section.
406 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
408 struct registry_callback_info *info = arg;
412 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
414 for (; ok; ok = SetupFindNextLine( &context, &context ))
416 WCHAR buffer[MAX_INF_STRING_LENGTH];
420 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
422 if (!(root_key = get_root_key( buffer, info->default_root )))
426 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
430 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
434 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
438 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
439 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
442 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
444 if (RegOpenKeyW( root_key, buffer, &hkey )) continue; /* ignore if it doesn't exist */
446 else if (RegCreateKeyW( root_key, buffer, &hkey ))
448 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
451 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
454 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
458 if (!do_reg_operation( hkey, buffer, &context, flags ))
469 /***********************************************************************
472 * Register or unregister a dll.
474 static BOOL do_register_dll( const struct register_dll_info *info, const WCHAR *path,
475 INT flags, INT timeout, const WCHAR *args )
479 SP_REGISTER_CONTROL_STATUSW status;
480 IMAGE_NT_HEADERS *nt;
482 status.cbSize = sizeof(status);
483 status.FileName = path;
484 status.FailureCode = SPREG_SUCCESS;
485 status.Win32Error = ERROR_SUCCESS;
489 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
490 (UINT_PTR)&status, !info->unregister ))
493 SetLastError( ERROR_OPERATION_ABORTED );
502 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
504 WARN( "could not load %s\n", debugstr_w(path) );
505 status.FailureCode = SPREG_LOADLIBRARY;
506 status.Win32Error = GetLastError();
510 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
512 /* file is an executable, not a dll */
513 STARTUPINFOW startup;
514 PROCESS_INFORMATION info;
517 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
518 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
520 FreeLibrary( module );
522 if (!args) args = default_args;
523 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
524 sprintfW( cmd_line, format, path, args );
525 memset( &startup, 0, sizeof(startup) );
526 startup.cb = sizeof(startup);
527 TRACE( "executing %s\n", debugstr_w(cmd_line) );
528 res = CreateProcessW( NULL, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
529 HeapFree( GetProcessHeap(), 0, cmd_line );
532 status.FailureCode = SPREG_LOADLIBRARY;
533 status.Win32Error = GetLastError();
536 CloseHandle( info.hThread );
538 if (WaitForSingleObject( info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
540 /* timed out, kill the process */
541 TerminateProcess( info.hProcess, 1 );
542 status.FailureCode = SPREG_TIMEOUT;
543 status.Win32Error = ERROR_TIMEOUT;
545 CloseHandle( info.hProcess );
549 if (flags & FLG_REGSVR_DLLREGISTER)
551 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
552 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
556 status.FailureCode = SPREG_GETPROCADDR;
557 status.Win32Error = GetLastError();
561 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
566 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
567 status.FailureCode = SPREG_REGSVR;
568 status.Win32Error = res;
573 if (flags & FLG_REGSVR_DLLINSTALL)
575 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
579 status.FailureCode = SPREG_GETPROCADDR;
580 status.Win32Error = GetLastError();
584 TRACE( "calling DllInstall(%d,%s) in %s\n",
585 !info->unregister, debugstr_w(args), debugstr_w(path) );
586 res = func( !info->unregister, args );
590 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
591 status.FailureCode = SPREG_REGSVR;
592 status.Win32Error = res;
598 if (module) FreeLibrary( module );
599 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
600 (UINT_PTR)&status, !info->unregister );
605 /***********************************************************************
606 * register_dlls_callback
608 * Called once for each RegisterDlls entry in a given section.
610 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
612 struct register_dll_info *info = arg;
615 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
617 for (; ok; ok = SetupFindNextLine( &context, &context ))
619 WCHAR *path, *args, *p;
620 WCHAR buffer[MAX_INF_STRING_LENGTH];
624 if (!(path = PARSER_get_dest_dir( &context ))) continue;
627 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
629 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
630 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
633 if (p == path || p[-1] != '\\') *p++ = '\\';
634 strcpyW( p, buffer );
637 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
640 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
642 /* get command line */
644 if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
647 ret = do_register_dll( info, path, flags, timeout, args );
650 HeapFree( GetProcessHeap(), 0, path );
656 /***********************************************************************
659 * Called once for each WineFakeDlls entry in a given section.
661 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
665 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
667 for (; ok; ok = SetupFindNextLine( &context, &context ))
670 WCHAR buffer[MAX_INF_STRING_LENGTH];
673 if (!(path = PARSER_get_dest_dir( &context ))) continue;
676 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
678 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
679 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
682 if (p == path || p[-1] != '\\') *p++ = '\\';
683 strcpyW( p, buffer );
686 if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
687 p = buffer; /* otherwise use target base name as default source */
689 create_fake_dll( path, p ); /* ignore errors */
692 HeapFree( GetProcessHeap(), 0, path );
698 /***********************************************************************
699 * update_ini_callback
701 * Called once for each UpdateInis entry in a given section.
703 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
707 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
709 for (; ok; ok = SetupFindNextLine( &context, &context ))
711 WCHAR buffer[MAX_INF_STRING_LENGTH];
712 WCHAR filename[MAX_INF_STRING_LENGTH];
713 WCHAR section[MAX_INF_STRING_LENGTH];
714 WCHAR entry[MAX_INF_STRING_LENGTH];
715 WCHAR string[MAX_INF_STRING_LENGTH];
718 if (!SetupGetStringFieldW( &context, 1, filename,
719 sizeof(filename)/sizeof(WCHAR), NULL ))
722 if (!SetupGetStringFieldW( &context, 2, section,
723 sizeof(section)/sizeof(WCHAR), NULL ))
726 if (!SetupGetStringFieldW( &context, 4, buffer,
727 sizeof(buffer)/sizeof(WCHAR), NULL ))
730 divider = strchrW(buffer,'=');
734 strcpyW(entry,buffer);
736 strcpyW(string,divider);
740 strcpyW(entry,buffer);
744 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
745 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
746 WritePrivateProfileStringW(section,entry,string,filename);
752 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
754 FIXME( "should update ini fields %s\n", debugstr_w(field) );
758 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
760 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
764 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
766 FIXME( "should do logconf %s\n", debugstr_w(field) );
770 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
772 FIXME( "should do bitreg %s\n", debugstr_w(field) );
776 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
778 FIXME( "should do profile items %s\n", debugstr_w(field) );
782 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
784 FIXME( "should do copy inf %s\n", debugstr_w(field) );
789 /***********************************************************************
790 * iterate_section_fields
792 * Iterate over all fields of a certain key of a certain section
794 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
795 iterate_fields_func callback, void *arg )
797 WCHAR static_buffer[200];
798 WCHAR *buffer = static_buffer;
799 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
803 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
806 UINT i, count = SetupGetFieldCount( &context );
807 for (i = 1; i <= count; i++)
809 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
811 if (!callback( hinf, buffer, arg ))
813 WARN("callback failed for %s %s err %d\n",
814 debugstr_w(section), debugstr_w(buffer), GetLastError() );
818 ok = SetupFindNextMatchLineW( &context, key, &context );
822 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
827 /***********************************************************************
828 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
830 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
831 PCSTR section, PCSTR src_root, UINT flags )
833 UNICODE_STRING sectionW;
836 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
838 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
842 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
847 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
849 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
850 srcW.Buffer, flags );
851 RtlFreeUnicodeString( &srcW );
853 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
855 RtlFreeUnicodeString( §ionW );
860 /***********************************************************************
861 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
863 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
864 PCWSTR section, PCWSTR src_root, UINT flags )
866 struct files_callback_info info;
869 info.src_root = src_root;
870 info.copy_flags = flags;
871 info.layout = hlayout;
872 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
876 /***********************************************************************
877 * SetupInstallFromInfSectionA (SETUPAPI.@)
879 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
880 HKEY key_root, PCSTR src_root, UINT copy_flags,
881 PSP_FILE_CALLBACK_A callback, PVOID context,
882 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
884 UNICODE_STRING sectionW, src_rootW;
885 struct callback_WtoA_context ctx;
888 src_rootW.Buffer = NULL;
889 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
891 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
895 if (RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
897 ctx.orig_context = context;
898 ctx.orig_handler = callback;
899 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
900 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
901 &ctx, devinfo, devinfo_data );
902 RtlFreeUnicodeString( §ionW );
904 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
906 RtlFreeUnicodeString( &src_rootW );
911 /***********************************************************************
912 * SetupInstallFromInfSectionW (SETUPAPI.@)
914 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
915 HKEY key_root, PCWSTR src_root, UINT copy_flags,
916 PSP_FILE_CALLBACK_W callback, PVOID context,
917 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
919 if (flags & SPINST_FILES)
921 struct files_callback_info info;
925 if (!(queue = SetupOpenFileQueue())) return FALSE;
927 info.src_root = src_root;
928 info.copy_flags = copy_flags;
930 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
931 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
932 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
933 SetupCommitFileQueueW( owner, queue, callback, context ));
934 SetupCloseFileQueue( queue );
935 if (!ret) return FALSE;
937 if (flags & SPINST_INIFILES)
939 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
940 !iterate_section_fields( hinf, section, UpdateIniFields,
941 update_ini_fields_callback, NULL ))
944 if (flags & SPINST_INI2REG)
946 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
949 if (flags & SPINST_LOGCONFIG)
951 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
954 if (flags & SPINST_REGSVR)
956 struct register_dll_info info;
958 info.unregister = FALSE;
959 if (flags & SPINST_REGISTERCALLBACKAWARE)
961 info.callback = callback;
962 info.callback_context = context;
964 else info.callback = NULL;
966 if (!iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info ))
969 if (!iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
972 if (flags & SPINST_UNREGSVR)
974 struct register_dll_info info;
976 info.unregister = TRUE;
977 if (flags & SPINST_REGISTERCALLBACKAWARE)
979 info.callback = callback;
980 info.callback_context = context;
982 else info.callback = NULL;
984 if (!iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info ))
987 if (flags & SPINST_REGISTRY)
989 struct registry_callback_info info;
991 info.default_root = key_root;
993 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
996 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
999 if (flags & SPINST_BITREG)
1001 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1004 if (flags & SPINST_PROFILEITEMS)
1006 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1009 if (flags & SPINST_COPYINF)
1011 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1019 /***********************************************************************
1020 * InstallHinfSectionW (SETUPAPI.@)
1022 * NOTE: 'cmdline' is <section> <mode> <path> from
1023 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1025 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1028 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
1029 #elif defined(__x86_64)
1030 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
1031 #else /* FIXME: other platforms */
1032 static const WCHAR nt_platformW[] = {'.','n','t',0};
1034 static const WCHAR nt_genericW[] = {'.','n','t',0};
1035 static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1037 WCHAR *s, *path, section[MAX_PATH + (sizeof(nt_platformW) + sizeof(servicesW)) / sizeof(WCHAR)];
1038 void *callback_context;
1042 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1044 lstrcpynW( section, cmdline, MAX_PATH );
1046 if (!(s = strchrW( section, ' ' ))) return;
1048 while (*s == ' ') s++;
1051 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1052 if (!(s = strchrW( s, ' ' ))) return;
1053 while (*s == ' ') s++;
1056 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1057 if (hinf == INVALID_HANDLE_VALUE) return;
1059 if (!(GetVersion() & 0x80000000))
1063 /* check for <section>.ntx86 (or corresponding name for the current platform)
1064 * and then <section>.nt */
1065 s = section + strlenW(section);
1066 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1067 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1069 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1070 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1072 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1075 callback_context = SetupInitDefaultQueueCallback( hwnd );
1076 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1077 SetupDefaultQueueCallbackW, callback_context,
1079 SetupTermDefaultQueueCallback( callback_context );
1080 strcatW( section, servicesW );
1081 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1082 SetupCloseInfFile( hinf );
1084 /* FIXME: should check the mode and maybe reboot */
1085 /* there isn't much point in doing that since we */
1086 /* don't yet handle deferred file copies anyway. */
1090 /***********************************************************************
1091 * InstallHinfSectionA (SETUPAPI.@)
1093 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1095 UNICODE_STRING cmdlineW;
1097 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1099 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1100 RtlFreeUnicodeString( &cmdlineW );
1105 /***********************************************************************
1108 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1110 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1112 struct registry_callback_info info;
1115 SERVICE_DESCRIPTIONW descr;
1116 WCHAR *display_name, *start_name, *load_order, *binary_path;
1117 INT service_type = 0, start_type = 0, error_control = 0;
1121 /* first the mandatory fields */
1123 if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1124 !SetupGetIntField( &context, 1, &service_type ))
1126 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1129 if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1130 !SetupGetIntField( &context, 1, &start_type ))
1132 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1135 if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1136 !SetupGetIntField( &context, 1, &error_control ))
1138 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1141 if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1143 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1147 /* now the optional fields */
1149 display_name = dup_section_line_field( hinf, section, DisplayName, 1 );
1150 start_name = dup_section_line_field( hinf, section, StartName, 1 );
1151 load_order = dup_section_line_field( hinf, section, LoadOrderGroup, 1 );
1152 descr.lpDescription = dup_section_line_field( hinf, section, Description, 1 );
1154 /* FIXME: Dependencies field */
1155 /* FIXME: Security field */
1157 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1158 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1159 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1161 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1162 service_type, start_type, error_control, binary_path,
1163 load_order, NULL, NULL, start_name, NULL );
1166 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1170 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1171 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1172 if (!service) goto done;
1174 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1175 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1177 QUERY_SERVICE_CONFIGW *config = NULL;
1179 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1180 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1181 config = HeapAlloc( GetProcessHeap(), 0, size );
1182 if (config && QueryServiceConfigW( service, config, size, &size ))
1184 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1185 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1186 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1188 HeapFree( GetProcessHeap(), 0, display_name );
1189 display_name = strdupW( config->lpDisplayName );
1191 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1193 HeapFree( GetProcessHeap(), 0, load_order );
1194 load_order = strdupW( config->lpLoadOrderGroup );
1197 HeapFree( GetProcessHeap(), 0, config );
1199 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1200 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1201 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1203 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1204 load_order, NULL, NULL, start_name, NULL, display_name );
1206 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1207 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1210 /* execute the AddReg, DelReg and BitReg entries */
1212 info.default_root = 0;
1213 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1215 RegOpenKeyW( hkey, name, &info.default_root );
1216 RegCloseKey( hkey );
1218 if (info.default_root)
1221 iterate_section_fields( hinf, section, DelReg, registry_callback, &info );
1222 info.delete = FALSE;
1223 iterate_section_fields( hinf, section, AddReg, registry_callback, &info );
1224 RegCloseKey( info.default_root );
1226 iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1228 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1229 CloseServiceHandle( service );
1232 if (!service) WARN( "failed err %u\n", GetLastError() );
1233 HeapFree( GetProcessHeap(), 0, binary_path );
1234 HeapFree( GetProcessHeap(), 0, display_name );
1235 HeapFree( GetProcessHeap(), 0, start_name );
1236 HeapFree( GetProcessHeap(), 0, load_order );
1237 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1238 return service != 0;
1242 /***********************************************************************
1245 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1247 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1251 SERVICE_STATUS status;
1253 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1255 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1256 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1259 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1260 TRACE( "deleting %s\n", debugstr_w(name) );
1261 ret = DeleteService( service );
1262 CloseServiceHandle( service );
1267 /***********************************************************************
1268 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1270 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1272 WCHAR service_name[MAX_INF_STRING_LENGTH];
1273 WCHAR service_section[MAX_INF_STRING_LENGTH];
1277 BOOL ok, ret = FALSE;
1279 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1281 if (!(ok = SetupFindFirstLineW( hinf, section, AddService, &context )))
1282 SetLastError( ERROR_SECTION_NOT_FOUND );
1285 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1287 if (!SetupGetIntField( &context, 2, §ion_flags )) section_flags = 0;
1288 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1290 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1292 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1295 if (!(ok = SetupFindFirstLineW( hinf, section, DelService, &context )))
1296 SetLastError( ERROR_SECTION_NOT_FOUND );
1299 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1301 if (!SetupGetIntField( &context, 2, §ion_flags )) section_flags = 0;
1302 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1303 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1305 if (ret) SetLastError( ERROR_SUCCESS );
1307 CloseServiceHandle( scm );
1312 /***********************************************************************
1313 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1315 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1317 UNICODE_STRING SectionNameW;
1320 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1322 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1323 RtlFreeUnicodeString( &SectionNameW );
1326 SetLastError( ERROR_NOT_ENOUGH_MEMORY );