mshtml.idl: Added IHTMLTableCell declaration.
[wine/multimedia.git] / dlls / setupapi / install.c
blob96eb9143bf1fc35bf0d92ebed1ae9821ea5913c7
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 DisplayName[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
95 static const WCHAR Description[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
96 static const WCHAR ServiceBinary[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
97 static const WCHAR StartName[] = {'S','t','a','r','t','N','a','m','e',0};
98 static const WCHAR LoadOrderGroup[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
99 static const WCHAR ServiceType[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
100 static const WCHAR StartType[] = {'S','t','a','r','t','T','y','p','e',0};
101 static const WCHAR ErrorControl[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
103 static const WCHAR ServicesKey[] = {'S','y','s','t','e','m','\\',
104 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
105 'S','e','r','v','i','c','e','s',0};
107 /***********************************************************************
108 * get_field_string
110 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
112 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
113 WCHAR *static_buffer, DWORD *size )
115 DWORD required;
117 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
118 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
120 /* now grow the buffer */
121 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
122 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
123 *size = required;
124 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
126 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
127 return NULL;
131 /***********************************************************************
132 * dup_section_line_field
134 * Retrieve the contents of a field in a newly-allocated buffer.
136 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
138 INFCONTEXT context;
139 DWORD size;
140 WCHAR *buffer;
142 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
143 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
144 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
145 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
146 return buffer;
149 /***********************************************************************
150 * copy_files_callback
152 * Called once for each CopyFiles entry in a given section.
154 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
156 struct files_callback_info *info = arg;
158 if (field[0] == '@') /* special case: copy single file */
159 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf, info->src_root, NULL, field+1, info->copy_flags );
160 else
161 SetupQueueCopySectionW( info->queue, info->src_root, info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
162 return TRUE;
166 /***********************************************************************
167 * delete_files_callback
169 * Called once for each DelFiles entry in a given section.
171 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
173 struct files_callback_info *info = arg;
174 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
175 return TRUE;
179 /***********************************************************************
180 * rename_files_callback
182 * Called once for each RenFiles entry in a given section.
184 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
186 struct files_callback_info *info = arg;
187 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
188 return TRUE;
192 /***********************************************************************
193 * get_root_key
195 * Retrieve the registry root key from its name.
197 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
199 static const WCHAR HKCR[] = {'H','K','C','R',0};
200 static const WCHAR HKCU[] = {'H','K','C','U',0};
201 static const WCHAR HKLM[] = {'H','K','L','M',0};
202 static const WCHAR HKU[] = {'H','K','U',0};
203 static const WCHAR HKR[] = {'H','K','R',0};
205 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
206 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
207 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
208 if (!strcmpiW( name, HKU )) return HKEY_USERS;
209 if (!strcmpiW( name, HKR )) return def_root;
210 return 0;
214 /***********************************************************************
215 * append_multi_sz_value
217 * Append a multisz string to a multisz registry value.
219 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
220 DWORD str_size )
222 DWORD size, type, total;
223 WCHAR *buffer, *p;
225 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
226 if (type != REG_MULTI_SZ) return;
228 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
229 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
231 /* compare each string against all the existing ones */
232 total = size;
233 while (*strings)
235 int len = strlenW(strings) + 1;
237 for (p = buffer; *p; p += strlenW(p) + 1)
238 if (!strcmpiW( p, strings )) break;
240 if (!*p) /* not found, need to append it */
242 memcpy( p, strings, len * sizeof(WCHAR) );
243 p[len] = 0;
244 total += len;
246 strings += len;
248 if (total != size)
250 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
251 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
253 done:
254 HeapFree( GetProcessHeap(), 0, buffer );
258 /***********************************************************************
259 * delete_multi_sz_value
261 * Remove a string from a multisz registry value.
263 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
265 DWORD size, type;
266 WCHAR *buffer, *src, *dst;
268 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
269 if (type != REG_MULTI_SZ) return;
270 /* allocate double the size, one for value before and one for after */
271 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
272 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
273 src = buffer;
274 dst = buffer + size;
275 while (*src)
277 int len = strlenW(src) + 1;
278 if (strcmpiW( src, string ))
280 memcpy( dst, src, len * sizeof(WCHAR) );
281 dst += len;
283 src += len;
285 *dst++ = 0;
286 if (dst != buffer + 2*size) /* did we remove something? */
288 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
289 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
290 (BYTE *)(buffer + size), dst - (buffer + size) );
292 done:
293 HeapFree( GetProcessHeap(), 0, buffer );
297 /***********************************************************************
298 * do_reg_operation
300 * Perform an add/delete registry operation depending on the flags.
302 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
304 DWORD type, size;
306 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
308 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
310 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
312 WCHAR *str;
314 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
315 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
316 SetupGetStringFieldW( context, 5, str, size, NULL );
317 delete_multi_sz_value( hkey, value, str );
318 HeapFree( GetProcessHeap(), 0, str );
320 else RegDeleteValueW( hkey, value );
322 else NtDeleteKey( hkey );
323 return TRUE;
326 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
328 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
330 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
331 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
332 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
335 switch(flags & FLG_ADDREG_TYPE_MASK)
337 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
338 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
339 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
340 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
341 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
342 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
343 default: type = flags >> 16; break;
346 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
347 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
349 static const WCHAR empty;
350 WCHAR *str = NULL;
352 if (type == REG_MULTI_SZ)
354 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
355 if (size)
357 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
358 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
360 if (flags & FLG_ADDREG_APPEND)
362 if (!str) return TRUE;
363 append_multi_sz_value( hkey, value, str, size );
364 HeapFree( GetProcessHeap(), 0, str );
365 return TRUE;
367 /* else fall through to normal string handling */
369 else
371 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
372 if (size)
374 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
375 SetupGetStringFieldW( context, 5, str, size, NULL );
376 if (type == REG_LINK) size--; /* no terminating null for symlinks */
380 if (type == REG_DWORD)
382 DWORD dw = str ? strtoulW( str, NULL, 0 ) : 0;
383 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
384 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
386 else
388 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
389 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
390 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)&empty, sizeof(WCHAR) );
392 HeapFree( GetProcessHeap(), 0, str );
393 return TRUE;
395 else /* get the binary data */
397 BYTE *data = NULL;
399 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
400 if (size)
402 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
403 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
404 SetupGetBinaryField( context, 5, data, size, NULL );
406 RegSetValueExW( hkey, value, 0, type, data, size );
407 HeapFree( GetProcessHeap(), 0, data );
408 return TRUE;
413 /***********************************************************************
414 * registry_callback
416 * Called once for each AddReg and DelReg entry in a given section.
418 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
420 struct registry_callback_info *info = arg;
421 INFCONTEXT context;
422 HKEY root_key, hkey;
424 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
426 for (; ok; ok = SetupFindNextLine( &context, &context ))
428 DWORD options = 0;
429 WCHAR buffer[MAX_INF_STRING_LENGTH];
430 INT flags;
432 /* get root */
433 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
434 continue;
435 if (!(root_key = get_root_key( buffer, info->default_root )))
436 continue;
438 /* get key */
439 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
440 *buffer = 0;
442 /* get flags */
443 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
445 if (!info->delete)
447 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
449 else
451 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
452 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
454 /* Wine extension: magic support for symlinks */
455 if (flags >> 16 == REG_LINK) options = REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK;
457 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
459 if (RegOpenKeyExW( root_key, buffer, options, MAXIMUM_ALLOWED, &hkey ))
460 continue; /* ignore if it doesn't exist */
462 else
464 DWORD res = RegCreateKeyExW( root_key, buffer, 0, NULL, options,
465 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
466 if (res == ERROR_ALREADY_EXISTS && (options & REG_OPTION_CREATE_LINK))
467 res = RegCreateKeyExW( root_key, buffer, 0, NULL, REG_OPTION_OPEN_LINK,
468 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
469 if (res)
471 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
472 continue;
475 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
477 /* get value name */
478 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
479 *buffer = 0;
481 /* and now do it */
482 if (!do_reg_operation( hkey, buffer, &context, flags ))
484 RegCloseKey( hkey );
485 return FALSE;
487 RegCloseKey( hkey );
489 return TRUE;
493 /***********************************************************************
494 * do_register_dll
496 * Register or unregister a dll.
498 static BOOL do_register_dll( struct register_dll_info *info, const WCHAR *path,
499 INT flags, INT timeout, const WCHAR *args )
501 HMODULE module;
502 HRESULT res;
503 SP_REGISTER_CONTROL_STATUSW status;
504 IMAGE_NT_HEADERS *nt;
506 status.cbSize = sizeof(status);
507 status.FileName = path;
508 status.FailureCode = SPREG_SUCCESS;
509 status.Win32Error = ERROR_SUCCESS;
511 if (info->callback)
513 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
514 (UINT_PTR)&status, !info->unregister ))
516 case FILEOP_ABORT:
517 SetLastError( ERROR_OPERATION_ABORTED );
518 return FALSE;
519 case FILEOP_SKIP:
520 return TRUE;
521 case FILEOP_DOIT:
522 break;
526 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
528 WARN( "could not load %s\n", debugstr_w(path) );
529 status.FailureCode = SPREG_LOADLIBRARY;
530 status.Win32Error = GetLastError();
531 goto done;
534 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
536 /* file is an executable, not a dll */
537 STARTUPINFOW startup;
538 PROCESS_INFORMATION process_info;
539 WCHAR *cmd_line;
540 BOOL res;
541 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
542 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
544 FreeLibrary( module );
545 module = NULL;
546 if (!args) args = default_args;
547 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
548 sprintfW( cmd_line, format, path, args );
549 memset( &startup, 0, sizeof(startup) );
550 startup.cb = sizeof(startup);
551 TRACE( "executing %s\n", debugstr_w(cmd_line) );
552 res = CreateProcessW( path, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &process_info );
553 HeapFree( GetProcessHeap(), 0, cmd_line );
554 if (!res)
556 status.FailureCode = SPREG_LOADLIBRARY;
557 status.Win32Error = GetLastError();
558 goto done;
560 CloseHandle( process_info.hThread );
562 if (WaitForSingleObject( process_info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
564 /* timed out, kill the process */
565 TerminateProcess( process_info.hProcess, 1 );
566 status.FailureCode = SPREG_TIMEOUT;
567 status.Win32Error = ERROR_TIMEOUT;
569 CloseHandle( process_info.hProcess );
570 goto done;
573 if (flags & FLG_REGSVR_DLLREGISTER)
575 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
576 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
578 if (!func)
580 status.FailureCode = SPREG_GETPROCADDR;
581 status.Win32Error = GetLastError();
582 goto done;
585 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
586 res = func();
588 if (FAILED(res))
590 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
591 status.FailureCode = SPREG_REGSVR;
592 status.Win32Error = res;
593 goto done;
597 if (flags & FLG_REGSVR_DLLINSTALL)
599 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
601 if (!func)
603 status.FailureCode = SPREG_GETPROCADDR;
604 status.Win32Error = GetLastError();
605 goto done;
608 TRACE( "calling DllInstall(%d,%s) in %s\n",
609 !info->unregister, debugstr_w(args), debugstr_w(path) );
610 res = func( !info->unregister, args );
612 if (FAILED(res))
614 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
615 status.FailureCode = SPREG_REGSVR;
616 status.Win32Error = res;
617 goto done;
621 done:
622 if (module)
624 if (info->modules_count >= info->modules_size)
626 int new_size = max( 32, info->modules_size * 2 );
627 HMODULE *new = info->modules ?
628 HeapReAlloc( GetProcessHeap(), 0, info->modules, new_size * sizeof(*new) ) :
629 HeapAlloc( GetProcessHeap(), 0, new_size * sizeof(*new) );
630 if (new)
632 info->modules_size = new_size;
633 info->modules = new;
636 if (info->modules_count < info->modules_size) info->modules[info->modules_count++] = module;
637 else FreeLibrary( module );
639 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
640 (UINT_PTR)&status, !info->unregister );
641 return TRUE;
645 /***********************************************************************
646 * register_dlls_callback
648 * Called once for each RegisterDlls entry in a given section.
650 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
652 struct register_dll_info *info = arg;
653 INFCONTEXT context;
654 BOOL ret = TRUE;
655 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
657 for (; ok; ok = SetupFindNextLine( &context, &context ))
659 WCHAR *path, *args, *p;
660 WCHAR buffer[MAX_INF_STRING_LENGTH];
661 INT flags, timeout;
663 /* get directory */
664 if (!(path = PARSER_get_dest_dir( &context ))) continue;
666 /* get dll name */
667 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
668 goto done;
669 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
670 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
671 path = p;
672 p += strlenW(p);
673 if (p == path || p[-1] != '\\') *p++ = '\\';
674 strcpyW( p, buffer );
676 /* get flags */
677 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
679 /* get timeout */
680 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
682 /* get command line */
683 args = NULL;
684 if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
685 args = buffer;
687 ret = do_register_dll( info, path, flags, timeout, args );
689 done:
690 HeapFree( GetProcessHeap(), 0, path );
691 if (!ret) break;
693 return ret;
696 /***********************************************************************
697 * fake_dlls_callback
699 * Called once for each WineFakeDlls entry in a given section.
701 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
703 INFCONTEXT context;
704 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
706 for (; ok; ok = SetupFindNextLine( &context, &context ))
708 WCHAR *path, *p;
709 WCHAR buffer[MAX_INF_STRING_LENGTH];
711 /* get directory */
712 if (!(path = PARSER_get_dest_dir( &context ))) continue;
714 /* get dll name */
715 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
716 goto done;
717 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
718 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
719 path = p;
720 p += strlenW(p);
721 if (p == path || p[-1] != '\\') *p++ = '\\';
722 strcpyW( p, buffer );
724 /* get source dll */
725 if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
726 p = buffer; /* otherwise use target base name as default source */
728 create_fake_dll( path, p ); /* ignore errors */
730 done:
731 HeapFree( GetProcessHeap(), 0, path );
733 return TRUE;
736 /***********************************************************************
737 * update_ini_callback
739 * Called once for each UpdateInis entry in a given section.
741 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
743 INFCONTEXT context;
745 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
747 for (; ok; ok = SetupFindNextLine( &context, &context ))
749 WCHAR buffer[MAX_INF_STRING_LENGTH];
750 WCHAR filename[MAX_INF_STRING_LENGTH];
751 WCHAR section[MAX_INF_STRING_LENGTH];
752 WCHAR entry[MAX_INF_STRING_LENGTH];
753 WCHAR string[MAX_INF_STRING_LENGTH];
754 LPWSTR divider;
756 if (!SetupGetStringFieldW( &context, 1, filename,
757 sizeof(filename)/sizeof(WCHAR), NULL ))
758 continue;
760 if (!SetupGetStringFieldW( &context, 2, section,
761 sizeof(section)/sizeof(WCHAR), NULL ))
762 continue;
764 if (!SetupGetStringFieldW( &context, 4, buffer,
765 sizeof(buffer)/sizeof(WCHAR), NULL ))
766 continue;
768 divider = strchrW(buffer,'=');
769 if (divider)
771 *divider = 0;
772 strcpyW(entry,buffer);
773 divider++;
774 strcpyW(string,divider);
776 else
778 strcpyW(entry,buffer);
779 string[0]=0;
782 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
783 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
784 WritePrivateProfileStringW(section,entry,string,filename);
787 return TRUE;
790 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
792 FIXME( "should update ini fields %s\n", debugstr_w(field) );
793 return TRUE;
796 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
798 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
799 return TRUE;
802 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
804 FIXME( "should do logconf %s\n", debugstr_w(field) );
805 return TRUE;
808 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
810 FIXME( "should do bitreg %s\n", debugstr_w(field) );
811 return TRUE;
814 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
816 WCHAR lnkpath[MAX_PATH];
817 LPWSTR cmdline=NULL, lnkpath_end;
818 unsigned int name_size;
819 INFCONTEXT name_context, context;
820 int attrs=0;
822 static const WCHAR dotlnk[] = {'.','l','n','k',0};
824 TRACE( "(%s)\n", debugstr_w(field) );
826 if (SetupFindFirstLineW( hinf, field, Name, &name_context ))
828 SetupGetIntField( &name_context, 2, &attrs );
829 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
831 else return TRUE;
833 /* calculate filename */
834 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
835 lnkpath_end = lnkpath + strlenW(lnkpath);
836 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
838 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, SubDir, &context ))
840 unsigned int subdir_size;
842 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
843 return TRUE;
845 lnkpath_end += subdir_size - 1;
846 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
849 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
850 return TRUE;
852 lnkpath_end += name_size - 1;
854 if (attrs & FLG_PROFITEM_GROUP)
856 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
858 else
860 IShellLinkW* shelllink=NULL;
861 IPersistFile* persistfile=NULL;
862 HRESULT initresult=E_FAIL;
864 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
865 strcpyW( lnkpath_end, dotlnk );
867 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
869 /* calculate command line */
870 if (SetupFindFirstLineW( hinf, field, CmdLine, &context ))
872 unsigned int dir_len=0, subdir_size=0, filename_size=0;
873 int dirid=0;
874 LPCWSTR dir;
875 LPWSTR cmdline_end;
877 SetupGetIntField( &context, 1, &dirid );
878 dir = DIRID_get_string( dirid );
880 if (dir) dir_len = strlenW(dir);
882 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
883 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
885 if (dir_len && filename_size)
887 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
889 strcpyW( cmdline_end, dir );
890 cmdline_end += dir_len;
891 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
893 if (subdir_size)
895 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
896 cmdline_end += subdir_size-1;
897 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
899 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
900 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
904 if (!cmdline) return TRUE;
906 initresult = CoInitialize(NULL);
908 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
909 &IID_IShellLinkW, (LPVOID*)&shelllink )))
910 goto done;
912 IShellLinkW_SetPath( shelllink, cmdline );
913 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
914 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
916 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
917 IPersistFile_Save( persistfile, lnkpath, FALSE );
918 IPersistFile_Release( persistfile );
920 IShellLinkW_Release( shelllink );
922 done:
923 if (SUCCEEDED(initresult)) CoUninitialize();
924 HeapFree( GetProcessHeap(), 0, cmdline );
927 return TRUE;
930 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
932 FIXME( "should do copy inf %s\n", debugstr_w(field) );
933 return TRUE;
937 /***********************************************************************
938 * iterate_section_fields
940 * Iterate over all fields of a certain key of a certain section
942 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
943 iterate_fields_func callback, void *arg )
945 WCHAR static_buffer[200];
946 WCHAR *buffer = static_buffer;
947 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
948 INFCONTEXT context;
949 BOOL ret = FALSE;
951 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
952 while (ok)
954 UINT i, count = SetupGetFieldCount( &context );
955 for (i = 1; i <= count; i++)
957 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
958 goto done;
959 if (!callback( hinf, buffer, arg ))
961 WARN("callback failed for %s %s err %d\n",
962 debugstr_w(section), debugstr_w(buffer), GetLastError() );
963 goto done;
966 ok = SetupFindNextMatchLineW( &context, key, &context );
968 ret = TRUE;
969 done:
970 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
971 return ret;
975 /***********************************************************************
976 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
978 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
979 PCSTR section, PCSTR src_root, UINT flags )
981 UNICODE_STRING sectionW;
982 BOOL ret = FALSE;
984 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
986 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
987 return FALSE;
989 if (!src_root)
990 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
991 NULL, flags );
992 else
994 UNICODE_STRING srcW;
995 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
997 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
998 srcW.Buffer, flags );
999 RtlFreeUnicodeString( &srcW );
1001 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1003 RtlFreeUnicodeString( &sectionW );
1004 return ret;
1008 /***********************************************************************
1009 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1011 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
1012 PCWSTR section, PCWSTR src_root, UINT flags )
1014 struct files_callback_info info;
1016 info.queue = queue;
1017 info.src_root = src_root;
1018 info.copy_flags = flags;
1019 info.layout = hlayout;
1020 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
1024 /***********************************************************************
1025 * SetupInstallFromInfSectionA (SETUPAPI.@)
1027 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1028 HKEY key_root, PCSTR src_root, UINT copy_flags,
1029 PSP_FILE_CALLBACK_A callback, PVOID context,
1030 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1032 UNICODE_STRING sectionW, src_rootW;
1033 struct callback_WtoA_context ctx;
1034 BOOL ret = FALSE;
1036 src_rootW.Buffer = NULL;
1037 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1039 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1040 return FALSE;
1043 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1045 ctx.orig_context = context;
1046 ctx.orig_handler = callback;
1047 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1048 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1049 &ctx, devinfo, devinfo_data );
1050 RtlFreeUnicodeString( &sectionW );
1052 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1054 RtlFreeUnicodeString( &src_rootW );
1055 return ret;
1059 /***********************************************************************
1060 * SetupInstallFromInfSectionW (SETUPAPI.@)
1062 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1063 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1064 PSP_FILE_CALLBACK_W callback, PVOID context,
1065 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1067 BOOL ret;
1068 int i;
1070 if (flags & SPINST_FILES)
1072 struct files_callback_info info;
1073 HSPFILEQ queue;
1075 if (!(queue = SetupOpenFileQueue())) return FALSE;
1076 info.queue = queue;
1077 info.src_root = src_root;
1078 info.copy_flags = copy_flags;
1079 info.layout = hinf;
1080 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
1081 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
1082 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
1083 SetupCommitFileQueueW( owner, queue, callback, context ));
1084 SetupCloseFileQueue( queue );
1085 if (!ret) return FALSE;
1087 if (flags & SPINST_INIFILES)
1089 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
1090 !iterate_section_fields( hinf, section, UpdateIniFields,
1091 update_ini_fields_callback, NULL ))
1092 return FALSE;
1094 if (flags & SPINST_INI2REG)
1096 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
1097 return FALSE;
1099 if (flags & SPINST_LOGCONFIG)
1101 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
1102 return FALSE;
1104 if (flags & SPINST_REGSVR)
1106 struct register_dll_info info;
1108 info.unregister = FALSE;
1109 info.modules_size = 0;
1110 info.modules_count = 0;
1111 info.modules = NULL;
1112 if (flags & SPINST_REGISTERCALLBACKAWARE)
1114 info.callback = callback;
1115 info.callback_context = context;
1117 else info.callback = NULL;
1119 if (iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
1120 cleanup_fake_dlls();
1121 else
1122 return FALSE;
1124 ret = iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info );
1125 for (i = 0; i < info.modules_count; i++) FreeLibrary( info.modules[i] );
1126 HeapFree( GetProcessHeap(), 0, info.modules );
1127 if (!ret) return FALSE;
1129 if (flags & SPINST_UNREGSVR)
1131 struct register_dll_info info;
1133 info.unregister = TRUE;
1134 info.modules_size = 0;
1135 info.modules_count = 0;
1136 info.modules = NULL;
1137 if (flags & SPINST_REGISTERCALLBACKAWARE)
1139 info.callback = callback;
1140 info.callback_context = context;
1142 else info.callback = NULL;
1144 ret = iterate_section_fields( hinf, section, UnregisterDlls, 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_REGISTRY)
1151 struct registry_callback_info info;
1153 info.default_root = key_root;
1154 info.delete = TRUE;
1155 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
1156 return FALSE;
1157 info.delete = FALSE;
1158 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
1159 return FALSE;
1161 if (flags & SPINST_BITREG)
1163 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1164 return FALSE;
1166 if (flags & SPINST_PROFILEITEMS)
1168 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1169 return FALSE;
1171 if (flags & SPINST_COPYINF)
1173 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1174 return FALSE;
1177 return TRUE;
1181 /***********************************************************************
1182 * InstallHinfSectionW (SETUPAPI.@)
1184 * NOTE: 'cmdline' is <section> <mode> <path> from
1185 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1187 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1189 #ifdef __i386__
1190 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
1191 #elif defined(__x86_64)
1192 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
1193 #else /* FIXME: other platforms */
1194 static const WCHAR nt_platformW[] = {'.','n','t',0};
1195 #endif
1196 static const WCHAR nt_genericW[] = {'.','n','t',0};
1197 static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1199 WCHAR *s, *path, section[MAX_PATH + (sizeof(nt_platformW) + sizeof(servicesW)) / sizeof(WCHAR)];
1200 void *callback_context;
1201 UINT mode;
1202 HINF hinf;
1204 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1206 lstrcpynW( section, cmdline, MAX_PATH );
1208 if (!(s = strchrW( section, ' ' ))) return;
1209 *s++ = 0;
1210 while (*s == ' ') s++;
1211 mode = atoiW( s );
1213 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1214 if (!(s = strchrW( s, ' ' ))) return;
1215 while (*s == ' ') s++;
1216 path = s;
1218 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1219 if (hinf == INVALID_HANDLE_VALUE) return;
1221 if (!(GetVersion() & 0x80000000))
1223 INFCONTEXT context;
1225 /* check for <section>.ntx86 (or corresponding name for the current platform)
1226 * and then <section>.nt */
1227 s = section + strlenW(section);
1228 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1229 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1231 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1232 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1234 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1237 callback_context = SetupInitDefaultQueueCallback( hwnd );
1238 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1239 SetupDefaultQueueCallbackW, callback_context,
1240 NULL, NULL );
1241 SetupTermDefaultQueueCallback( callback_context );
1242 strcatW( section, servicesW );
1243 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1244 SetupCloseInfFile( hinf );
1246 /* FIXME: should check the mode and maybe reboot */
1247 /* there isn't much point in doing that since we */
1248 /* don't yet handle deferred file copies anyway. */
1249 if (mode & 7) TRACE( "should consider reboot, mode %u\n", mode );
1253 /***********************************************************************
1254 * InstallHinfSectionA (SETUPAPI.@)
1256 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1258 UNICODE_STRING cmdlineW;
1260 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1262 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1263 RtlFreeUnicodeString( &cmdlineW );
1268 /***********************************************************************
1269 * add_service
1271 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1273 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1275 struct registry_callback_info info;
1276 SC_HANDLE service;
1277 INFCONTEXT context;
1278 SERVICE_DESCRIPTIONW descr;
1279 WCHAR *display_name, *start_name, *load_order, *binary_path;
1280 INT service_type = 0, start_type = 0, error_control = 0;
1281 DWORD size;
1282 HKEY hkey;
1284 /* first the mandatory fields */
1286 if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1287 !SetupGetIntField( &context, 1, &service_type ))
1289 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1290 return FALSE;
1292 if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1293 !SetupGetIntField( &context, 1, &start_type ))
1295 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1296 return FALSE;
1298 if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1299 !SetupGetIntField( &context, 1, &error_control ))
1301 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1302 return FALSE;
1304 if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1306 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1307 return FALSE;
1310 /* now the optional fields */
1312 display_name = dup_section_line_field( hinf, section, DisplayName, 1 );
1313 start_name = dup_section_line_field( hinf, section, StartName, 1 );
1314 load_order = dup_section_line_field( hinf, section, LoadOrderGroup, 1 );
1315 descr.lpDescription = dup_section_line_field( hinf, section, Description, 1 );
1317 /* FIXME: Dependencies field */
1318 /* FIXME: Security field */
1320 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1321 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1322 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1324 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1325 service_type, start_type, error_control, binary_path,
1326 load_order, NULL, NULL, start_name, NULL );
1327 if (service)
1329 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1331 else
1333 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1334 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1335 if (!service) goto done;
1337 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1338 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1340 QUERY_SERVICE_CONFIGW *config = NULL;
1342 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1343 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1344 config = HeapAlloc( GetProcessHeap(), 0, size );
1345 if (config && QueryServiceConfigW( service, config, size, &size ))
1347 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1348 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1349 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1351 HeapFree( GetProcessHeap(), 0, display_name );
1352 display_name = strdupW( config->lpDisplayName );
1354 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1356 HeapFree( GetProcessHeap(), 0, load_order );
1357 load_order = strdupW( config->lpLoadOrderGroup );
1360 HeapFree( GetProcessHeap(), 0, config );
1362 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1363 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1364 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1366 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1367 load_order, NULL, NULL, start_name, NULL, display_name );
1369 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1370 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1373 /* execute the AddReg, DelReg and BitReg entries */
1375 info.default_root = 0;
1376 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1378 RegOpenKeyW( hkey, name, &info.default_root );
1379 RegCloseKey( hkey );
1381 if (info.default_root)
1383 info.delete = TRUE;
1384 iterate_section_fields( hinf, section, DelReg, registry_callback, &info );
1385 info.delete = FALSE;
1386 iterate_section_fields( hinf, section, AddReg, registry_callback, &info );
1387 RegCloseKey( info.default_root );
1389 iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1391 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1392 CloseServiceHandle( service );
1394 done:
1395 if (!service) WARN( "failed err %u\n", GetLastError() );
1396 HeapFree( GetProcessHeap(), 0, binary_path );
1397 HeapFree( GetProcessHeap(), 0, display_name );
1398 HeapFree( GetProcessHeap(), 0, start_name );
1399 HeapFree( GetProcessHeap(), 0, load_order );
1400 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1401 return service != 0;
1405 /***********************************************************************
1406 * del_service
1408 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1410 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1412 BOOL ret;
1413 SC_HANDLE service;
1414 SERVICE_STATUS status;
1416 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1418 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1419 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1420 return FALSE;
1422 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1423 TRACE( "deleting %s\n", debugstr_w(name) );
1424 ret = DeleteService( service );
1425 CloseServiceHandle( service );
1426 return ret;
1430 /***********************************************************************
1431 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1433 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1435 WCHAR service_name[MAX_INF_STRING_LENGTH];
1436 WCHAR service_section[MAX_INF_STRING_LENGTH];
1437 SC_HANDLE scm;
1438 INFCONTEXT context;
1439 INT section_flags;
1440 BOOL ok, ret = FALSE;
1442 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1444 if (!(ok = SetupFindFirstLineW( hinf, section, AddService, &context )))
1445 SetLastError( ERROR_SECTION_NOT_FOUND );
1446 while (ok)
1448 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1449 continue;
1450 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1451 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1452 continue;
1453 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1454 goto done;
1455 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1458 if (!(ok = SetupFindFirstLineW( hinf, section, DelService, &context )))
1459 SetLastError( ERROR_SECTION_NOT_FOUND );
1460 while (ok)
1462 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1463 continue;
1464 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1465 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1466 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1468 if (ret) SetLastError( ERROR_SUCCESS );
1469 done:
1470 CloseServiceHandle( scm );
1471 return ret;
1475 /***********************************************************************
1476 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1478 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1480 UNICODE_STRING SectionNameW;
1481 BOOL ret = FALSE;
1483 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1485 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1486 RtlFreeUnicodeString( &SectionNameW );
1488 else
1489 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1491 return ret;
1495 /***********************************************************************
1496 * SetupGetInfFileListA (SETUPAPI.@)
1498 BOOL WINAPI SetupGetInfFileListA(PCSTR dir, DWORD style, PSTR buffer,
1499 DWORD insize, PDWORD outsize)
1501 UNICODE_STRING dirW;
1502 PWSTR bufferW = NULL;
1503 BOOL ret = FALSE;
1504 DWORD outsizeA, outsizeW;
1506 if ( dir )
1507 RtlCreateUnicodeStringFromAsciiz( &dirW, dir );
1508 else
1509 dirW.Buffer = NULL;
1511 if ( buffer )
1512 bufferW = HeapAlloc( GetProcessHeap(), 0, insize * sizeof( WCHAR ));
1514 ret = SetupGetInfFileListW( dirW.Buffer, style, bufferW, insize, &outsizeW);
1516 if ( ret )
1518 outsizeA = WideCharToMultiByte( CP_ACP, 0, bufferW, outsizeW,
1519 buffer, insize, NULL, NULL);
1520 if ( outsize ) *outsize = outsizeA;
1523 HeapFree( GetProcessHeap(), 0, bufferW );
1524 RtlFreeUnicodeString( &dirW );
1525 return ret;
1529 /***********************************************************************
1530 * SetupGetInfFileListW (SETUPAPI.@)
1532 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1533 DWORD insize, PDWORD outsize)
1535 static WCHAR inf[] = {'\\','*','.','i','n','f',0 };
1536 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1537 DWORD dir_len, name_len = 20, size ;
1538 WIN32_FIND_DATAW finddata;
1539 HANDLE hdl;
1540 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1541 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1543 FIXME( "unknown inf_style(s) 0x%x\n",
1544 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1545 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1546 if( outsize ) *outsize = 1;
1547 return TRUE;
1549 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1551 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1552 if( outsize ) *outsize = 1;
1553 return TRUE;
1555 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1556 FIXME("ignored inf_style(s) %s %s\n",
1557 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1558 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1559 if( dir )
1561 DWORD att;
1562 DWORD msize;
1563 dir_len = strlenW( dir );
1564 if ( !dir_len ) return FALSE;
1565 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1566 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1567 if( !filter )
1569 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1570 return FALSE;
1572 strcpyW( filter, dir );
1573 if ( '\\' == filter[dir_len - 1] )
1574 filter[--dir_len] = 0;
1576 att = GetFileAttributesW( filter );
1577 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1579 HeapFree( GetProcessHeap(), 0, filter );
1580 SetLastError( ERROR_DIRECTORY );
1581 return FALSE;
1584 else
1586 WCHAR infdir[] = {'\\','i','n','f',0 };
1587 DWORD msize;
1588 dir_len = GetWindowsDirectoryW( NULL, 0 );
1589 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1590 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1591 if( !filter )
1593 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1594 return FALSE;
1596 GetWindowsDirectoryW( filter, msize );
1597 strcatW( filter, infdir );
1599 strcatW( filter, inf );
1601 hdl = FindFirstFileW( filter , &finddata );
1602 if ( hdl == INVALID_HANDLE_VALUE )
1604 if( outsize ) *outsize = 1;
1605 HeapFree( GetProcessHeap(), 0, filter );
1606 return TRUE;
1608 size = 1;
1611 static const WCHAR key[] =
1612 {'S','i','g','n','a','t','u','r','e',0 };
1613 static const WCHAR section[] =
1614 {'V','e','r','s','i','o','n',0 };
1615 static const WCHAR sig_win4_1[] =
1616 {'$','C','h','i','c','a','g','o','$',0 };
1617 static const WCHAR sig_win4_2[] =
1618 {'$','W','I','N','D','O','W','S',' ','N','T','$',0 };
1619 WCHAR signature[ MAX_PATH ];
1620 BOOL valid = FALSE;
1621 DWORD len = strlenW( finddata.cFileName );
1622 if (!fullname || ( name_len < len ))
1624 name_len = ( name_len < len ) ? len : name_len;
1625 HeapFree( GetProcessHeap(), 0, fullname );
1626 fullname = HeapAlloc( GetProcessHeap(), 0,
1627 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1628 if( !fullname )
1630 HeapFree( GetProcessHeap(), 0, filter );
1631 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1632 return FALSE;
1634 strcpyW( fullname, filter );
1636 fullname[ dir_len + 1] = 0; /* keep '\\' */
1637 strcatW( fullname, finddata.cFileName );
1638 if (!GetPrivateProfileStringW( section, key, NULL, signature, MAX_PATH, fullname ))
1639 signature[0] = 0;
1640 if( INF_STYLE_OLDNT & style )
1641 valid = strcmpiW( sig_win4_1, signature ) &&
1642 strcmpiW( sig_win4_2, signature );
1643 if( INF_STYLE_WIN4 & style )
1644 valid = valid || !strcmpiW( sig_win4_1, signature ) ||
1645 !strcmpiW( sig_win4_2, signature );
1646 if( valid )
1648 size += 1 + strlenW( finddata.cFileName );
1649 if( ptr && insize >= size )
1651 strcpyW( ptr, finddata.cFileName );
1652 ptr += 1 + strlenW( finddata.cFileName );
1653 *ptr = 0;
1657 while( FindNextFileW( hdl, &finddata ));
1658 FindClose( hdl );
1660 HeapFree( GetProcessHeap(), 0, fullname );
1661 HeapFree( GetProcessHeap(), 0, filter );
1662 if( outsize ) *outsize = size;
1663 return TRUE;