wined3d: Use the correct colour sources in process_vertices_strided().
[wine.git] / dlls / setupapi / install.c
blob6a3213ce7c3f0ae515757c7abfb351d9bec1fa1d
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;
158 WCHAR src_root[MAX_PATH], *p;
160 if (!info->src_root)
162 strcpyW( src_root, PARSER_get_inf_filename( hinf ) );
163 if ((p = strrchrW( src_root, '\\' ))) *p = 0;
166 if (field[0] == '@') /* special case: copy single file */
167 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf,
168 info->src_root ? info->src_root : src_root, field+1, field+1, info->copy_flags );
169 else
170 SetupQueueCopySectionW( info->queue, info->src_root ? info->src_root : src_root,
171 info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
172 return TRUE;
176 /***********************************************************************
177 * delete_files_callback
179 * Called once for each DelFiles entry in a given section.
181 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
183 struct files_callback_info *info = arg;
184 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
185 return TRUE;
189 /***********************************************************************
190 * rename_files_callback
192 * Called once for each RenFiles entry in a given section.
194 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
196 struct files_callback_info *info = arg;
197 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
198 return TRUE;
202 /***********************************************************************
203 * get_root_key
205 * Retrieve the registry root key from its name.
207 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
209 static const WCHAR HKCR[] = {'H','K','C','R',0};
210 static const WCHAR HKCU[] = {'H','K','C','U',0};
211 static const WCHAR HKLM[] = {'H','K','L','M',0};
212 static const WCHAR HKU[] = {'H','K','U',0};
213 static const WCHAR HKR[] = {'H','K','R',0};
215 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
216 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
217 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
218 if (!strcmpiW( name, HKU )) return HKEY_USERS;
219 if (!strcmpiW( name, HKR )) return def_root;
220 return 0;
224 /***********************************************************************
225 * append_multi_sz_value
227 * Append a multisz string to a multisz registry value.
229 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
230 DWORD str_size )
232 DWORD size, type, total;
233 WCHAR *buffer, *p;
235 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
236 if (type != REG_MULTI_SZ) return;
238 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
239 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
241 /* compare each string against all the existing ones */
242 total = size;
243 while (*strings)
245 int len = strlenW(strings) + 1;
247 for (p = buffer; *p; p += strlenW(p) + 1)
248 if (!strcmpiW( p, strings )) break;
250 if (!*p) /* not found, need to append it */
252 memcpy( p, strings, len * sizeof(WCHAR) );
253 p[len] = 0;
254 total += len * sizeof(WCHAR);
256 strings += len;
258 if (total != size)
260 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
261 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
263 done:
264 HeapFree( GetProcessHeap(), 0, buffer );
268 /***********************************************************************
269 * delete_multi_sz_value
271 * Remove a string from a multisz registry value.
273 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
275 DWORD size, type;
276 WCHAR *buffer, *src, *dst;
278 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
279 if (type != REG_MULTI_SZ) return;
280 /* allocate double the size, one for value before and one for after */
281 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
282 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
283 src = buffer;
284 dst = buffer + size;
285 while (*src)
287 int len = strlenW(src) + 1;
288 if (strcmpiW( src, string ))
290 memcpy( dst, src, len * sizeof(WCHAR) );
291 dst += len;
293 src += len;
295 *dst++ = 0;
296 if (dst != buffer + 2*size) /* did we remove something? */
298 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
299 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
300 (BYTE *)(buffer + size), dst - (buffer + size) );
302 done:
303 HeapFree( GetProcessHeap(), 0, buffer );
307 /***********************************************************************
308 * do_reg_operation
310 * Perform an add/delete registry operation depending on the flags.
312 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
314 DWORD type, size;
316 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
318 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
320 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
322 WCHAR *str;
324 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
325 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
326 SetupGetStringFieldW( context, 5, str, size, NULL );
327 delete_multi_sz_value( hkey, value, str );
328 HeapFree( GetProcessHeap(), 0, str );
330 else RegDeleteValueW( hkey, value );
332 else
334 RegDeleteTreeW( hkey, NULL );
335 NtDeleteKey( hkey );
337 return TRUE;
340 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
342 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
344 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
345 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
346 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
349 switch(flags & FLG_ADDREG_TYPE_MASK)
351 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
352 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
353 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
354 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
355 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
356 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
357 default: type = flags >> 16; break;
360 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
361 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
363 static const WCHAR empty;
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 ? strtoulW( 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 *)&empty, 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 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
556 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
558 FreeLibrary( module );
559 module = NULL;
560 if (!args) args = default_args;
561 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
562 sprintfW( cmd_line, format, 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 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
685 path = p;
686 p += strlenW(p);
687 if (p == path || p[-1] != '\\') *p++ = '\\';
688 strcpyW( 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 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
733 path = p;
734 p += strlenW(p);
735 if (p == path || p[-1] != '\\') *p++ = '\\';
736 strcpyW( 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 = strchrW(buffer,'=');
780 if (divider)
782 *divider = 0;
783 strcpyW(entry,buffer);
784 divider++;
785 strcpyW(string,divider);
787 else
789 strcpyW(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 static const WCHAR dotlnk[] = {'.','l','n','k',0};
835 TRACE( "(%s)\n", debugstr_w(field) );
837 if (SetupFindFirstLineW( hinf, field, Name, &name_context ))
839 SetupGetIntField( &name_context, 2, &attrs );
840 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
842 else return TRUE;
844 /* calculate filename */
845 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
846 lnkpath_end = lnkpath + strlenW(lnkpath);
847 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
849 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, SubDir, &context ))
851 unsigned int subdir_size;
853 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
854 return TRUE;
856 lnkpath_end += subdir_size - 1;
857 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
860 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
861 return TRUE;
863 lnkpath_end += name_size - 1;
865 if (attrs & FLG_PROFITEM_GROUP)
867 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
869 else
871 IShellLinkW* shelllink=NULL;
872 IPersistFile* persistfile=NULL;
873 HRESULT initresult=E_FAIL;
875 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
876 strcpyW( lnkpath_end, dotlnk );
878 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
880 /* calculate command line */
881 if (SetupFindFirstLineW( hinf, field, CmdLine, &context ))
883 unsigned int dir_len=0, subdir_size=0, filename_size=0;
884 int dirid=0;
885 LPCWSTR dir;
886 LPWSTR cmdline_end;
888 SetupGetIntField( &context, 1, &dirid );
889 dir = DIRID_get_string( dirid );
891 if (dir) dir_len = strlenW(dir);
893 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
894 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
896 if (dir_len && filename_size)
898 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
900 strcpyW( cmdline_end, dir );
901 cmdline_end += dir_len;
902 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
904 if (subdir_size)
906 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
907 cmdline_end += subdir_size-1;
908 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
910 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
911 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
915 if (!cmdline) return TRUE;
917 initresult = CoInitialize(NULL);
919 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
920 &IID_IShellLinkW, (LPVOID*)&shelllink )))
921 goto done;
923 IShellLinkW_SetPath( shelllink, cmdline );
924 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
925 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
927 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
928 IPersistFile_Save( persistfile, lnkpath, FALSE );
929 IPersistFile_Release( persistfile );
931 IShellLinkW_Release( shelllink );
933 done:
934 if (SUCCEEDED(initresult)) CoUninitialize();
935 HeapFree( GetProcessHeap(), 0, cmdline );
938 return TRUE;
941 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
943 FIXME( "should do copy inf %s\n", debugstr_w(field) );
944 return TRUE;
948 /***********************************************************************
949 * iterate_section_fields
951 * Iterate over all fields of a certain key of a certain section
953 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
954 iterate_fields_func callback, void *arg )
956 WCHAR static_buffer[200];
957 WCHAR *buffer = static_buffer;
958 DWORD size = ARRAY_SIZE( static_buffer );
959 INFCONTEXT context;
960 BOOL ret = FALSE;
962 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
963 while (ok)
965 UINT i, count = SetupGetFieldCount( &context );
966 for (i = 1; i <= count; i++)
968 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
969 goto done;
970 if (!callback( hinf, buffer, arg ))
972 WARN("callback failed for %s %s err %d\n",
973 debugstr_w(section), debugstr_w(buffer), GetLastError() );
974 goto done;
977 ok = SetupFindNextMatchLineW( &context, key, &context );
979 ret = TRUE;
980 done:
981 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
982 return ret;
986 /***********************************************************************
987 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
989 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
990 PCSTR section, PCSTR src_root, UINT flags )
992 UNICODE_STRING sectionW;
993 BOOL ret = FALSE;
995 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
997 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
998 return FALSE;
1000 if (!src_root)
1001 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1002 NULL, flags );
1003 else
1005 UNICODE_STRING srcW;
1006 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
1008 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
1009 srcW.Buffer, flags );
1010 RtlFreeUnicodeString( &srcW );
1012 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1014 RtlFreeUnicodeString( &sectionW );
1015 return ret;
1019 /***********************************************************************
1020 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1022 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1023 PCWSTR section, PCWSTR src_root, UINT flags )
1025 struct files_callback_info info;
1027 info.queue = queue;
1028 info.src_root = src_root;
1029 info.copy_flags = flags;
1030 info.layout = hlayout;
1031 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
1035 /***********************************************************************
1036 * SetupInstallFromInfSectionA (SETUPAPI.@)
1038 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1039 HKEY key_root, PCSTR src_root, UINT copy_flags,
1040 PSP_FILE_CALLBACK_A callback, PVOID context,
1041 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1043 UNICODE_STRING sectionW, src_rootW;
1044 struct callback_WtoA_context ctx;
1045 BOOL ret = FALSE;
1047 src_rootW.Buffer = NULL;
1048 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1050 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1051 return FALSE;
1054 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1056 ctx.orig_context = context;
1057 ctx.orig_handler = callback;
1058 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1059 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1060 &ctx, devinfo, devinfo_data );
1061 RtlFreeUnicodeString( &sectionW );
1063 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1065 RtlFreeUnicodeString( &src_rootW );
1066 return ret;
1070 /***********************************************************************
1071 * SetupInstallFromInfSectionW (SETUPAPI.@)
1073 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1074 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1075 PSP_FILE_CALLBACK_W callback, PVOID context,
1076 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1078 BOOL ret;
1079 int i;
1081 if (flags & SPINST_REGISTRY)
1083 struct registry_callback_info info;
1085 info.default_root = key_root;
1086 info.delete = FALSE;
1087 if (!iterate_section_fields( hinf, section, WinePreInstall, registry_callback, &info ))
1088 return FALSE;
1090 if (flags & SPINST_FILES)
1092 struct files_callback_info info;
1093 HSPFILEQ queue;
1095 if (!(queue = SetupOpenFileQueue())) return FALSE;
1096 info.queue = queue;
1097 info.src_root = src_root;
1098 info.copy_flags = copy_flags;
1099 info.layout = hinf;
1100 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
1101 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
1102 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
1103 SetupCommitFileQueueW( owner, queue, callback, context ));
1104 SetupCloseFileQueue( queue );
1105 if (!ret) return FALSE;
1107 if (flags & SPINST_INIFILES)
1109 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
1110 !iterate_section_fields( hinf, section, UpdateIniFields,
1111 update_ini_fields_callback, NULL ))
1112 return FALSE;
1114 if (flags & SPINST_INI2REG)
1116 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
1117 return FALSE;
1119 if (flags & SPINST_LOGCONFIG)
1121 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
1122 return FALSE;
1124 if (flags & SPINST_REGSVR)
1126 struct register_dll_info info;
1128 info.unregister = FALSE;
1129 info.modules_size = 0;
1130 info.modules_count = 0;
1131 info.modules = NULL;
1132 if (flags & SPINST_REGISTERCALLBACKAWARE)
1134 info.callback = callback;
1135 info.callback_context = context;
1137 else info.callback = NULL;
1139 if (iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
1140 cleanup_fake_dlls();
1141 else
1142 return FALSE;
1144 ret = iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info );
1145 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1146 HeapFree( GetProcessHeap(), 0, info.modules );
1147 if (!ret) return FALSE;
1149 if (flags & SPINST_UNREGSVR)
1151 struct register_dll_info info;
1153 info.unregister = TRUE;
1154 info.modules_size = 0;
1155 info.modules_count = 0;
1156 info.modules = NULL;
1157 if (flags & SPINST_REGISTERCALLBACKAWARE)
1159 info.callback = callback;
1160 info.callback_context = context;
1162 else info.callback = NULL;
1164 ret = iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info );
1165 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1166 HeapFree( GetProcessHeap(), 0, info.modules );
1167 if (!ret) return FALSE;
1169 if (flags & SPINST_REGISTRY)
1171 struct registry_callback_info info;
1173 info.default_root = key_root;
1174 info.delete = TRUE;
1175 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
1176 return FALSE;
1177 info.delete = FALSE;
1178 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
1179 return FALSE;
1181 if (flags & SPINST_BITREG)
1183 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1184 return FALSE;
1186 if (flags & SPINST_PROFILEITEMS)
1188 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1189 return FALSE;
1191 if (flags & SPINST_COPYINF)
1193 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1194 return FALSE;
1197 SetLastError(ERROR_SUCCESS);
1198 return TRUE;
1202 /***********************************************************************
1203 * InstallHinfSectionW (SETUPAPI.@)
1205 * NOTE: 'cmdline' is <section> <mode> <path> from
1206 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1208 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1210 #ifdef __i386__
1211 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
1212 #elif defined(__x86_64__)
1213 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
1214 #else /* FIXME: other platforms */
1215 static const WCHAR nt_platformW[] = {'.','n','t',0};
1216 #endif
1217 static const WCHAR nt_genericW[] = {'.','n','t',0};
1218 static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1220 WCHAR *s, *path, section[MAX_PATH + ARRAY_SIZE( nt_platformW ) + ARRAY_SIZE( servicesW )];
1221 void *callback_context;
1222 UINT mode;
1223 HINF hinf;
1225 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1227 lstrcpynW( section, cmdline, MAX_PATH );
1229 if (!(s = strchrW( section, ' ' ))) return;
1230 *s++ = 0;
1231 while (*s == ' ') s++;
1232 mode = atoiW( s );
1234 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1235 if (!(s = strchrW( s, ' ' ))) return;
1236 while (*s == ' ') s++;
1237 path = s;
1239 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1240 if (hinf == INVALID_HANDLE_VALUE) return;
1242 if (!(GetVersion() & 0x80000000))
1244 INFCONTEXT context;
1246 /* check for <section>.ntx86 (or corresponding name for the current platform)
1247 * and then <section>.nt */
1248 s = section + strlenW(section);
1249 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1250 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1252 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1253 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1255 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1258 callback_context = SetupInitDefaultQueueCallback( hwnd );
1259 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1260 SetupDefaultQueueCallbackW, callback_context,
1261 NULL, NULL );
1262 SetupTermDefaultQueueCallback( callback_context );
1263 strcatW( section, servicesW );
1264 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1265 SetupCloseInfFile( hinf );
1267 /* FIXME: should check the mode and maybe reboot */
1268 /* there isn't much point in doing that since we */
1269 /* don't yet handle deferred file copies anyway. */
1270 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1274 /***********************************************************************
1275 * InstallHinfSectionA (SETUPAPI.@)
1277 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1279 UNICODE_STRING cmdlineW;
1281 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1283 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1284 RtlFreeUnicodeString( &cmdlineW );
1289 /***********************************************************************
1290 * add_service
1292 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1294 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1296 struct registry_callback_info info;
1297 SC_HANDLE service;
1298 INFCONTEXT context;
1299 SERVICE_DESCRIPTIONW descr;
1300 WCHAR *display_name, *start_name, *load_order, *binary_path;
1301 INT service_type = 0, start_type = 0, error_control = 0;
1302 DWORD size;
1303 HKEY hkey;
1305 /* first the mandatory fields */
1307 if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1308 !SetupGetIntField( &context, 1, &service_type ))
1310 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1311 return FALSE;
1313 if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1314 !SetupGetIntField( &context, 1, &start_type ))
1316 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1317 return FALSE;
1319 if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1320 !SetupGetIntField( &context, 1, &error_control ))
1322 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1323 return FALSE;
1325 if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1327 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1328 return FALSE;
1331 /* now the optional fields */
1333 display_name = dup_section_line_field( hinf, section, DisplayName, 1 );
1334 start_name = dup_section_line_field( hinf, section, StartName, 1 );
1335 load_order = dup_section_line_field( hinf, section, LoadOrderGroup, 1 );
1336 descr.lpDescription = dup_section_line_field( hinf, section, Description, 1 );
1338 /* FIXME: Dependencies field */
1339 /* FIXME: Security field */
1341 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1342 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1343 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1345 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1346 service_type, start_type, error_control, binary_path,
1347 load_order, NULL, NULL, start_name, NULL );
1348 if (service)
1350 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1352 else
1354 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1355 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1356 if (!service) goto done;
1358 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1359 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1361 QUERY_SERVICE_CONFIGW *config = NULL;
1363 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1364 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1365 config = HeapAlloc( GetProcessHeap(), 0, size );
1366 if (config && QueryServiceConfigW( service, config, size, &size ))
1368 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1369 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1370 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1372 HeapFree( GetProcessHeap(), 0, display_name );
1373 display_name = strdupW( config->lpDisplayName );
1375 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1377 HeapFree( GetProcessHeap(), 0, load_order );
1378 load_order = strdupW( config->lpLoadOrderGroup );
1381 HeapFree( GetProcessHeap(), 0, config );
1383 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1384 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1385 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1387 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1388 load_order, NULL, NULL, start_name, NULL, display_name );
1390 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1391 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1394 /* execute the AddReg, DelReg and BitReg entries */
1396 info.default_root = 0;
1397 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1399 RegOpenKeyW( hkey, name, &info.default_root );
1400 RegCloseKey( hkey );
1402 if (info.default_root)
1404 info.delete = TRUE;
1405 iterate_section_fields( hinf, section, DelReg, registry_callback, &info );
1406 info.delete = FALSE;
1407 iterate_section_fields( hinf, section, AddReg, registry_callback, &info );
1408 RegCloseKey( info.default_root );
1410 iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1412 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1413 CloseServiceHandle( service );
1415 done:
1416 if (!service) WARN( "failed err %u\n", GetLastError() );
1417 HeapFree( GetProcessHeap(), 0, binary_path );
1418 HeapFree( GetProcessHeap(), 0, display_name );
1419 HeapFree( GetProcessHeap(), 0, start_name );
1420 HeapFree( GetProcessHeap(), 0, load_order );
1421 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1422 return service != 0;
1426 /***********************************************************************
1427 * del_service
1429 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1431 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1433 BOOL ret;
1434 SC_HANDLE service;
1435 SERVICE_STATUS status;
1437 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1439 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1440 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1441 return FALSE;
1443 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1444 TRACE( "deleting %s\n", debugstr_w(name) );
1445 ret = DeleteService( service );
1446 CloseServiceHandle( service );
1447 return ret;
1451 /***********************************************************************
1452 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1454 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1456 WCHAR service_name[MAX_INF_STRING_LENGTH];
1457 WCHAR service_section[MAX_INF_STRING_LENGTH];
1458 SC_HANDLE scm;
1459 INFCONTEXT context;
1460 INT section_flags;
1461 BOOL ok, ret = TRUE;
1463 if (!(ok = SetupFindFirstLineW( hinf, section, NULL, &context )))
1465 SetLastError( ERROR_SECTION_NOT_FOUND );
1466 return FALSE;
1468 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1470 ok = SetupFindFirstLineW( hinf, section, AddService, &context );
1471 while (ok)
1473 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1474 continue;
1475 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1476 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1477 continue;
1478 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1479 goto done;
1480 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1483 ok = SetupFindFirstLineW( hinf, section, DelService, &context );
1484 while (ok)
1486 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1487 continue;
1488 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1489 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1490 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1492 if (ret) SetLastError( ERROR_SUCCESS );
1493 done:
1494 CloseServiceHandle( scm );
1495 return ret;
1499 /***********************************************************************
1500 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1502 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1504 UNICODE_STRING SectionNameW;
1505 BOOL ret = FALSE;
1507 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1509 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1510 RtlFreeUnicodeString( &SectionNameW );
1512 else
1513 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1515 return ret;
1519 /***********************************************************************
1520 * SetupGetInfFileListA (SETUPAPI.@)
1522 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1523 DWORD insize, PDWORD outsize)
1525 UNICODE_STRING dirW;
1526 PWSTR bufferW = NULL;
1527 BOOL ret = FALSE;
1528 DWORD outsizeA, outsizeW;
1530 if ( dir )
1531 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1532 else
1533 dirW.Buffer = NULL;
1535 if ( buffer )
1536 bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof( WCHAR ));
1538 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1540 if ( ret )
1542 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1543 buffer, insize, NULL, NULL);
1544 if ( outsize ) *outsize = outsizeA;
1547 HeapFree( GetProcessHeap(), 0, bufferW );
1548 RtlFreeUnicodeString( &dirW );
1549 return ret;
1553 /***********************************************************************
1554 * SetupGetInfFileListW (SETUPAPI.@)
1556 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1557 DWORD insize, PDWORD outsize)
1559 static const WCHAR inf[] = {'\\','*','.','i','n','f',0 };
1560 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1561 DWORD dir_len, name_len = 20, size ;
1562 WIN32_FIND_DATAW finddata;
1563 HANDLE hdl;
1564 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1565 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1567 FIXME( "unknown inf_style(s) 0x%x\n",
1568 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1569 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1570 if( outsize ) *outsize = 1;
1571 return TRUE;
1573 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1575 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1576 if( outsize ) *outsize = 1;
1577 return TRUE;
1579 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1580 FIXME("ignored inf_style(s) %s %s\n",
1581 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1582 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1583 if( dir )
1585 DWORD att;
1586 DWORD msize;
1587 dir_len = strlenW( dir );
1588 if ( !dir_len ) return FALSE;
1589 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1590 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1591 if( !filter )
1593 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1594 return FALSE;
1596 strcpyW( filter, dir );
1597 if ( '\\' == filter[dir_len - 1] )
1598 filter[--dir_len] = 0;
1600 att = GetFileAttributesW( filter );
1601 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1603 HeapFree( GetProcessHeap(), 0, filter );
1604 SetLastError( ERROR_DIRECTORY );
1605 return FALSE;
1608 else
1610 static const WCHAR infdir[] = {'\\','i','n','f',0};
1611 DWORD msize;
1612 dir_len = GetWindowsDirectoryW( NULL, 0 );
1613 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1614 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1615 if( !filter )
1617 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1618 return FALSE;
1620 GetWindowsDirectoryW( filter, msize );
1621 strcatW( filter, infdir );
1623 strcatW( filter, inf );
1625 hdl = FindFirstFileW( filter , &finddata );
1626 if ( hdl == INVALID_HANDLE_VALUE )
1628 if( outsize ) *outsize = 1;
1629 HeapFree( GetProcessHeap(), 0, filter );
1630 return TRUE;
1632 size = 1;
1635 static const WCHAR key[] =
1636 {'S','i','g','n','a','t','u','r','e',0 };
1637 static const WCHAR section[] =
1638 {'V','e','r','s','i','o','n',0 };
1639 static const WCHAR sig_win4_1[] =
1640 {'$','C','h','i','c','a','g','o','$',0 };
1641 static const WCHAR sig_win4_2[] =
1642 {'$','W','I','N','D','O','W','S',' ','N','T','$',0 };
1643 WCHAR signature[ MAX_PATH ];
1644 BOOL valid = FALSE;
1645 DWORD len = strlenW( finddata.cFileName );
1646 if (!fullname || ( name_len < len ))
1648 name_len = ( name_len < len ) ? len : name_len;
1649 HeapFree( GetProcessHeap(), 0, fullname );
1650 fullname = HeapAlloc( GetProcessHeap(), 0,
1651 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1652 if( !fullname )
1654 FindClose( hdl );
1655 HeapFree( GetProcessHeap(), 0, filter );
1656 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1657 return FALSE;
1659 strcpyW( fullname, filter );
1661 fullname[ dir_len + 1] = 0; /* keep '\\' */
1662 strcatW( fullname, finddata.cFileName );
1663 if (!GetPrivateProfileStringW( section, key, NULL, signature, MAX_PATH, fullname ))
1664 signature[0] = 0;
1665 if( INF_STYLE_OLDNT & style )
1666 valid = strcmpiW( sig_win4_1, signature ) &&
1667 strcmpiW( sig_win4_2, signature );
1668 if( INF_STYLE_WIN4 & style )
1669 valid = valid || !strcmpiW( sig_win4_1, signature ) ||
1670 !strcmpiW( sig_win4_2, signature );
1671 if( valid )
1673 size += 1 + strlenW( finddata.cFileName );
1674 if( ptr && insize >= size )
1676 strcpyW( ptr, finddata.cFileName );
1677 ptr += 1 + strlenW( finddata.cFileName );
1678 *ptr = 0;
1682 while( FindNextFileW( hdl, &finddata ));
1683 FindClose( hdl );
1685 HeapFree( GetProcessHeap(), 0, fullname );
1686 HeapFree( GetProcessHeap(), 0, filter );
1687 if( outsize ) *outsize = size;
1688 return TRUE;