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