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