winmm: dwBytesRecorded controls how much is played, not dwBufferLength.
[wine/hramrach.git] / dlls / setupapi / install.c
blobcb476af52b1a680f60b7eeae551ee23e2da122ed
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;
68 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
70 /* Unicode constants */
71 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
72 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
73 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
74 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
75 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
76 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
77 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
78 static const WCHAR BitReg[] = {'B','i','t','R','e','g',0};
79 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
80 static const WCHAR CopyINF[] = {'C','o','p','y','I','N','F',0};
81 static const WCHAR AddService[] = {'A','d','d','S','e','r','v','i','c','e',0};
82 static const WCHAR DelService[] = {'D','e','l','S','e','r','v','i','c','e',0};
83 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
84 static const WCHAR RegisterDlls[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
85 static const WCHAR UnregisterDlls[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
86 static const WCHAR ProfileItems[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
87 static const WCHAR Name[] = {'N','a','m','e',0};
88 static const WCHAR CmdLine[] = {'C','m','d','L','i','n','e',0};
89 static const WCHAR SubDir[] = {'S','u','b','D','i','r',0};
90 static const WCHAR WineFakeDlls[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
91 static const WCHAR DisplayName[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
92 static const WCHAR Description[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
93 static const WCHAR ServiceBinary[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
94 static const WCHAR StartName[] = {'S','t','a','r','t','N','a','m','e',0};
95 static const WCHAR LoadOrderGroup[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
96 static const WCHAR ServiceType[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
97 static const WCHAR StartType[] = {'S','t','a','r','t','T','y','p','e',0};
98 static const WCHAR ErrorControl[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
100 static const WCHAR ServicesKey[] = {'S','y','s','t','e','m','\\',
101 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
102 'S','e','r','v','i','c','e','s',0};
104 /***********************************************************************
105 * get_field_string
107 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
109 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
110 WCHAR *static_buffer, DWORD *size )
112 DWORD required;
114 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
115 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
117 /* now grow the buffer */
118 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
119 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
120 *size = required;
121 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
123 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
124 return NULL;
128 /***********************************************************************
129 * dup_section_line_field
131 * Retrieve the contents of a field in a newly-allocated buffer.
133 static WCHAR *dup_section_line_field( HINF hinf, const WCHAR *section, const WCHAR *line, DWORD index )
135 INFCONTEXT context;
136 DWORD size;
137 WCHAR *buffer;
139 if (!SetupFindFirstLineW( hinf, section, line, &context )) return NULL;
140 if (!SetupGetStringFieldW( &context, index, NULL, 0, &size )) return NULL;
141 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
142 if (!SetupGetStringFieldW( &context, index, buffer, size, NULL )) buffer[0] = 0;
143 return buffer;
146 /***********************************************************************
147 * copy_files_callback
149 * Called once for each CopyFiles entry in a given section.
151 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
153 struct files_callback_info *info = arg;
155 if (field[0] == '@') /* special case: copy single file */
156 SetupQueueDefaultCopyW( info->queue, info->layout ? info->layout : hinf, info->src_root, NULL, field+1, info->copy_flags );
157 else
158 SetupQueueCopySectionW( info->queue, info->src_root, info->layout ? info->layout : hinf, hinf, field, info->copy_flags );
159 return TRUE;
163 /***********************************************************************
164 * delete_files_callback
166 * Called once for each DelFiles entry in a given section.
168 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
170 struct files_callback_info *info = arg;
171 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
172 return TRUE;
176 /***********************************************************************
177 * rename_files_callback
179 * Called once for each RenFiles entry in a given section.
181 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
183 struct files_callback_info *info = arg;
184 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
185 return TRUE;
189 /***********************************************************************
190 * get_root_key
192 * Retrieve the registry root key from its name.
194 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
196 static const WCHAR HKCR[] = {'H','K','C','R',0};
197 static const WCHAR HKCU[] = {'H','K','C','U',0};
198 static const WCHAR HKLM[] = {'H','K','L','M',0};
199 static const WCHAR HKU[] = {'H','K','U',0};
200 static const WCHAR HKR[] = {'H','K','R',0};
202 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
203 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
204 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
205 if (!strcmpiW( name, HKU )) return HKEY_USERS;
206 if (!strcmpiW( name, HKR )) return def_root;
207 return 0;
211 /***********************************************************************
212 * append_multi_sz_value
214 * Append a multisz string to a multisz registry value.
216 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
217 DWORD str_size )
219 DWORD size, type, total;
220 WCHAR *buffer, *p;
222 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
223 if (type != REG_MULTI_SZ) return;
225 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
226 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
228 /* compare each string against all the existing ones */
229 total = size;
230 while (*strings)
232 int len = strlenW(strings) + 1;
234 for (p = buffer; *p; p += strlenW(p) + 1)
235 if (!strcmpiW( p, strings )) break;
237 if (!*p) /* not found, need to append it */
239 memcpy( p, strings, len * sizeof(WCHAR) );
240 p[len] = 0;
241 total += len;
243 strings += len;
245 if (total != size)
247 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
248 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
250 done:
251 HeapFree( GetProcessHeap(), 0, buffer );
255 /***********************************************************************
256 * delete_multi_sz_value
258 * Remove a string from a multisz registry value.
260 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
262 DWORD size, type;
263 WCHAR *buffer, *src, *dst;
265 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
266 if (type != REG_MULTI_SZ) return;
267 /* allocate double the size, one for value before and one for after */
268 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
269 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
270 src = buffer;
271 dst = buffer + size;
272 while (*src)
274 int len = strlenW(src) + 1;
275 if (strcmpiW( src, string ))
277 memcpy( dst, src, len * sizeof(WCHAR) );
278 dst += len;
280 src += len;
282 *dst++ = 0;
283 if (dst != buffer + 2*size) /* did we remove something? */
285 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
286 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
287 (BYTE *)(buffer + size), dst - (buffer + size) );
289 done:
290 HeapFree( GetProcessHeap(), 0, buffer );
294 /***********************************************************************
295 * do_reg_operation
297 * Perform an add/delete registry operation depending on the flags.
299 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
301 DWORD type, size;
303 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
305 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
307 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
309 WCHAR *str;
311 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
312 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
313 SetupGetStringFieldW( context, 5, str, size, NULL );
314 delete_multi_sz_value( hkey, value, str );
315 HeapFree( GetProcessHeap(), 0, str );
317 else RegDeleteValueW( hkey, value );
319 else NtDeleteKey( hkey );
320 return TRUE;
323 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
325 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
327 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
328 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
329 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
332 switch(flags & FLG_ADDREG_TYPE_MASK)
334 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
335 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
336 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
337 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
338 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
339 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
340 default: type = flags >> 16; break;
343 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
344 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
346 static const WCHAR empty;
347 WCHAR *str = NULL;
349 if (type == REG_MULTI_SZ)
351 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
352 if (size)
354 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
355 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
357 if (flags & FLG_ADDREG_APPEND)
359 if (!str) return TRUE;
360 append_multi_sz_value( hkey, value, str, size );
361 HeapFree( GetProcessHeap(), 0, str );
362 return TRUE;
364 /* else fall through to normal string handling */
366 else
368 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
369 if (size)
371 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
372 SetupGetStringFieldW( context, 5, str, size, NULL );
373 if (type == REG_LINK) size--; /* no terminating null for symlinks */
377 if (type == REG_DWORD)
379 DWORD dw = str ? strtoulW( str, NULL, 0 ) : 0;
380 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
381 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
383 else
385 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
386 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
387 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)&empty, sizeof(WCHAR) );
389 HeapFree( GetProcessHeap(), 0, str );
390 return TRUE;
392 else /* get the binary data */
394 BYTE *data = NULL;
396 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
397 if (size)
399 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
400 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
401 SetupGetBinaryField( context, 5, data, size, NULL );
403 RegSetValueExW( hkey, value, 0, type, data, size );
404 HeapFree( GetProcessHeap(), 0, data );
405 return TRUE;
410 /***********************************************************************
411 * registry_callback
413 * Called once for each AddReg and DelReg entry in a given section.
415 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
417 struct registry_callback_info *info = arg;
418 INFCONTEXT context;
419 HKEY root_key, hkey;
421 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
423 for (; ok; ok = SetupFindNextLine( &context, &context ))
425 DWORD options = 0;
426 WCHAR buffer[MAX_INF_STRING_LENGTH];
427 INT flags;
429 /* get root */
430 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
431 continue;
432 if (!(root_key = get_root_key( buffer, info->default_root )))
433 continue;
435 /* get key */
436 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
437 *buffer = 0;
439 /* get flags */
440 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
442 if (!info->delete)
444 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
446 else
448 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
449 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
451 /* Wine extension: magic support for symlinks */
452 if (flags >> 16 == REG_LINK) options = REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK;
454 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
456 if (RegOpenKeyExW( root_key, buffer, options, MAXIMUM_ALLOWED, &hkey ))
457 continue; /* ignore if it doesn't exist */
459 else
461 DWORD res = RegCreateKeyExW( root_key, buffer, 0, NULL, options,
462 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
463 if (res == ERROR_ALREADY_EXISTS && (options & REG_OPTION_CREATE_LINK))
464 res = RegCreateKeyExW( root_key, buffer, 0, NULL, REG_OPTION_OPEN_LINK,
465 MAXIMUM_ALLOWED, NULL, &hkey, NULL );
466 if (res)
468 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
469 continue;
472 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
474 /* get value name */
475 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
476 *buffer = 0;
478 /* and now do it */
479 if (!do_reg_operation( hkey, buffer, &context, flags ))
481 RegCloseKey( hkey );
482 return FALSE;
484 RegCloseKey( hkey );
486 return TRUE;
490 /***********************************************************************
491 * do_register_dll
493 * Register or unregister a dll.
495 static BOOL do_register_dll( const struct register_dll_info *info, const WCHAR *path,
496 INT flags, INT timeout, const WCHAR *args )
498 HMODULE module;
499 HRESULT res;
500 SP_REGISTER_CONTROL_STATUSW status;
501 IMAGE_NT_HEADERS *nt;
503 status.cbSize = sizeof(status);
504 status.FileName = path;
505 status.FailureCode = SPREG_SUCCESS;
506 status.Win32Error = ERROR_SUCCESS;
508 if (info->callback)
510 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
511 (UINT_PTR)&status, !info->unregister ))
513 case FILEOP_ABORT:
514 SetLastError( ERROR_OPERATION_ABORTED );
515 return FALSE;
516 case FILEOP_SKIP:
517 return TRUE;
518 case FILEOP_DOIT:
519 break;
523 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
525 WARN( "could not load %s\n", debugstr_w(path) );
526 status.FailureCode = SPREG_LOADLIBRARY;
527 status.Win32Error = GetLastError();
528 goto done;
531 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
533 /* file is an executable, not a dll */
534 STARTUPINFOW startup;
535 PROCESS_INFORMATION info;
536 WCHAR *cmd_line;
537 BOOL res;
538 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
539 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
541 FreeLibrary( module );
542 module = NULL;
543 if (!args) args = default_args;
544 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
545 sprintfW( cmd_line, format, path, args );
546 memset( &startup, 0, sizeof(startup) );
547 startup.cb = sizeof(startup);
548 TRACE( "executing %s\n", debugstr_w(cmd_line) );
549 res = CreateProcessW( NULL, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
550 HeapFree( GetProcessHeap(), 0, cmd_line );
551 if (!res)
553 status.FailureCode = SPREG_LOADLIBRARY;
554 status.Win32Error = GetLastError();
555 goto done;
557 CloseHandle( info.hThread );
559 if (WaitForSingleObject( info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
561 /* timed out, kill the process */
562 TerminateProcess( info.hProcess, 1 );
563 status.FailureCode = SPREG_TIMEOUT;
564 status.Win32Error = ERROR_TIMEOUT;
566 CloseHandle( info.hProcess );
567 goto done;
570 if (flags & FLG_REGSVR_DLLREGISTER)
572 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
573 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
575 if (!func)
577 status.FailureCode = SPREG_GETPROCADDR;
578 status.Win32Error = GetLastError();
579 goto done;
582 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
583 res = func();
585 if (FAILED(res))
587 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
588 status.FailureCode = SPREG_REGSVR;
589 status.Win32Error = res;
590 goto done;
594 if (flags & FLG_REGSVR_DLLINSTALL)
596 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
598 if (!func)
600 status.FailureCode = SPREG_GETPROCADDR;
601 status.Win32Error = GetLastError();
602 goto done;
605 TRACE( "calling DllInstall(%d,%s) in %s\n",
606 !info->unregister, debugstr_w(args), debugstr_w(path) );
607 res = func( !info->unregister, args );
609 if (FAILED(res))
611 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
612 status.FailureCode = SPREG_REGSVR;
613 status.Win32Error = res;
614 goto done;
618 done:
619 if (module) FreeLibrary( module );
620 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
621 (UINT_PTR)&status, !info->unregister );
622 return TRUE;
626 /***********************************************************************
627 * register_dlls_callback
629 * Called once for each RegisterDlls entry in a given section.
631 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
633 struct register_dll_info *info = arg;
634 INFCONTEXT context;
635 BOOL ret = TRUE;
636 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
638 for (; ok; ok = SetupFindNextLine( &context, &context ))
640 WCHAR *path, *args, *p;
641 WCHAR buffer[MAX_INF_STRING_LENGTH];
642 INT flags, timeout;
644 /* get directory */
645 if (!(path = PARSER_get_dest_dir( &context ))) continue;
647 /* get dll name */
648 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
649 goto done;
650 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
651 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
652 path = p;
653 p += strlenW(p);
654 if (p == path || p[-1] != '\\') *p++ = '\\';
655 strcpyW( p, buffer );
657 /* get flags */
658 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
660 /* get timeout */
661 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
663 /* get command line */
664 args = NULL;
665 if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
666 args = buffer;
668 ret = do_register_dll( info, path, flags, timeout, args );
670 done:
671 HeapFree( GetProcessHeap(), 0, path );
672 if (!ret) break;
674 return ret;
677 /***********************************************************************
678 * fake_dlls_callback
680 * Called once for each WineFakeDlls entry in a given section.
682 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
684 INFCONTEXT context;
685 BOOL ret = TRUE;
686 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
688 for (; ok; ok = SetupFindNextLine( &context, &context ))
690 WCHAR *path, *p;
691 WCHAR buffer[MAX_INF_STRING_LENGTH];
693 /* get directory */
694 if (!(path = PARSER_get_dest_dir( &context ))) continue;
696 /* get dll name */
697 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
698 goto done;
699 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
700 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
701 path = p;
702 p += strlenW(p);
703 if (p == path || p[-1] != '\\') *p++ = '\\';
704 strcpyW( p, buffer );
706 /* get source dll */
707 if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
708 p = buffer; /* otherwise use target base name as default source */
710 create_fake_dll( path, p ); /* ignore errors */
712 done:
713 HeapFree( GetProcessHeap(), 0, path );
714 if (!ret) break;
716 cleanup_fake_dlls();
717 return ret;
720 /***********************************************************************
721 * update_ini_callback
723 * Called once for each UpdateInis entry in a given section.
725 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
727 INFCONTEXT context;
729 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
731 for (; ok; ok = SetupFindNextLine( &context, &context ))
733 WCHAR buffer[MAX_INF_STRING_LENGTH];
734 WCHAR filename[MAX_INF_STRING_LENGTH];
735 WCHAR section[MAX_INF_STRING_LENGTH];
736 WCHAR entry[MAX_INF_STRING_LENGTH];
737 WCHAR string[MAX_INF_STRING_LENGTH];
738 LPWSTR divider;
740 if (!SetupGetStringFieldW( &context, 1, filename,
741 sizeof(filename)/sizeof(WCHAR), NULL ))
742 continue;
744 if (!SetupGetStringFieldW( &context, 2, section,
745 sizeof(section)/sizeof(WCHAR), NULL ))
746 continue;
748 if (!SetupGetStringFieldW( &context, 4, buffer,
749 sizeof(buffer)/sizeof(WCHAR), NULL ))
750 continue;
752 divider = strchrW(buffer,'=');
753 if (divider)
755 *divider = 0;
756 strcpyW(entry,buffer);
757 divider++;
758 strcpyW(string,divider);
760 else
762 strcpyW(entry,buffer);
763 string[0]=0;
766 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
767 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
768 WritePrivateProfileStringW(section,entry,string,filename);
771 return TRUE;
774 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
776 FIXME( "should update ini fields %s\n", debugstr_w(field) );
777 return TRUE;
780 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
782 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
783 return TRUE;
786 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
788 FIXME( "should do logconf %s\n", debugstr_w(field) );
789 return TRUE;
792 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
794 FIXME( "should do bitreg %s\n", debugstr_w(field) );
795 return TRUE;
798 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
800 WCHAR lnkpath[MAX_PATH];
801 LPWSTR cmdline=NULL, lnkpath_end;
802 unsigned int name_size;
803 INFCONTEXT name_context, context;
804 int attrs=0;
806 static const WCHAR dotlnk[] = {'.','l','n','k',0};
808 TRACE( "(%s)\n", debugstr_w(field) );
810 if (SetupFindFirstLineW( hinf, field, Name, &name_context ))
812 SetupGetIntField( &name_context, 2, &attrs );
813 if (attrs & ~FLG_PROFITEM_GROUP) FIXME( "unhandled attributes: %x\n", attrs );
815 else return TRUE;
817 /* calculate filename */
818 SHGetFolderPathW( NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, lnkpath );
819 lnkpath_end = lnkpath + strlenW(lnkpath);
820 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
822 if (!(attrs & FLG_PROFITEM_GROUP) && SetupFindFirstLineW( hinf, field, SubDir, &context ))
824 unsigned int subdir_size;
826 if (!SetupGetStringFieldW( &context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &subdir_size ))
827 return TRUE;
829 lnkpath_end += subdir_size - 1;
830 if (lnkpath_end[-1] != '\\') *lnkpath_end++ = '\\';
833 if (!SetupGetStringFieldW( &name_context, 1, lnkpath_end, (lnkpath+MAX_PATH)-lnkpath_end, &name_size ))
834 return TRUE;
836 lnkpath_end += name_size - 1;
838 if (attrs & FLG_PROFITEM_GROUP)
840 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE );
842 else
844 IShellLinkW* shelllink=NULL;
845 IPersistFile* persistfile=NULL;
846 HRESULT initresult=E_FAIL;
848 if (lnkpath+MAX_PATH < lnkpath_end + 5) return TRUE;
849 strcpyW( lnkpath_end, dotlnk );
851 TRACE( "link path: %s\n", debugstr_w(lnkpath) );
853 /* calculate command line */
854 if (SetupFindFirstLineW( hinf, field, CmdLine, &context ))
856 unsigned int dir_len=0, subdir_size=0, filename_size=0;
857 int dirid=0;
858 LPCWSTR dir;
859 LPWSTR cmdline_end;
861 SetupGetIntField( &context, 1, &dirid );
862 dir = DIRID_get_string( dirid );
864 if (dir) dir_len = strlenW(dir);
866 SetupGetStringFieldW( &context, 2, NULL, 0, &subdir_size );
867 SetupGetStringFieldW( &context, 3, NULL, 0, &filename_size );
869 if (dir_len && filename_size)
871 cmdline = cmdline_end = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (dir_len+subdir_size+filename_size+1) );
873 strcpyW( cmdline_end, dir );
874 cmdline_end += dir_len;
875 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
877 if (subdir_size)
879 SetupGetStringFieldW( &context, 2, cmdline_end, subdir_size, NULL );
880 cmdline_end += subdir_size-1;
881 if (cmdline_end[-1] != '\\') *cmdline_end++ = '\\';
883 SetupGetStringFieldW( &context, 3, cmdline_end, filename_size, NULL );
884 TRACE( "cmdline: %s\n", debugstr_w(cmdline));
888 if (!cmdline) return TRUE;
890 initresult = CoInitialize(NULL);
892 if (FAILED(CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
893 &IID_IShellLinkW, (LPVOID*)&shelllink )))
894 goto done;
896 IShellLinkW_SetPath( shelllink, cmdline );
897 SHPathPrepareForWriteW( NULL, NULL, lnkpath, SHPPFW_DIRCREATE|SHPPFW_IGNOREFILENAME );
898 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink, &IID_IPersistFile, (LPVOID*)&persistfile)))
900 TRACE( "writing link: %s\n", debugstr_w(lnkpath) );
901 IPersistFile_Save( persistfile, lnkpath, FALSE );
902 IPersistFile_Release( persistfile );
904 IShellLinkW_Release( shelllink );
906 done:
907 if (SUCCEEDED(initresult)) CoUninitialize();
908 HeapFree( GetProcessHeap(), 0, cmdline );
911 return TRUE;
914 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
916 FIXME( "should do copy inf %s\n", debugstr_w(field) );
917 return TRUE;
921 /***********************************************************************
922 * iterate_section_fields
924 * Iterate over all fields of a certain key of a certain section
926 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
927 iterate_fields_func callback, void *arg )
929 WCHAR static_buffer[200];
930 WCHAR *buffer = static_buffer;
931 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
932 INFCONTEXT context;
933 BOOL ret = FALSE;
935 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
936 while (ok)
938 UINT i, count = SetupGetFieldCount( &context );
939 for (i = 1; i <= count; i++)
941 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
942 goto done;
943 if (!callback( hinf, buffer, arg ))
945 WARN("callback failed for %s %s err %d\n",
946 debugstr_w(section), debugstr_w(buffer), GetLastError() );
947 goto done;
950 ok = SetupFindNextMatchLineW( &context, key, &context );
952 ret = TRUE;
953 done:
954 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
955 return ret;
959 /***********************************************************************
960 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
962 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
963 PCSTR section, PCSTR src_root, UINT flags )
965 UNICODE_STRING sectionW;
966 BOOL ret = FALSE;
968 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
970 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
971 return FALSE;
973 if (!src_root)
974 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
975 NULL, flags );
976 else
978 UNICODE_STRING srcW;
979 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
981 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
982 srcW.Buffer, flags );
983 RtlFreeUnicodeString( &srcW );
985 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
987 RtlFreeUnicodeString( &sectionW );
988 return ret;
992 /***********************************************************************
993 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
995 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
996 PCWSTR section, PCWSTR src_root, UINT flags )
998 struct files_callback_info info;
1000 info.queue = queue;
1001 info.src_root = src_root;
1002 info.copy_flags = flags;
1003 info.layout = hlayout;
1004 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
1008 /***********************************************************************
1009 * SetupInstallFromInfSectionA (SETUPAPI.@)
1011 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
1012 HKEY key_root, PCSTR src_root, UINT copy_flags,
1013 PSP_FILE_CALLBACK_A callback, PVOID context,
1014 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1016 UNICODE_STRING sectionW, src_rootW;
1017 struct callback_WtoA_context ctx;
1018 BOOL ret = FALSE;
1020 src_rootW.Buffer = NULL;
1021 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
1023 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1024 return FALSE;
1027 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
1029 ctx.orig_context = context;
1030 ctx.orig_handler = callback;
1031 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
1032 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
1033 &ctx, devinfo, devinfo_data );
1034 RtlFreeUnicodeString( &sectionW );
1036 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1038 RtlFreeUnicodeString( &src_rootW );
1039 return ret;
1043 /***********************************************************************
1044 * SetupInstallFromInfSectionW (SETUPAPI.@)
1046 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
1047 HKEY key_root, PCWSTR src_root, UINT copy_flags,
1048 PSP_FILE_CALLBACK_W callback, PVOID context,
1049 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
1051 if (flags & SPINST_FILES)
1053 struct files_callback_info info;
1054 HSPFILEQ queue;
1055 BOOL ret;
1057 if (!(queue = SetupOpenFileQueue())) return FALSE;
1058 info.queue = queue;
1059 info.src_root = src_root;
1060 info.copy_flags = copy_flags;
1061 info.layout = hinf;
1062 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
1063 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
1064 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
1065 SetupCommitFileQueueW( owner, queue, callback, context ));
1066 SetupCloseFileQueue( queue );
1067 if (!ret) return FALSE;
1069 if (flags & SPINST_INIFILES)
1071 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
1072 !iterate_section_fields( hinf, section, UpdateIniFields,
1073 update_ini_fields_callback, NULL ))
1074 return FALSE;
1076 if (flags & SPINST_INI2REG)
1078 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
1079 return FALSE;
1081 if (flags & SPINST_LOGCONFIG)
1083 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
1084 return FALSE;
1086 if (flags & SPINST_REGSVR)
1088 struct register_dll_info info;
1090 info.unregister = FALSE;
1091 if (flags & SPINST_REGISTERCALLBACKAWARE)
1093 info.callback = callback;
1094 info.callback_context = context;
1096 else info.callback = NULL;
1098 if (!iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
1099 return FALSE;
1101 if (!iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info ))
1102 return FALSE;
1104 if (flags & SPINST_UNREGSVR)
1106 struct register_dll_info info;
1108 info.unregister = TRUE;
1109 if (flags & SPINST_REGISTERCALLBACKAWARE)
1111 info.callback = callback;
1112 info.callback_context = context;
1114 else info.callback = NULL;
1116 if (!iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info ))
1117 return FALSE;
1119 if (flags & SPINST_REGISTRY)
1121 struct registry_callback_info info;
1123 info.default_root = key_root;
1124 info.delete = TRUE;
1125 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
1126 return FALSE;
1127 info.delete = FALSE;
1128 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
1129 return FALSE;
1131 if (flags & SPINST_BITREG)
1133 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
1134 return FALSE;
1136 if (flags & SPINST_PROFILEITEMS)
1138 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
1139 return FALSE;
1141 if (flags & SPINST_COPYINF)
1143 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
1144 return FALSE;
1147 return TRUE;
1151 /***********************************************************************
1152 * InstallHinfSectionW (SETUPAPI.@)
1154 * NOTE: 'cmdline' is <section> <mode> <path> from
1155 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1157 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
1159 #ifdef __i386__
1160 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
1161 #elif defined(__x86_64)
1162 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
1163 #else /* FIXME: other platforms */
1164 static const WCHAR nt_platformW[] = {'.','n','t',0};
1165 #endif
1166 static const WCHAR nt_genericW[] = {'.','n','t',0};
1167 static const WCHAR servicesW[] = {'.','S','e','r','v','i','c','e','s',0};
1169 WCHAR *s, *path, section[MAX_PATH + (sizeof(nt_platformW) + sizeof(servicesW)) / sizeof(WCHAR)];
1170 void *callback_context;
1171 UINT mode;
1172 HINF hinf;
1174 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1176 lstrcpynW( section, cmdline, MAX_PATH );
1178 if (!(s = strchrW( section, ' ' ))) return;
1179 *s++ = 0;
1180 while (*s == ' ') s++;
1181 mode = atoiW( s );
1183 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1184 if (!(s = strchrW( s, ' ' ))) return;
1185 while (*s == ' ') s++;
1186 path = s;
1188 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1189 if (hinf == INVALID_HANDLE_VALUE) return;
1191 if (!(GetVersion() & 0x80000000))
1193 INFCONTEXT context;
1195 /* check for <section>.ntx86 (or corresponding name for the current platform)
1196 * and then <section>.nt */
1197 s = section + strlenW(section);
1198 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1199 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1201 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1202 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1204 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1207 callback_context = SetupInitDefaultQueueCallback( hwnd );
1208 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1209 SetupDefaultQueueCallbackW, callback_context,
1210 NULL, NULL );
1211 SetupTermDefaultQueueCallback( callback_context );
1212 strcatW( section, servicesW );
1213 SetupInstallServicesFromInfSectionW( hinf, section, 0 );
1214 SetupCloseInfFile( hinf );
1216 /* FIXME: should check the mode and maybe reboot */
1217 /* there isn't much point in doing that since we */
1218 /* don't yet handle deferred file copies anyway. */
1222 /***********************************************************************
1223 * InstallHinfSectionA (SETUPAPI.@)
1225 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1227 UNICODE_STRING cmdlineW;
1229 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1231 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1232 RtlFreeUnicodeString( &cmdlineW );
1237 /***********************************************************************
1238 * add_service
1240 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1242 static BOOL add_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, const WCHAR *section, DWORD flags )
1244 struct registry_callback_info info;
1245 SC_HANDLE service;
1246 INFCONTEXT context;
1247 SERVICE_DESCRIPTIONW descr;
1248 WCHAR *display_name, *start_name, *load_order, *binary_path;
1249 INT service_type = 0, start_type = 0, error_control = 0;
1250 DWORD size;
1251 HKEY hkey;
1253 /* first the mandatory fields */
1255 if (!SetupFindFirstLineW( hinf, section, ServiceType, &context ) ||
1256 !SetupGetIntField( &context, 1, &service_type ))
1258 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1259 return FALSE;
1261 if (!SetupFindFirstLineW( hinf, section, StartType, &context ) ||
1262 !SetupGetIntField( &context, 1, &start_type ))
1264 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1265 return FALSE;
1267 if (!SetupFindFirstLineW( hinf, section, ErrorControl, &context ) ||
1268 !SetupGetIntField( &context, 1, &error_control ))
1270 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1271 return FALSE;
1273 if (!(binary_path = dup_section_line_field( hinf, section, ServiceBinary, 1 )))
1275 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT );
1276 return FALSE;
1279 /* now the optional fields */
1281 display_name = dup_section_line_field( hinf, section, DisplayName, 1 );
1282 start_name = dup_section_line_field( hinf, section, StartName, 1 );
1283 load_order = dup_section_line_field( hinf, section, LoadOrderGroup, 1 );
1284 descr.lpDescription = dup_section_line_field( hinf, section, Description, 1 );
1286 /* FIXME: Dependencies field */
1287 /* FIXME: Security field */
1289 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1290 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1291 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name), flags );
1293 service = CreateServiceW( scm, name, display_name, SERVICE_ALL_ACCESS,
1294 service_type, start_type, error_control, binary_path,
1295 load_order, NULL, NULL, start_name, NULL );
1296 if (service)
1298 if (descr.lpDescription) ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1300 else
1302 if (GetLastError() != ERROR_SERVICE_EXISTS) goto done;
1303 service = OpenServiceW( scm, name, SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG|SERVICE_START );
1304 if (!service) goto done;
1306 if (flags & (SPSVCINST_NOCLOBBER_DISPLAYNAME | SPSVCINST_NOCLOBBER_STARTTYPE |
1307 SPSVCINST_NOCLOBBER_ERRORCONTROL | SPSVCINST_NOCLOBBER_LOADORDERGROUP))
1309 QUERY_SERVICE_CONFIGW *config = NULL;
1311 if (!QueryServiceConfigW( service, NULL, 0, &size ) &&
1312 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1313 config = HeapAlloc( GetProcessHeap(), 0, size );
1314 if (config && QueryServiceConfigW( service, config, size, &size ))
1316 if (flags & SPSVCINST_NOCLOBBER_STARTTYPE) start_type = config->dwStartType;
1317 if (flags & SPSVCINST_NOCLOBBER_ERRORCONTROL) error_control = config->dwErrorControl;
1318 if (flags & SPSVCINST_NOCLOBBER_DISPLAYNAME)
1320 HeapFree( GetProcessHeap(), 0, display_name );
1321 display_name = strdupW( config->lpDisplayName );
1323 if (flags & SPSVCINST_NOCLOBBER_LOADORDERGROUP)
1325 HeapFree( GetProcessHeap(), 0, load_order );
1326 load_order = strdupW( config->lpLoadOrderGroup );
1329 HeapFree( GetProcessHeap(), 0, config );
1331 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1332 debugstr_w(name), debugstr_w(display_name), service_type, start_type, error_control,
1333 debugstr_w(binary_path), debugstr_w(load_order), debugstr_w(start_name) );
1335 ChangeServiceConfigW( service, service_type, start_type, error_control, binary_path,
1336 load_order, NULL, NULL, start_name, NULL, display_name );
1338 if (!(flags & SPSVCINST_NOCLOBBER_DESCRIPTION))
1339 ChangeServiceConfig2W( service, SERVICE_CONFIG_DESCRIPTION, &descr );
1342 /* execute the AddReg, DelReg and BitReg entries */
1344 info.default_root = 0;
1345 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, ServicesKey, &hkey ))
1347 RegOpenKeyW( hkey, name, &info.default_root );
1348 RegCloseKey( hkey );
1350 if (info.default_root)
1352 info.delete = TRUE;
1353 iterate_section_fields( hinf, section, DelReg, registry_callback, &info );
1354 info.delete = FALSE;
1355 iterate_section_fields( hinf, section, AddReg, registry_callback, &info );
1356 RegCloseKey( info.default_root );
1358 iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL );
1360 if (flags & SPSVCINST_STARTSERVICE) StartServiceW( service, 0, NULL );
1361 CloseServiceHandle( service );
1363 done:
1364 if (!service) WARN( "failed err %u\n", GetLastError() );
1365 HeapFree( GetProcessHeap(), 0, binary_path );
1366 HeapFree( GetProcessHeap(), 0, display_name );
1367 HeapFree( GetProcessHeap(), 0, start_name );
1368 HeapFree( GetProcessHeap(), 0, load_order );
1369 HeapFree( GetProcessHeap(), 0, descr.lpDescription );
1370 return service != 0;
1374 /***********************************************************************
1375 * del_service
1377 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1379 static BOOL del_service( SC_HANDLE scm, HINF hinf, const WCHAR *name, DWORD flags )
1381 BOOL ret;
1382 SC_HANDLE service;
1383 SERVICE_STATUS status;
1385 if (!(service = OpenServiceW( scm, name, SERVICE_STOP | DELETE )))
1387 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) return TRUE;
1388 WARN( "cannot open %s err %u\n", debugstr_w(name), GetLastError() );
1389 return FALSE;
1391 if (flags & SPSVCINST_STOPSERVICE) ControlService( service, SERVICE_CONTROL_STOP, &status );
1392 TRACE( "deleting %s\n", debugstr_w(name) );
1393 ret = DeleteService( service );
1394 CloseServiceHandle( service );
1395 return ret;
1399 /***********************************************************************
1400 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1402 BOOL WINAPI SetupInstallServicesFromInfSectionW( HINF hinf, PCWSTR section, DWORD flags )
1404 WCHAR service_name[MAX_INF_STRING_LENGTH];
1405 WCHAR service_section[MAX_INF_STRING_LENGTH];
1406 SC_HANDLE scm;
1407 INFCONTEXT context;
1408 INT section_flags;
1409 BOOL ok, ret = FALSE;
1411 if (!(scm = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS ))) return FALSE;
1413 if (!(ok = SetupFindFirstLineW( hinf, section, AddService, &context )))
1414 SetLastError( ERROR_SECTION_NOT_FOUND );
1415 while (ok)
1417 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1418 continue;
1419 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1420 if (!SetupGetStringFieldW( &context, 3, service_section, MAX_INF_STRING_LENGTH, NULL ))
1421 continue;
1422 if (!(ret = add_service( scm, hinf, service_name, service_section, section_flags | flags )))
1423 goto done;
1424 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1427 if (!(ok = SetupFindFirstLineW( hinf, section, DelService, &context )))
1428 SetLastError( ERROR_SECTION_NOT_FOUND );
1429 while (ok)
1431 if (!SetupGetStringFieldW( &context, 1, service_name, MAX_INF_STRING_LENGTH, NULL ))
1432 continue;
1433 if (!SetupGetIntField( &context, 2, &section_flags )) section_flags = 0;
1434 if (!(ret = del_service( scm, hinf, service_name, section_flags | flags ))) goto done;
1435 ok = SetupFindNextMatchLineW( &context, AddService, &context );
1437 if (ret) SetLastError( ERROR_SUCCESS );
1438 done:
1439 CloseServiceHandle( scm );
1440 return ret;
1444 /***********************************************************************
1445 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1447 BOOL WINAPI SetupInstallServicesFromInfSectionA( HINF Inf, PCSTR SectionName, DWORD Flags)
1449 UNICODE_STRING SectionNameW;
1450 BOOL ret = FALSE;
1452 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW, SectionName ))
1454 ret = SetupInstallServicesFromInfSectionW( Inf, SectionNameW.Buffer, Flags );
1455 RtlFreeUnicodeString( &SectionNameW );
1457 else
1458 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1460 return ret;
1464 /***********************************************************************
1465 * SetupGetInfFileListW (SETUPAPI.@)
1467 BOOL WINAPI SetupGetInfFileListW(PCWSTR dir, DWORD style, PWSTR buffer,
1468 DWORD insize, PDWORD outsize)
1470 static WCHAR inf[] = {'\\','*','.','i','n','f',0 };
1471 WCHAR *filter, *fullname = NULL, *ptr = buffer;
1472 DWORD dir_len, name_len = 20, size ;
1473 WIN32_FIND_DATAW finddata;
1474 HANDLE hdl;
1475 if (style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1476 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1478 FIXME( "unknown inf_style(s) 0x%x\n",
1479 style & ~( INF_STYLE_OLDNT | INF_STYLE_WIN4 |
1480 INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ));
1481 if( outsize ) *outsize = 1;
1482 return TRUE;
1484 if ((style & ( INF_STYLE_OLDNT | INF_STYLE_WIN4 )) == INF_STYLE_NONE)
1486 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1487 if( outsize ) *outsize = 1;
1488 return TRUE;
1490 if (style & ( INF_STYLE_CACHE_ENABLE | INF_STYLE_CACHE_DISABLE ))
1491 FIXME("ignored inf_style(s) %s %s\n",
1492 ( style & INF_STYLE_CACHE_ENABLE ) ? "INF_STYLE_CACHE_ENABLE" : "",
1493 ( style & INF_STYLE_CACHE_DISABLE ) ? "INF_STYLE_CACHE_DISABLE" : "");
1494 if( dir )
1496 DWORD att;
1497 DWORD msize;
1498 dir_len = strlenW( dir );
1499 if ( !dir_len ) return FALSE;
1500 msize = ( 7 + dir_len ) * sizeof( WCHAR ); /* \\*.inf\0 */
1501 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1502 if( !filter )
1504 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1505 return FALSE;
1507 strcpyW( filter, dir );
1508 if ( '\\' == filter[dir_len - 1] )
1509 filter[--dir_len] = 0;
1511 att = GetFileAttributesW( filter );
1512 if (att != INVALID_FILE_ATTRIBUTES && !(att & FILE_ATTRIBUTE_DIRECTORY))
1514 HeapFree( GetProcessHeap(), 0, filter );
1515 SetLastError( ERROR_DIRECTORY );
1516 return FALSE;
1519 else
1521 WCHAR infdir[] = {'\\','i','n','f',0 };
1522 DWORD msize;
1523 dir_len = GetWindowsDirectoryW( NULL, 0 );
1524 msize = ( 7 + 4 + dir_len ) * sizeof( WCHAR );
1525 filter = HeapAlloc( GetProcessHeap(), 0, msize );
1526 if( !filter )
1528 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1529 return FALSE;
1531 GetWindowsDirectoryW( filter, msize );
1532 strcatW( filter, infdir );
1534 strcatW( filter, inf );
1536 hdl = FindFirstFileW( filter , &finddata );
1537 if ( hdl == INVALID_HANDLE_VALUE )
1539 if( outsize ) *outsize = 1;
1540 HeapFree( GetProcessHeap(), 0, filter );
1541 return TRUE;
1543 size = 1;
1546 static const WCHAR key[] =
1547 {'S','i','g','n','a','t','u','r','e',0 };
1548 static const WCHAR section[] =
1549 {'V','e','r','s','i','o','n',0 };
1550 static const WCHAR sig_win4_1[] =
1551 {'$','C','h','i','c','a','g','o','$',0 };
1552 static const WCHAR sig_win4_2[] =
1553 {'$','W','I','N','D','O','W','S',' ','N','T','$',0 };
1554 WCHAR signature[ MAX_PATH ];
1555 BOOL valid = FALSE;
1556 DWORD len = strlenW( finddata.cFileName );
1557 if (!fullname || ( name_len < len ))
1559 name_len = ( name_len < len ) ? len : name_len;
1560 HeapFree( GetProcessHeap(), 0, fullname );
1561 fullname = HeapAlloc( GetProcessHeap(), 0,
1562 ( 2 + dir_len + name_len) * sizeof( WCHAR ));
1563 if( !fullname )
1565 HeapFree( GetProcessHeap(), 0, filter );
1566 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1567 return FALSE;
1569 strcpyW( fullname, filter );
1571 fullname[ dir_len + 1] = 0; /* keep '\\' */
1572 strcatW( fullname, finddata.cFileName );
1573 if (!GetPrivateProfileStringW( section, key, NULL, signature, MAX_PATH, fullname ))
1574 signature[0] = 0;
1575 if( INF_STYLE_OLDNT & style )
1576 valid = strcmpiW( sig_win4_1, signature ) &&
1577 strcmpiW( sig_win4_2, signature );
1578 if( INF_STYLE_WIN4 & style )
1579 valid = valid || !strcmpiW( sig_win4_1, signature ) ||
1580 !strcmpiW( sig_win4_2, signature );
1581 if( valid )
1583 size += 1 + strlenW( finddata.cFileName );
1584 if( ptr && insize >= size )
1586 strcpyW( ptr, finddata.cFileName );
1587 ptr += 1 + strlenW( finddata.cFileName );
1588 *ptr = 0;
1592 while( FindNextFileW( hdl, &finddata ));
1593 FindClose( hdl );
1595 HeapFree( GetProcessHeap(), 0, fullname );
1596 HeapFree( GetProcessHeap(), 0, filter );
1597 if( outsize ) *outsize = size;
1598 return TRUE;