msvcrt: Use the remquo()/remquof() implementation from the bundled musl library.
[wine.git] / dlls / setupapi / install.c
blob8a28bfcc4e1ab2c706a8b457983a90d20f71f920
1 /*
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
21 #include <stdarg.h>
22 #include <stdbool.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "winerror.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winsvc.h"
35 #include "shlobj.h"
36 #include "shlwapi.h"
37 #include "objidl.h"
38 #include "objbase.h"
39 #include "setupapi.h"
40 #include "setupapi_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
45 /* info passed to callback functions dealing with files */
46 struct files_callback_info
48 HSPFILEQ queue;
49 PCWSTR src_root;
50 UINT copy_flags;
51 HINF layout;
54 /* info passed to callback functions dealing with the registry */
55 struct registry_callback_info
57 HKEY default_root;
58 BOOL delete;
61 /* info passed to callback functions dealing with registering dlls */
62 struct register_dll_info
64 PSP_FILE_CALLBACK_W callback;
65 PVOID callback_context;
66 BOOL unregister;
67 int modules_size;
68 int modules_count;
69 HMODULE *modules;
72 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
75 /***********************************************************************
76 * get_field_string
78 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
80 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
81 WCHAR *static_buffer, DWORD *size )
83 DWORD required;
85 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
86 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
88 /* now grow the buffer */
89 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
90 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
91 *size = required;
92 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
94 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
95 return NULL;
99 /***********************************************************************
100 * dup_section_line_field
102 * Retrieve the contents of a field in a newly-allocated buffer.
104 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
106 INFCONTEXT context;
107 DWORD size;
108 WCHAR *buffer;
110 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
111 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
112 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
113 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
114 return buffer;
117 static void get_inf_src_path( HINF hinf, WCHAR *path )
119 const WCHAR *inf_path = PARSER_get_inf_filename( hinf );
120 WCHAR pnf_path[MAX_PATH];
121 FILE *pnf;
123 wcscpy( pnf_path, inf_path );
124 PathRemoveExtensionW( pnf_path );
125 PathAddExtensionW( pnf_path, L".pnf" );
126 if ((pnf = _wfopen( pnf_path, L"r" )))
128 if (fgetws( path, MAX_PATH, pnf ) && !wcscmp( path, PNF_HEADER ))
130 fgetws( path, MAX_PATH, pnf );
131 TRACE("using original source path %s\n", debugstr_w(path));
132 fclose( pnf );
133 return;
135 fclose( pnf );
137 wcscpy( path, inf_path );
140 /***********************************************************************
141 * copy_files_callback
143 * Called once for each CopyFiles entry in a given section.
145 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
147 INFCONTEXT context;
148 struct files_callback_info *info = arg;
149 WCHAR src_root[MAX_PATH], *p;
151 if (!info->src_root)
153 const WCHAR *build_dir = _wgetenv( L"WINEBUILDDIR" );
154 const WCHAR *data_dir = _wgetenv( L"WINEDATADIR" );
156 if ((build_dir || data_dir) && SetupFindFirstLineW( hinf, L"WineSourceDirs", field, &context ))
158 lstrcpyW( src_root, build_dir ? build_dir : data_dir );
159 src_root[1] = '\\'; /* change \??\ to \\?\ */
160 p = src_root + lstrlenW(src_root);
161 *p++ = '\\';
162 if (!build_dir || !SetupGetStringFieldW( &context, 2, p, MAX_PATH - (p - src_root), NULL ))
164 if (!SetupGetStringFieldW( &context, 1, p, MAX_PATH - (p - src_root), NULL )) p[-1] = 0;
167 else
169 get_inf_src_path( hinf, src_root );
170 if ((p = wcsrchr( src_root, '\\' ))) *p = 0;
174 if (field[0] == '@') /* special case: copy single file */
175 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf,
176 info->src_root ? info->src_root : src_root, field+1, field+1, info->copy_flags );
177 else
178 SetupQueueCopySectionW( info->queue, info->src_root ? info->src_root : src_root,
179 info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
180 return TRUE;
184 /***********************************************************************
185 * delete_files_callback
187 * Called once for each DelFiles entry in a given section.
189 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
191 struct files_callback_info *info = arg;
192 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
193 return TRUE;
197 /***********************************************************************
198 * rename_files_callback
200 * Called once for each RenFiles entry in a given section.
202 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
204 struct files_callback_info *info = arg;
205 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
206 return TRUE;
210 /***********************************************************************
211 * get_root_key
213 * Retrieve the registry root key from its name.
215 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
217 if (!wcsicmp( name, L"HKCR" )) return HKEY_CLASSES_ROOT;
218 if (!wcsicmp( name, L"HKCU" )) return HKEY_CURRENT_USER;
219 if (!wcsicmp( name, L"HKLM" )) return HKEY_LOCAL_MACHINE;
220 if (!wcsicmp( name, L"HKU" )) return HKEY_USERS;
221 if (!wcsicmp( name, L"HKR" )) return def_root;
222 return 0;
226 /***********************************************************************
227 * append_multi_sz_value
229 * Append a multisz string to a multisz registry value.
231 static bool append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
232 DWORD str_size )
234 DWORD size, type, total;
235 WCHAR *buffer, *p;
236 LONG ret;
238 if ((ret = RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )))
240 if (ret != ERROR_FILE_NOT_FOUND)
242 ERR( "failed to query value %s, error %lu\n", debugstr_w(value), ret );
243 SetLastError( ret );
244 return false;
247 if ((ret = RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)strings, str_size * sizeof(WCHAR) )))
249 ERR( "failed to set value %s, error %lu\n", debugstr_w(value), ret );
250 SetLastError( ret );
251 return false;
254 return true;
257 if (type != REG_MULTI_SZ)
259 WARN( "value %s exists but has wrong type %#lx\n", debugstr_w(value), type );
260 SetLastError( ERROR_INVALID_DATA );
261 return false;
264 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return false;
265 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size ))
267 HeapFree( GetProcessHeap(), 0, buffer );
268 return false;
271 /* compare each string against all the existing ones */
272 total = size;
273 while (*strings)
275 int len = lstrlenW(strings) + 1;
277 for (p = buffer; *p; p += lstrlenW(p) + 1)
278 if (!wcsicmp( p, strings )) break;
280 if (!*p) /* not found, need to append it */
282 memcpy( p, strings, len * sizeof(WCHAR) );
283 p[len] = 0;
284 total += len * sizeof(WCHAR);
286 strings += len;
288 if (total != size)
290 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
291 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
294 HeapFree( GetProcessHeap(), 0, buffer );
295 return true;
299 /***********************************************************************
300 * delete_multi_sz_value
302 * Remove a string from a multisz registry value.
304 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
306 DWORD size, type;
307 WCHAR *buffer, *src, *dst;
309 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
310 if (type != REG_MULTI_SZ) return;
311 /* allocate double the size, one for value before and one for after */
312 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
313 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
314 src = buffer;
315 dst = buffer + size;
316 while (*src)
318 int len = lstrlenW(src) + 1;
319 if (wcsicmp( src, string ))
321 memcpy( dst, src, len * sizeof(WCHAR) );
322 dst += len;
324 src += len;
326 *dst++ = 0;
327 if (dst != buffer + 2*size) /* did we remove something? */
329 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
330 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
331 (BYTE *)(buffer + size), dst - (buffer + size) );
333 done:
334 HeapFree( GetProcessHeap(), 0, buffer );
338 /***********************************************************************
339 * do_reg_operation
341 * Perform an add/delete registry operation depending on the flags.
343 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
345 DWORD type, size;
347 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
349 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
351 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
353 WCHAR *str;
355 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
356 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
357 SetupGetStringFieldW( context, 5, str, size, NULL );
358 delete_multi_sz_value( hkey, value, str );
359 HeapFree( GetProcessHeap(), 0, str );
361 else RegDeleteValueW( hkey, value );
363 else
365 RegDeleteTreeW( hkey, NULL );
366 NtDeleteKey( hkey );
368 return TRUE;
371 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
373 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
375 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
376 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
377 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
380 switch(flags & FLG_ADDREG_TYPE_MASK)
382 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
383 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
384 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
385 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
386 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
387 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
388 default: type = flags >> 16; break;
391 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
392 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
394 WCHAR *str = NULL;
396 if (type == REG_MULTI_SZ)
398 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
399 if (size)
401 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
402 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
404 if (flags & FLG_ADDREG_APPEND)
406 if (!str) return TRUE;
407 if (!append_multi_sz_value( hkey, value, str, size ))
409 HeapFree( GetProcessHeap(), 0, str );
410 return FALSE;
412 HeapFree( GetProcessHeap(), 0, str );
413 return TRUE;
415 /* else fall through to normal string handling */
417 else
419 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
420 if (size)
422 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
423 SetupGetStringFieldW( context, 5, str, size, NULL );
424 if (type == REG_LINK) size--; /* no terminating null for symlinks */
428 if (type == REG_DWORD)
430 DWORD dw = str ? wcstoul( str, NULL, 0 ) : 0;
431 TRACE( "setting dword %s to %lx\n", debugstr_w(value), dw );
432 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
434 else
436 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
437 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
438 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)L"", sizeof(WCHAR) );
440 HeapFree( GetProcessHeap(), 0, str );
441 return TRUE;
443 else /* get the binary data */
445 BYTE *data = NULL;
447 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
448 if (size)
450 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
451 TRACE( "setting binary data %s len %ld\n", debugstr_w(value), size );
452 SetupGetBinaryField( context, 5, data, size, NULL );
454 RegSetValueExW( hkey, value, 0, type, data, size );
455 HeapFree( GetProcessHeap(), 0, data );
456 return TRUE;
461 /***********************************************************************
462 * registry_callback
464 * Called once for each AddReg and DelReg entry in a given section.
466 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
468 struct registry_callback_info *info = arg;
469 INFCONTEXT context;
470 HKEY root_key, hkey;
472 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
474 for (; ok; ok = SetupFindNextLine( &context, &context ))
476 DWORD options = 0;
477 WCHAR buffer[MAX_INF_STRING_LENGTH];
478 INT flags;
480 /* get root */
481 if (!SetupGetStringFieldW( &context, 1, buffer, ARRAY_SIZE( buffer ), NULL ))
482 continue;
483 if (!(root_key = get_root_key( buffer, info->default_root )))
484 continue;
486 /* get key */
487 if (!SetupGetStringFieldW( &context, 2, buffer, ARRAY_SIZE( buffer ), NULL ))
488 *buffer = 0;
490 /* get flags */
491 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
493 if (!info->delete)
495 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
497 else
499 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
500 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
502 /* Wine extension: magic support for symlinks */
503 if (flags >> 16 == REG_LINK) options = REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK;
505 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
507 if (RegOpenKeyExW( root_key, buffer, options, MAXIMUM_ALLOWED, &hkey ))
508 continue; /* ignore if it doesn't exist */
510 else
512 DWORD res = RegCreateKeyExW( root_key, buffer, 0, NULL, options,
513 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
514 if (res == ERROR_ALREADY_EXISTS && (options & REG_OPTION_CREATE_LINK))
515 res = RegCreateKeyExW( root_key, buffer, 0, NULL, REG_OPTION_OPEN_LINK,
516 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
517 if (res)
519 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
520 continue;
523 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
525 /* get value name */
526 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
527 *buffer = 0;
529 /* and now do it */
530 if (!do_reg_operation( hkey, buffer, &context, flags ))
532 RegCloseKey( hkey );
533 return FALSE;
535 RegCloseKey( hkey );
537 return TRUE;
541 /***********************************************************************
542 * do_register_dll
544 * Register or unregister a dll.
546 static BOOL do_register_dll( struct register_dll_info *info, const WCHAR *path,
547 INT flags, INT timeout, const WCHAR *args )
549 HMODULE module;
550 HRESULT res;
551 SP_REGISTER_CONTROL_STATUSW status;
552 IMAGE_NT_HEADERS *nt;
554 status.cbSize = sizeof(status);
555 status.FileName = path;
556 status.FailureCode = SPREG_SUCCESS;
557 status.Win32Error = ERROR_SUCCESS;
559 if (info->callback)
561 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
562 (UINT_PTR)&status, !info->unregister ))
564 case FILEOP_ABORT:
565 SetLastError( ERROR_OPERATION_ABORTED );
566 return FALSE;
567 case FILEOP_SKIP:
568 return TRUE;
569 case FILEOP_DOIT:
570 break;
574 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
576 WARN( "could not load %s\n", debugstr_w(path) );
577 status.FailureCode = SPREG_LOADLIBRARY;
578 status.Win32Error = GetLastError();
579 goto done;
582 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
584 /* file is an executable, not a dll */
585 STARTUPINFOW startup;
586 PROCESS_INFORMATION process_info;
587 WCHAR *cmd_line;
588 BOOL res;
589 DWORD len;
591 FreeLibrary( module );
592 module = NULL;
593 if (!args) args = L"/RegServer";
594 len = lstrlenW(path) + lstrlenW(args) + 4;
595 cmd_line = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
596 swprintf( cmd_line, len, L"\"%s\" %s", path, args );
597 memset( &startup, 0, sizeof(startup) );
598 startup.cb = sizeof(startup);
599 TRACE( "executing %s\n", debugstr_w(cmd_line) );
600 res = CreateProcessW( path, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &process_info );
601 HeapFree( GetProcessHeap(), 0, cmd_line );
602 if (!res)
604 status.FailureCode = SPREG_LOADLIBRARY;
605 status.Win32Error = GetLastError();
606 goto done;
608 CloseHandle( process_info.hThread );
610 if (WaitForSingleObject( process_info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
612 /* timed out, kill the process */
613 TerminateProcess( process_info.hProcess, 1 );
614 status.FailureCode = SPREG_TIMEOUT;
615 status.Win32Error = ERROR_TIMEOUT;
617 CloseHandle( process_info.hProcess );
618 goto done;
621 if (flags & FLG_REGSVR_DLLREGISTER)
623 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
624 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
626 if (!func)
628 status.FailureCode = SPREG_GETPROCADDR;
629 status.Win32Error = GetLastError();
630 goto done;
633 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
634 res = func();
636 if (FAILED(res))
638 WARN( "calling %s in %s returned error %lx\n", entry_point, debugstr_w(path), res );
639 status.FailureCode = SPREG_REGSVR;
640 status.Win32Error = res;
641 goto done;
645 if (flags & FLG_REGSVR_DLLINSTALL)
647 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
649 if (!func)
651 status.FailureCode = SPREG_GETPROCADDR;
652 status.Win32Error = GetLastError();
653 goto done;
656 TRACE( "calling DllInstall(%d,%s) in %s\n",
657 !info->unregister, debugstr_w(args), debugstr_w(path) );
658 res = func( !info->unregister, args );
660 if (FAILED(res))
662 WARN( "calling DllInstall in %s returned error %lx\n", debugstr_w(path), res );
663 status.FailureCode = SPREG_REGSVR;
664 status.Win32Error = res;
665 goto done;
669 done:
670 if (module)
672 if (info->modules_count >= info->modules_size)
674 int new_size = max( 32, info->modules_size * 2 );
675 HMODULE *new = info->modules ?
676 HeapReAlloc( GetProcessHeap(), 0, info->modules, new_size * sizeof(*new) ) :
677 HeapAlloc( GetProcessHeap(), 0, new_size * sizeof(*new) );
678 if (new)
680 info->modules_size = new_size;
681 info->modules = new;
684 if (info->modules_count < info->modules_size) info->modules[info->modules_count++] = module;
685 else FreeLibrary( module );
687 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
688 (UINT_PTR)&status, !info->unregister );
689 return TRUE;
693 /***********************************************************************
694 * register_dlls_callback
696 * Called once for each RegisterDlls entry in a given section.
698 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
700 struct register_dll_info *info = arg;
701 INFCONTEXT context;
702 BOOL ret = TRUE;
703 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
705 for (; ok; ok = SetupFindNextLine( &context, &context ))
707 WCHAR *path, *args, *p;
708 WCHAR buffer[MAX_INF_STRING_LENGTH];
709 INT flags, timeout;
711 /* get directory */
712 if (!(path = PARSER_get_dest_dir( &context ))) continue;
714 /* get dll name */
715 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
716 goto done;
717 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
718 (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
719 path = p;
720 p += lstrlenW(p);
721 if (p == path || p[-1] != '\\') *p++ = '\\';
722 lstrcpyW( p, buffer );
724 /* get flags */
725 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
727 /* get timeout */
728 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
730 /* get command line */
731 args = NULL;
732 if (SetupGetStringFieldW( &context, 6, buffer, ARRAY_SIZE( buffer ), NULL ))
733 args = buffer;
735 ret = do_register_dll( info, path, flags, timeout, args );
737 done:
738 HeapFree( GetProcessHeap(), 0, path );
739 if (!ret) break;
741 return ret;
744 /***********************************************************************
745 * fake_dlls_callback
747 * Called once for each WineFakeDlls entry in a given section.
749 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
751 INFCONTEXT context;
752 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
754 for (; ok; ok = SetupFindNextLine( &context, &context ))
756 WCHAR *path, *p;
757 WCHAR buffer[MAX_INF_STRING_LENGTH];
759 /* get directory */
760 if (!(path = PARSER_get_dest_dir( &context ))) continue;
762 /* get dll name */
763 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
764 goto done;
765 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
766 (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
767 path = p;
768 p += lstrlenW(p);
769 if (p == path || p[-1] != '\\') *p++ = '\\';
770 lstrcpyW( p, buffer );
772 /* get source dll */
773 if (SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
774 p = buffer; /* otherwise use target base name as default source */
776 create_fake_dll( path, p ); /* ignore errors */
778 done:
779 HeapFree( GetProcessHeap(), 0, path );
781 return TRUE;
784 /***********************************************************************
785 * update_ini_callback
787 * Called once for each UpdateInis entry in a given section.
789 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
791 INFCONTEXT context;
793 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
795 for (; ok; ok = SetupFindNextLine( &context, &context ))
797 WCHAR buffer[MAX_INF_STRING_LENGTH];
798 WCHAR filename[MAX_INF_STRING_LENGTH];
799 WCHAR section[MAX_INF_STRING_LENGTH];
800 WCHAR entry[MAX_INF_STRING_LENGTH];
801 WCHAR string[MAX_INF_STRING_LENGTH];
802 LPWSTR divider;
804 if (!SetupGetStringFieldW( &context, 1, filename, ARRAY_SIZE( filename ), NULL ))
805 continue;
807 if (!SetupGetStringFieldW( &context, 2, section, ARRAY_SIZE( section ), NULL ))
808 continue;
810 if (!SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
811 continue;
813 divider = wcschr(buffer,'=');
814 if (divider)
816 *divider = 0;
817 lstrcpyW(entry,buffer);
818 divider++;
819 lstrcpyW(string,divider);
821 else
823 lstrcpyW(entry,buffer);
824 string[0]=0;
827 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
828 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
829 WritePrivateProfileStringW(section,entry,string,filename);
832 return TRUE;
835 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
837 FIXME( "should update ini fields %s\n", debugstr_w(field) );
838 return TRUE;
841 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
843 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
844 return TRUE;
847 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
849 FIXME( "should do logconf %s\n", debugstr_w(field) );
850 return TRUE;
853 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
855 FIXME( "should do bitreg %s\n", debugstr_w(field) );
856 return TRUE;
859 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
861 WCHAR lnkpath[MAX_PATH];
862 LPWSTR cmdline=NULL, lnkpath_end;
863 DWORD name_size;
864 INFCONTEXT name_context, context;
865 int attrs=0;
867 TRACE( "(%s)\n", debugstr_w(field) );
869 if (SetupFindFirstLineW( hinf, field, L"Name", &name_context ))
871 SetupGetIntField( &name_context, 2, &attrs );
872 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
874 else return TRUE;
876 /* calculate filename */
877 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
878 lnkpath_end = lnkpath + lstrlenW(lnkpath);
879 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
881 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, L"SubDir", &context ))
883 DWORD subdir_size;
885 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
886 return TRUE;
888 lnkpath_end += subdir_size - 1;
889 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
892 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
893 return TRUE;
895 lnkpath_end += name_size - 1;
897 if (attrs & FLG_PROFITEM_GROUP)
899 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
901 else
903 IShellLinkW* shelllink=NULL;
904 IPersistFile* persistfile=NULL;
905 HRESULT initresult=E_FAIL;
907 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
908 lstrcpyW( lnkpath_end, L".lnk" );
910 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
912 /* calculate command line */
913 if (SetupFindFirstLineW( hinf, field, L"CmdLine", &context ))
915 unsigned int dir_len=0;
916 DWORD subdir_size=0, filename_size=0;
917 int dirid=0;
918 LPCWSTR dir;
919 LPWSTR cmdline_end;
921 SetupGetIntField( &context, 1, &dirid );
922 dir = DIRID_get_string( dirid );
924 if (dir) dir_len = lstrlenW(dir);
926 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
927 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
929 if (dir_len && filename_size)
931 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
933 lstrcpyW( cmdline_end, dir );
934 cmdline_end += dir_len;
935 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
937 if (subdir_size)
939 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
940 cmdline_end += subdir_size-1;
941 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
943 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
944 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
948 if (!cmdline) return TRUE;
950 initresult = CoInitialize(NULL);
952 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
953 &IID_IShellLinkW, (LPVOID*)&shelllink )))
954 goto done;
956 IShellLinkW_SetPath( shelllink, cmdline );
957 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
958 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
960 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
961 IPersistFile_Save( persistfile, lnkpath, FALSE );
962 IPersistFile_Release( persistfile );
964 IShellLinkW_Release( shelllink );
966 done:
967 if (SUCCEEDED(initresult)) CoUninitialize();
968 HeapFree( GetProcessHeap(), 0, cmdline );
971 return TRUE;
974 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
976 FIXME( "should do copy inf %s\n", debugstr_w(field) );
977 return TRUE;
981 /***********************************************************************
982 * iterate_section_fields
984 * Iterate over all fields of a certain key of a certain section
986 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
987 iterate_fields_func callback, void *arg )
989 WCHAR static_buffer[200];
990 WCHAR *buffer = static_buffer;
991 DWORD size = ARRAY_SIZE( static_buffer );
992 INFCONTEXT context;
993 BOOL ret = FALSE;
995 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
996 while (ok)
998 UINT i, count = SetupGetFieldCount( &context );
999 for (i = 1; i <= count; i++)
1001 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
1002 goto done;
1003 if (!callback( hinf, buffer, arg ))
1005 WARN("callback failed for %s %s err %ld\n",
1006 debugstr_w(section), debugstr_w(buffer), GetLastError() );
1007 goto done;
1010 ok = SetupFindNextMatchLineW( &context, key, &context );
1012 ret = TRUE;
1013 done:
1014 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1015 return ret;
1019 /***********************************************************************
1020 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
1022 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
1023 PCSTR section, PCSTR src_root, UINT flags )
1025 UNICODE_STRING sectionW;
1026 BOOL ret = FALSE;
1028 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1030 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1031 return FALSE;
1033 if (!src_root)
1034 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1035 NULL, flags );
1036 else
1038 UNICODE_STRING srcW;
1039 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
1041 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1042 srcW.Buffer, flags );
1043 RtlFreeUnicodeString( &srcW );
1045 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1047 RtlFreeUnicodeString( &sectionW );
1048 return ret;
1052 /***********************************************************************
1053 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1055 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1056 PCWSTR section, PCWSTR src_root, UINT flags )
1058 struct files_callback_info info;
1060 info.queue = queue;
1061 info.src_root = src_root;
1062 info.copy_flags = flags;
1063 info.layout = hlayout;
1064 return iterate_section_fields( hinf, section, L"CopyFiles", copy_files_callback, &info ) &&
1065 iterate_section_fields( hinf, section, L"DelFiles", delete_files_callback, &info ) &&
1066 iterate_section_fields( hinf, section, L"RenFiles", rename_files_callback, &info );
1070 /***********************************************************************
1071 * SetupInstallFromInfSectionA (SETUPAPI.@)
1073 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1074 HKEY key_root, PCSTR src_root, UINT copy_flags,
1075 PSP_FILE_CALLBACK_A callback, PVOID context,
1076 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1078 UNICODE_STRING sectionW, src_rootW;
1079 struct callback_WtoA_context ctx;
1080 BOOL ret = FALSE;
1082 src_rootW.Buffer = NULL;
1083 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1085 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1086 return FALSE;
1089 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1091 ctx.orig_context = context;
1092 ctx.orig_handler = callback;
1093 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1094 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1095 &ctx, devinfo, devinfo_data );
1096 RtlFreeUnicodeString( &sectionW );
1098 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1100 RtlFreeUnicodeString( &src_rootW );
1101 return ret;
1105 /***********************************************************************
1106 * SetupInstallFromInfSectionW (SETUPAPI.@)
1108 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1109 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1110 PSP_FILE_CALLBACK_W callback, PVOID context,
1111 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1113 BOOL ret;
1114 int i;
1116 if (flags & SPINST_REGSVR)
1118 if (iterate_section_fields( hinf, section, L"WineFakeDlls", fake_dlls_callback, NULL ))
1119 cleanup_fake_dlls();
1120 else
1121 return FALSE;
1123 if (flags & SPINST_FILES)
1125 HSPFILEQ queue;
1127 if (!(queue = SetupOpenFileQueue())) return FALSE;
1128 ret = (SetupInstallFilesFromInfSectionW( hinf, NULL, queue, section, src_root, copy_flags ) &&
1129 SetupCommitFileQueueW( owner, queue, callback, context ));
1130 SetupCloseFileQueue( queue );
1131 if (!ret) return FALSE;
1133 if (flags & SPINST_INIFILES)
1135 if (!iterate_section_fields( hinf, section, L"UpdateInis", update_ini_callback, NULL ) ||
1136 !iterate_section_fields( hinf, section, L"UpdateIniFields",
1137 update_ini_fields_callback, NULL ))
1138 return FALSE;
1140 if (flags & SPINST_INI2REG)
1142 if (!iterate_section_fields( hinf, section, L"Ini2Reg", ini2reg_callback, NULL ))
1143 return FALSE;
1145 if (flags & SPINST_LOGCONFIG)
1147 if (!iterate_section_fields( hinf, section, L"LogConf", logconf_callback, NULL ))
1148 return FALSE;
1150 if (flags & SPINST_REGSVR)
1152 struct register_dll_info info = { .unregister = FALSE };
1153 HRESULT hr;
1155 if (flags & SPINST_REGISTERCALLBACKAWARE)
1157 info.callback = callback;
1158 info.callback_context = context;
1161 hr = CoInitialize(NULL);
1163 ret = iterate_section_fields( hinf, section, L"RegisterDlls", register_dlls_callback, &info );
1164 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1166 if (SUCCEEDED(hr))
1167 CoUninitialize();
1169 HeapFree( GetProcessHeap(), 0, info.modules );
1170 if (!ret) return FALSE;
1172 if (flags & SPINST_UNREGSVR)
1174 struct register_dll_info info = { .unregister = TRUE };
1175 HRESULT hr;
1177 if (flags & SPINST_REGISTERCALLBACKAWARE)
1179 info.callback = callback;
1180 info.callback_context = context;
1183 hr = CoInitialize(NULL);
1185 ret = iterate_section_fields( hinf, section, L"UnregisterDlls", register_dlls_callback, &info );
1186 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1188 if (SUCCEEDED(hr))
1189 CoUninitialize();
1191 HeapFree( GetProcessHeap(), 0, info.modules );
1192 if (!ret) return FALSE;
1194 if (flags & SPINST_REGISTRY)
1196 struct registry_callback_info info;
1198 info.default_root = key_root;
1199 info.delete = TRUE;
1200 if (!iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info ))
1201 return FALSE;
1202 info.delete = FALSE;
1203 if (!iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info ))
1204 return FALSE;
1206 if (flags & SPINST_BITREG)
1208 if (!iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL ))
1209 return FALSE;
1211 if (flags & SPINST_PROFILEITEMS)
1213 if (!iterate_section_fields( hinf, section, L"ProfileItems", profile_items_callback, NULL ))
1214 return FALSE;
1216 if (flags & SPINST_COPYINF)
1218 if (!iterate_section_fields( hinf, section, L"CopyINF", copy_inf_callback, NULL ))
1219 return FALSE;
1222 SetLastError(ERROR_SUCCESS);
1223 return TRUE;
1227 /***********************************************************************
1228 * InstallHinfSectionW (SETUPAPI.@)
1230 * NOTE: 'cmdline' is <section> <mode> <path> from
1231 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1233 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1235 #ifdef __i386__
1236 static const WCHAR nt_platformW[] = L".ntx86";
1237 #elif defined(__x86_64__)
1238 static const WCHAR nt_platformW[] = L".ntamd64";
1239 #elif defined(__arm__)
1240 static const WCHAR nt_platformW[] = L".ntarm";
1241 #elif defined(__aarch64__)
1242 static const WCHAR nt_platformW[] = L".ntarm64";
1243 #else /* FIXME: other platforms */
1244 static const WCHAR nt_platformW[] = L".nt";
1245 #endif
1247 WCHAR *s, *path, section[MAX_PATH + ARRAY_SIZE( nt_platformW ) + ARRAY_SIZE( L".Services" )];
1248 void *callback_context;
1249 UINT mode;
1250 HINF hinf;
1252 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1254 lstrcpynW( section, cmdline, MAX_PATH );
1256 if (!(s = wcschr( section, ' ' ))) return;
1257 *s++ = 0;
1258 while (*s == ' ') s++;
1259 mode = wcstol( s, NULL, 10 );
1261 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1262 if (!(s = wcschr( s, ' ' ))) return;
1263 while (*s == ' ') s++;
1264 path = s;
1266 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1267 if (hinf == INVALID_HANDLE_VALUE) return;
1269 if (!(GetVersion() & 0x80000000))
1271 INFCONTEXT context;
1273 /* check for <section>.ntx86 (or corresponding name for the current platform)
1274 * and then <section>.nt */
1275 s = section + lstrlenW(section);
1276 lstrcpyW( s, nt_platformW );
1277 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1279 lstrcpyW( s, L".nt" );
1280 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1282 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1285 callback_context = SetupInitDefaultQueueCallback( hwnd );
1286 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1287 SetupDefaultQueueCallbackW, callback_context,
1288 NULL, NULL );
1289 SetupTermDefaultQueueCallback( callback_context );
1290 lstrcatW( section, L".Services" );
1291 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1292 SetupCloseInfFile( hinf );
1294 /* FIXME: should check the mode and maybe reboot */
1295 /* there isn't much point in doing that since we */
1296 /* don't yet handle deferred file copies anyway. */
1297 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1301 /***********************************************************************
1302 * InstallHinfSectionA (SETUPAPI.@)
1304 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1306 UNICODE_STRING cmdlineW;
1308 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1310 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1311 RtlFreeUnicodeString( &cmdlineW );
1316 /***********************************************************************
1317 * add_service
1319 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1321 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1323 struct registry_callback_info info;
1324 SC_HANDLE service;
1325 INFCONTEXT context;
1326 SERVICE_DESCRIPTIONW descr;
1327 WCHAR *display_name, *start_name, *load_order, *binary_path;
1328 INT service_type = 0, start_type = 0, error_control = 0;
1329 DWORD size;
1330 HKEY hkey;
1332 /* first the mandatory fields */
1334 if (!SetupFindFirstLineW( hinf, section, L"ServiceType", &context ) ||
1335 !SetupGetIntField( &context, 1, &service_type ))
1337 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1338 return FALSE;
1340 if (!SetupFindFirstLineW( hinf, section, L"StartType", &context ) ||
1341 !SetupGetIntField( &context, 1, &start_type ))
1343 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1344 return FALSE;
1346 if (!SetupFindFirstLineW( hinf, section, L"ErrorControl", &context ) ||
1347 !SetupGetIntField( &context, 1, &error_control ))
1349 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1350 return FALSE;
1352 if (!(binary_path = dup_section_line_field( hinf, section, L"ServiceBinary", 1 )))
1354 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1355 return FALSE;
1358 /* now the optional fields */
1360 display_name = dup_section_line_field( hinf, section, L"DisplayName", 1 );
1361 start_name = dup_section_line_field( hinf, section, L"StartName", 1 );
1362 load_order = dup_section_line_field( hinf, section, L"LoadOrderGroup", 1 );
1363 descr.lpDescription = dup_section_line_field( hinf, section, L"Description", 1 );
1365 /* FIXME: Dependencies field */
1366 /* FIXME: Security field */
1368 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %lx\n",
1369 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1370 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1372 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1373 service_type, start_type, error_control, binary_path,
1374 load_order, NULL, NULL, start_name, NULL );
1375 if (service)
1377 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1379 else
1381 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1382 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1383 if (!service) goto done;
1385 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1386 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1388 QUERY_SERVICE_CONFIGW *config = NULL;
1390 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1391 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1392 config = HeapAlloc( GetProcessHeap(), 0, size );
1393 if (config && QueryServiceConfigW( service, config, size, &size ))
1395 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1396 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1397 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1399 HeapFree( GetProcessHeap(), 0, display_name );
1400 display_name = strdupW( config->lpDisplayName );
1402 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1404 HeapFree( GetProcessHeap(), 0, load_order );
1405 load_order = strdupW( config->lpLoadOrderGroup );
1408 HeapFree( GetProcessHeap(), 0, config );
1410 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1411 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1412 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1414 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1415 load_order, NULL, NULL, start_name, NULL, display_name );
1417 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1418 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1421 /* execute the AddReg, DelReg and BitReg entries */
1423 info.default_root = 0;
1424 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", &hkey ))
1426 RegOpenKeyW( hkey, name, &info.default_root );
1427 RegCloseKey( hkey );
1429 if (info.default_root)
1431 info.delete = TRUE;
1432 iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info );
1433 info.delete = FALSE;
1434 iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info );
1435 RegCloseKey( info.default_root );
1437 iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL );
1439 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1440 CloseServiceHandle( service );
1442 done:
1443 if (!service) WARN( "failed err %lu\n", GetLastError() );
1444 HeapFree( GetProcessHeap(), 0, binary_path );
1445 HeapFree( GetProcessHeap(), 0, display_name );
1446 HeapFree( GetProcessHeap(), 0, start_name );
1447 HeapFree( GetProcessHeap(), 0, load_order );
1448 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1449 return service != 0;
1453 /***********************************************************************
1454 * del_service
1456 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1458 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1460 BOOL ret;
1461 SC_HANDLE service;
1462 SERVICE_STATUS status;
1464 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1466 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1467 WARN( "cannot open %s err %lu\n", debugstr_w(name), GetLastError() );
1468 return FALSE;
1470 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1471 TRACE( "deleting %s\n", debugstr_w(name) );
1472 ret = DeleteService( service );
1473 CloseServiceHandle( service );
1474 return ret;
1478 /***********************************************************************
1479 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1481 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1483 WCHAR service_name[MAX_INF_STRING_LENGTH];
1484 WCHAR service_section[MAX_INF_STRING_LENGTH];
1485 SC_HANDLE scm;
1486 INFCONTEXT context;
1487 INT section_flags;
1488 BOOL ret = TRUE;
1490 if (!SetupFindFirstLineW( hinf, section, NULL, &context ))
1492 SetLastError( ERROR_SECTION_NOT_FOUND );
1493 return FALSE;
1495 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1497 if (SetupFindFirstLineW( hinf, section, L"AddService", &context ))
1501 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1502 continue;
1503 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1504 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1505 continue;
1506 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1507 goto done;
1508 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1511 if (SetupFindFirstLineW( hinf, section, L"DelService", &context ))
1515 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1516 continue;
1517 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1518 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1519 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1521 if (ret) SetLastError( ERROR_SUCCESS );
1522 done:
1523 CloseServiceHandle( scm );
1524 return ret;
1528 /***********************************************************************
1529 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1531 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1533 UNICODE_STRING SectionNameW;
1534 BOOL ret = FALSE;
1536 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1538 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1539 RtlFreeUnicodeString( &SectionNameW );
1541 else
1542 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1544 return ret;
1548 /***********************************************************************
1549 * SetupGetInfFileListA (SETUPAPI.@)
1551 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1552 DWORD insize, PDWORD outsize)
1554 UNICODE_STRING dirW;
1555 PWSTR bufferW = NULL;
1556 BOOL ret = FALSE;
1557 DWORD outsizeA, outsizeW;
1559 if ( dir )
1560 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1561 else
1562 dirW.Buffer = NULL;
1564 if ( buffer )
1565 bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof( WCHAR ));
1567 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1569 if ( ret )
1571 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1572 buffer, insize, NULL, NULL);
1573 if ( outsize ) *outsize = outsizeA;
1576 HeapFree( GetProcessHeap(), 0, bufferW );
1577 RtlFreeUnicodeString( &dirW );
1578 return ret;
1582 /***********************************************************************
1583 * SetupGetInfFileListW (SETUPAPI.@)
1585 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1586 DWORD insize, PDWORD outsize)
1588 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1589 DWORD dir_len, name_len = 20, size ;
1590 WIN32_FIND_DATAW finddata;
1591 HANDLE hdl;
1592 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1593 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1595 FIXME( "unknown inf_style(s) 0x%lx\n",
1596 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1597 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1598 if( outsize ) *outsize = 1;
1599 return TRUE;
1601 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1603 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1604 if( outsize ) *outsize = 1;
1605 return TRUE;
1607 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1608 FIXME("ignored inf_style(s) %s %s\n",
1609 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1610 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1611 if( dir )
1613 DWORD att;
1614 DWORD msize;
1615 dir_len = lstrlenW( dir );
1616 if ( !dir_len ) return FALSE;
1617 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1618 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1619 if( !filter )
1621 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1622 return FALSE;
1624 lstrcpyW( filter, dir );
1625 if ( '\\' == filter[dir_len - 1] )
1626 filter[--dir_len] = 0;
1628 att = GetFileAttributesW( filter );
1629 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1631 HeapFree( GetProcessHeap(), 0, filter );
1632 SetLastError( ERROR_DIRECTORY );
1633 return FALSE;
1636 else
1638 DWORD msize;
1639 dir_len = GetWindowsDirectoryW( NULL, 0 );
1640 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1641 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1642 if( !filter )
1644 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1645 return FALSE;
1647 GetWindowsDirectoryW( filter, msize );
1648 lstrcatW( filter, L"\\inf" );
1650 lstrcatW( filter, L"\\*.inf" );
1652 hdl = FindFirstFileW( filter , &finddata );
1653 if ( hdl == INVALID_HANDLE_VALUE )
1655 if( outsize ) *outsize = 1;
1656 HeapFree( GetProcessHeap(), 0, filter );
1657 return TRUE;
1659 size = 1;
1662 WCHAR signature[ MAX_PATH ];
1663 BOOL valid = FALSE;
1664 DWORD len = lstrlenW( finddata.cFileName );
1665 if (!fullname || ( name_len < len ))
1667 name_len = ( name_len < len ) ? len : name_len;
1668 HeapFree( GetProcessHeap(), 0, fullname );
1669 fullname = HeapAlloc( GetProcessHeap(), 0,
1670 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1671 if( !fullname )
1673 FindClose( hdl );
1674 HeapFree( GetProcessHeap(), 0, filter );
1675 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1676 return FALSE;
1678 lstrcpyW( fullname, filter );
1680 fullname[ dir_len + 1] = 0; /* keep '\\' */
1681 lstrcatW( fullname, finddata.cFileName );
1682 if (!GetPrivateProfileStringW( L"Version", L"Signature", NULL, signature, MAX_PATH, fullname ))
1683 signature[0] = 0;
1684 if( INF_STYLE_OLDNT & style )
1685 valid = wcsicmp( L"$Chicago$", signature ) && wcsicmp( L"$WINDOWS NT$", signature );
1686 if( INF_STYLE_WIN4 & style )
1687 valid = valid || !wcsicmp( L"$Chicago$", signature ) ||
1688 !wcsicmp( L"$WINDOWS NT$", signature );
1689 if( valid )
1691 size += 1 + lstrlenW( finddata.cFileName );
1692 if( ptr && insize >= size )
1694 lstrcpyW( ptr, finddata.cFileName );
1695 ptr += 1 + lstrlenW( finddata.cFileName );
1696 *ptr = 0;
1700 while( FindNextFileW( hdl, &finddata ));
1701 FindClose( hdl );
1703 HeapFree( GetProcessHeap(), 0, fullname );
1704 HeapFree( GetProcessHeap(), 0, filter );
1705 if( outsize ) *outsize = size;
1706 return TRUE;