mf/tests: Test input type for WMA decoder DMO.
[wine.git] / dlls / setupapi / install.c
blob17150a405968aef5537655de3fb6e252f811fa2a
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) free( buffer );
90 if (!(buffer = malloc( required * sizeof(WCHAR) ))) return NULL;
91 *size = required;
92 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
94 if (buffer != static_buffer) free( 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 = malloc( 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 = malloc( (size + str_size) * sizeof(WCHAR) ))) return false;
265 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size ))
267 free( 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 free( 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 = malloc( 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 free( 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 = malloc( size * sizeof(WCHAR) ))) return FALSE;
357 SetupGetStringFieldW( context, 5, str, size, NULL );
358 delete_multi_sz_value( hkey, value, str );
359 free( 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 = malloc( 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 free( str );
410 return FALSE;
412 free( 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 = malloc( 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 free( 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 = malloc( 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 free( 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 = malloc( 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 free( 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 = realloc( info->modules, new_size * sizeof(*new) );
676 if (new)
678 info->modules_size = new_size;
679 info->modules = new;
682 if (info->modules_count < info->modules_size) info->modules[info->modules_count++] = module;
683 else FreeLibrary( module );
685 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
686 (UINT_PTR)&status, !info->unregister );
687 return TRUE;
691 /***********************************************************************
692 * register_dlls_callback
694 * Called once for each RegisterDlls entry in a given section.
696 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
698 struct register_dll_info *info = arg;
699 INFCONTEXT context;
700 BOOL ret = TRUE;
701 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
703 for (; ok; ok = SetupFindNextLine( &context, &context ))
705 WCHAR *path, *args, *p;
706 WCHAR buffer[MAX_INF_STRING_LENGTH];
707 INT flags, timeout;
709 /* get directory */
710 if (!(path = PARSER_get_dest_dir( &context ))) continue;
712 /* get dll name */
713 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
714 goto done;
715 if (!(p = realloc( path, (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) )))
716 goto done;
717 path = p;
718 p += lstrlenW(p);
719 if (p == path || p[-1] != '\\') *p++ = '\\';
720 lstrcpyW( p, buffer );
722 /* get flags */
723 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
725 /* get timeout */
726 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
728 /* get command line */
729 args = NULL;
730 if (SetupGetStringFieldW( &context, 6, buffer, ARRAY_SIZE( buffer ), NULL ))
731 args = buffer;
733 ret = do_register_dll( info, path, flags, timeout, args );
735 done:
736 free( path );
737 if (!ret) break;
739 return ret;
742 /***********************************************************************
743 * fake_dlls_callback
745 * Called once for each WineFakeDlls entry in a given section.
747 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
749 INFCONTEXT context;
750 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
752 for (; ok; ok = SetupFindNextLine( &context, &context ))
754 WCHAR *path, *p;
755 WCHAR buffer[MAX_INF_STRING_LENGTH];
757 /* get directory */
758 if (!(path = PARSER_get_dest_dir( &context ))) continue;
760 /* get dll name */
761 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
762 goto done;
763 if (!(p = realloc( path, (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) )))
764 goto done;
765 path = p;
766 p += lstrlenW(p);
767 if (p == path || p[-1] != '\\') *p++ = '\\';
768 lstrcpyW( p, buffer );
770 /* get source dll */
771 if (SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
772 p = buffer; /* otherwise use target base name as default source */
774 create_fake_dll( path, p ); /* ignore errors */
776 done:
777 free( path );
779 return TRUE;
782 /***********************************************************************
783 * update_ini_callback
785 * Called once for each UpdateInis entry in a given section.
787 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
789 INFCONTEXT context;
791 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
793 for (; ok; ok = SetupFindNextLine( &context, &context ))
795 WCHAR buffer[MAX_INF_STRING_LENGTH];
796 WCHAR filename[MAX_INF_STRING_LENGTH];
797 WCHAR section[MAX_INF_STRING_LENGTH];
798 WCHAR entry[MAX_INF_STRING_LENGTH];
799 WCHAR string[MAX_INF_STRING_LENGTH];
800 LPWSTR divider;
802 if (!SetupGetStringFieldW( &context, 1, filename, ARRAY_SIZE( filename ), NULL ))
803 continue;
805 if (!SetupGetStringFieldW( &context, 2, section, ARRAY_SIZE( section ), NULL ))
806 continue;
808 if (!SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
809 continue;
811 divider = wcschr(buffer,'=');
812 if (divider)
814 *divider = 0;
815 lstrcpyW(entry,buffer);
816 divider++;
817 lstrcpyW(string,divider);
819 else
821 lstrcpyW(entry,buffer);
822 string[0]=0;
825 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
826 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
827 WritePrivateProfileStringW(section,entry,string,filename);
830 return TRUE;
833 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
835 FIXME( "should update ini fields %s\n", debugstr_w(field) );
836 return TRUE;
839 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
841 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
842 return TRUE;
845 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
847 FIXME( "should do logconf %s\n", debugstr_w(field) );
848 return TRUE;
851 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
853 FIXME( "should do bitreg %s\n", debugstr_w(field) );
854 return TRUE;
857 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
859 WCHAR lnkpath[MAX_PATH];
860 LPWSTR cmdline=NULL, lnkpath_end;
861 DWORD name_size;
862 INFCONTEXT name_context, context;
863 int attrs=0;
865 TRACE( "(%s)\n", debugstr_w(field) );
867 if (SetupFindFirstLineW( hinf, field, L"Name", &name_context ))
869 SetupGetIntField( &name_context, 2, &attrs );
870 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
872 else return TRUE;
874 /* calculate filename */
875 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
876 lnkpath_end = lnkpath + lstrlenW(lnkpath);
877 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
879 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, L"SubDir", &context ))
881 DWORD subdir_size;
883 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
884 return TRUE;
886 lnkpath_end += subdir_size - 1;
887 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
890 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
891 return TRUE;
893 lnkpath_end += name_size - 1;
895 if (attrs & FLG_PROFITEM_GROUP)
897 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
899 else
901 IShellLinkW* shelllink=NULL;
902 IPersistFile* persistfile=NULL;
903 HRESULT initresult=E_FAIL;
905 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
906 lstrcpyW( lnkpath_end, L".lnk" );
908 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
910 /* calculate command line */
911 if (SetupFindFirstLineW( hinf, field, L"CmdLine", &context ))
913 unsigned int dir_len=0;
914 DWORD subdir_size=0, filename_size=0;
915 int dirid=0;
916 LPCWSTR dir;
917 LPWSTR cmdline_end;
919 SetupGetIntField( &context, 1, &dirid );
920 dir = DIRID_get_string( dirid );
922 if (dir) dir_len = lstrlenW(dir);
924 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
925 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
927 if (dir_len && filename_size)
929 cmdline = cmdline_end = malloc( sizeof(WCHAR) * (dir_len + subdir_size + filename_size + 1) );
931 lstrcpyW( cmdline_end, dir );
932 cmdline_end += dir_len;
933 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
935 if (subdir_size)
937 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
938 cmdline_end += subdir_size-1;
939 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
941 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
942 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
946 if (!cmdline) return TRUE;
948 initresult = CoInitialize(NULL);
950 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
951 &IID_IShellLinkW, (LPVOID*)&shelllink )))
952 goto done;
954 IShellLinkW_SetPath( shelllink, cmdline );
955 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
956 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
958 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
959 IPersistFile_Save( persistfile, lnkpath, FALSE );
960 IPersistFile_Release( persistfile );
962 IShellLinkW_Release( shelllink );
964 done:
965 if (SUCCEEDED(initresult)) CoUninitialize();
966 free( cmdline );
969 return TRUE;
972 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
974 FIXME( "should do copy inf %s\n", debugstr_w(field) );
975 return TRUE;
979 /***********************************************************************
980 * iterate_section_fields
982 * Iterate over all fields of a certain key of a certain section
984 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
985 iterate_fields_func callback, void *arg )
987 WCHAR static_buffer[200];
988 WCHAR *buffer = static_buffer;
989 DWORD size = ARRAY_SIZE( static_buffer );
990 INFCONTEXT context;
991 BOOL ret = FALSE;
993 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
994 while (ok)
996 UINT i, count = SetupGetFieldCount( &context );
997 for (i = 1; i <= count; i++)
999 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
1000 goto done;
1001 if (!callback( hinf, buffer, arg ))
1003 WARN("callback failed for %s %s err %ld\n",
1004 debugstr_w(section), debugstr_w(buffer), GetLastError() );
1005 goto done;
1008 ok = SetupFindNextMatchLineW( &context, key, &context );
1010 ret = TRUE;
1011 done:
1012 if (buffer != static_buffer) free( buffer );
1013 return ret;
1017 /***********************************************************************
1018 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
1020 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
1021 PCSTR section, PCSTR src_root, UINT flags )
1023 UNICODE_STRING sectionW;
1024 BOOL ret = FALSE;
1026 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1028 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1029 return FALSE;
1031 if (!src_root)
1032 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1033 NULL, flags );
1034 else
1036 UNICODE_STRING srcW;
1037 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
1039 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1040 srcW.Buffer, flags );
1041 RtlFreeUnicodeString( &srcW );
1043 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1045 RtlFreeUnicodeString( &sectionW );
1046 return ret;
1050 /***********************************************************************
1051 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1053 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1054 PCWSTR section, PCWSTR src_root, UINT flags )
1056 struct files_callback_info info;
1058 info.queue = queue;
1059 info.src_root = src_root;
1060 info.copy_flags = flags;
1061 info.layout = hlayout;
1062 return iterate_section_fields( hinf, section, L"CopyFiles", copy_files_callback, &info ) &&
1063 iterate_section_fields( hinf, section, L"DelFiles", delete_files_callback, &info ) &&
1064 iterate_section_fields( hinf, section, L"RenFiles", rename_files_callback, &info );
1068 /***********************************************************************
1069 * SetupInstallFromInfSectionA (SETUPAPI.@)
1071 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1072 HKEY key_root, PCSTR src_root, UINT copy_flags,
1073 PSP_FILE_CALLBACK_A callback, PVOID context,
1074 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1076 UNICODE_STRING sectionW, src_rootW;
1077 struct callback_WtoA_context ctx;
1078 BOOL ret = FALSE;
1080 src_rootW.Buffer = NULL;
1081 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1083 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1084 return FALSE;
1087 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1089 ctx.orig_context = context;
1090 ctx.orig_handler = callback;
1091 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1092 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1093 &ctx, devinfo, devinfo_data );
1094 RtlFreeUnicodeString( &sectionW );
1096 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1098 RtlFreeUnicodeString( &src_rootW );
1099 return ret;
1103 /***********************************************************************
1104 * SetupInstallFromInfSectionW (SETUPAPI.@)
1106 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1107 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1108 PSP_FILE_CALLBACK_W callback, PVOID context,
1109 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1111 BOOL ret;
1112 int i;
1114 if (flags & SPINST_REGSVR)
1116 if (iterate_section_fields( hinf, section, L"WineFakeDlls", fake_dlls_callback, NULL ))
1117 cleanup_fake_dlls();
1118 else
1119 return FALSE;
1121 if (flags & SPINST_FILES)
1123 HSPFILEQ queue;
1125 if (!(queue = SetupOpenFileQueue())) return FALSE;
1126 ret = (SetupInstallFilesFromInfSectionW( hinf, NULL, queue, section, src_root, copy_flags ) &&
1127 SetupCommitFileQueueW( owner, queue, callback, context ));
1128 SetupCloseFileQueue( queue );
1129 if (!ret) return FALSE;
1131 if (flags & SPINST_INIFILES)
1133 if (!iterate_section_fields( hinf, section, L"UpdateInis", update_ini_callback, NULL ) ||
1134 !iterate_section_fields( hinf, section, L"UpdateIniFields",
1135 update_ini_fields_callback, NULL ))
1136 return FALSE;
1138 if (flags & SPINST_INI2REG)
1140 if (!iterate_section_fields( hinf, section, L"Ini2Reg", ini2reg_callback, NULL ))
1141 return FALSE;
1143 if (flags & SPINST_LOGCONFIG)
1145 if (!iterate_section_fields( hinf, section, L"LogConf", logconf_callback, NULL ))
1146 return FALSE;
1148 if (flags & SPINST_REGSVR)
1150 struct register_dll_info info = { .unregister = FALSE };
1151 HRESULT hr;
1153 if (flags & SPINST_REGISTERCALLBACKAWARE)
1155 info.callback = callback;
1156 info.callback_context = context;
1159 hr = CoInitialize(NULL);
1161 ret = iterate_section_fields( hinf, section, L"RegisterDlls", register_dlls_callback, &info );
1162 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1164 if (SUCCEEDED(hr))
1165 CoUninitialize();
1167 free( info.modules );
1168 if (!ret) return FALSE;
1170 if (flags & SPINST_UNREGSVR)
1172 struct register_dll_info info = { .unregister = TRUE };
1173 HRESULT hr;
1175 if (flags & SPINST_REGISTERCALLBACKAWARE)
1177 info.callback = callback;
1178 info.callback_context = context;
1181 hr = CoInitialize(NULL);
1183 ret = iterate_section_fields( hinf, section, L"UnregisterDlls", register_dlls_callback, &info );
1184 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1186 if (SUCCEEDED(hr))
1187 CoUninitialize();
1189 free( info.modules );
1190 if (!ret) return FALSE;
1192 if (flags & SPINST_REGISTRY)
1194 struct registry_callback_info info;
1196 info.default_root = key_root;
1197 info.delete = TRUE;
1198 if (!iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info ))
1199 return FALSE;
1200 info.delete = FALSE;
1201 if (!iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info ))
1202 return FALSE;
1204 if (flags & SPINST_BITREG)
1206 if (!iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL ))
1207 return FALSE;
1209 if (flags & SPINST_PROFILEITEMS)
1211 if (!iterate_section_fields( hinf, section, L"ProfileItems", profile_items_callback, NULL ))
1212 return FALSE;
1214 if (flags & SPINST_COPYINF)
1216 if (!iterate_section_fields( hinf, section, L"CopyINF", copy_inf_callback, NULL ))
1217 return FALSE;
1220 SetLastError(ERROR_SUCCESS);
1221 return TRUE;
1225 /***********************************************************************
1226 * InstallHinfSectionW (SETUPAPI.@)
1228 * NOTE: 'cmdline' is <section> <mode> <path> from
1229 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1231 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1233 #ifdef __i386__
1234 static const WCHAR nt_platformW[] = L".ntx86";
1235 #elif defined(__x86_64__)
1236 static const WCHAR nt_platformW[] = L".ntamd64";
1237 #elif defined(__arm__)
1238 static const WCHAR nt_platformW[] = L".ntarm";
1239 #elif defined(__aarch64__)
1240 static const WCHAR nt_platformW[] = L".ntarm64";
1241 #else /* FIXME: other platforms */
1242 static const WCHAR nt_platformW[] = L".nt";
1243 #endif
1245 WCHAR *s, *path, section[MAX_PATH + ARRAY_SIZE( nt_platformW ) + ARRAY_SIZE( L".Services" )];
1246 void *callback_context;
1247 UINT mode;
1248 HINF hinf;
1250 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1252 lstrcpynW( section, cmdline, MAX_PATH );
1254 if (!(s = wcschr( section, ' ' ))) return;
1255 *s++ = 0;
1256 while (*s == ' ') s++;
1257 mode = wcstol( s, NULL, 10 );
1259 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1260 if (!(s = wcschr( s, ' ' ))) return;
1261 while (*s == ' ') s++;
1262 path = s;
1264 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1265 if (hinf == INVALID_HANDLE_VALUE) return;
1267 if (!(GetVersion() & 0x80000000))
1269 INFCONTEXT context;
1271 /* check for <section>.ntx86 (or corresponding name for the current platform)
1272 * and then <section>.nt */
1273 s = section + lstrlenW(section);
1274 lstrcpyW( s, nt_platformW );
1275 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1277 lstrcpyW( s, L".nt" );
1278 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1280 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1283 callback_context = SetupInitDefaultQueueCallback( hwnd );
1284 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1285 SetupDefaultQueueCallbackW, callback_context,
1286 NULL, NULL );
1287 SetupTermDefaultQueueCallback( callback_context );
1288 lstrcatW( section, L".Services" );
1289 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1290 SetupCloseInfFile( hinf );
1292 /* FIXME: should check the mode and maybe reboot */
1293 /* there isn't much point in doing that since we */
1294 /* don't yet handle deferred file copies anyway. */
1295 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1299 /***********************************************************************
1300 * InstallHinfSectionA (SETUPAPI.@)
1302 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1304 UNICODE_STRING cmdlineW;
1306 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1308 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1309 RtlFreeUnicodeString( &cmdlineW );
1314 /***********************************************************************
1315 * add_service
1317 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1319 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1321 struct registry_callback_info info;
1322 SC_HANDLE service;
1323 INFCONTEXT context;
1324 SERVICE_DESCRIPTIONW descr;
1325 WCHAR *display_name, *start_name, *load_order, *binary_path;
1326 INT service_type = 0, start_type = 0, error_control = 0;
1327 DWORD size;
1328 HKEY hkey;
1330 /* first the mandatory fields */
1332 if (!SetupFindFirstLineW( hinf, section, L"ServiceType", &context ) ||
1333 !SetupGetIntField( &context, 1, &service_type ))
1335 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1336 return FALSE;
1338 if (!SetupFindFirstLineW( hinf, section, L"StartType", &context ) ||
1339 !SetupGetIntField( &context, 1, &start_type ))
1341 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1342 return FALSE;
1344 if (!SetupFindFirstLineW( hinf, section, L"ErrorControl", &context ) ||
1345 !SetupGetIntField( &context, 1, &error_control ))
1347 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1348 return FALSE;
1350 if (!(binary_path = dup_section_line_field( hinf, section, L"ServiceBinary", 1 )))
1352 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1353 return FALSE;
1356 /* now the optional fields */
1358 display_name = dup_section_line_field( hinf, section, L"DisplayName", 1 );
1359 start_name = dup_section_line_field( hinf, section, L"StartName", 1 );
1360 load_order = dup_section_line_field( hinf, section, L"LoadOrderGroup", 1 );
1361 descr.lpDescription = dup_section_line_field( hinf, section, L"Description", 1 );
1363 /* FIXME: Dependencies field */
1364 /* FIXME: Security field */
1366 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %lx\n",
1367 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1368 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1370 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1371 service_type, start_type, error_control, binary_path,
1372 load_order, NULL, NULL, start_name, NULL );
1373 if (service)
1375 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1377 else
1379 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1380 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1381 if (!service) goto done;
1383 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1384 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1386 QUERY_SERVICE_CONFIGW *config = NULL;
1388 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1389 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1390 config = malloc( size );
1391 if (config && QueryServiceConfigW( service, config, size, &size ))
1393 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1394 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1395 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1397 free( display_name );
1398 display_name = wcsdup( config->lpDisplayName );
1400 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1402 free( load_order );
1403 load_order = wcsdup( config->lpLoadOrderGroup );
1406 free( config );
1408 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1409 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1410 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1412 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1413 load_order, NULL, NULL, start_name, NULL, display_name );
1415 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1416 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1419 /* execute the AddReg, DelReg and BitReg entries */
1421 info.default_root = 0;
1422 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", &hkey ))
1424 RegOpenKeyW( hkey, name, &info.default_root );
1425 RegCloseKey( hkey );
1427 if (info.default_root)
1429 info.delete = TRUE;
1430 iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info );
1431 info.delete = FALSE;
1432 iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info );
1433 RegCloseKey( info.default_root );
1435 iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL );
1437 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1438 CloseServiceHandle( service );
1440 done:
1441 if (!service) WARN( "failed err %lu\n", GetLastError() );
1442 free( binary_path );
1443 free( display_name );
1444 free( start_name );
1445 free( load_order );
1446 free( descr.lpDescription );
1447 return service != 0;
1451 /***********************************************************************
1452 * del_service
1454 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1456 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1458 BOOL ret;
1459 SC_HANDLE service;
1460 SERVICE_STATUS status;
1462 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1464 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1465 WARN( "cannot open %s err %lu\n", debugstr_w(name), GetLastError() );
1466 return FALSE;
1468 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1469 TRACE( "deleting %s\n", debugstr_w(name) );
1470 ret = DeleteService( service );
1471 CloseServiceHandle( service );
1472 return ret;
1476 /***********************************************************************
1477 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1479 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1481 WCHAR service_name[MAX_INF_STRING_LENGTH];
1482 WCHAR service_section[MAX_INF_STRING_LENGTH];
1483 SC_HANDLE scm;
1484 INFCONTEXT context;
1485 INT section_flags;
1486 BOOL ret = TRUE;
1488 if (!SetupFindFirstLineW( hinf, section, NULL, &context ))
1490 SetLastError( ERROR_SECTION_NOT_FOUND );
1491 return FALSE;
1493 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1495 if (SetupFindFirstLineW( hinf, section, L"AddService", &context ))
1499 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1500 continue;
1501 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1502 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1503 continue;
1504 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1505 goto done;
1506 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1509 if (SetupFindFirstLineW( hinf, section, L"DelService", &context ))
1513 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1514 continue;
1515 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1516 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1517 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1519 if (ret) SetLastError( ERROR_SUCCESS );
1520 done:
1521 CloseServiceHandle( scm );
1522 return ret;
1526 /***********************************************************************
1527 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1529 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1531 UNICODE_STRING SectionNameW;
1532 BOOL ret = FALSE;
1534 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1536 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1537 RtlFreeUnicodeString( &SectionNameW );
1539 else
1540 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1542 return ret;
1546 /***********************************************************************
1547 * SetupGetInfFileListA (SETUPAPI.@)
1549 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1550 DWORD insize, PDWORD outsize)
1552 UNICODE_STRING dirW;
1553 PWSTR bufferW = NULL;
1554 BOOL ret = FALSE;
1555 DWORD outsizeA, outsizeW;
1557 if ( dir )
1558 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1559 else
1560 dirW.Buffer = NULL;
1562 if ( buffer )
1563 bufferW = malloc( insize * sizeof( WCHAR ));
1565 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1567 if ( ret )
1569 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1570 buffer, insize, NULL, NULL);
1571 if ( outsize ) *outsize = outsizeA;
1574 free( bufferW );
1575 RtlFreeUnicodeString( &dirW );
1576 return ret;
1580 /***********************************************************************
1581 * SetupGetInfFileListW (SETUPAPI.@)
1583 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1584 DWORD insize, PDWORD outsize)
1586 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1587 DWORD dir_len, name_len = 20, size ;
1588 WIN32_FIND_DATAW finddata;
1589 HANDLE hdl;
1590 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1591 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1593 FIXME( "unknown inf_style(s) 0x%lx\n",
1594 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1595 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1596 if( outsize ) *outsize = 1;
1597 return TRUE;
1599 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1601 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1602 if( outsize ) *outsize = 1;
1603 return TRUE;
1605 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1606 FIXME("ignored inf_style(s) %s %s\n",
1607 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1608 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1609 if( dir )
1611 DWORD att;
1612 DWORD msize;
1613 dir_len = lstrlenW( dir );
1614 if ( !dir_len ) return FALSE;
1615 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1616 filter = malloc( msize );
1617 if( !filter )
1619 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1620 return FALSE;
1622 lstrcpyW( filter, dir );
1623 if ( '\\' == filter[dir_len - 1] )
1624 filter[--dir_len] = 0;
1626 att = GetFileAttributesW( filter );
1627 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1629 free( filter );
1630 SetLastError( ERROR_DIRECTORY );
1631 return FALSE;
1634 else
1636 DWORD msize;
1637 dir_len = GetWindowsDirectoryW( NULL, 0 );
1638 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1639 filter = malloc( msize );
1640 if( !filter )
1642 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1643 return FALSE;
1645 GetWindowsDirectoryW( filter, msize );
1646 lstrcatW( filter, L"\\inf" );
1648 lstrcatW( filter, L"\\*.inf" );
1650 hdl = FindFirstFileW( filter , &finddata );
1651 if ( hdl == INVALID_HANDLE_VALUE )
1653 if( outsize ) *outsize = 1;
1654 free( filter );
1655 return TRUE;
1657 size = 1;
1660 WCHAR signature[ MAX_PATH ];
1661 BOOL valid = FALSE;
1662 DWORD len = lstrlenW( finddata.cFileName );
1663 if (!fullname || ( name_len < len ))
1665 name_len = ( name_len < len ) ? len : name_len;
1666 free( fullname );
1667 fullname = malloc( (2 + dir_len + name_len) * sizeof( WCHAR ) );
1668 if( !fullname )
1670 FindClose( hdl );
1671 free( filter );
1672 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1673 return FALSE;
1675 lstrcpyW( fullname, filter );
1677 fullname[ dir_len + 1] = 0; /* keep '\\' */
1678 lstrcatW( fullname, finddata.cFileName );
1679 if (!GetPrivateProfileStringW( L"Version", L"Signature", NULL, signature, MAX_PATH, fullname ))
1680 signature[0] = 0;
1681 if( INF_STYLE_OLDNT & style )
1682 valid = wcsicmp( L"$Chicago$", signature ) && wcsicmp( L"$WINDOWS NT$", signature );
1683 if( INF_STYLE_WIN4 & style )
1684 valid = valid || !wcsicmp( L"$Chicago$", signature ) ||
1685 !wcsicmp( L"$WINDOWS NT$", signature );
1686 if( valid )
1688 size += 1 + lstrlenW( finddata.cFileName );
1689 if( ptr && insize >= size )
1691 lstrcpyW( ptr, finddata.cFileName );
1692 ptr += 1 + lstrlenW( finddata.cFileName );
1693 *ptr = 0;
1697 while( FindNextFileW( hdl, &finddata ));
1698 FindClose( hdl );
1700 free( fullname );
1701 free( filter );
1702 if( outsize ) *outsize = size;
1703 return TRUE;