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
32 #include "setupapi_private.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
38 /* info passed to callback functions dealing with files */
39 struct files_callback_info
47 /* info passed to callback functions dealing with the registry */
48 struct registry_callback_info
54 /* info passed to callback functions dealing with registering dlls */
55 struct register_dll_info
57 PSP_FILE_CALLBACK_W callback;
58 PVOID callback_context;
62 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
64 /* Unicode constants */
65 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
66 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
67 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
68 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
69 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
70 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
71 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
72 static const WCHAR BitReg[] = {'B','i','t','R','e','g',0};
73 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
74 static const WCHAR CopyINF[] = {'C','o','p','y','I','N','F',0};
75 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
76 static const WCHAR RegisterDlls[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
77 static const WCHAR UnregisterDlls[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
78 static const WCHAR ProfileItems[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
79 static const WCHAR WineFakeDlls[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
82 /***********************************************************************
85 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
87 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
88 WCHAR *static_buffer, DWORD *size )
92 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
93 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
95 /* now grow the buffer */
96 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
97 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
99 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
101 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
106 /***********************************************************************
107 * copy_files_callback
109 * Called once for each CopyFiles entry in a given section.
111 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
113 struct files_callback_info *info = arg;
115 if (field[0] == '@') /* special case: copy single file */
116 SetupQueueDefaultCopyW( info->queue, info->layout, info->src_root, NULL, field, info->copy_flags );
118 SetupQueueCopySectionW( info->queue, info->src_root, info->layout, hinf, field, info->copy_flags );
123 /***********************************************************************
124 * delete_files_callback
126 * Called once for each DelFiles entry in a given section.
128 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
130 struct files_callback_info *info = arg;
131 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
136 /***********************************************************************
137 * rename_files_callback
139 * Called once for each RenFiles entry in a given section.
141 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
143 struct files_callback_info *info = arg;
144 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
149 /***********************************************************************
152 * Retrieve the registry root key from its name.
154 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
156 static const WCHAR HKCR[] = {'H','K','C','R',0};
157 static const WCHAR HKCU[] = {'H','K','C','U',0};
158 static const WCHAR HKLM[] = {'H','K','L','M',0};
159 static const WCHAR HKU[] = {'H','K','U',0};
160 static const WCHAR HKR[] = {'H','K','R',0};
162 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
163 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
164 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
165 if (!strcmpiW( name, HKU )) return HKEY_USERS;
166 if (!strcmpiW( name, HKR )) return def_root;
171 /***********************************************************************
172 * append_multi_sz_value
174 * Append a multisz string to a multisz registry value.
176 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
179 DWORD size, type, total;
182 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
183 if (type != REG_MULTI_SZ) return;
185 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
186 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
188 /* compare each string against all the existing ones */
192 int len = strlenW(strings) + 1;
194 for (p = buffer; *p; p += strlenW(p) + 1)
195 if (!strcmpiW( p, strings )) break;
197 if (!*p) /* not found, need to append it */
199 memcpy( p, strings, len * sizeof(WCHAR) );
207 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
208 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
211 HeapFree( GetProcessHeap(), 0, buffer );
215 /***********************************************************************
216 * delete_multi_sz_value
218 * Remove a string from a multisz registry value.
220 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
223 WCHAR *buffer, *src, *dst;
225 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
226 if (type != REG_MULTI_SZ) return;
227 /* allocate double the size, one for value before and one for after */
228 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
229 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
234 int len = strlenW(src) + 1;
235 if (strcmpiW( src, string ))
237 memcpy( dst, src, len * sizeof(WCHAR) );
243 if (dst != buffer + 2*size) /* did we remove something? */
245 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
246 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
247 (BYTE *)(buffer + size), dst - (buffer + size) );
250 HeapFree( GetProcessHeap(), 0, buffer );
254 /***********************************************************************
257 * Perform an add/delete registry operation depending on the flags.
259 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
263 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
265 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
267 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
271 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
272 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
273 SetupGetStringFieldW( context, 5, str, size, NULL );
274 delete_multi_sz_value( hkey, value, str );
275 HeapFree( GetProcessHeap(), 0, str );
277 else RegDeleteValueW( hkey, value );
279 else RegDeleteKeyW( hkey, NULL );
283 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
285 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
287 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
288 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
289 if (!exists && (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
292 switch(flags & FLG_ADDREG_TYPE_MASK)
294 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
295 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
296 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
297 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
298 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
299 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
300 default: type = flags >> 16; break;
303 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
304 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
306 static const WCHAR empty;
309 if (type == REG_MULTI_SZ)
311 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
314 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
315 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
317 if (flags & FLG_ADDREG_APPEND)
319 if (!str) return TRUE;
320 append_multi_sz_value( hkey, value, str, size );
321 HeapFree( GetProcessHeap(), 0, str );
324 /* else fall through to normal string handling */
328 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
331 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
332 SetupGetStringFieldW( context, 5, str, size, NULL );
336 if (type == REG_DWORD)
338 DWORD dw = str ? strtoulW( str, NULL, 0 ) : 0;
339 TRACE( "setting dword %s to %x\n", debugstr_w(value), dw );
340 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
344 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
345 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
346 else RegSetValueExW( hkey, value, 0, type, (const BYTE *)&empty, sizeof(WCHAR) );
348 HeapFree( GetProcessHeap(), 0, str );
351 else /* get the binary data */
355 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
358 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
359 TRACE( "setting binary data %s len %d\n", debugstr_w(value), size );
360 SetupGetBinaryField( context, 5, data, size, NULL );
362 RegSetValueExW( hkey, value, 0, type, data, size );
363 HeapFree( GetProcessHeap(), 0, data );
369 /***********************************************************************
372 * Called once for each AddReg and DelReg entry in a given section.
374 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
376 struct registry_callback_info *info = arg;
380 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
382 for (; ok; ok = SetupFindNextLine( &context, &context ))
384 WCHAR buffer[MAX_INF_STRING_LENGTH];
388 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
390 if (!(root_key = get_root_key( buffer, info->default_root )))
394 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
398 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
402 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
406 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
407 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
410 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
412 if (RegOpenKeyW( root_key, buffer, &hkey )) continue; /* ignore if it doesn't exist */
414 else if (RegCreateKeyW( root_key, buffer, &hkey ))
416 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
419 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
422 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
426 if (!do_reg_operation( hkey, buffer, &context, flags ))
437 /***********************************************************************
440 * Register or unregister a dll.
442 static BOOL do_register_dll( const struct register_dll_info *info, const WCHAR *path,
443 INT flags, INT timeout, const WCHAR *args )
447 SP_REGISTER_CONTROL_STATUSW status;
448 IMAGE_NT_HEADERS *nt;
450 status.cbSize = sizeof(status);
451 status.FileName = path;
452 status.FailureCode = SPREG_SUCCESS;
453 status.Win32Error = ERROR_SUCCESS;
457 switch(info->callback( info->callback_context, SPFILENOTIFY_STARTREGISTRATION,
458 (UINT_PTR)&status, !info->unregister ))
461 SetLastError( ERROR_OPERATION_ABORTED );
470 if (!(module = LoadLibraryExW( path, 0, LOAD_WITH_ALTERED_SEARCH_PATH )))
472 WARN( "could not load %s\n", debugstr_w(path) );
473 status.FailureCode = SPREG_LOADLIBRARY;
474 status.Win32Error = GetLastError();
478 if ((nt = RtlImageNtHeader( module )) && !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
480 /* file is an executable, not a dll */
481 STARTUPINFOW startup;
482 PROCESS_INFORMATION info;
485 static const WCHAR format[] = {'"','%','s','"',' ','%','s',0};
486 static const WCHAR default_args[] = {'/','R','e','g','S','e','r','v','e','r',0};
488 FreeLibrary( module );
490 if (!args) args = default_args;
491 cmd_line = HeapAlloc( GetProcessHeap(), 0, (strlenW(path) + strlenW(args) + 4) * sizeof(WCHAR) );
492 sprintfW( cmd_line, format, path, args );
493 memset( &startup, 0, sizeof(startup) );
494 startup.cb = sizeof(startup);
495 TRACE( "executing %s\n", debugstr_w(cmd_line) );
496 res = CreateProcessW( NULL, cmd_line, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info );
497 HeapFree( GetProcessHeap(), 0, cmd_line );
500 status.FailureCode = SPREG_LOADLIBRARY;
501 status.Win32Error = GetLastError();
504 CloseHandle( info.hThread );
506 if (WaitForSingleObject( info.hProcess, timeout*1000 ) == WAIT_TIMEOUT)
508 /* timed out, kill the process */
509 TerminateProcess( info.hProcess, 1 );
510 status.FailureCode = SPREG_TIMEOUT;
511 status.Win32Error = ERROR_TIMEOUT;
513 CloseHandle( info.hProcess );
517 if (flags & FLG_REGSVR_DLLREGISTER)
519 const char *entry_point = info->unregister ? "DllUnregisterServer" : "DllRegisterServer";
520 HRESULT (WINAPI *func)(void) = (void *)GetProcAddress( module, entry_point );
524 status.FailureCode = SPREG_GETPROCADDR;
525 status.Win32Error = GetLastError();
529 TRACE( "calling %s in %s\n", entry_point, debugstr_w(path) );
534 WARN( "calling %s in %s returned error %x\n", entry_point, debugstr_w(path), res );
535 status.FailureCode = SPREG_REGSVR;
536 status.Win32Error = res;
541 if (flags & FLG_REGSVR_DLLINSTALL)
543 HRESULT (WINAPI *func)(BOOL,LPCWSTR) = (void *)GetProcAddress( module, "DllInstall" );
547 status.FailureCode = SPREG_GETPROCADDR;
548 status.Win32Error = GetLastError();
552 TRACE( "calling DllInstall(%d,%s) in %s\n",
553 !info->unregister, debugstr_w(args), debugstr_w(path) );
554 res = func( !info->unregister, args );
558 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path), res );
559 status.FailureCode = SPREG_REGSVR;
560 status.Win32Error = res;
566 if (module) FreeLibrary( module );
567 if (info->callback) info->callback( info->callback_context, SPFILENOTIFY_ENDREGISTRATION,
568 (UINT_PTR)&status, !info->unregister );
573 /***********************************************************************
574 * register_dlls_callback
576 * Called once for each RegisterDlls entry in a given section.
578 static BOOL register_dlls_callback( HINF hinf, PCWSTR field, void *arg )
580 struct register_dll_info *info = arg;
583 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
585 for (; ok; ok = SetupFindNextLine( &context, &context ))
587 WCHAR *path, *args, *p;
588 WCHAR buffer[MAX_INF_STRING_LENGTH];
592 if (!(path = PARSER_get_dest_dir( &context ))) continue;
595 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
597 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
598 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
601 if (p == path || p[-1] != '\\') *p++ = '\\';
602 strcpyW( p, buffer );
605 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
608 if (!SetupGetIntField( &context, 5, &timeout )) timeout = 60;
610 /* get command line */
612 if (SetupGetStringFieldW( &context, 6, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
615 ret = do_register_dll( info, path, flags, timeout, args );
618 HeapFree( GetProcessHeap(), 0, path );
624 /***********************************************************************
627 * Called once for each WineFakeDlls entry in a given section.
629 static BOOL fake_dlls_callback( HINF hinf, PCWSTR field, void *arg )
633 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
635 for (; ok; ok = SetupFindNextLine( &context, &context ))
638 WCHAR buffer[MAX_INF_STRING_LENGTH];
641 if (!(path = PARSER_get_dest_dir( &context ))) continue;
644 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
646 if (!(p = HeapReAlloc( GetProcessHeap(), 0, path,
647 (strlenW(path) + strlenW(buffer) + 2) * sizeof(WCHAR) ))) goto done;
650 if (p == path || p[-1] != '\\') *p++ = '\\';
651 strcpyW( p, buffer );
654 if (SetupGetStringFieldW( &context, 4, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
655 p = buffer; /* otherwise use target base name as default source */
657 create_fake_dll( path, p ); /* ignore errors */
660 HeapFree( GetProcessHeap(), 0, path );
666 /***********************************************************************
667 * update_ini_callback
669 * Called once for each UpdateInis entry in a given section.
671 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
675 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
677 for (; ok; ok = SetupFindNextLine( &context, &context ))
679 WCHAR buffer[MAX_INF_STRING_LENGTH];
680 WCHAR filename[MAX_INF_STRING_LENGTH];
681 WCHAR section[MAX_INF_STRING_LENGTH];
682 WCHAR entry[MAX_INF_STRING_LENGTH];
683 WCHAR string[MAX_INF_STRING_LENGTH];
686 if (!SetupGetStringFieldW( &context, 1, filename,
687 sizeof(filename)/sizeof(WCHAR), NULL ))
690 if (!SetupGetStringFieldW( &context, 2, section,
691 sizeof(section)/sizeof(WCHAR), NULL ))
694 if (!SetupGetStringFieldW( &context, 4, buffer,
695 sizeof(buffer)/sizeof(WCHAR), NULL ))
698 divider = strchrW(buffer,'=');
702 strcpyW(entry,buffer);
704 strcpyW(string,divider);
708 strcpyW(entry,buffer);
712 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
713 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
714 WritePrivateProfileStringW(section,entry,string,filename);
720 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
722 FIXME( "should update ini fields %s\n", debugstr_w(field) );
726 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
728 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
732 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
734 FIXME( "should do logconf %s\n", debugstr_w(field) );
738 static BOOL bitreg_callback( HINF hinf, PCWSTR field, void *arg )
740 FIXME( "should do bitreg %s\n", debugstr_w(field) );
744 static BOOL profile_items_callback( HINF hinf, PCWSTR field, void *arg )
746 FIXME( "should do profile items %s\n", debugstr_w(field) );
750 static BOOL copy_inf_callback( HINF hinf, PCWSTR field, void *arg )
752 FIXME( "should do copy inf %s\n", debugstr_w(field) );
757 /***********************************************************************
758 * iterate_section_fields
760 * Iterate over all fields of a certain key of a certain section
762 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
763 iterate_fields_func callback, void *arg )
765 WCHAR static_buffer[200];
766 WCHAR *buffer = static_buffer;
767 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
771 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
774 UINT i, count = SetupGetFieldCount( &context );
775 for (i = 1; i <= count; i++)
777 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
779 if (!callback( hinf, buffer, arg ))
781 WARN("callback failed for %s %s err %d\n",
782 debugstr_w(section), debugstr_w(buffer), GetLastError() );
786 ok = SetupFindNextMatchLineW( &context, key, &context );
790 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
795 /***********************************************************************
796 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
798 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
799 PCSTR section, PCSTR src_root, UINT flags )
801 UNICODE_STRING sectionW;
804 if (!RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
806 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
810 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
815 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
817 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
818 srcW.Buffer, flags );
819 RtlFreeUnicodeString( &srcW );
821 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
823 RtlFreeUnicodeString( §ionW );
828 /***********************************************************************
829 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
831 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
832 PCWSTR section, PCWSTR src_root, UINT flags )
834 struct files_callback_info info;
837 info.src_root = src_root;
838 info.copy_flags = flags;
839 info.layout = hlayout;
840 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
844 /***********************************************************************
845 * SetupInstallFromInfSectionA (SETUPAPI.@)
847 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
848 HKEY key_root, PCSTR src_root, UINT copy_flags,
849 PSP_FILE_CALLBACK_A callback, PVOID context,
850 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
852 UNICODE_STRING sectionW, src_rootW;
853 struct callback_WtoA_context ctx;
856 src_rootW.Buffer = NULL;
857 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
859 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
863 if (RtlCreateUnicodeStringFromAsciiz( §ionW, section ))
865 ctx.orig_context = context;
866 ctx.orig_handler = callback;
867 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
868 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
869 &ctx, devinfo, devinfo_data );
870 RtlFreeUnicodeString( §ionW );
872 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
874 RtlFreeUnicodeString( &src_rootW );
879 /***********************************************************************
880 * SetupInstallFromInfSectionW (SETUPAPI.@)
882 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
883 HKEY key_root, PCWSTR src_root, UINT copy_flags,
884 PSP_FILE_CALLBACK_W callback, PVOID context,
885 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
887 if (flags & SPINST_FILES)
889 struct files_callback_info info;
893 if (!(queue = SetupOpenFileQueue())) return FALSE;
895 info.src_root = src_root;
896 info.copy_flags = copy_flags;
898 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
899 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
900 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
901 SetupCommitFileQueueW( owner, queue, callback, context ));
902 SetupCloseFileQueue( queue );
903 if (!ret) return FALSE;
905 if (flags & SPINST_INIFILES)
907 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
908 !iterate_section_fields( hinf, section, UpdateIniFields,
909 update_ini_fields_callback, NULL ))
912 if (flags & SPINST_INI2REG)
914 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
917 if (flags & SPINST_LOGCONFIG)
919 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
922 if (flags & SPINST_REGSVR)
924 struct register_dll_info info;
926 info.unregister = FALSE;
927 if (flags & SPINST_REGISTERCALLBACKAWARE)
929 info.callback = callback;
930 info.callback_context = context;
932 else info.callback = NULL;
934 if (!iterate_section_fields( hinf, section, RegisterDlls, register_dlls_callback, &info ))
937 if (!iterate_section_fields( hinf, section, WineFakeDlls, fake_dlls_callback, NULL ))
940 if (flags & SPINST_UNREGSVR)
942 struct register_dll_info info;
944 info.unregister = TRUE;
945 if (flags & SPINST_REGISTERCALLBACKAWARE)
947 info.callback = callback;
948 info.callback_context = context;
950 else info.callback = NULL;
952 if (!iterate_section_fields( hinf, section, UnregisterDlls, register_dlls_callback, &info ))
955 if (flags & SPINST_REGISTRY)
957 struct registry_callback_info info;
959 info.default_root = key_root;
961 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
964 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
967 if (flags & SPINST_BITREG)
969 if (!iterate_section_fields( hinf, section, BitReg, bitreg_callback, NULL ))
972 if (flags & SPINST_PROFILEITEMS)
974 if (!iterate_section_fields( hinf, section, ProfileItems, profile_items_callback, NULL ))
977 if (flags & SPINST_COPYINF)
979 if (!iterate_section_fields( hinf, section, CopyINF, copy_inf_callback, NULL ))
987 /***********************************************************************
988 * InstallHinfSectionW (SETUPAPI.@)
990 * NOTE: 'cmdline' is <section> <mode> <path> from
991 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
993 void WINAPI InstallHinfSectionW( HWND hwnd, HINSTANCE handle, LPCWSTR cmdline, INT show )
996 static const WCHAR nt_platformW[] = {'.','n','t','x','8','6',0};
997 #elif defined(__x86_64)
998 static const WCHAR nt_platformW[] = {'.','n','t','a','m','d','6','4',0};
999 #else /* FIXME: other platforms */
1000 static const WCHAR nt_platformW[] = {'.','n','t',0};
1002 static const WCHAR nt_genericW[] = {'.','n','t',0};
1004 WCHAR *s, *d, *path, section[MAX_PATH + sizeof(nt_platformW)/sizeof(WCHAR)];
1005 void *callback_context;
1006 UINT mode, in_quotes, bcount;
1009 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd, handle, debugstr_w(cmdline));
1011 lstrcpynW( section, cmdline, MAX_PATH );
1013 if (!(s = strchrW( section, ' ' ))) return;
1015 while (*s == ' ') s++;
1018 if (!(s = strchrW( s, ' ' ))) return;
1019 while (*s == ' ') s++;
1021 /* The inf path may be quoted. Code adapted from CommandLineToArgvW() */
1027 if (*s==0 || ((*s=='\t' || *s==' ') && !in_quotes)) {
1028 /* end of this command line argument */
1030 } else if (*s=='\\') {
1034 } else if (*s=='"') {
1036 if ((bcount & 1)==0) {
1037 /* Preceded by an even number of '\', this is half that
1038 * number of '\', plus a quote which we erase.
1041 in_quotes=!in_quotes;
1044 /* Preceded by an odd number of '\', this is half that
1045 * number of '\' followed by a '"'
1053 /* a regular character */
1060 hinf = SetupOpenInfFileW( path, NULL, INF_STYLE_WIN4, NULL );
1061 if (hinf == INVALID_HANDLE_VALUE) return;
1063 if (!(GetVersion() & 0x80000000))
1067 /* check for <section>.ntx86 (or corresponding name for the current platform)
1068 * and then <section>.nt */
1069 s = section + strlenW(section);
1070 memcpy( s, nt_platformW, sizeof(nt_platformW) );
1071 if (!(SetupFindFirstLineW( hinf, section, NULL, &context )))
1073 memcpy( s, nt_genericW, sizeof(nt_genericW) );
1074 if (!(SetupFindFirstLineW( hinf, section, NULL, &context ))) *s = 0;
1076 if (*s) TRACE( "using section %s instead\n", debugstr_w(section) );
1079 callback_context = SetupInitDefaultQueueCallback( hwnd );
1080 SetupInstallFromInfSectionW( hwnd, hinf, section, SPINST_ALL, NULL, NULL, SP_COPY_NEWER,
1081 SetupDefaultQueueCallbackW, callback_context,
1083 SetupTermDefaultQueueCallback( callback_context );
1084 SetupCloseInfFile( hinf );
1086 /* FIXME: should check the mode and maybe reboot */
1087 /* there isn't much point in doing that since we */
1088 /* don't yet handle deferred file copies anyway. */
1092 /***********************************************************************
1093 * InstallHinfSectionA (SETUPAPI.@)
1095 void WINAPI InstallHinfSectionA( HWND hwnd, HINSTANCE handle, LPCSTR cmdline, INT show )
1097 UNICODE_STRING cmdlineW;
1099 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW, cmdline ))
1101 InstallHinfSectionW( hwnd, handle, cmdlineW.Buffer, show );
1102 RtlFreeUnicodeString( &cmdlineW );