wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / setupapi / install.c
blob23f0fbdd4f14dbcd6066912ab3c8bebb2eeaffd0
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>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "winerror.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "winsvc.h"
34 #include "shlobj.h"
35 #include "shlwapi.h"
36 #include "objidl.h"
37 #include "objbase.h"
38 #include "setupapi.h"
39 #include "setupapi_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
44 /* info passed to callback functions dealing with files */
45 struct files_callback_info
47 HSPFILEQ queue;
48 PCWSTR src_root;
49 UINT copy_flags;
50 HINF layout;
53 /* info passed to callback functions dealing with the registry */
54 struct registry_callback_info
56 HKEY default_root;
57 BOOL delete;
60 /* info passed to callback functions dealing with registering dlls */
61 struct register_dll_info
63 PSP_FILE_CALLBACK_W callback;
64 PVOID callback_context;
65 BOOL unregister;
66 int modules_size;
67 int modules_count;
68 HMODULE *modules;
71 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
74 /***********************************************************************
75 * get_field_string
77 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
79 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
80 WCHAR *static_buffer, DWORD *size )
82 DWORD required;
84 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
85 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
87 /* now grow the buffer */
88 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
89 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
90 *size = required;
91 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
93 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
94 return NULL;
98 /***********************************************************************
99 * dup_section_line_field
101 * Retrieve the contents of a field in a newly-allocated buffer.
103 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
105 INFCONTEXT context;
106 DWORD size;
107 WCHAR *buffer;
109 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
110 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
111 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
112 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
113 return buffer;
116 static void get_inf_src_path( HINF hinf, WCHAR *path )
118 const WCHAR *inf_path = PARSER_get_inf_filename( hinf );
119 WCHAR pnf_path[MAX_PATH];
120 FILE *pnf;
122 wcscpy( pnf_path, inf_path );
123 PathRemoveExtensionW( pnf_path );
124 PathAddExtensionW( pnf_path, L".pnf" );
125 if ((pnf = _wfopen( pnf_path, L"r" )))
127 if (fgetws( path, MAX_PATH, pnf ) && !wcscmp( path, PNF_HEADER ))
129 fgetws( path, MAX_PATH, pnf );
130 TRACE("using original source path %s\n", debugstr_w(path));
131 fclose( pnf );
132 return;
134 fclose( pnf );
136 wcscpy( path, inf_path );
139 /***********************************************************************
140 * copy_files_callback
142 * Called once for each CopyFiles entry in a given section.
144 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
146 INFCONTEXT context;
147 struct files_callback_info *info = arg;
148 WCHAR src_root[MAX_PATH], *p;
150 if (!info->src_root)
152 const WCHAR *build_dir = _wgetenv( L"WINEBUILDDIR" );
153 const WCHAR *data_dir = _wgetenv( L"WINEDATADIR" );
155 if ((build_dir || data_dir) && SetupFindFirstLineW( hinf, L"WineSourceDirs", field, &context ))
157 lstrcpyW( src_root, build_dir ? build_dir : data_dir );
158 src_root[1] = '\\'; /* change \??\ to \\?\ */
159 p = src_root + lstrlenW(src_root);
160 *p++ = '\\';
161 if (!build_dir || !SetupGetStringFieldW( &context, 2, p, MAX_PATH - (p - src_root), NULL ))
163 if (!SetupGetStringFieldW( &context, 1, p, MAX_PATH - (p - src_root), NULL )) p[-1] = 0;
166 else
168 get_inf_src_path( hinf, src_root );
169 if ((p = wcsrchr( src_root, '\\' ))) *p = 0;
173 if (field[0] == '@') /* special case: copy single file */
174 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf,
175 info->src_root ? info->src_root : src_root, field+1, field+1, info->copy_flags );
176 else
177 SetupQueueCopySectionW( info->queue, info->src_root ? info->src_root : src_root,
178 info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
179 return TRUE;
183 /***********************************************************************
184 * delete_files_callback
186 * Called once for each DelFiles entry in a given section.
188 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
190 struct files_callback_info *info = arg;
191 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
192 return TRUE;
196 /***********************************************************************
197 * rename_files_callback
199 * Called once for each RenFiles entry in a given section.
201 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
203 struct files_callback_info *info = arg;
204 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
205 return TRUE;
209 /***********************************************************************
210 * get_root_key
212 * Retrieve the registry root key from its name.
214 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
216 if (!wcsicmp( name, L"HKCR" )) return HKEY_CLASSES_ROOT;
217 if (!wcsicmp( name, L"HKCU" )) return HKEY_CURRENT_USER;
218 if (!wcsicmp( name, L"HKLM" )) return HKEY_LOCAL_MACHINE;
219 if (!wcsicmp( name, L"HKU" )) return HKEY_USERS;
220 if (!wcsicmp( name, L"HKR" )) return def_root;
221 return 0;
225 /***********************************************************************
226 * append_multi_sz_value
228 * Append a multisz string to a multisz registry value.
230 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
231 DWORD str_size )
233 DWORD size, type, total;
234 WCHAR *buffer, *p;
236 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
237 if (type != REG_MULTI_SZ) return;
239 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
240 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
242 /* compare each string against all the existing ones */
243 total = size;
244 while (*strings)
246 int len = lstrlenW(strings) + 1;
248 for (p = buffer; *p; p += lstrlenW(p) + 1)
249 if (!wcsicmp( p, strings )) break;
251 if (!*p) /* not found, need to append it */
253 memcpy( p, strings, len * sizeof(WCHAR) );
254 p[len] = 0;
255 total += len * sizeof(WCHAR);
257 strings += len;
259 if (total != size)
261 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
262 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
264 done:
265 HeapFree( GetProcessHeap(), 0, buffer );
269 /***********************************************************************
270 * delete_multi_sz_value
272 * Remove a string from a multisz registry value.
274 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
276 DWORD size, type;
277 WCHAR *buffer, *src, *dst;
279 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
280 if (type != REG_MULTI_SZ) return;
281 /* allocate double the size, one for value before and one for after */
282 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
283 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
284 src = buffer;
285 dst = buffer + size;
286 while (*src)
288 int len = lstrlenW(src) + 1;
289 if (wcsicmp( src, string ))
291 memcpy( dst, src, len * sizeof(WCHAR) );
292 dst += len;
294 src += len;
296 *dst++ = 0;
297 if (dst != buffer + 2*size) /* did we remove something? */
299 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
300 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
301 (BYTE *)(buffer + size), dst - (buffer + size) );
303 done:
304 HeapFree( GetProcessHeap(), 0, buffer );
308 /***********************************************************************
309 * do_reg_operation
311 * Perform an add/delete registry operation depending on the flags.
313 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
315 DWORD type, size;
317 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
319 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
321 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
323 WCHAR *str;
325 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
326 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
327 SetupGetStringFieldW( context, 5, str, size, NULL );
328 delete_multi_sz_value( hkey, value, str );
329 HeapFree( GetProcessHeap(), 0, str );
331 else RegDeleteValueW( hkey, value );
333 else
335 RegDeleteTreeW( hkey, NULL );
336 NtDeleteKey( hkey );
338 return TRUE;
341 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
343 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
345 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
346 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
347 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
350 switch(flags & FLG_ADDREG_TYPE_MASK)
352 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
353 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
354 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
355 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
356 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
357 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
358 default: type = flags >> 16; break;
361 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
362 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
364 WCHAR *str = NULL;
366 if (type == REG_MULTI_SZ)
368 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
369 if (size)
371 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
372 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
374 if (flags & FLG_ADDREG_APPEND)
376 if (!str) return TRUE;
377 append_multi_sz_value( hkey, value, str, size );
378 HeapFree( GetProcessHeap(), 0, str );
379 return TRUE;
381 /* else fall through to normal string handling */
383 else
385 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
386 if (size)
388 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
389 SetupGetStringFieldW( context, 5, str, size, NULL );
390 if (type == REG_LINK) size--; /* no terminating null for symlinks */
394 if (type == REG_DWORD)
396 DWORD dw = str ? wcstoul( str, NULL, 0 ) : 0;
397 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
398 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
400 else
402 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
403 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
404 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)L"", sizeof(WCHAR) );
406 HeapFree( GetProcessHeap(), 0, str );
407 return TRUE;
409 else /* get the binary data */
411 BYTE *data = NULL;
413 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
414 if (size)
416 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
417 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
418 SetupGetBinaryField( context, 5, data, size, NULL );
420 RegSetValueExW( hkey, value, 0, type, data, size );
421 HeapFree( GetProcessHeap(), 0, data );
422 return TRUE;
427 /***********************************************************************
428 * registry_callback
430 * Called once for each AddReg and DelReg entry in a given section.
432 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
434 struct registry_callback_info *info = arg;
435 INFCONTEXT context;
436 HKEY root_key, hkey;
438 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
440 for (; ok; ok = SetupFindNextLine( &context, &context ))
442 DWORD options = 0;
443 WCHAR buffer[MAX_INF_STRING_LENGTH];
444 INT flags;
446 /* get root */
447 if (!SetupGetStringFieldW( &context, 1, buffer, ARRAY_SIZE( buffer ), NULL ))
448 continue;
449 if (!(root_key = get_root_key( buffer, info->default_root )))
450 continue;
452 /* get key */
453 if (!SetupGetStringFieldW( &context, 2, buffer, ARRAY_SIZE( buffer ), NULL ))
454 *buffer = 0;
456 /* get flags */
457 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
459 if (!info->delete)
461 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
463 else
465 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
466 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
468 /* Wine extension: magic support for symlinks */
469 if (flags >> 16 == REG_LINK) options = REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK;
471 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
473 if (RegOpenKeyExW( root_key, buffer, options, MAXIMUM_ALLOWED, &hkey ))
474 continue; /* ignore if it doesn't exist */
476 else
478 DWORD res = RegCreateKeyExW( root_key, buffer, 0, NULL, options,
479 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
480 if (res == ERROR_ALREADY_EXISTS && (options & REG_OPTION_CREATE_LINK))
481 res = RegCreateKeyExW( root_key, buffer, 0, NULL, REG_OPTION_OPEN_LINK,
482 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
483 if (res)
485 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
486 continue;
489 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
491 /* get value name */
492 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
493 *buffer = 0;
495 /* and now do it */
496 if (!do_reg_operation( hkey, buffer, &context, flags ))
498 RegCloseKey( hkey );
499 return FALSE;
501 RegCloseKey( hkey );
503 return TRUE;
507 /***********************************************************************
508 * do_register_dll
510 * Register or unregister a dll.
512 static BOOL do_register_dll( struct register_dll_info *info, const WCHAR *path,
513 INT flags, INT timeout, const WCHAR *args )
515 HMODULE module;
516 HRESULT res;
517 SP_REGISTER_CONTROL_STATUSW status;
518 IMAGE_NT_HEADERS *nt;
520 status.cbSize = sizeof(status);
521 status.FileName = path;
522 status.FailureCode = SPREG_SUCCESS;
523 status.Win32Error = ERROR_SUCCESS;
525 if (info->callback)
527 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
528 (UINT_PTR)&status, !info->unregister ))
530 case FILEOP_ABORT:
531 SetLastError( ERROR_OPERATION_ABORTED );
532 return FALSE;
533 case FILEOP_SKIP:
534 return TRUE;
535 case FILEOP_DOIT:
536 break;
540 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
542 WARN( "could not load %s\n", debugstr_w(path) );
543 status.FailureCode = SPREG_LOADLIBRARY;
544 status.Win32Error = GetLastError();
545 goto done;
548 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
550 /* file is an executable, not a dll */
551 STARTUPINFOW startup;
552 PROCESS_INFORMATION process_info;
553 WCHAR *cmd_line;
554 BOOL res;
555 DWORD len;
557 FreeLibrary( module );
558 module = NULL;
559 if (!args) args = L"/RegServer";
560 len = lstrlenW(path) + lstrlenW(args) + 4;
561 cmd_line = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
562 swprintf( cmd_line, len, L"\"%s\" %s", path, args );
563 memset( &startup, 0, sizeof(startup) );
564 startup.cb = sizeof(startup);
565 TRACE( "executing %s\n", debugstr_w(cmd_line) );
566 res = CreateProcessW( path, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &process_info );
567 HeapFree( GetProcessHeap(), 0, cmd_line );
568 if (!res)
570 status.FailureCode = SPREG_LOADLIBRARY;
571 status.Win32Error = GetLastError();
572 goto done;
574 CloseHandle( process_info.hThread );
576 if (WaitForSingleObject( process_info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
578 /* timed out, kill the process */
579 TerminateProcess( process_info.hProcess, 1 );
580 status.FailureCode = SPREG_TIMEOUT;
581 status.Win32Error = ERROR_TIMEOUT;
583 CloseHandle( process_info.hProcess );
584 goto done;
587 if (flags & FLG_REGSVR_DLLREGISTER)
589 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
590 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
592 if (!func)
594 status.FailureCode = SPREG_GETPROCADDR;
595 status.Win32Error = GetLastError();
596 goto done;
599 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
600 res = func();
602 if (FAILED(res))
604 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
605 status.FailureCode = SPREG_REGSVR;
606 status.Win32Error = res;
607 goto done;
611 if (flags & FLG_REGSVR_DLLINSTALL)
613 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
615 if (!func)
617 status.FailureCode = SPREG_GETPROCADDR;
618 status.Win32Error = GetLastError();
619 goto done;
622 TRACE( "calling DllInstall(%d,%s) in %s\n",
623 !info->unregister, debugstr_w(args), debugstr_w(path) );
624 res = func( !info->unregister, args );
626 if (FAILED(res))
628 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
629 status.FailureCode = SPREG_REGSVR;
630 status.Win32Error = res;
631 goto done;
635 done:
636 if (module)
638 if (info->modules_count >= info->modules_size)
640 int new_size = max( 32, info->modules_size * 2 );
641 HMODULE *new = info->modules ?
642 HeapReAlloc( GetProcessHeap(), 0, info->modules, new_size * sizeof(*new) ) :
643 HeapAlloc( GetProcessHeap(), 0, new_size * sizeof(*new) );
644 if (new)
646 info->modules_size = new_size;
647 info->modules = new;
650 if (info->modules_count < info->modules_size) info->modules[info->modules_count++] = module;
651 else FreeLibrary( module );
653 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
654 (UINT_PTR)&status, !info->unregister );
655 return TRUE;
659 /***********************************************************************
660 * register_dlls_callback
662 * Called once for each RegisterDlls entry in a given section.
664 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
666 struct register_dll_info *info = arg;
667 INFCONTEXT context;
668 BOOL ret = TRUE;
669 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
671 for (; ok; ok = SetupFindNextLine( &context, &context ))
673 WCHAR *path, *args, *p;
674 WCHAR buffer[MAX_INF_STRING_LENGTH];
675 INT flags, timeout;
677 /* get directory */
678 if (!(path = PARSER_get_dest_dir( &context ))) continue;
680 /* get dll name */
681 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
682 goto done;
683 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
684 (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
685 path = p;
686 p += lstrlenW(p);
687 if (p == path || p[-1] != '\\') *p++ = '\\';
688 lstrcpyW( p, buffer );
690 /* get flags */
691 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
693 /* get timeout */
694 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
696 /* get command line */
697 args = NULL;
698 if (SetupGetStringFieldW( &context, 6, buffer, ARRAY_SIZE( buffer ), NULL ))
699 args = buffer;
701 ret = do_register_dll( info, path, flags, timeout, args );
703 done:
704 HeapFree( GetProcessHeap(), 0, path );
705 if (!ret) break;
707 return ret;
710 /***********************************************************************
711 * fake_dlls_callback
713 * Called once for each WineFakeDlls entry in a given section.
715 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
717 INFCONTEXT context;
718 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
720 for (; ok; ok = SetupFindNextLine( &context, &context ))
722 WCHAR *path, *p;
723 WCHAR buffer[MAX_INF_STRING_LENGTH];
725 /* get directory */
726 if (!(path = PARSER_get_dest_dir( &context ))) continue;
728 /* get dll name */
729 if (!SetupGetStringFieldW( &context, 3, buffer, ARRAY_SIZE( buffer ), NULL ))
730 goto done;
731 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
732 (lstrlenW(path) + lstrlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
733 path = p;
734 p += lstrlenW(p);
735 if (p == path || p[-1] != '\\') *p++ = '\\';
736 lstrcpyW( p, buffer );
738 /* get source dll */
739 if (SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
740 p = buffer; /* otherwise use target base name as default source */
742 create_fake_dll( path, p ); /* ignore errors */
744 done:
745 HeapFree( GetProcessHeap(), 0, path );
747 return TRUE;
750 /***********************************************************************
751 * update_ini_callback
753 * Called once for each UpdateInis entry in a given section.
755 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
757 INFCONTEXT context;
759 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
761 for (; ok; ok = SetupFindNextLine( &context, &context ))
763 WCHAR buffer[MAX_INF_STRING_LENGTH];
764 WCHAR filename[MAX_INF_STRING_LENGTH];
765 WCHAR section[MAX_INF_STRING_LENGTH];
766 WCHAR entry[MAX_INF_STRING_LENGTH];
767 WCHAR string[MAX_INF_STRING_LENGTH];
768 LPWSTR divider;
770 if (!SetupGetStringFieldW( &context, 1, filename, ARRAY_SIZE( filename ), NULL ))
771 continue;
773 if (!SetupGetStringFieldW( &context, 2, section, ARRAY_SIZE( section ), NULL ))
774 continue;
776 if (!SetupGetStringFieldW( &context, 4, buffer, ARRAY_SIZE( buffer ), NULL ))
777 continue;
779 divider = wcschr(buffer,'=');
780 if (divider)
782 *divider = 0;
783 lstrcpyW(entry,buffer);
784 divider++;
785 lstrcpyW(string,divider);
787 else
789 lstrcpyW(entry,buffer);
790 string[0]=0;
793 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
794 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
795 WritePrivateProfileStringW(section,entry,string,filename);
798 return TRUE;
801 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
803 FIXME( "should update ini fields %s\n", debugstr_w(field) );
804 return TRUE;
807 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
809 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
810 return TRUE;
813 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
815 FIXME( "should do logconf %s\n", debugstr_w(field) );
816 return TRUE;
819 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
821 FIXME( "should do bitreg %s\n", debugstr_w(field) );
822 return TRUE;
825 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
827 WCHAR lnkpath[MAX_PATH];
828 LPWSTR cmdline=NULL, lnkpath_end;
829 unsigned int name_size;
830 INFCONTEXT name_context, context;
831 int attrs=0;
833 TRACE( "(%s)\n", debugstr_w(field) );
835 if (SetupFindFirstLineW( hinf, field, L"Name", &name_context ))
837 SetupGetIntField( &name_context, 2, &attrs );
838 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
840 else return TRUE;
842 /* calculate filename */
843 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
844 lnkpath_end = lnkpath + lstrlenW(lnkpath);
845 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
847 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, L"SubDir", &context ))
849 unsigned int subdir_size;
851 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
852 return TRUE;
854 lnkpath_end += subdir_size - 1;
855 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
858 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
859 return TRUE;
861 lnkpath_end += name_size - 1;
863 if (attrs & FLG_PROFITEM_GROUP)
865 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
867 else
869 IShellLinkW* shelllink=NULL;
870 IPersistFile* persistfile=NULL;
871 HRESULT initresult=E_FAIL;
873 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
874 lstrcpyW( lnkpath_end, L".lnk" );
876 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
878 /* calculate command line */
879 if (SetupFindFirstLineW( hinf, field, L"CmdLine", &context ))
881 unsigned int dir_len=0, subdir_size=0, filename_size=0;
882 int dirid=0;
883 LPCWSTR dir;
884 LPWSTR cmdline_end;
886 SetupGetIntField( &context, 1, &dirid );
887 dir = DIRID_get_string( dirid );
889 if (dir) dir_len = lstrlenW(dir);
891 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
892 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
894 if (dir_len && filename_size)
896 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
898 lstrcpyW( cmdline_end, dir );
899 cmdline_end += dir_len;
900 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
902 if (subdir_size)
904 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
905 cmdline_end += subdir_size-1;
906 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
908 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
909 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
913 if (!cmdline) return TRUE;
915 initresult = CoInitialize(NULL);
917 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
918 &IID_IShellLinkW, (LPVOID*)&shelllink )))
919 goto done;
921 IShellLinkW_SetPath( shelllink, cmdline );
922 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
923 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
925 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
926 IPersistFile_Save( persistfile, lnkpath, FALSE );
927 IPersistFile_Release( persistfile );
929 IShellLinkW_Release( shelllink );
931 done:
932 if (SUCCEEDED(initresult)) CoUninitialize();
933 HeapFree( GetProcessHeap(), 0, cmdline );
936 return TRUE;
939 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
941 FIXME( "should do copy inf %s\n", debugstr_w(field) );
942 return TRUE;
946 /***********************************************************************
947 * iterate_section_fields
949 * Iterate over all fields of a certain key of a certain section
951 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
952 iterate_fields_func callback, void *arg )
954 WCHAR static_buffer[200];
955 WCHAR *buffer = static_buffer;
956 DWORD size = ARRAY_SIZE( static_buffer );
957 INFCONTEXT context;
958 BOOL ret = FALSE;
960 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
961 while (ok)
963 UINT i, count = SetupGetFieldCount( &context );
964 for (i = 1; i <= count; i++)
966 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
967 goto done;
968 if (!callback( hinf, buffer, arg ))
970 WARN("callback failed for %s %s err %d\n",
971 debugstr_w(section), debugstr_w(buffer), GetLastError() );
972 goto done;
975 ok = SetupFindNextMatchLineW( &context, key, &context );
977 ret = TRUE;
978 done:
979 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
980 return ret;
984 /***********************************************************************
985 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
987 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
988 PCSTR section, PCSTR src_root, UINT flags )
990 UNICODE_STRING sectionW;
991 BOOL ret = FALSE;
993 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
995 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
996 return FALSE;
998 if (!src_root)
999 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1000 NULL, flags );
1001 else
1003 UNICODE_STRING srcW;
1004 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
1006 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1007 srcW.Buffer, flags );
1008 RtlFreeUnicodeString( &srcW );
1010 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1012 RtlFreeUnicodeString( &sectionW );
1013 return ret;
1017 /***********************************************************************
1018 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1020 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1021 PCWSTR section, PCWSTR src_root, UINT flags )
1023 struct files_callback_info info;
1025 info.queue = queue;
1026 info.src_root = src_root;
1027 info.copy_flags = flags;
1028 info.layout = hlayout;
1029 return iterate_section_fields( hinf, section, L"CopyFiles", copy_files_callback, &info );
1033 /***********************************************************************
1034 * SetupInstallFromInfSectionA (SETUPAPI.@)
1036 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1037 HKEY key_root, PCSTR src_root, UINT copy_flags,
1038 PSP_FILE_CALLBACK_A callback, PVOID context,
1039 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1041 UNICODE_STRING sectionW, src_rootW;
1042 struct callback_WtoA_context ctx;
1043 BOOL ret = FALSE;
1045 src_rootW.Buffer = NULL;
1046 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1048 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1049 return FALSE;
1052 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1054 ctx.orig_context = context;
1055 ctx.orig_handler = callback;
1056 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1057 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1058 &ctx, devinfo, devinfo_data );
1059 RtlFreeUnicodeString( &sectionW );
1061 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1063 RtlFreeUnicodeString( &src_rootW );
1064 return ret;
1068 /***********************************************************************
1069 * SetupInstallFromInfSectionW (SETUPAPI.@)
1071 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1072 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1073 PSP_FILE_CALLBACK_W callback, PVOID context,
1074 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1076 BOOL ret;
1077 int i;
1079 if (flags & SPINST_REGISTRY)
1081 struct registry_callback_info info;
1083 info.default_root = key_root;
1084 info.delete = FALSE;
1085 if (!iterate_section_fields( hinf, section, L"WinePreInstall", registry_callback, &info ))
1086 return FALSE;
1088 if (flags & SPINST_REGSVR)
1090 if (iterate_section_fields( hinf, section, L"WineFakeDlls", fake_dlls_callback, NULL ))
1091 cleanup_fake_dlls();
1092 else
1093 return FALSE;
1095 if (flags & SPINST_FILES)
1097 struct files_callback_info info;
1098 HSPFILEQ queue;
1100 if (!(queue = SetupOpenFileQueue())) return FALSE;
1101 info.queue = queue;
1102 info.src_root = src_root;
1103 info.copy_flags = copy_flags;
1104 info.layout = hinf;
1105 ret = (iterate_section_fields( hinf, section, L"CopyFiles", copy_files_callback, &info ) &&
1106 iterate_section_fields( hinf, section, L"DelFiles", delete_files_callback, &info ) &&
1107 iterate_section_fields( hinf, section, L"RenFiles", rename_files_callback, &info ) &&
1108 SetupCommitFileQueueW( owner, queue, callback, context ));
1109 SetupCloseFileQueue( queue );
1110 if (!ret) return FALSE;
1112 if (flags & SPINST_INIFILES)
1114 if (!iterate_section_fields( hinf, section, L"UpdateInis", update_ini_callback, NULL ) ||
1115 !iterate_section_fields( hinf, section, L"UpdateIniFields",
1116 update_ini_fields_callback, NULL ))
1117 return FALSE;
1119 if (flags & SPINST_INI2REG)
1121 if (!iterate_section_fields( hinf, section, L"Ini2Reg", ini2reg_callback, NULL ))
1122 return FALSE;
1124 if (flags & SPINST_LOGCONFIG)
1126 if (!iterate_section_fields( hinf, section, L"LogConf", logconf_callback, NULL ))
1127 return FALSE;
1129 if (flags & SPINST_REGSVR)
1131 struct register_dll_info info = { .unregister = FALSE };
1132 HRESULT hr;
1134 if (flags & SPINST_REGISTERCALLBACKAWARE)
1136 info.callback = callback;
1137 info.callback_context = context;
1140 hr = CoInitialize(NULL);
1142 ret = iterate_section_fields( hinf, section, L"RegisterDlls", register_dlls_callback, &info );
1143 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1145 if (SUCCEEDED(hr))
1146 CoUninitialize();
1148 HeapFree( GetProcessHeap(), 0, info.modules );
1149 if (!ret) return FALSE;
1151 if (flags & SPINST_UNREGSVR)
1153 struct register_dll_info info = { .unregister = TRUE };
1154 HRESULT hr;
1156 if (flags & SPINST_REGISTERCALLBACKAWARE)
1158 info.callback = callback;
1159 info.callback_context = context;
1162 hr = CoInitialize(NULL);
1164 ret = iterate_section_fields( hinf, section, L"UnregisterDlls", register_dlls_callback, &info );
1165 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1167 if (SUCCEEDED(hr))
1168 CoUninitialize();
1170 HeapFree( GetProcessHeap(), 0, info.modules );
1171 if (!ret) return FALSE;
1173 if (flags & SPINST_REGISTRY)
1175 struct registry_callback_info info;
1177 info.default_root = key_root;
1178 info.delete = TRUE;
1179 if (!iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info ))
1180 return FALSE;
1181 info.delete = FALSE;
1182 if (!iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info ))
1183 return FALSE;
1185 if (flags & SPINST_BITREG)
1187 if (!iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL ))
1188 return FALSE;
1190 if (flags & SPINST_PROFILEITEMS)
1192 if (!iterate_section_fields( hinf, section, L"ProfileItems", profile_items_callback, NULL ))
1193 return FALSE;
1195 if (flags & SPINST_COPYINF)
1197 if (!iterate_section_fields( hinf, section, L"CopyINF", copy_inf_callback, NULL ))
1198 return FALSE;
1201 SetLastError(ERROR_SUCCESS);
1202 return TRUE;
1206 /***********************************************************************
1207 * InstallHinfSectionW (SETUPAPI.@)
1209 * NOTE: 'cmdline' is <section> <mode> <path> from
1210 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1212 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1214 #ifdef __i386__
1215 static const WCHAR nt_platformW[] = L".ntx86";
1216 #elif defined(__x86_64__)
1217 static const WCHAR nt_platformW[] = L".ntamd64";
1218 #elif defined(__arm__)
1219 static const WCHAR nt_platformW[] = L".ntarm";
1220 #elif defined(__aarch64__)
1221 static const WCHAR nt_platformW[] = L".ntarm64";
1222 #else /* FIXME: other platforms */
1223 static const WCHAR nt_platformW[] = L".nt";
1224 #endif
1226 WCHAR *s, *path, section[MAX_PATH + ARRAY_SIZE( nt_platformW ) + ARRAY_SIZE( L".Services" )];
1227 void *callback_context;
1228 UINT mode;
1229 HINF hinf;
1231 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1233 lstrcpynW( section, cmdline, MAX_PATH );
1235 if (!(s = wcschr( section, ' ' ))) return;
1236 *s++ = 0;
1237 while (*s == ' ') s++;
1238 mode = wcstol( s, NULL, 10 );
1240 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1241 if (!(s = wcschr( s, ' ' ))) return;
1242 while (*s == ' ') s++;
1243 path = s;
1245 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1246 if (hinf == INVALID_HANDLE_VALUE) return;
1248 if (!(GetVersion() & 0x80000000))
1250 INFCONTEXT context;
1252 /* check for <section>.ntx86 (or corresponding name for the current platform)
1253 * and then <section>.nt */
1254 s = section + lstrlenW(section);
1255 lstrcpyW( s, nt_platformW );
1256 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1258 lstrcpyW( s, L".nt" );
1259 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1261 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1264 callback_context = SetupInitDefaultQueueCallback( hwnd );
1265 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1266 SetupDefaultQueueCallbackW, callback_context,
1267 NULL, NULL );
1268 SetupTermDefaultQueueCallback( callback_context );
1269 lstrcatW( section, L".Services" );
1270 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1271 SetupCloseInfFile( hinf );
1273 /* FIXME: should check the mode and maybe reboot */
1274 /* there isn't much point in doing that since we */
1275 /* don't yet handle deferred file copies anyway. */
1276 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1280 /***********************************************************************
1281 * InstallHinfSectionA (SETUPAPI.@)
1283 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1285 UNICODE_STRING cmdlineW;
1287 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1289 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1290 RtlFreeUnicodeString( &cmdlineW );
1295 /***********************************************************************
1296 * add_service
1298 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1300 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1302 struct registry_callback_info info;
1303 SC_HANDLE service;
1304 INFCONTEXT context;
1305 SERVICE_DESCRIPTIONW descr;
1306 WCHAR *display_name, *start_name, *load_order, *binary_path;
1307 INT service_type = 0, start_type = 0, error_control = 0;
1308 DWORD size;
1309 HKEY hkey;
1311 /* first the mandatory fields */
1313 if (!SetupFindFirstLineW( hinf, section, L"ServiceType", &context ) ||
1314 !SetupGetIntField( &context, 1, &service_type ))
1316 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1317 return FALSE;
1319 if (!SetupFindFirstLineW( hinf, section, L"StartType", &context ) ||
1320 !SetupGetIntField( &context, 1, &start_type ))
1322 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1323 return FALSE;
1325 if (!SetupFindFirstLineW( hinf, section, L"ErrorControl", &context ) ||
1326 !SetupGetIntField( &context, 1, &error_control ))
1328 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1329 return FALSE;
1331 if (!(binary_path = dup_section_line_field( hinf, section, L"ServiceBinary", 1 )))
1333 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1334 return FALSE;
1337 /* now the optional fields */
1339 display_name = dup_section_line_field( hinf, section, L"DisplayName", 1 );
1340 start_name = dup_section_line_field( hinf, section, L"StartName", 1 );
1341 load_order = dup_section_line_field( hinf, section, L"LoadOrderGroup", 1 );
1342 descr.lpDescription = dup_section_line_field( hinf, section, L"Description", 1 );
1344 /* FIXME: Dependencies field */
1345 /* FIXME: Security field */
1347 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1348 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1349 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1351 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1352 service_type, start_type, error_control, binary_path,
1353 load_order, NULL, NULL, start_name, NULL );
1354 if (service)
1356 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1358 else
1360 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1361 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1362 if (!service) goto done;
1364 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1365 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1367 QUERY_SERVICE_CONFIGW *config = NULL;
1369 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1370 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1371 config = HeapAlloc( GetProcessHeap(), 0, size );
1372 if (config && QueryServiceConfigW( service, config, size, &size ))
1374 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1375 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1376 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1378 HeapFree( GetProcessHeap(), 0, display_name );
1379 display_name = strdupW( config->lpDisplayName );
1381 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1383 HeapFree( GetProcessHeap(), 0, load_order );
1384 load_order = strdupW( config->lpLoadOrderGroup );
1387 HeapFree( GetProcessHeap(), 0, config );
1389 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1390 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1391 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1393 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1394 load_order, NULL, NULL, start_name, NULL, display_name );
1396 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1397 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1400 /* execute the AddReg, DelReg and BitReg entries */
1402 info.default_root = 0;
1403 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services", &hkey ))
1405 RegOpenKeyW( hkey, name, &info.default_root );
1406 RegCloseKey( hkey );
1408 if (info.default_root)
1410 info.delete = TRUE;
1411 iterate_section_fields( hinf, section, L"DelReg", registry_callback, &info );
1412 info.delete = FALSE;
1413 iterate_section_fields( hinf, section, L"AddReg", registry_callback, &info );
1414 RegCloseKey( info.default_root );
1416 iterate_section_fields( hinf, section, L"BitReg", bitreg_callback, NULL );
1418 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1419 CloseServiceHandle( service );
1421 done:
1422 if (!service) WARN( "failed err %u\n", GetLastError() );
1423 HeapFree( GetProcessHeap(), 0, binary_path );
1424 HeapFree( GetProcessHeap(), 0, display_name );
1425 HeapFree( GetProcessHeap(), 0, start_name );
1426 HeapFree( GetProcessHeap(), 0, load_order );
1427 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1428 return service != 0;
1432 /***********************************************************************
1433 * del_service
1435 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1437 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1439 BOOL ret;
1440 SC_HANDLE service;
1441 SERVICE_STATUS status;
1443 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1445 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1446 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1447 return FALSE;
1449 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1450 TRACE( "deleting %s\n", debugstr_w(name) );
1451 ret = DeleteService( service );
1452 CloseServiceHandle( service );
1453 return ret;
1457 /***********************************************************************
1458 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1460 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1462 WCHAR service_name[MAX_INF_STRING_LENGTH];
1463 WCHAR service_section[MAX_INF_STRING_LENGTH];
1464 SC_HANDLE scm;
1465 INFCONTEXT context;
1466 INT section_flags;
1467 BOOL ret = TRUE;
1469 if (!SetupFindFirstLineW( hinf, section, NULL, &context ))
1471 SetLastError( ERROR_SECTION_NOT_FOUND );
1472 return FALSE;
1474 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1476 if (SetupFindFirstLineW( hinf, section, L"AddService", &context ))
1480 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1481 continue;
1482 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1483 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1484 continue;
1485 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1486 goto done;
1487 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1490 if (SetupFindFirstLineW( hinf, section, L"DelService", &context ))
1494 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1495 continue;
1496 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1497 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1498 } while (SetupFindNextMatchLineW( &context, L"AddService", &context ));
1500 if (ret) SetLastError( ERROR_SUCCESS );
1501 done:
1502 CloseServiceHandle( scm );
1503 return ret;
1507 /***********************************************************************
1508 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1510 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1512 UNICODE_STRING SectionNameW;
1513 BOOL ret = FALSE;
1515 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1517 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1518 RtlFreeUnicodeString( &SectionNameW );
1520 else
1521 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1523 return ret;
1527 /***********************************************************************
1528 * SetupGetInfFileListA (SETUPAPI.@)
1530 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1531 DWORD insize, PDWORD outsize)
1533 UNICODE_STRING dirW;
1534 PWSTR bufferW = NULL;
1535 BOOL ret = FALSE;
1536 DWORD outsizeA, outsizeW;
1538 if ( dir )
1539 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1540 else
1541 dirW.Buffer = NULL;
1543 if ( buffer )
1544 bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof( WCHAR ));
1546 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1548 if ( ret )
1550 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1551 buffer, insize, NULL, NULL);
1552 if ( outsize ) *outsize = outsizeA;
1555 HeapFree( GetProcessHeap(), 0, bufferW );
1556 RtlFreeUnicodeString( &dirW );
1557 return ret;
1561 /***********************************************************************
1562 * SetupGetInfFileListW (SETUPAPI.@)
1564 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1565 DWORD insize, PDWORD outsize)
1567 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1568 DWORD dir_len, name_len = 20, size ;
1569 WIN32_FIND_DATAW finddata;
1570 HANDLE hdl;
1571 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1572 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1574 FIXME( "unknown inf_style(s) 0x%x\n",
1575 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1576 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1577 if( outsize ) *outsize = 1;
1578 return TRUE;
1580 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1582 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1583 if( outsize ) *outsize = 1;
1584 return TRUE;
1586 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1587 FIXME("ignored inf_style(s) %s %s\n",
1588 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1589 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1590 if( dir )
1592 DWORD att;
1593 DWORD msize;
1594 dir_len = lstrlenW( dir );
1595 if ( !dir_len ) return FALSE;
1596 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1597 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1598 if( !filter )
1600 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1601 return FALSE;
1603 lstrcpyW( filter, dir );
1604 if ( '\\' == filter[dir_len - 1] )
1605 filter[--dir_len] = 0;
1607 att = GetFileAttributesW( filter );
1608 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1610 HeapFree( GetProcessHeap(), 0, filter );
1611 SetLastError( ERROR_DIRECTORY );
1612 return FALSE;
1615 else
1617 DWORD msize;
1618 dir_len = GetWindowsDirectoryW( NULL, 0 );
1619 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1620 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1621 if( !filter )
1623 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1624 return FALSE;
1626 GetWindowsDirectoryW( filter, msize );
1627 lstrcatW( filter, L"\\inf" );
1629 lstrcatW( filter, L"\\*.inf" );
1631 hdl = FindFirstFileW( filter , &finddata );
1632 if ( hdl == INVALID_HANDLE_VALUE )
1634 if( outsize ) *outsize = 1;
1635 HeapFree( GetProcessHeap(), 0, filter );
1636 return TRUE;
1638 size = 1;
1641 WCHAR signature[ MAX_PATH ];
1642 BOOL valid = FALSE;
1643 DWORD len = lstrlenW( finddata.cFileName );
1644 if (!fullname || ( name_len < len ))
1646 name_len = ( name_len < len ) ? len : name_len;
1647 HeapFree( GetProcessHeap(), 0, fullname );
1648 fullname = HeapAlloc( GetProcessHeap(), 0,
1649 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1650 if( !fullname )
1652 FindClose( hdl );
1653 HeapFree( GetProcessHeap(), 0, filter );
1654 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1655 return FALSE;
1657 lstrcpyW( fullname, filter );
1659 fullname[ dir_len + 1] = 0; /* keep '\\' */
1660 lstrcatW( fullname, finddata.cFileName );
1661 if (!GetPrivateProfileStringW( L"Version", L"Signature", NULL, signature, MAX_PATH, fullname ))
1662 signature[0] = 0;
1663 if( INF_STYLE_OLDNT & style )
1664 valid = wcsicmp( L"$Chicago$", signature ) && wcsicmp( L"$WINDOWS NT$", signature );
1665 if( INF_STYLE_WIN4 & style )
1666 valid = valid || !wcsicmp( L"$Chicago$", signature ) ||
1667 !wcsicmp( L"$WINDOWS NT$", signature );
1668 if( valid )
1670 size += 1 + lstrlenW( finddata.cFileName );
1671 if( ptr && insize >= size )
1673 lstrcpyW( ptr, finddata.cFileName );
1674 ptr += 1 + lstrlenW( finddata.cFileName );
1675 *ptr = 0;
1679 while( FindNextFileW( hdl, &finddata ));
1680 FindClose( hdl );
1682 HeapFree( GetProcessHeap(), 0, fullname );
1683 HeapFree( GetProcessHeap(), 0, filter );
1684 if( outsize ) *outsize = size;
1685 return TRUE;