gphoto2.ds: Set supported groups.
[wine.git] / dlls / setupapi / install.c
blob0593df892ef9b89382f640ca466e7d404ae6101e
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 "objidl.h"
36 #include "objbase.h"
37 #include "setupapi.h"
38 #include "setupapi_private.h"
39 #include "wine/unicode.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 );
73 /* Unicode constants */
74 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
75 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
76 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
77 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
78 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
79 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
80 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
81 static const WCHAR BitReg[] = {'B','i','t','R','e','g',0};
82 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
83 static const WCHAR CopyINF[] = {'C','o','p','y','I','N','F',0};
84 static const WCHAR AddService[] = {'A','d','d','S','e','r','v','i','c','e',0};
85 static const WCHAR DelService[] = {'D','e','l','S','e','r','v','i','c','e',0};
86 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
87 static const WCHAR RegisterDlls[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
88 static const WCHAR UnregisterDlls[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
89 static const WCHAR ProfileItems[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
90 static const WCHAR Name[] = {'N','a','m','e',0};
91 static const WCHAR CmdLine[] = {'C','m','d','L','i','n','e',0};
92 static const WCHAR SubDir[] = {'S','u','b','D','i','r',0};
93 static const WCHAR WineFakeDlls[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
94 static const WCHAR WinePreInstall[] = {'W','i','n','e','P','r','e','I','n','s','t','a','l','l',0};
95 static const WCHAR DisplayName[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
96 static const WCHAR Description[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
97 static const WCHAR ServiceBinary[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
98 static const WCHAR StartName[] = {'S','t','a','r','t','N','a','m','e',0};
99 static const WCHAR LoadOrderGroup[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
100 static const WCHAR ServiceType[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
101 static const WCHAR StartType[] = {'S','t','a','r','t','T','y','p','e',0};
102 static const WCHAR ErrorControl[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
104 static const WCHAR ServicesKey[] = {'S','y','s','t','e','m','\\',
105 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
106 'S','e','r','v','i','c','e','s',0};
108 /***********************************************************************
109 * get_field_string
111 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
113 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
114 WCHAR *static_buffer, DWORD *size )
116 DWORD required;
118 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
119 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
121 /* now grow the buffer */
122 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
123 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
124 *size = required;
125 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
127 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
128 return NULL;
132 /***********************************************************************
133 * dup_section_line_field
135 * Retrieve the contents of a field in a newly-allocated buffer.
137 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
139 INFCONTEXT context;
140 DWORD size;
141 WCHAR *buffer;
143 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
144 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
145 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
146 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
147 return buffer;
150 /***********************************************************************
151 * copy_files_callback
153 * Called once for each CopyFiles entry in a given section.
155 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
157 struct files_callback_info *info = arg;
159 if (field[0] == '@') /* special case: copy single file */
160 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf, info->src_root, NULL, field+1, info->copy_flags );
161 else
162 SetupQueueCopySectionW( info->queue, info->src_root, info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
163 return TRUE;
167 /***********************************************************************
168 * delete_files_callback
170 * Called once for each DelFiles entry in a given section.
172 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
174 struct files_callback_info *info = arg;
175 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
176 return TRUE;
180 /***********************************************************************
181 * rename_files_callback
183 * Called once for each RenFiles entry in a given section.
185 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
187 struct files_callback_info *info = arg;
188 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
189 return TRUE;
193 /***********************************************************************
194 * get_root_key
196 * Retrieve the registry root key from its name.
198 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
200 static const WCHAR HKCR[] = {'H','K','C','R',0};
201 static const WCHAR HKCU[] = {'H','K','C','U',0};
202 static const WCHAR HKLM[] = {'H','K','L','M',0};
203 static const WCHAR HKU[] = {'H','K','U',0};
204 static const WCHAR HKR[] = {'H','K','R',0};
206 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
207 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
208 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
209 if (!strcmpiW( name, HKU )) return HKEY_USERS;
210 if (!strcmpiW( name, HKR )) return def_root;
211 return 0;
215 /***********************************************************************
216 * append_multi_sz_value
218 * Append a multisz string to a multisz registry value.
220 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
221 DWORD str_size )
223 DWORD size, type, total;
224 WCHAR *buffer, *p;
226 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
227 if (type != REG_MULTI_SZ) return;
229 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
230 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
232 /* compare each string against all the existing ones */
233 total = size;
234 while (*strings)
236 int len = strlenW(strings) + 1;
238 for (p = buffer; *p; p += strlenW(p) + 1)
239 if (!strcmpiW( p, strings )) break;
241 if (!*p) /* not found, need to append it */
243 memcpy( p, strings, len * sizeof(WCHAR) );
244 p[len] = 0;
245 total += len * sizeof(WCHAR);
247 strings += len;
249 if (total != size)
251 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
252 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
254 done:
255 HeapFree( GetProcessHeap(), 0, buffer );
259 /***********************************************************************
260 * delete_multi_sz_value
262 * Remove a string from a multisz registry value.
264 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
266 DWORD size, type;
267 WCHAR *buffer, *src, *dst;
269 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
270 if (type != REG_MULTI_SZ) return;
271 /* allocate double the size, one for value before and one for after */
272 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
273 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
274 src = buffer;
275 dst = buffer + size;
276 while (*src)
278 int len = strlenW(src) + 1;
279 if (strcmpiW( src, string ))
281 memcpy( dst, src, len * sizeof(WCHAR) );
282 dst += len;
284 src += len;
286 *dst++ = 0;
287 if (dst != buffer + 2*size) /* did we remove something? */
289 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
290 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
291 (BYTE *)(buffer + size), dst - (buffer + size) );
293 done:
294 HeapFree( GetProcessHeap(), 0, buffer );
298 /***********************************************************************
299 * do_reg_operation
301 * Perform an add/delete registry operation depending on the flags.
303 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
305 DWORD type, size;
307 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
309 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
311 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
313 WCHAR *str;
315 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
316 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
317 SetupGetStringFieldW( context, 5, str, size, NULL );
318 delete_multi_sz_value( hkey, value, str );
319 HeapFree( GetProcessHeap(), 0, str );
321 else RegDeleteValueW( hkey, value );
323 else
325 RegDeleteTreeW( hkey, NULL );
326 NtDeleteKey( hkey );
328 return TRUE;
331 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
333 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
335 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
336 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
337 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
340 switch(flags & FLG_ADDREG_TYPE_MASK)
342 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
343 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
344 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
345 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
346 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
347 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
348 default: type = flags >> 16; break;
351 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
352 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
354 static const WCHAR empty;
355 WCHAR *str = NULL;
357 if (type == REG_MULTI_SZ)
359 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
360 if (size)
362 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
363 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
365 if (flags & FLG_ADDREG_APPEND)
367 if (!str) return TRUE;
368 append_multi_sz_value( hkey, value, str, size );
369 HeapFree( GetProcessHeap(), 0, str );
370 return TRUE;
372 /* else fall through to normal string handling */
374 else
376 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
377 if (size)
379 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
380 SetupGetStringFieldW( context, 5, str, size, NULL );
381 if (type == REG_LINK) size--; /* no terminating null for symlinks */
385 if (type == REG_DWORD)
387 DWORD dw = str ? strtoulW( str, NULL, 0 ) : 0;
388 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
389 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
391 else
393 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
394 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
395 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)&empty, sizeof(WCHAR) );
397 HeapFree( GetProcessHeap(), 0, str );
398 return TRUE;
400 else /* get the binary data */
402 BYTE *data = NULL;
404 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
405 if (size)
407 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
408 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
409 SetupGetBinaryField( context, 5, data, size, NULL );
411 RegSetValueExW( hkey, value, 0, type, data, size );
412 HeapFree( GetProcessHeap(), 0, data );
413 return TRUE;
418 /***********************************************************************
419 * registry_callback
421 * Called once for each AddReg and DelReg entry in a given section.
423 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
425 struct registry_callback_info *info = arg;
426 INFCONTEXT context;
427 HKEY root_key, hkey;
429 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
431 for (; ok; ok = SetupFindNextLine( &context, &context ))
433 DWORD options = 0;
434 WCHAR buffer[MAX_INF_STRING_LENGTH];
435 INT flags;
437 /* get root */
438 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
439 continue;
440 if (!(root_key = get_root_key( buffer, info->default_root )))
441 continue;
443 /* get key */
444 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
445 *buffer = 0;
447 /* get flags */
448 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
450 if (!info->delete)
452 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
454 else
456 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
457 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
459 /* Wine extension: magic support for symlinks */
460 if (flags >> 16 == REG_LINK) options = REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK;
462 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
464 if (RegOpenKeyExW( root_key, buffer, options, MAXIMUM_ALLOWED, &hkey ))
465 continue; /* ignore if it doesn't exist */
467 else
469 DWORD res = RegCreateKeyExW( root_key, buffer, 0, NULL, options,
470 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
471 if (res == ERROR_ALREADY_EXISTS && (options & REG_OPTION_CREATE_LINK))
472 res = RegCreateKeyExW( root_key, buffer, 0, NULL, REG_OPTION_OPEN_LINK,
473 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
474 if (res)
476 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
477 continue;
480 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
482 /* get value name */
483 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
484 *buffer = 0;
486 /* and now do it */
487 if (!do_reg_operation( hkey, buffer, &context, flags ))
489 RegCloseKey( hkey );
490 return FALSE;
492 RegCloseKey( hkey );
494 return TRUE;
498 /***********************************************************************
499 * do_register_dll
501 * Register or unregister a dll.
503 static BOOL do_register_dll( struct register_dll_info *info, const WCHAR *path,
504 INT flags, INT timeout, const WCHAR *args )
506 HMODULE module;
507 HRESULT res;
508 SP_REGISTER_CONTROL_STATUSW status;
509 IMAGE_NT_HEADERS *nt;
511 status.cbSize = sizeof(status);
512 status.FileName = path;
513 status.FailureCode = SPREG_SUCCESS;
514 status.Win32Error = ERROR_SUCCESS;
516 if (info->callback)
518 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
519 (UINT_PTR)&status, !info->unregister ))
521 case FILEOP_ABORT:
522 SetLastError( ERROR_OPERATION_ABORTED );
523 return FALSE;
524 case FILEOP_SKIP:
525 return TRUE;
526 case FILEOP_DOIT:
527 break;
531 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
533 WARN( "could not load %s\n", debugstr_w(path) );
534 status.FailureCode = SPREG_LOADLIBRARY;
535 status.Win32Error = GetLastError();
536 goto done;
539 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
541 /* file is an executable, not a dll */
542 STARTUPINFOW startup;
543 PROCESS_INFORMATION process_info;
544 WCHAR *cmd_line;
545 BOOL res;
546 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
547 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
549 FreeLibrary( module );
550 module = NULL;
551 if (!args) args = default_args;
552 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
553 sprintfW( cmd_line, format, path, args );
554 memset( &startup, 0, sizeof(startup) );
555 startup.cb = sizeof(startup);
556 TRACE( "executing %s\n", debugstr_w(cmd_line) );
557 res = CreateProcessW( path, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &process_info );
558 HeapFree( GetProcessHeap(), 0, cmd_line );
559 if (!res)
561 status.FailureCode = SPREG_LOADLIBRARY;
562 status.Win32Error = GetLastError();
563 goto done;
565 CloseHandle( process_info.hThread );
567 if (WaitForSingleObject( process_info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
569 /* timed out, kill the process */
570 TerminateProcess( process_info.hProcess, 1 );
571 status.FailureCode = SPREG_TIMEOUT;
572 status.Win32Error = ERROR_TIMEOUT;
574 CloseHandle( process_info.hProcess );
575 goto done;
578 if (flags & FLG_REGSVR_DLLREGISTER)
580 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
581 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
583 if (!func)
585 status.FailureCode = SPREG_GETPROCADDR;
586 status.Win32Error = GetLastError();
587 goto done;
590 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
591 res = func();
593 if (FAILED(res))
595 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
596 status.FailureCode = SPREG_REGSVR;
597 status.Win32Error = res;
598 goto done;
602 if (flags & FLG_REGSVR_DLLINSTALL)
604 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
606 if (!func)
608 status.FailureCode = SPREG_GETPROCADDR;
609 status.Win32Error = GetLastError();
610 goto done;
613 TRACE( "calling DllInstall(%d,%s) in %s\n",
614 !info->unregister, debugstr_w(args), debugstr_w(path) );
615 res = func( !info->unregister, args );
617 if (FAILED(res))
619 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
620 status.FailureCode = SPREG_REGSVR;
621 status.Win32Error = res;
622 goto done;
626 done:
627 if (module)
629 if (info->modules_count >= info->modules_size)
631 int new_size = max( 32, info->modules_size * 2 );
632 HMODULE *new = info->modules ?
633 HeapReAlloc( GetProcessHeap(), 0, info->modules, new_size * sizeof(*new) ) :
634 HeapAlloc( GetProcessHeap(), 0, new_size * sizeof(*new) );
635 if (new)
637 info->modules_size = new_size;
638 info->modules = new;
641 if (info->modules_count < info->modules_size) info->modules[info->modules_count++] = module;
642 else FreeLibrary( module );
644 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
645 (UINT_PTR)&status, !info->unregister );
646 return TRUE;
650 /***********************************************************************
651 * register_dlls_callback
653 * Called once for each RegisterDlls entry in a given section.
655 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
657 struct register_dll_info *info = arg;
658 INFCONTEXT context;
659 BOOL ret = TRUE;
660 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
662 for (; ok; ok = SetupFindNextLine( &context, &context ))
664 WCHAR *path, *args, *p;
665 WCHAR buffer[MAX_INF_STRING_LENGTH];
666 INT flags, timeout;
668 /* get directory */
669 if (!(path = PARSER_get_dest_dir( &context ))) continue;
671 /* get dll name */
672 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
673 goto done;
674 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
675 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
676 path = p;
677 p += strlenW(p);
678 if (p == path || p[-1] != '\\') *p++ = '\\';
679 strcpyW( p, buffer );
681 /* get flags */
682 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
684 /* get timeout */
685 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
687 /* get command line */
688 args = NULL;
689 if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
690 args = buffer;
692 ret = do_register_dll( info, path, flags, timeout, args );
694 done:
695 HeapFree( GetProcessHeap(), 0, path );
696 if (!ret) break;
698 return ret;
701 /***********************************************************************
702 * fake_dlls_callback
704 * Called once for each WineFakeDlls entry in a given section.
706 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
708 INFCONTEXT context;
709 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
711 for (; ok; ok = SetupFindNextLine( &context, &context ))
713 WCHAR *path, *p;
714 WCHAR buffer[MAX_INF_STRING_LENGTH];
716 /* get directory */
717 if (!(path = PARSER_get_dest_dir( &context ))) continue;
719 /* get dll name */
720 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
721 goto done;
722 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
723 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
724 path = p;
725 p += strlenW(p);
726 if (p == path || p[-1] != '\\') *p++ = '\\';
727 strcpyW( p, buffer );
729 /* get source dll */
730 if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
731 p = buffer; /* otherwise use target base name as default source */
733 create_fake_dll( path, p ); /* ignore errors */
735 done:
736 HeapFree( GetProcessHeap(), 0, path );
738 return TRUE;
741 /***********************************************************************
742 * update_ini_callback
744 * Called once for each UpdateInis entry in a given section.
746 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
748 INFCONTEXT context;
750 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
752 for (; ok; ok = SetupFindNextLine( &context, &context ))
754 WCHAR buffer[MAX_INF_STRING_LENGTH];
755 WCHAR filename[MAX_INF_STRING_LENGTH];
756 WCHAR section[MAX_INF_STRING_LENGTH];
757 WCHAR entry[MAX_INF_STRING_LENGTH];
758 WCHAR string[MAX_INF_STRING_LENGTH];
759 LPWSTR divider;
761 if (!SetupGetStringFieldW( &context, 1, filename,
762 sizeof(filename)/sizeof(WCHAR), NULL ))
763 continue;
765 if (!SetupGetStringFieldW( &context, 2, section,
766 sizeof(section)/sizeof(WCHAR), NULL ))
767 continue;
769 if (!SetupGetStringFieldW( &context, 4, buffer,
770 sizeof(buffer)/sizeof(WCHAR), NULL ))
771 continue;
773 divider = strchrW(buffer,'=');
774 if (divider)
776 *divider = 0;
777 strcpyW(entry,buffer);
778 divider++;
779 strcpyW(string,divider);
781 else
783 strcpyW(entry,buffer);
784 string[0]=0;
787 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
788 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
789 WritePrivateProfileStringW(section,entry,string,filename);
792 return TRUE;
795 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
797 FIXME( "should update ini fields %s\n", debugstr_w(field) );
798 return TRUE;
801 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
803 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
804 return TRUE;
807 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
809 FIXME( "should do logconf %s\n", debugstr_w(field) );
810 return TRUE;
813 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
815 FIXME( "should do bitreg %s\n", debugstr_w(field) );
816 return TRUE;
819 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
821 WCHAR lnkpath[MAX_PATH];
822 LPWSTR cmdline=NULL, lnkpath_end;
823 unsigned int name_size;
824 INFCONTEXT name_context, context;
825 int attrs=0;
827 static const WCHAR dotlnk[] = {'.','l','n','k',0};
829 TRACE( "(%s)\n", debugstr_w(field) );
831 if (SetupFindFirstLineW( hinf, field, Name, &name_context ))
833 SetupGetIntField( &name_context, 2, &attrs );
834 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
836 else return TRUE;
838 /* calculate filename */
839 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
840 lnkpath_end = lnkpath + strlenW(lnkpath);
841 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
843 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, SubDir, &context ))
845 unsigned int subdir_size;
847 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
848 return TRUE;
850 lnkpath_end += subdir_size - 1;
851 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
854 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
855 return TRUE;
857 lnkpath_end += name_size - 1;
859 if (attrs & FLG_PROFITEM_GROUP)
861 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
863 else
865 IShellLinkW* shelllink=NULL;
866 IPersistFile* persistfile=NULL;
867 HRESULT initresult=E_FAIL;
869 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
870 strcpyW( lnkpath_end, dotlnk );
872 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
874 /* calculate command line */
875 if (SetupFindFirstLineW( hinf, field, CmdLine, &context ))
877 unsigned int dir_len=0, subdir_size=0, filename_size=0;
878 int dirid=0;
879 LPCWSTR dir;
880 LPWSTR cmdline_end;
882 SetupGetIntField( &context, 1, &dirid );
883 dir = DIRID_get_string( dirid );
885 if (dir) dir_len = strlenW(dir);
887 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
888 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
890 if (dir_len && filename_size)
892 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
894 strcpyW( cmdline_end, dir );
895 cmdline_end += dir_len;
896 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
898 if (subdir_size)
900 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
901 cmdline_end += subdir_size-1;
902 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
904 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
905 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
909 if (!cmdline) return TRUE;
911 initresult = CoInitialize(NULL);
913 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
914 &IID_IShellLinkW, (LPVOID*)&shelllink )))
915 goto done;
917 IShellLinkW_SetPath( shelllink, cmdline );
918 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
919 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
921 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
922 IPersistFile_Save( persistfile, lnkpath, FALSE );
923 IPersistFile_Release( persistfile );
925 IShellLinkW_Release( shelllink );
927 done:
928 if (SUCCEEDED(initresult)) CoUninitialize();
929 HeapFree( GetProcessHeap(), 0, cmdline );
932 return TRUE;
935 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
937 FIXME( "should do copy inf %s\n", debugstr_w(field) );
938 return TRUE;
942 /***********************************************************************
943 * iterate_section_fields
945 * Iterate over all fields of a certain key of a certain section
947 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
948 iterate_fields_func callback, void *arg )
950 WCHAR static_buffer[200];
951 WCHAR *buffer = static_buffer;
952 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
953 INFCONTEXT context;
954 BOOL ret = FALSE;
956 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
957 while (ok)
959 UINT i, count = SetupGetFieldCount( &context );
960 for (i = 1; i <= count; i++)
962 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
963 goto done;
964 if (!callback( hinf, buffer, arg ))
966 WARN("callback failed for %s %s err %d\n",
967 debugstr_w(section), debugstr_w(buffer), GetLastError() );
968 goto done;
971 ok = SetupFindNextMatchLineW( &context, key, &context );
973 ret = TRUE;
974 done:
975 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
976 return ret;
980 /***********************************************************************
981 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
983 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
984 PCSTR section, PCSTR src_root, UINT flags )
986 UNICODE_STRING sectionW;
987 BOOL ret = FALSE;
989 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
991 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
992 return FALSE;
994 if (!src_root)
995 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
996 NULL, flags );
997 else
999 UNICODE_STRING srcW;
1000 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
1002 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1003 srcW.Buffer, flags );
1004 RtlFreeUnicodeString( &srcW );
1006 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1008 RtlFreeUnicodeString( &sectionW );
1009 return ret;
1013 /***********************************************************************
1014 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1016 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1017 PCWSTR section, PCWSTR src_root, UINT flags )
1019 struct files_callback_info info;
1021 info.queue = queue;
1022 info.src_root = src_root;
1023 info.copy_flags = flags;
1024 info.layout = hlayout;
1025 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
1029 /***********************************************************************
1030 * SetupInstallFromInfSectionA (SETUPAPI.@)
1032 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1033 HKEY key_root, PCSTR src_root, UINT copy_flags,
1034 PSP_FILE_CALLBACK_A callback, PVOID context,
1035 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1037 UNICODE_STRING sectionW, src_rootW;
1038 struct callback_WtoA_context ctx;
1039 BOOL ret = FALSE;
1041 src_rootW.Buffer = NULL;
1042 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1044 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1045 return FALSE;
1048 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1050 ctx.orig_context = context;
1051 ctx.orig_handler = callback;
1052 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1053 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1054 &ctx, devinfo, devinfo_data );
1055 RtlFreeUnicodeString( &sectionW );
1057 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1059 RtlFreeUnicodeString( &src_rootW );
1060 return ret;
1064 /***********************************************************************
1065 * SetupInstallFromInfSectionW (SETUPAPI.@)
1067 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1068 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1069 PSP_FILE_CALLBACK_W callback, PVOID context,
1070 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1072 BOOL ret;
1073 int i;
1075 if (flags & SPINST_REGISTRY)
1077 struct registry_callback_info info;
1079 info.default_root = key_root;
1080 info.delete = FALSE;
1081 if (!iterate_section_fields( hinf, section, WinePreInstall, registry_callback, &info ))
1082 return FALSE;
1084 if (flags & SPINST_FILES)
1086 struct files_callback_info info;
1087 HSPFILEQ queue;
1089 if (!(queue = SetupOpenFileQueue())) return FALSE;
1090 info.queue = queue;
1091 info.src_root = src_root;
1092 info.copy_flags = copy_flags;
1093 info.layout = hinf;
1094 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
1095 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
1096 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
1097 SetupCommitFileQueueW( owner, queue, callback, context ));
1098 SetupCloseFileQueue( queue );
1099 if (!ret) return FALSE;
1101 if (flags & SPINST_INIFILES)
1103 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
1104 !iterate_section_fields( hinf, section, UpdateIniFields,
1105 update_ini_fields_callback, NULL ))
1106 return FALSE;
1108 if (flags & SPINST_INI2REG)
1110 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
1111 return FALSE;
1113 if (flags & SPINST_LOGCONFIG)
1115 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
1116 return FALSE;
1118 if (flags & SPINST_REGSVR)
1120 struct register_dll_info info;
1122 info.unregister = FALSE;
1123 info.modules_size = 0;
1124 info.modules_count = 0;
1125 info.modules = NULL;
1126 if (flags & SPINST_REGISTERCALLBACKAWARE)
1128 info.callback = callback;
1129 info.callback_context = context;
1131 else info.callback = NULL;
1133 if (iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
1134 cleanup_fake_dlls();
1135 else
1136 return FALSE;
1138 ret = iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info );
1139 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1140 HeapFree( GetProcessHeap(), 0, info.modules );
1141 if (!ret) return FALSE;
1143 if (flags & SPINST_UNREGSVR)
1145 struct register_dll_info info;
1147 info.unregister = TRUE;
1148 info.modules_size = 0;
1149 info.modules_count = 0;
1150 info.modules = NULL;
1151 if (flags & SPINST_REGISTERCALLBACKAWARE)
1153 info.callback = callback;
1154 info.callback_context = context;
1156 else info.callback = NULL;
1158 ret = iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info );
1159 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1160 HeapFree( GetProcessHeap(), 0, info.modules );
1161 if (!ret) return FALSE;
1163 if (flags & SPINST_REGISTRY)
1165 struct registry_callback_info info;
1167 info.default_root = key_root;
1168 info.delete = TRUE;
1169 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
1170 return FALSE;
1171 info.delete = FALSE;
1172 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
1173 return FALSE;
1175 if (flags & SPINST_BITREG)
1177 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1178 return FALSE;
1180 if (flags & SPINST_PROFILEITEMS)
1182 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1183 return FALSE;
1185 if (flags & SPINST_COPYINF)
1187 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1188 return FALSE;
1191 return TRUE;
1195 /***********************************************************************
1196 * InstallHinfSectionW (SETUPAPI.@)
1198 * NOTE: 'cmdline' is <section> <mode> <path> from
1199 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1201 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1203 #ifdef __i386__
1204 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
1205 #elif defined(__x86_64)
1206 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
1207 #else /* FIXME: other platforms */
1208 static const WCHAR nt_platformW[] = {'.','n','t',0};
1209 #endif
1210 static const WCHAR nt_genericW[] = {'.','n','t',0};
1211 static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1213 WCHAR *s, *path, section[MAX_PATH + (sizeof(nt_platformW) + sizeof(servicesW)) / sizeof(WCHAR)];
1214 void *callback_context;
1215 UINT mode;
1216 HINF hinf;
1218 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1220 lstrcpynW( section, cmdline, MAX_PATH );
1222 if (!(s = strchrW( section, ' ' ))) return;
1223 *s++ = 0;
1224 while (*s == ' ') s++;
1225 mode = atoiW( s );
1227 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1228 if (!(s = strchrW( s, ' ' ))) return;
1229 while (*s == ' ') s++;
1230 path = s;
1232 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1233 if (hinf == INVALID_HANDLE_VALUE) return;
1235 if (!(GetVersion() & 0x80000000))
1237 INFCONTEXT context;
1239 /* check for <section>.ntx86 (or corresponding name for the current platform)
1240 * and then <section>.nt */
1241 s = section + strlenW(section);
1242 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1243 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1245 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1246 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1248 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1251 callback_context = SetupInitDefaultQueueCallback( hwnd );
1252 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1253 SetupDefaultQueueCallbackW, callback_context,
1254 NULL, NULL );
1255 SetupTermDefaultQueueCallback( callback_context );
1256 strcatW( section, servicesW );
1257 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1258 SetupCloseInfFile( hinf );
1260 /* FIXME: should check the mode and maybe reboot */
1261 /* there isn't much point in doing that since we */
1262 /* don't yet handle deferred file copies anyway. */
1263 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1267 /***********************************************************************
1268 * InstallHinfSectionA (SETUPAPI.@)
1270 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1272 UNICODE_STRING cmdlineW;
1274 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1276 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1277 RtlFreeUnicodeString( &cmdlineW );
1282 /***********************************************************************
1283 * add_service
1285 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1287 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1289 struct registry_callback_info info;
1290 SC_HANDLE service;
1291 INFCONTEXT context;
1292 SERVICE_DESCRIPTIONW descr;
1293 WCHAR *display_name, *start_name, *load_order, *binary_path;
1294 INT service_type = 0, start_type = 0, error_control = 0;
1295 DWORD size;
1296 HKEY hkey;
1298 /* first the mandatory fields */
1300 if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1301 !SetupGetIntField( &context, 1, &service_type ))
1303 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1304 return FALSE;
1306 if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1307 !SetupGetIntField( &context, 1, &start_type ))
1309 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1310 return FALSE;
1312 if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1313 !SetupGetIntField( &context, 1, &error_control ))
1315 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1316 return FALSE;
1318 if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1320 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1321 return FALSE;
1324 /* now the optional fields */
1326 display_name = dup_section_line_field( hinf, section, DisplayName, 1 );
1327 start_name = dup_section_line_field( hinf, section, StartName, 1 );
1328 load_order = dup_section_line_field( hinf, section, LoadOrderGroup, 1 );
1329 descr.lpDescription = dup_section_line_field( hinf, section, Description, 1 );
1331 /* FIXME: Dependencies field */
1332 /* FIXME: Security field */
1334 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1335 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1336 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1338 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1339 service_type, start_type, error_control, binary_path,
1340 load_order, NULL, NULL, start_name, NULL );
1341 if (service)
1343 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1345 else
1347 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1348 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1349 if (!service) goto done;
1351 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1352 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1354 QUERY_SERVICE_CONFIGW *config = NULL;
1356 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1357 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1358 config = HeapAlloc( GetProcessHeap(), 0, size );
1359 if (config && QueryServiceConfigW( service, config, size, &size ))
1361 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1362 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1363 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1365 HeapFree( GetProcessHeap(), 0, display_name );
1366 display_name = strdupW( config->lpDisplayName );
1368 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1370 HeapFree( GetProcessHeap(), 0, load_order );
1371 load_order = strdupW( config->lpLoadOrderGroup );
1374 HeapFree( GetProcessHeap(), 0, config );
1376 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1377 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1378 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1380 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1381 load_order, NULL, NULL, start_name, NULL, display_name );
1383 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1384 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1387 /* execute the AddReg, DelReg and BitReg entries */
1389 info.default_root = 0;
1390 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1392 RegOpenKeyW( hkey, name, &info.default_root );
1393 RegCloseKey( hkey );
1395 if (info.default_root)
1397 info.delete = TRUE;
1398 iterate_section_fields( hinf, section, DelReg, registry_callback, &info );
1399 info.delete = FALSE;
1400 iterate_section_fields( hinf, section, AddReg, registry_callback, &info );
1401 RegCloseKey( info.default_root );
1403 iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1405 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1406 CloseServiceHandle( service );
1408 done:
1409 if (!service) WARN( "failed err %u\n", GetLastError() );
1410 HeapFree( GetProcessHeap(), 0, binary_path );
1411 HeapFree( GetProcessHeap(), 0, display_name );
1412 HeapFree( GetProcessHeap(), 0, start_name );
1413 HeapFree( GetProcessHeap(), 0, load_order );
1414 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1415 return service != 0;
1419 /***********************************************************************
1420 * del_service
1422 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1424 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1426 BOOL ret;
1427 SC_HANDLE service;
1428 SERVICE_STATUS status;
1430 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1432 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1433 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1434 return FALSE;
1436 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1437 TRACE( "deleting %s\n", debugstr_w(name) );
1438 ret = DeleteService( service );
1439 CloseServiceHandle( service );
1440 return ret;
1444 /***********************************************************************
1445 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1447 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1449 WCHAR service_name[MAX_INF_STRING_LENGTH];
1450 WCHAR service_section[MAX_INF_STRING_LENGTH];
1451 SC_HANDLE scm;
1452 INFCONTEXT context;
1453 INT section_flags;
1454 BOOL ok, ret = TRUE;
1456 if (!(ok = SetupFindFirstLineW( hinf, section, NULL, &context )))
1458 SetLastError( ERROR_SECTION_NOT_FOUND );
1459 return FALSE;
1461 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1463 ok = SetupFindFirstLineW( hinf, section, AddService, &context );
1464 while (ok)
1466 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1467 continue;
1468 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1469 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1470 continue;
1471 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1472 goto done;
1473 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1476 ok = SetupFindFirstLineW( hinf, section, DelService, &context );
1477 while (ok)
1479 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1480 continue;
1481 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1482 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1483 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1485 if (ret) SetLastError( ERROR_SUCCESS );
1486 done:
1487 CloseServiceHandle( scm );
1488 return ret;
1492 /***********************************************************************
1493 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1495 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1497 UNICODE_STRING SectionNameW;
1498 BOOL ret = FALSE;
1500 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1502 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1503 RtlFreeUnicodeString( &SectionNameW );
1505 else
1506 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1508 return ret;
1512 /***********************************************************************
1513 * SetupGetInfFileListA (SETUPAPI.@)
1515 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1516 DWORD insize, PDWORD outsize)
1518 UNICODE_STRING dirW;
1519 PWSTR bufferW = NULL;
1520 BOOL ret = FALSE;
1521 DWORD outsizeA, outsizeW;
1523 if ( dir )
1524 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1525 else
1526 dirW.Buffer = NULL;
1528 if ( buffer )
1529 bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof( WCHAR ));
1531 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1533 if ( ret )
1535 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1536 buffer, insize, NULL, NULL);
1537 if ( outsize ) *outsize = outsizeA;
1540 HeapFree( GetProcessHeap(), 0, bufferW );
1541 RtlFreeUnicodeString( &dirW );
1542 return ret;
1546 /***********************************************************************
1547 * SetupGetInfFileListW (SETUPAPI.@)
1549 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1550 DWORD insize, PDWORD outsize)
1552 static const WCHAR inf[] = {'\\','*','.','i','n','f',0 };
1553 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1554 DWORD dir_len, name_len = 20, size ;
1555 WIN32_FIND_DATAW finddata;
1556 HANDLE hdl;
1557 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1558 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1560 FIXME( "unknown inf_style(s) 0x%x\n",
1561 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1562 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1563 if( outsize ) *outsize = 1;
1564 return TRUE;
1566 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1568 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1569 if( outsize ) *outsize = 1;
1570 return TRUE;
1572 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1573 FIXME("ignored inf_style(s) %s %s\n",
1574 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1575 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1576 if( dir )
1578 DWORD att;
1579 DWORD msize;
1580 dir_len = strlenW( dir );
1581 if ( !dir_len ) return FALSE;
1582 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1583 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1584 if( !filter )
1586 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1587 return FALSE;
1589 strcpyW( filter, dir );
1590 if ( '\\' == filter[dir_len - 1] )
1591 filter[--dir_len] = 0;
1593 att = GetFileAttributesW( filter );
1594 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1596 HeapFree( GetProcessHeap(), 0, filter );
1597 SetLastError( ERROR_DIRECTORY );
1598 return FALSE;
1601 else
1603 WCHAR infdir[] = {'\\','i','n','f',0 };
1604 DWORD msize;
1605 dir_len = GetWindowsDirectoryW( NULL, 0 );
1606 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1607 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1608 if( !filter )
1610 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1611 return FALSE;
1613 GetWindowsDirectoryW( filter, msize );
1614 strcatW( filter, infdir );
1616 strcatW( filter, inf );
1618 hdl = FindFirstFileW( filter , &finddata );
1619 if ( hdl == INVALID_HANDLE_VALUE )
1621 if( outsize ) *outsize = 1;
1622 HeapFree( GetProcessHeap(), 0, filter );
1623 return TRUE;
1625 size = 1;
1628 static const WCHAR key[] =
1629 {'S','i','g','n','a','t','u','r','e',0 };
1630 static const WCHAR section[] =
1631 {'V','e','r','s','i','o','n',0 };
1632 static const WCHAR sig_win4_1[] =
1633 {'$','C','h','i','c','a','g','o','$',0 };
1634 static const WCHAR sig_win4_2[] =
1635 {'$','W','I','N','D','O','W','S',' ','N','T','$',0 };
1636 WCHAR signature[ MAX_PATH ];
1637 BOOL valid = FALSE;
1638 DWORD len = strlenW( finddata.cFileName );
1639 if (!fullname || ( name_len < len ))
1641 name_len = ( name_len < len ) ? len : name_len;
1642 HeapFree( GetProcessHeap(), 0, fullname );
1643 fullname = HeapAlloc( GetProcessHeap(), 0,
1644 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1645 if( !fullname )
1647 FindClose( hdl );
1648 HeapFree( GetProcessHeap(), 0, filter );
1649 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1650 return FALSE;
1652 strcpyW( fullname, filter );
1654 fullname[ dir_len + 1] = 0; /* keep '\\' */
1655 strcatW( fullname, finddata.cFileName );
1656 if (!GetPrivateProfileStringW( section, key, NULL, signature, MAX_PATH, fullname ))
1657 signature[0] = 0;
1658 if( INF_STYLE_OLDNT & style )
1659 valid = strcmpiW( sig_win4_1, signature ) &&
1660 strcmpiW( sig_win4_2, signature );
1661 if( INF_STYLE_WIN4 & style )
1662 valid = valid || !strcmpiW( sig_win4_1, signature ) ||
1663 !strcmpiW( sig_win4_2, signature );
1664 if( valid )
1666 size += 1 + strlenW( finddata.cFileName );
1667 if( ptr && insize >= size )
1669 strcpyW( ptr, finddata.cFileName );
1670 ptr += 1 + strlenW( finddata.cFileName );
1671 *ptr = 0;
1675 while( FindNextFileW( hdl, &finddata ));
1676 FindClose( hdl );
1678 HeapFree( GetProcessHeap(), 0, fullname );
1679 HeapFree( GetProcessHeap(), 0, filter );
1680 if( outsize ) *outsize = size;
1681 return TRUE;