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
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
53 /* info passed to callback functions dealing with the registry */
54 struct registry_callback_info
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
;
71 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
74 /***********************************************************************
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
)
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
;
91 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
93 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
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
)
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;
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
];
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
));
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
)
147 struct files_callback_info
*info
= arg
;
148 WCHAR src_root
[MAX_PATH
], *p
;
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
);
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;
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
);
177 SetupQueueCopySectionW( info
->queue
, info
->src_root
? info
->src_root
: src_root
,
178 info
->layout
? info
->layout
: hinf
, hinf
, field
, info
->copy_flags
);
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
);
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
);
209 /***********************************************************************
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
;
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
,
233 DWORD size
, type
, total
;
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 */
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
) );
255 total
+= len
* sizeof(WCHAR
);
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
);
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
)
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
;
288 int len
= lstrlenW(src
) + 1;
289 if (wcsicmp( src
, string
))
291 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
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
) );
304 HeapFree( GetProcessHeap(), 0, buffer
);
308 /***********************************************************************
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
)
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
)
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
);
335 RegDeleteTreeW( hkey
, NULL
);
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))
366 if (type
== REG_MULTI_SZ
)
368 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
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
);
381 /* else fall through to normal string handling */
385 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
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
) );
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
);
409 else /* get the binary data */
413 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
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
);
427 /***********************************************************************
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
;
438 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
440 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
443 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
447 if (!SetupGetStringFieldW( &context
, 1, buffer
, ARRAY_SIZE( buffer
), NULL
))
449 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
453 if (!SetupGetStringFieldW( &context
, 2, buffer
, ARRAY_SIZE( buffer
), NULL
))
457 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
461 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
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 */
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
);
485 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
489 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
492 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
496 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
507 /***********************************************************************
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
)
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
;
527 switch(info
->callback( info
->callback_context
, SPFILENOTIFY_STARTREGISTRATION
,
528 (UINT_PTR
)&status
, !info
->unregister
))
531 SetLastError( ERROR_OPERATION_ABORTED
);
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();
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
;
557 FreeLibrary( module
);
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
);
570 status
.FailureCode
= SPREG_LOADLIBRARY
;
571 status
.Win32Error
= GetLastError();
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
);
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
);
594 status
.FailureCode
= SPREG_GETPROCADDR
;
595 status
.Win32Error
= GetLastError();
599 TRACE( "calling %s in %s\n", entry_point
, debugstr_w(path
) );
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
;
611 if (flags
& FLG_REGSVR_DLLINSTALL
)
613 HRESULT (WINAPI
*func
)(BOOL
,LPCWSTR
) = (void *)GetProcAddress( module
, "DllInstall" );
617 status
.FailureCode
= SPREG_GETPROCADDR
;
618 status
.Win32Error
= GetLastError();
622 TRACE( "calling DllInstall(%d,%s) in %s\n",
623 !info
->unregister
, debugstr_w(args
), debugstr_w(path
) );
624 res
= func( !info
->unregister
, args
);
628 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path
), res
);
629 status
.FailureCode
= SPREG_REGSVR
;
630 status
.Win32Error
= res
;
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) );
646 info
->modules_size
= new_size
;
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
);
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
;
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
];
678 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
681 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
683 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
684 (lstrlenW(path
) + lstrlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
687 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
688 lstrcpyW( p
, buffer
);
691 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
694 if (!SetupGetIntField( &context
, 5, &timeout
)) timeout
= 60;
696 /* get command line */
698 if (SetupGetStringFieldW( &context
, 6, buffer
, ARRAY_SIZE( buffer
), NULL
))
701 ret
= do_register_dll( info
, path
, flags
, timeout
, args
);
704 HeapFree( GetProcessHeap(), 0, path
);
710 /***********************************************************************
713 * Called once for each WineFakeDlls entry in a given section.
715 static BOOL
fake_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
718 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
720 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
723 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
726 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
729 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
731 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
732 (lstrlenW(path
) + lstrlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
735 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
736 lstrcpyW( p
, buffer
);
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 */
745 HeapFree( GetProcessHeap(), 0, path
);
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
)
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
];
770 if (!SetupGetStringFieldW( &context
, 1, filename
, ARRAY_SIZE( filename
), NULL
))
773 if (!SetupGetStringFieldW( &context
, 2, section
, ARRAY_SIZE( section
), NULL
))
776 if (!SetupGetStringFieldW( &context
, 4, buffer
, ARRAY_SIZE( buffer
), NULL
))
779 divider
= wcschr(buffer
,'=');
783 lstrcpyW(entry
,buffer
);
785 lstrcpyW(string
,divider
);
789 lstrcpyW(entry
,buffer
);
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
);
801 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
803 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
807 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
809 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
813 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
815 FIXME( "should do logconf %s\n", debugstr_w(field
) );
819 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
821 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
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
;
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
);
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
))
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
))
861 lnkpath_end
+= name_size
- 1;
863 if (attrs
& FLG_PROFITEM_GROUP
)
865 SHPathPrepareForWriteW( NULL
, NULL
, lnkpath
, SHPPFW_DIRCREATE
);
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;
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
++ = '\\';
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
)))
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
);
932 if (SUCCEEDED(initresult
)) CoUninitialize();
933 HeapFree( GetProcessHeap(), 0, cmdline
);
939 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
941 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
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
);
960 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
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
)))
968 if (!callback( hinf
, buffer
, arg
))
970 WARN("callback failed for %s %s err %d\n",
971 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
975 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
979 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
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
;
993 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
995 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
999 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
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( §ionW
);
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
;
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
;
1045 src_rootW
.Buffer
= NULL
;
1046 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
1048 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1052 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, 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( §ionW
);
1061 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1063 RtlFreeUnicodeString( &src_rootW
);
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
)
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
))
1088 if (flags
& SPINST_REGSVR
)
1090 if (iterate_section_fields( hinf
, section
, L
"WineFakeDlls", fake_dlls_callback
, NULL
))
1091 cleanup_fake_dlls();
1095 if (flags
& SPINST_FILES
)
1097 struct files_callback_info info
;
1100 if (!(queue
= SetupOpenFileQueue())) return FALSE
;
1102 info
.src_root
= src_root
;
1103 info
.copy_flags
= copy_flags
;
1105 ret
= (iterate_section_fields( hinf
, section
, L
"CopyFiles", copy_files_callback
, &info
) &&
1106 iterate_section_fields( hinf
, section
, L
"DelFiles", delete_files_callback
, &info
) &&
1107 iterate_section_fields( hinf
, section
, L
"RenFiles", rename_files_callback
, &info
) &&
1108 SetupCommitFileQueueW( owner
, queue
, callback
, context
));
1109 SetupCloseFileQueue( queue
);
1110 if (!ret
) return FALSE
;
1112 if (flags
& SPINST_INIFILES
)
1114 if (!iterate_section_fields( hinf
, section
, L
"UpdateInis", update_ini_callback
, NULL
) ||
1115 !iterate_section_fields( hinf
, section
, L
"UpdateIniFields",
1116 update_ini_fields_callback
, NULL
))
1119 if (flags
& SPINST_INI2REG
)
1121 if (!iterate_section_fields( hinf
, section
, L
"Ini2Reg", ini2reg_callback
, NULL
))
1124 if (flags
& SPINST_LOGCONFIG
)
1126 if (!iterate_section_fields( hinf
, section
, L
"LogConf", logconf_callback
, NULL
))
1129 if (flags
& SPINST_REGSVR
)
1131 struct register_dll_info info
= { .unregister
= FALSE
};
1134 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1136 info
.callback
= callback
;
1137 info
.callback_context
= context
;
1140 hr
= CoInitialize(NULL
);
1142 ret
= iterate_section_fields( hinf
, section
, L
"RegisterDlls", register_dlls_callback
, &info
);
1143 for (i
= 0; i
< info
.modules_count
; i
++) FreeLibrary( info
.modules
[i
] );
1148 HeapFree( GetProcessHeap(), 0, info
.modules
);
1149 if (!ret
) return FALSE
;
1151 if (flags
& SPINST_UNREGSVR
)
1153 struct register_dll_info info
= { .unregister
= TRUE
};
1156 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1158 info
.callback
= callback
;
1159 info
.callback_context
= context
;
1162 hr
= CoInitialize(NULL
);
1164 ret
= iterate_section_fields( hinf
, section
, L
"UnregisterDlls", register_dlls_callback
, &info
);
1165 for (i
= 0; i
< info
.modules_count
; i
++) FreeLibrary( info
.modules
[i
] );
1170 HeapFree( GetProcessHeap(), 0, info
.modules
);
1171 if (!ret
) return FALSE
;
1173 if (flags
& SPINST_REGISTRY
)
1175 struct registry_callback_info info
;
1177 info
.default_root
= key_root
;
1179 if (!iterate_section_fields( hinf
, section
, L
"DelReg", registry_callback
, &info
))
1181 info
.delete = FALSE
;
1182 if (!iterate_section_fields( hinf
, section
, L
"AddReg", registry_callback
, &info
))
1185 if (flags
& SPINST_BITREG
)
1187 if (!iterate_section_fields( hinf
, section
, L
"BitReg", bitreg_callback
, NULL
))
1190 if (flags
& SPINST_PROFILEITEMS
)
1192 if (!iterate_section_fields( hinf
, section
, L
"ProfileItems", profile_items_callback
, NULL
))
1195 if (flags
& SPINST_COPYINF
)
1197 if (!iterate_section_fields( hinf
, section
, L
"CopyINF", copy_inf_callback
, NULL
))
1201 SetLastError(ERROR_SUCCESS
);
1206 /***********************************************************************
1207 * InstallHinfSectionW (SETUPAPI.@)
1209 * NOTE: 'cmdline' is <section> <mode> <path> from
1210 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1212 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
1215 static const WCHAR nt_platformW
[] = L
".ntx86";
1216 #elif defined(__x86_64__)
1217 static const WCHAR nt_platformW
[] = L
".ntamd64";
1218 #elif defined(__arm__)
1219 static const WCHAR nt_platformW
[] = L
".ntarm";
1220 #elif defined(__aarch64__)
1221 static const WCHAR nt_platformW
[] = L
".ntarm64";
1222 #else /* FIXME: other platforms */
1223 static const WCHAR nt_platformW
[] = L
".nt";
1226 WCHAR
*s
, *path
, section
[MAX_PATH
+ ARRAY_SIZE( nt_platformW
) + ARRAY_SIZE( L
".Services" )];
1227 void *callback_context
;
1231 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
1233 lstrcpynW( section
, cmdline
, MAX_PATH
);
1235 if (!(s
= wcschr( section
, ' ' ))) return;
1237 while (*s
== ' ') s
++;
1238 mode
= wcstol( s
, NULL
, 10 );
1240 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1241 if (!(s
= wcschr( s
, ' ' ))) return;
1242 while (*s
== ' ') s
++;
1245 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
1246 if (hinf
== INVALID_HANDLE_VALUE
) return;
1248 if (!(GetVersion() & 0x80000000))
1252 /* check for <section>.ntx86 (or corresponding name for the current platform)
1253 * and then <section>.nt */
1254 s
= section
+ lstrlenW(section
);
1255 lstrcpyW( s
, nt_platformW
);
1256 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
)))
1258 lstrcpyW( s
, L
".nt" );
1259 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
))) *s
= 0;
1261 if (*s
) TRACE( "using section %s instead\n", debugstr_w(section
) );
1264 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
1265 SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
, SP_COPY_NEWER
,
1266 SetupDefaultQueueCallbackW
, callback_context
,
1268 SetupTermDefaultQueueCallback( callback_context
);
1269 lstrcatW( section
, L
".Services" );
1270 SetupInstallServicesFromInfSectionW( hinf
, section
, 0 );
1271 SetupCloseInfFile( hinf
);
1273 /* FIXME: should check the mode and maybe reboot */
1274 /* there isn't much point in doing that since we */
1275 /* don't yet handle deferred file copies anyway. */
1276 if (mode
& 7) TRACE( "should consider reboot, mode %u\n", mode
);
1280 /***********************************************************************
1281 * InstallHinfSectionA (SETUPAPI.@)
1283 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
1285 UNICODE_STRING cmdlineW
;
1287 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
1289 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
1290 RtlFreeUnicodeString( &cmdlineW
);
1295 /***********************************************************************
1298 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1300 static BOOL
add_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, const WCHAR
*section
, DWORD flags
)
1302 struct registry_callback_info info
;
1305 SERVICE_DESCRIPTIONW descr
;
1306 WCHAR
*display_name
, *start_name
, *load_order
, *binary_path
;
1307 INT service_type
= 0, start_type
= 0, error_control
= 0;
1311 /* first the mandatory fields */
1313 if (!SetupFindFirstLineW( hinf
, section
, L
"ServiceType", &context
) ||
1314 !SetupGetIntField( &context
, 1, &service_type
))
1316 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1319 if (!SetupFindFirstLineW( hinf
, section
, L
"StartType", &context
) ||
1320 !SetupGetIntField( &context
, 1, &start_type
))
1322 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1325 if (!SetupFindFirstLineW( hinf
, section
, L
"ErrorControl", &context
) ||
1326 !SetupGetIntField( &context
, 1, &error_control
))
1328 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1331 if (!(binary_path
= dup_section_line_field( hinf
, section
, L
"ServiceBinary", 1 )))
1333 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1337 /* now the optional fields */
1339 display_name
= dup_section_line_field( hinf
, section
, L
"DisplayName", 1 );
1340 start_name
= dup_section_line_field( hinf
, section
, L
"StartName", 1 );
1341 load_order
= dup_section_line_field( hinf
, section
, L
"LoadOrderGroup", 1 );
1342 descr
.lpDescription
= dup_section_line_field( hinf
, section
, L
"Description", 1 );
1344 /* FIXME: Dependencies field */
1345 /* FIXME: Security field */
1347 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1348 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1349 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
), flags
);
1351 service
= CreateServiceW( scm
, name
, display_name
, SERVICE_ALL_ACCESS
,
1352 service_type
, start_type
, error_control
, binary_path
,
1353 load_order
, NULL
, NULL
, start_name
, NULL
);
1356 if (descr
.lpDescription
) ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1360 if (GetLastError() != ERROR_SERVICE_EXISTS
) goto done
;
1361 service
= OpenServiceW( scm
, name
, SERVICE_QUERY_CONFIG
|SERVICE_CHANGE_CONFIG
|SERVICE_START
);
1362 if (!service
) goto done
;
1364 if (flags
& (SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
|
1365 SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
))
1367 QUERY_SERVICE_CONFIGW
*config
= NULL
;
1369 if (!QueryServiceConfigW( service
, NULL
, 0, &size
) &&
1370 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1371 config
= HeapAlloc( GetProcessHeap(), 0, size
);
1372 if (config
&& QueryServiceConfigW( service
, config
, size
, &size
))
1374 if (flags
& SPSVCINST_NOCLOBBER_STARTTYPE
) start_type
= config
->dwStartType
;
1375 if (flags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) error_control
= config
->dwErrorControl
;
1376 if (flags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
)
1378 HeapFree( GetProcessHeap(), 0, display_name
);
1379 display_name
= strdupW( config
->lpDisplayName
);
1381 if (flags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
)
1383 HeapFree( GetProcessHeap(), 0, load_order
);
1384 load_order
= strdupW( config
->lpLoadOrderGroup
);
1387 HeapFree( GetProcessHeap(), 0, config
);
1389 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1390 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1391 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
) );
1393 ChangeServiceConfigW( service
, service_type
, start_type
, error_control
, binary_path
,
1394 load_order
, NULL
, NULL
, start_name
, NULL
, display_name
);
1396 if (!(flags
& SPSVCINST_NOCLOBBER_DESCRIPTION
))
1397 ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1400 /* execute the AddReg, DelReg and BitReg entries */
1402 info
.default_root
= 0;
1403 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, L
"System\\CurrentControlSet\\Services", &hkey
))
1405 RegOpenKeyW( hkey
, name
, &info
.default_root
);
1406 RegCloseKey( hkey
);
1408 if (info
.default_root
)
1411 iterate_section_fields( hinf
, section
, L
"DelReg", registry_callback
, &info
);
1412 info
.delete = FALSE
;
1413 iterate_section_fields( hinf
, section
, L
"AddReg", registry_callback
, &info
);
1414 RegCloseKey( info
.default_root
);
1416 iterate_section_fields( hinf
, section
, L
"BitReg", bitreg_callback
, NULL
);
1418 if (flags
& SPSVCINST_STARTSERVICE
) StartServiceW( service
, 0, NULL
);
1419 CloseServiceHandle( service
);
1422 if (!service
) WARN( "failed err %u\n", GetLastError() );
1423 HeapFree( GetProcessHeap(), 0, binary_path
);
1424 HeapFree( GetProcessHeap(), 0, display_name
);
1425 HeapFree( GetProcessHeap(), 0, start_name
);
1426 HeapFree( GetProcessHeap(), 0, load_order
);
1427 HeapFree( GetProcessHeap(), 0, descr
.lpDescription
);
1428 return service
!= 0;
1432 /***********************************************************************
1435 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1437 static BOOL
del_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, DWORD flags
)
1441 SERVICE_STATUS status
;
1443 if (!(service
= OpenServiceW( scm
, name
, SERVICE_STOP
| DELETE
)))
1445 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
) return TRUE
;
1446 WARN( "cannot open %s err %u\n", debugstr_w(name
), GetLastError() );
1449 if (flags
& SPSVCINST_STOPSERVICE
) ControlService( service
, SERVICE_CONTROL_STOP
, &status
);
1450 TRACE( "deleting %s\n", debugstr_w(name
) );
1451 ret
= DeleteService( service
);
1452 CloseServiceHandle( service
);
1457 /***********************************************************************
1458 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1460 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF hinf
, PCWSTR section
, DWORD flags
)
1462 WCHAR service_name
[MAX_INF_STRING_LENGTH
];
1463 WCHAR service_section
[MAX_INF_STRING_LENGTH
];
1469 if (!SetupFindFirstLineW( hinf
, section
, NULL
, &context
))
1471 SetLastError( ERROR_SECTION_NOT_FOUND
);
1474 if (!(scm
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_ALL_ACCESS
))) return FALSE
;
1476 if (SetupFindFirstLineW( hinf
, section
, L
"AddService", &context
))
1480 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1482 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1483 if (!SetupGetStringFieldW( &context
, 3, service_section
, MAX_INF_STRING_LENGTH
, NULL
))
1485 if (!(ret
= add_service( scm
, hinf
, service_name
, service_section
, section_flags
| flags
)))
1487 } while (SetupFindNextMatchLineW( &context
, L
"AddService", &context
));
1490 if (SetupFindFirstLineW( hinf
, section
, L
"DelService", &context
))
1494 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1496 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1497 if (!(ret
= del_service( scm
, hinf
, service_name
, section_flags
| flags
))) goto done
;
1498 } while (SetupFindNextMatchLineW( &context
, L
"AddService", &context
));
1500 if (ret
) SetLastError( ERROR_SUCCESS
);
1502 CloseServiceHandle( scm
);
1507 /***********************************************************************
1508 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1510 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF Inf
, PCSTR SectionName
, DWORD Flags
)
1512 UNICODE_STRING SectionNameW
;
1515 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW
, SectionName
))
1517 ret
= SetupInstallServicesFromInfSectionW( Inf
, SectionNameW
.Buffer
, Flags
);
1518 RtlFreeUnicodeString( &SectionNameW
);
1521 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1527 /***********************************************************************
1528 * SetupGetInfFileListA (SETUPAPI.@)
1530 BOOL WINAPI
SetupGetInfFileListA(PCSTR dir
, DWORD style
, PSTR buffer
,
1531 DWORD insize
, PDWORD outsize
)
1533 UNICODE_STRING dirW
;
1534 PWSTR bufferW
= NULL
;
1536 DWORD outsizeA
, outsizeW
;
1539 RtlCreateUnicodeStringFromAsciiz( &dirW
, dir
);
1544 bufferW
= HeapAlloc( GetProcessHeap(), 0, insize
* sizeof( WCHAR
));
1546 ret
= SetupGetInfFileListW( dirW
.Buffer
, style
, bufferW
, insize
, &outsizeW
);
1550 outsizeA
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, outsizeW
,
1551 buffer
, insize
, NULL
, NULL
);
1552 if ( outsize
) *outsize
= outsizeA
;
1555 HeapFree( GetProcessHeap(), 0, bufferW
);
1556 RtlFreeUnicodeString( &dirW
);
1561 /***********************************************************************
1562 * SetupGetInfFileListW (SETUPAPI.@)
1564 BOOL WINAPI
SetupGetInfFileListW(PCWSTR dir
, DWORD style
, PWSTR buffer
,
1565 DWORD insize
, PDWORD outsize
)
1567 WCHAR
*filter
, *fullname
= NULL
, *ptr
= buffer
;
1568 DWORD dir_len
, name_len
= 20, size
;
1569 WIN32_FIND_DATAW finddata
;
1571 if (style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1572 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1574 FIXME( "unknown inf_style(s) 0x%x\n",
1575 style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1576 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
));
1577 if( outsize
) *outsize
= 1;
1580 if ((style
& ( INF_STYLE_OLDNT
| INF_STYLE_WIN4
)) == INF_STYLE_NONE
)
1582 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1583 if( outsize
) *outsize
= 1;
1586 if (style
& ( INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1587 FIXME("ignored inf_style(s) %s %s\n",
1588 ( style
& INF_STYLE_CACHE_ENABLE
) ? "INF_STYLE_CACHE_ENABLE" : "",
1589 ( style
& INF_STYLE_CACHE_DISABLE
) ? "INF_STYLE_CACHE_DISABLE" : "");
1594 dir_len
= lstrlenW( dir
);
1595 if ( !dir_len
) return FALSE
;
1596 msize
= ( 7 + dir_len
) * sizeof( WCHAR
); /* \\*.inf\0 */
1597 filter
= HeapAlloc( GetProcessHeap(), 0, msize
);
1600 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1603 lstrcpyW( filter
, dir
);
1604 if ( '\\' == filter
[dir_len
- 1] )
1605 filter
[--dir_len
] = 0;
1607 att
= GetFileAttributesW( filter
);
1608 if (att
!= INVALID_FILE_ATTRIBUTES
&& !(att
& FILE_ATTRIBUTE_DIRECTORY
))
1610 HeapFree( GetProcessHeap(), 0, filter
);
1611 SetLastError( ERROR_DIRECTORY
);
1618 dir_len
= GetWindowsDirectoryW( NULL
, 0 );
1619 msize
= ( 7 + 4 + dir_len
) * sizeof( WCHAR
);
1620 filter
= HeapAlloc( GetProcessHeap(), 0, msize
);
1623 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1626 GetWindowsDirectoryW( filter
, msize
);
1627 lstrcatW( filter
, L
"\\inf" );
1629 lstrcatW( filter
, L
"\\*.inf" );
1631 hdl
= FindFirstFileW( filter
, &finddata
);
1632 if ( hdl
== INVALID_HANDLE_VALUE
)
1634 if( outsize
) *outsize
= 1;
1635 HeapFree( GetProcessHeap(), 0, filter
);
1641 WCHAR signature
[ MAX_PATH
];
1643 DWORD len
= lstrlenW( finddata
.cFileName
);
1644 if (!fullname
|| ( name_len
< len
))
1646 name_len
= ( name_len
< len
) ? len
: name_len
;
1647 HeapFree( GetProcessHeap(), 0, fullname
);
1648 fullname
= HeapAlloc( GetProcessHeap(), 0,
1649 ( 2 + dir_len
+ name_len
) * sizeof( WCHAR
));
1653 HeapFree( GetProcessHeap(), 0, filter
);
1654 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1657 lstrcpyW( fullname
, filter
);
1659 fullname
[ dir_len
+ 1] = 0; /* keep '\\' */
1660 lstrcatW( fullname
, finddata
.cFileName
);
1661 if (!GetPrivateProfileStringW( L
"Version", L
"Signature", NULL
, signature
, MAX_PATH
, fullname
))
1663 if( INF_STYLE_OLDNT
& style
)
1664 valid
= wcsicmp( L
"$Chicago$", signature
) && wcsicmp( L
"$WINDOWS NT$", signature
);
1665 if( INF_STYLE_WIN4
& style
)
1666 valid
= valid
|| !wcsicmp( L
"$Chicago$", signature
) ||
1667 !wcsicmp( L
"$WINDOWS NT$", signature
);
1670 size
+= 1 + lstrlenW( finddata
.cFileName
);
1671 if( ptr
&& insize
>= size
)
1673 lstrcpyW( ptr
, finddata
.cFileName
);
1674 ptr
+= 1 + lstrlenW( finddata
.cFileName
);
1679 while( FindNextFileW( hdl
, &finddata
));
1682 HeapFree( GetProcessHeap(), 0, fullname
);
1683 HeapFree( GetProcessHeap(), 0, filter
);
1684 if( outsize
) *outsize
= size
;