shell32: Fix the Ukrainian translation.
[wine] / dlls / setupapi / install.c
1 /*
2  * Setupapi install routines
3  *
4  * Copyright 2002 Alexandre Julliard for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "winternl.h"
27 #include "winerror.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "winsvc.h"
32 #include "setupapi.h"
33 #include "setupapi_private.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
38
39 /* info passed to callback functions dealing with files */
40 struct files_callback_info
41 {
42     HSPFILEQ queue;
43     PCWSTR   src_root;
44     UINT     copy_flags;
45     HINF     layout;
46 };
47
48 /* info passed to callback functions dealing with the registry */
49 struct registry_callback_info
50 {
51     HKEY default_root;
52     BOOL delete;
53 };
54
55 /* info passed to callback functions dealing with registering dlls */
56 struct register_dll_info
57 {
58     PSP_FILE_CALLBACK_W callback;
59     PVOID               callback_context;
60     BOOL                unregister;
61 };
62
63 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
64
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};
91
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};
95
96 /***********************************************************************
97  *            get_field_string
98  *
99  * Retrieve the contents of a field, dynamically growing the buffer if necessary.
100  */
101 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
102                                 WCHAR *static_buffer, DWORD *size )
103 {
104     DWORD required;
105
106     if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
107     if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
108     {
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;
112         *size = required;
113         if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
114     }
115     if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
116     return NULL;
117 }
118
119
120 /***********************************************************************
121  *            dup_section_line_field
122  *
123  * Retrieve the contents of a field in a newly-allocated buffer.
124  */
125 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
126 {
127     INFCONTEXT context;
128     DWORD size;
129     WCHAR *buffer;
130
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;
135     return buffer;
136 }
137
138 /***********************************************************************
139  *            copy_files_callback
140  *
141  * Called once for each CopyFiles entry in a given section.
142  */
143 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
144 {
145     struct files_callback_info *info = arg;
146
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 );
149     else
150         SetupQueueCopySectionW( info->queue, info->src_root, info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
151     return TRUE;
152 }
153
154
155 /***********************************************************************
156  *            delete_files_callback
157  *
158  * Called once for each DelFiles entry in a given section.
159  */
160 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
161 {
162     struct files_callback_info *info = arg;
163     SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
164     return TRUE;
165 }
166
167
168 /***********************************************************************
169  *            rename_files_callback
170  *
171  * Called once for each RenFiles entry in a given section.
172  */
173 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
174 {
175     struct files_callback_info *info = arg;
176     SetupQueueRenameSectionW( info->queue, hinf, 0, field );
177     return TRUE;
178 }
179
180
181 /***********************************************************************
182  *            get_root_key
183  *
184  * Retrieve the registry root key from its name.
185  */
186 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
187 {
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};
193
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;
199     return 0;
200 }
201
202
203 /***********************************************************************
204  *            append_multi_sz_value
205  *
206  * Append a multisz string to a multisz registry value.
207  */
208 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
209                                    DWORD str_size )
210 {
211     DWORD size, type, total;
212     WCHAR *buffer, *p;
213
214     if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
215     if (type != REG_MULTI_SZ) return;
216
217     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
218     if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
219
220     /* compare each string against all the existing ones */
221     total = size;
222     while (*strings)
223     {
224         int len = strlenW(strings) + 1;
225
226         for (p = buffer; *p; p += strlenW(p) + 1)
227             if (!strcmpiW( p, strings )) break;
228
229         if (!*p)  /* not found, need to append it */
230         {
231             memcpy( p, strings, len * sizeof(WCHAR) );
232             p[len] = 0;
233             total += len;
234         }
235         strings += len;
236     }
237     if (total != size)
238     {
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 );
241     }
242  done:
243     HeapFree( GetProcessHeap(), 0, buffer );
244 }
245
246
247 /***********************************************************************
248  *            delete_multi_sz_value
249  *
250  * Remove a string from a multisz registry value.
251  */
252 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
253 {
254     DWORD size, type;
255     WCHAR *buffer, *src, *dst;
256
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;
262     src = buffer;
263     dst = buffer + size;
264     while (*src)
265     {
266         int len = strlenW(src) + 1;
267         if (strcmpiW( src, string ))
268         {
269             memcpy( dst, src, len * sizeof(WCHAR) );
270             dst += len;
271         }
272         src += len;
273     }
274     *dst++ = 0;
275     if (dst != buffer + 2*size)  /* did we remove something? */
276     {
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) );
280     }
281  done:
282     HeapFree( GetProcessHeap(), 0, buffer );
283 }
284
285
286 /***********************************************************************
287  *            do_reg_operation
288  *
289  * Perform an add/delete registry operation depending on the flags.
290  */
291 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
292 {
293     DWORD type, size;
294
295     if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL))  /* deletion */
296     {
297         if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
298         {
299             if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
300             {
301                 WCHAR *str;
302
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 );
308             }
309             else RegDeleteValueW( hkey, value );
310         }
311         else NtDeleteKey( hkey );
312         return TRUE;
313     }
314
315     if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
316
317     if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
318     {
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;
322     }
323
324     switch(flags & FLG_ADDREG_TYPE_MASK)
325     {
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;
333     }
334
335     if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
336         (type == REG_DWORD && SetupGetFieldCount(context) == 5))
337     {
338         static const WCHAR empty;
339         WCHAR *str = NULL;
340
341         if (type == REG_MULTI_SZ)
342         {
343             if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
344             if (size)
345             {
346                 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
347                 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
348             }
349             if (flags & FLG_ADDREG_APPEND)
350             {
351                 if (!str) return TRUE;
352                 append_multi_sz_value( hkey, value, str, size );
353                 HeapFree( GetProcessHeap(), 0, str );
354                 return TRUE;
355             }
356             /* else fall through to normal string handling */
357         }
358         else
359         {
360             if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
361             if (size)
362             {
363                 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
364                 SetupGetStringFieldW( context, 5, str, size, NULL );
365             }
366         }
367
368         if (type == REG_DWORD)
369         {
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) );
373         }
374         else
375         {
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) );
379         }
380         HeapFree( GetProcessHeap(), 0, str );
381         return TRUE;
382     }
383     else  /* get the binary data */
384     {
385         BYTE *data = NULL;
386
387         if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
388         if (size)
389         {
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 );
393         }
394         RegSetValueExW( hkey, value, 0, type, data, size );
395         HeapFree( GetProcessHeap(), 0, data );
396         return TRUE;
397     }
398 }
399
400
401 /***********************************************************************
402  *            registry_callback
403  *
404  * Called once for each AddReg and DelReg entry in a given section.
405  */
406 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
407 {
408     struct registry_callback_info *info = arg;
409     INFCONTEXT context;
410     HKEY root_key, hkey;
411
412     BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
413
414     for (; ok; ok = SetupFindNextLine( &context, &context ))
415     {
416         WCHAR buffer[MAX_INF_STRING_LENGTH];
417         INT flags;
418
419         /* get root */
420         if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
421             continue;
422         if (!(root_key = get_root_key( buffer, info->default_root )))
423             continue;
424
425         /* get key */
426         if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
427             *buffer = 0;
428
429         /* get flags */
430         if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
431
432         if (!info->delete)
433         {
434             if (flags & FLG_ADDREG_DELREG_BIT) continue;  /* ignore this entry */
435         }
436         else
437         {
438             if (!flags) flags = FLG_ADDREG_DELREG_BIT;
439             else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue;  /* ignore this entry */
440         }
441
442         if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
443         {
444             if (RegOpenKeyW( root_key, buffer, &hkey )) continue;  /* ignore if it doesn't exist */
445         }
446         else if (RegCreateKeyW( root_key, buffer, &hkey ))
447         {
448             ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
449             continue;
450         }
451         TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
452
453         /* get value name */
454         if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
455             *buffer = 0;
456
457         /* and now do it */
458         if (!do_reg_operation( hkey, buffer, &context, flags ))
459         {
460             RegCloseKey( hkey );
461             return FALSE;
462         }
463         RegCloseKey( hkey );
464     }
465     return TRUE;
466 }
467
468
469 /***********************************************************************
470  *            do_register_dll
471  *
472  * Register or unregister a dll.
473  */
474 static BOOL do_register_dll( const struct register_dll_info *info, const WCHAR *path,
475                              INT flags, INT timeout, const WCHAR *args )
476 {
477     HMODULE module;
478     HRESULT res;
479     SP_REGISTER_CONTROL_STATUSW status;
480     IMAGE_NT_HEADERS *nt;
481
482     status.cbSize = sizeof(status);
483     status.FileName = path;
484     status.FailureCode = SPREG_SUCCESS;
485     status.Win32Error = ERROR_SUCCESS;
486
487     if (info->callback)
488     {
489         switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
490                                (UINT_PTR)&status, !info->unregister ))
491         {
492         case FILEOP_ABORT:
493             SetLastError( ERROR_OPERATION_ABORTED );
494             return FALSE;
495         case FILEOP_SKIP:
496             return TRUE;
497         case FILEOP_DOIT:
498             break;
499         }
500     }
501
502     if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
503     {
504         WARN( "could not load %s\n", debugstr_w(path) );
505         status.FailureCode = SPREG_LOADLIBRARY;
506         status.Win32Error = GetLastError();
507         goto done;
508     }
509
510     if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
511     {
512         /* file is an executable, not a dll */
513         STARTUPINFOW startup;
514         PROCESS_INFORMATION info;
515         WCHAR *cmd_line;
516         BOOL res;
517         static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
518         static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
519
520         FreeLibrary( module );
521         module = NULL;
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 );
530         if (!res)
531         {
532             status.FailureCode = SPREG_LOADLIBRARY;
533             status.Win32Error = GetLastError();
534             goto done;
535         }
536         CloseHandle( info.hThread );
537
538         if (WaitForSingleObject( info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
539         {
540             /* timed out, kill the process */
541             TerminateProcess( info.hProcess, 1 );
542             status.FailureCode = SPREG_TIMEOUT;
543             status.Win32Error = ERROR_TIMEOUT;
544         }
545         CloseHandle( info.hProcess );
546         goto done;
547     }
548
549     if (flags & FLG_REGSVR_DLLREGISTER)
550     {
551         const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
552         HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
553
554         if (!func)
555         {
556             status.FailureCode = SPREG_GETPROCADDR;
557             status.Win32Error = GetLastError();
558             goto done;
559         }
560
561         TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
562         res = func();
563
564         if (FAILED(res))
565         {
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;
569             goto done;
570         }
571     }
572
573     if (flags & FLG_REGSVR_DLLINSTALL)
574     {
575         HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
576
577         if (!func)
578         {
579             status.FailureCode = SPREG_GETPROCADDR;
580             status.Win32Error = GetLastError();
581             goto done;
582         }
583
584         TRACE( "calling DllInstall(%d,%s) in %s\n",
585                !info->unregister, debugstr_w(args), debugstr_w(path) );
586         res = func( !info->unregister, args );
587
588         if (FAILED(res))
589         {
590             WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
591             status.FailureCode = SPREG_REGSVR;
592             status.Win32Error = res;
593             goto done;
594         }
595     }
596
597 done:
598     if (module) FreeLibrary( module );
599     if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
600                                         (UINT_PTR)&status, !info->unregister );
601     return TRUE;
602 }
603
604
605 /***********************************************************************
606  *            register_dlls_callback
607  *
608  * Called once for each RegisterDlls entry in a given section.
609  */
610 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
611 {
612     struct register_dll_info *info = arg;
613     INFCONTEXT context;
614     BOOL ret = TRUE;
615     BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
616
617     for (; ok; ok = SetupFindNextLine( &context, &context ))
618     {
619         WCHAR *path, *args, *p;
620         WCHAR buffer[MAX_INF_STRING_LENGTH];
621         INT flags, timeout;
622
623         /* get directory */
624         if (!(path = PARSER_get_dest_dir( &context ))) continue;
625
626         /* get dll name */
627         if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
628             goto done;
629         if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
630                                (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
631         path = p;
632         p += strlenW(p);
633         if (p == path || p[-1] != '\\') *p++ = '\\';
634         strcpyW( p, buffer );
635
636         /* get flags */
637         if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
638
639         /* get timeout */
640         if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
641
642         /* get command line */
643         args = NULL;
644         if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
645             args = buffer;
646
647         ret = do_register_dll( info, path, flags, timeout, args );
648
649     done:
650         HeapFree( GetProcessHeap(), 0, path );
651         if (!ret) break;
652     }
653     return ret;
654 }
655
656 /***********************************************************************
657  *            fake_dlls_callback
658  *
659  * Called once for each WineFakeDlls entry in a given section.
660  */
661 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
662 {
663     INFCONTEXT context;
664     BOOL ret = TRUE;
665     BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
666
667     for (; ok; ok = SetupFindNextLine( &context, &context ))
668     {
669         WCHAR *path, *p;
670         WCHAR buffer[MAX_INF_STRING_LENGTH];
671
672         /* get directory */
673         if (!(path = PARSER_get_dest_dir( &context ))) continue;
674
675         /* get dll name */
676         if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
677             goto done;
678         if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
679                                (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
680         path = p;
681         p += strlenW(p);
682         if (p == path || p[-1] != '\\') *p++ = '\\';
683         strcpyW( p, buffer );
684
685         /* get source dll */
686         if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
687             p = buffer;  /* otherwise use target base name as default source */
688
689         create_fake_dll( path, p );  /* ignore errors */
690
691     done:
692         HeapFree( GetProcessHeap(), 0, path );
693         if (!ret) break;
694     }
695     return ret;
696 }
697
698 /***********************************************************************
699  *            update_ini_callback
700  *
701  * Called once for each UpdateInis entry in a given section.
702  */
703 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
704 {
705     INFCONTEXT context;
706
707     BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
708
709     for (; ok; ok = SetupFindNextLine( &context, &context ))
710     {
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];
716         LPWSTR divider;
717
718         if (!SetupGetStringFieldW( &context, 1, filename,
719                                    sizeof(filename)/sizeof(WCHAR), NULL ))
720             continue;
721
722         if (!SetupGetStringFieldW( &context, 2, section,
723                                    sizeof(section)/sizeof(WCHAR), NULL ))
724             continue;
725
726         if (!SetupGetStringFieldW( &context, 4, buffer,
727                                    sizeof(buffer)/sizeof(WCHAR), NULL ))
728             continue;
729
730         divider = strchrW(buffer,'=');
731         if (divider)
732         {
733             *divider = 0;
734             strcpyW(entry,buffer);
735             divider++;
736             strcpyW(string,divider);
737         }
738         else
739         {
740             strcpyW(entry,buffer);
741             string[0]=0;
742         }
743
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);
747
748     }
749     return TRUE;
750 }
751
752 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
753 {
754     FIXME( "should update ini fields %s\n", debugstr_w(field) );
755     return TRUE;
756 }
757
758 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
759 {
760     FIXME( "should do ini2reg %s\n", debugstr_w(field) );
761     return TRUE;
762 }
763
764 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
765 {
766     FIXME( "should do logconf %s\n", debugstr_w(field) );
767     return TRUE;
768 }
769
770 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
771 {
772     FIXME( "should do bitreg %s\n", debugstr_w(field) );
773     return TRUE;
774 }
775
776 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
777 {
778     FIXME( "should do profile items %s\n", debugstr_w(field) );
779     return TRUE;
780 }
781
782 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
783 {
784     FIXME( "should do copy inf %s\n", debugstr_w(field) );
785     return TRUE;
786 }
787
788
789 /***********************************************************************
790  *            iterate_section_fields
791  *
792  * Iterate over all fields of a certain key of a certain section
793  */
794 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
795                                     iterate_fields_func callback, void *arg )
796 {
797     WCHAR static_buffer[200];
798     WCHAR *buffer = static_buffer;
799     DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
800     INFCONTEXT context;
801     BOOL ret = FALSE;
802
803     BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
804     while (ok)
805     {
806         UINT i, count = SetupGetFieldCount( &context );
807         for (i = 1; i <= count; i++)
808         {
809             if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
810                 goto done;
811             if (!callback( hinf, buffer, arg ))
812             {
813                 WARN("callback failed for %s %s err %d\n",
814                      debugstr_w(section), debugstr_w(buffer), GetLastError() );
815                 goto done;
816             }
817         }
818         ok = SetupFindNextMatchLineW( &context, key, &context );
819     }
820     ret = TRUE;
821  done:
822     if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
823     return ret;
824 }
825
826
827 /***********************************************************************
828  *            SetupInstallFilesFromInfSectionA   (SETUPAPI.@)
829  */
830 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
831                                               PCSTR section, PCSTR src_root, UINT flags )
832 {
833     UNICODE_STRING sectionW;
834     BOOL ret = FALSE;
835
836     if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
837     {
838         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
839         return FALSE;
840     }
841     if (!src_root)
842         ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
843                                                 NULL, flags );
844     else
845     {
846         UNICODE_STRING srcW;
847         if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
848         {
849             ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
850                                                     srcW.Buffer, flags );
851             RtlFreeUnicodeString( &srcW );
852         }
853         else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
854     }
855     RtlFreeUnicodeString( &sectionW );
856     return ret;
857 }
858
859
860 /***********************************************************************
861  *            SetupInstallFilesFromInfSectionW   (SETUPAPI.@)
862  */
863 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
864                                               PCWSTR section, PCWSTR src_root, UINT flags )
865 {
866     struct files_callback_info info;
867
868     info.queue      = queue;
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 );
873 }
874
875
876 /***********************************************************************
877  *            SetupInstallFromInfSectionA   (SETUPAPI.@)
878  */
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 )
883 {
884     UNICODE_STRING sectionW, src_rootW;
885     struct callback_WtoA_context ctx;
886     BOOL ret = FALSE;
887
888     src_rootW.Buffer = NULL;
889     if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
890     {
891         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
892         return FALSE;
893     }
894
895     if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
896     {
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( &sectionW );
903     }
904     else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
905
906     RtlFreeUnicodeString( &src_rootW );
907     return ret;
908 }
909
910
911 /***********************************************************************
912  *            SetupInstallFromInfSectionW   (SETUPAPI.@)
913  */
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 )
918 {
919     if (flags & SPINST_FILES)
920     {
921         struct files_callback_info info;
922         HSPFILEQ queue;
923         BOOL ret;
924
925         if (!(queue = SetupOpenFileQueue())) return FALSE;
926         info.queue      = queue;
927         info.src_root   = src_root;
928         info.copy_flags = copy_flags;
929         info.layout     = hinf;
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;
936     }
937     if (flags & SPINST_INIFILES)
938     {
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 ))
942             return FALSE;
943     }
944     if (flags & SPINST_INI2REG)
945     {
946         if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
947             return FALSE;
948     }
949     if (flags & SPINST_LOGCONFIG)
950     {
951         if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
952             return FALSE;
953     }
954     if (flags & SPINST_REGSVR)
955     {
956         struct register_dll_info info;
957
958         info.unregister = FALSE;
959         if (flags & SPINST_REGISTERCALLBACKAWARE)
960         {
961             info.callback         = callback;
962             info.callback_context = context;
963         }
964         else info.callback = NULL;
965
966         if (!iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info ))
967             return FALSE;
968
969         if (!iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
970             return FALSE;
971     }
972     if (flags & SPINST_UNREGSVR)
973     {
974         struct register_dll_info info;
975
976         info.unregister = TRUE;
977         if (flags & SPINST_REGISTERCALLBACKAWARE)
978         {
979             info.callback         = callback;
980             info.callback_context = context;
981         }
982         else info.callback = NULL;
983
984         if (!iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info ))
985             return FALSE;
986     }
987     if (flags & SPINST_REGISTRY)
988     {
989         struct registry_callback_info info;
990
991         info.default_root = key_root;
992         info.delete = TRUE;
993         if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
994             return FALSE;
995         info.delete = FALSE;
996         if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
997             return FALSE;
998     }
999     if (flags & SPINST_BITREG)
1000     {
1001         if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1002             return FALSE;
1003     }
1004     if (flags & SPINST_PROFILEITEMS)
1005     {
1006         if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1007             return FALSE;
1008     }
1009     if (flags & SPINST_COPYINF)
1010     {
1011         if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1012             return FALSE;
1013     }
1014
1015     return TRUE;
1016 }
1017
1018
1019 /***********************************************************************
1020  *              InstallHinfSectionW  (SETUPAPI.@)
1021  *
1022  * NOTE: 'cmdline' is <section> <mode> <path> from
1023  *   RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1024  */
1025 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1026 {
1027 #ifdef __i386__
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};
1033 #endif
1034     static const WCHAR nt_genericW[] = {'.','n','t',0};
1035     static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1036
1037     WCHAR *s, *path, section[MAX_PATH + (sizeof(nt_platformW) + sizeof(servicesW)) / sizeof(WCHAR)];
1038     void *callback_context;
1039     UINT mode;
1040     HINF hinf;
1041
1042     TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1043
1044     lstrcpynW( section, cmdline, MAX_PATH );
1045
1046     if (!(s = strchrW( section, ' ' ))) return;
1047     *s++ = 0;
1048     while (*s == ' ') s++;
1049     mode = atoiW( s );
1050
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++;
1054     path = s;
1055
1056     hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1057     if (hinf == INVALID_HANDLE_VALUE) return;
1058
1059     if (!(GetVersion() & 0x80000000))
1060     {
1061         INFCONTEXT context;
1062
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 )))
1068         {
1069             memcpy( s, nt_genericW, sizeof(nt_genericW) );
1070             if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1071         }
1072         if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1073     }
1074
1075     callback_context = SetupInitDefaultQueueCallback( hwnd );
1076     SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1077                                  SetupDefaultQueueCallbackW, callback_context,
1078                                  NULL, NULL );
1079     SetupTermDefaultQueueCallback( callback_context );
1080     strcatW( section, servicesW );
1081     SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1082     SetupCloseInfFile( hinf );
1083
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. */
1087 }
1088
1089
1090 /***********************************************************************
1091  *              InstallHinfSectionA  (SETUPAPI.@)
1092  */
1093 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1094 {
1095     UNICODE_STRING cmdlineW;
1096
1097     if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1098     {
1099         InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1100         RtlFreeUnicodeString( &cmdlineW );
1101     }
1102 }
1103
1104
1105 /***********************************************************************
1106  *            add_service
1107  *
1108  * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1109  */
1110 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1111 {
1112     struct registry_callback_info info;
1113     SC_HANDLE service;
1114     INFCONTEXT context;
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;
1118     DWORD size;
1119     HKEY hkey;
1120
1121     /* first the mandatory fields */
1122
1123     if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1124         !SetupGetIntField( &context, 1, &service_type ))
1125     {
1126         SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1127         return FALSE;
1128     }
1129     if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1130         !SetupGetIntField( &context, 1, &start_type ))
1131     {
1132         SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1133         return FALSE;
1134     }
1135     if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1136         !SetupGetIntField( &context, 1, &error_control ))
1137     {
1138         SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1139         return FALSE;
1140     }
1141     if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1142     {
1143         SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1144         return FALSE;
1145     }
1146
1147     /* now the optional fields */
1148
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 );
1153
1154     /* FIXME: Dependencies field */
1155     /* FIXME: Security field */
1156
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 );
1160
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 );
1164     if (service)
1165     {
1166         if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1167     }
1168     else
1169     {
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;
1173
1174         if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1175                      SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1176         {
1177             QUERY_SERVICE_CONFIGW *config = NULL;
1178
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 ))
1183             {
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)
1187                 {
1188                     HeapFree( GetProcessHeap(), 0, display_name );
1189                     display_name = strdupW( config->lpDisplayName );
1190                 }
1191                 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1192                 {
1193                     HeapFree( GetProcessHeap(), 0, load_order );
1194                     load_order = strdupW( config->lpLoadOrderGroup );
1195                 }
1196             }
1197             HeapFree( GetProcessHeap(), 0, config );
1198         }
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) );
1202
1203         ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1204                               load_order, NULL, NULL, start_name, NULL, display_name );
1205
1206         if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1207             ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1208     }
1209
1210     /* execute the AddReg, DelReg and BitReg entries */
1211
1212     info.default_root = 0;
1213     if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1214     {
1215         RegOpenKeyW( hkey, name, &info.default_root );
1216         RegCloseKey( hkey );
1217     }
1218     if (info.default_root)
1219     {
1220         info.delete = TRUE;
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 );
1225     }
1226     iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1227
1228     if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1229     CloseServiceHandle( service );
1230
1231 done:
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;
1239 }
1240
1241
1242 /***********************************************************************
1243  *            del_service
1244  *
1245  * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1246  */
1247 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1248 {
1249     BOOL ret;
1250     SC_HANDLE service;
1251     SERVICE_STATUS status;
1252
1253     if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1254     {
1255         if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1256         WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1257         return FALSE;
1258     }
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 );
1263     return ret;
1264 }
1265
1266
1267 /***********************************************************************
1268  *              SetupInstallServicesFromInfSectionW  (SETUPAPI.@)
1269  */
1270 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1271 {
1272     WCHAR service_name[MAX_INF_STRING_LENGTH];
1273     WCHAR service_section[MAX_INF_STRING_LENGTH];
1274     SC_HANDLE scm;
1275     INFCONTEXT context;
1276     INT section_flags;
1277     BOOL ok, ret = FALSE;
1278
1279     if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1280
1281     if (!(ok = SetupFindFirstLineW( hinf, section, AddService, &context )))
1282         SetLastError( ERROR_SECTION_NOT_FOUND );
1283     while (ok)
1284     {
1285         if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1286             continue;
1287         if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1288         if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1289             continue;
1290         if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1291             goto done;
1292         ok = SetupFindNextMatchLineW( &context, AddService, &context );
1293     }
1294
1295     if (!(ok = SetupFindFirstLineW( hinf, section, DelService, &context )))
1296         SetLastError( ERROR_SECTION_NOT_FOUND );
1297     while (ok)
1298     {
1299         if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1300             continue;
1301         if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1302         if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1303         ok = SetupFindNextMatchLineW( &context, AddService, &context );
1304     }
1305     if (ret) SetLastError( ERROR_SUCCESS );
1306  done:
1307     CloseServiceHandle( scm );
1308     return ret;
1309 }
1310
1311
1312 /***********************************************************************
1313  *              SetupInstallServicesFromInfSectionA  (SETUPAPI.@)
1314  */
1315 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1316 {
1317     UNICODE_STRING SectionNameW;
1318     BOOL ret = FALSE;
1319
1320     if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1321     {
1322         ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1323         RtlFreeUnicodeString( &SectionNameW );
1324     }
1325     else
1326         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1327
1328     return ret;
1329 }