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( NULL
, 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
)
705 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
707 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
710 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
713 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
716 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
718 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
719 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
722 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
723 strcpyW( p
, buffer
);
726 if (SetupGetStringFieldW( &context
, 4, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
727 p
= buffer
; /* otherwise use target base name as default source */
729 create_fake_dll( path
, p
); /* ignore errors */
732 HeapFree( GetProcessHeap(), 0, path
);
738 /***********************************************************************
739 * update_ini_callback
741 * Called once for each UpdateInis entry in a given section.
743 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
747 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
749 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
751 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
752 WCHAR filename
[MAX_INF_STRING_LENGTH
];
753 WCHAR section
[MAX_INF_STRING_LENGTH
];
754 WCHAR entry
[MAX_INF_STRING_LENGTH
];
755 WCHAR string
[MAX_INF_STRING_LENGTH
];
758 if (!SetupGetStringFieldW( &context
, 1, filename
,
759 sizeof(filename
)/sizeof(WCHAR
), NULL
))
762 if (!SetupGetStringFieldW( &context
, 2, section
,
763 sizeof(section
)/sizeof(WCHAR
), NULL
))
766 if (!SetupGetStringFieldW( &context
, 4, buffer
,
767 sizeof(buffer
)/sizeof(WCHAR
), NULL
))
770 divider
= strchrW(buffer
,'=');
774 strcpyW(entry
,buffer
);
776 strcpyW(string
,divider
);
780 strcpyW(entry
,buffer
);
784 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
785 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
786 WritePrivateProfileStringW(section
,entry
,string
,filename
);
792 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
794 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
798 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
800 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
804 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
806 FIXME( "should do logconf %s\n", debugstr_w(field
) );
810 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
812 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
816 static BOOL
profile_items_callback( HINF hinf
, PCWSTR field
, void *arg
)
818 WCHAR lnkpath
[MAX_PATH
];
819 LPWSTR cmdline
=NULL
, lnkpath_end
;
820 unsigned int name_size
;
821 INFCONTEXT name_context
, context
;
824 static const WCHAR dotlnk
[] = {'.','l','n','k',0};
826 TRACE( "(%s)\n", debugstr_w(field
) );
828 if (SetupFindFirstLineW( hinf
, field
, Name
, &name_context
))
830 SetupGetIntField( &name_context
, 2, &attrs
);
831 if (attrs
& ~FLG_PROFITEM_GROUP
) FIXME( "unhandled attributes: %x\n", attrs
);
835 /* calculate filename */
836 SHGetFolderPathW( NULL
, CSIDL_COMMON_PROGRAMS
, NULL
, SHGFP_TYPE_CURRENT
, lnkpath
);
837 lnkpath_end
= lnkpath
+ strlenW(lnkpath
);
838 if (lnkpath_end
[-1] != '\\') *lnkpath_end
++ = '\\';
840 if (!(attrs
& FLG_PROFITEM_GROUP
) && SetupFindFirstLineW( hinf
, field
, SubDir
, &context
))
842 unsigned int subdir_size
;
844 if (!SetupGetStringFieldW( &context
, 1, lnkpath_end
, (lnkpath
+MAX_PATH
)-lnkpath_end
, &subdir_size
))
847 lnkpath_end
+= subdir_size
- 1;
848 if (lnkpath_end
[-1] != '\\') *lnkpath_end
++ = '\\';
851 if (!SetupGetStringFieldW( &name_context
, 1, lnkpath_end
, (lnkpath
+MAX_PATH
)-lnkpath_end
, &name_size
))
854 lnkpath_end
+= name_size
- 1;
856 if (attrs
& FLG_PROFITEM_GROUP
)
858 SHPathPrepareForWriteW( NULL
, NULL
, lnkpath
, SHPPFW_DIRCREATE
);
862 IShellLinkW
* shelllink
=NULL
;
863 IPersistFile
* persistfile
=NULL
;
864 HRESULT initresult
=E_FAIL
;
866 if (lnkpath
+MAX_PATH
< lnkpath_end
+ 5) return TRUE
;
867 strcpyW( lnkpath_end
, dotlnk
);
869 TRACE( "link path: %s\n", debugstr_w(lnkpath
) );
871 /* calculate command line */
872 if (SetupFindFirstLineW( hinf
, field
, CmdLine
, &context
))
874 unsigned int dir_len
=0, subdir_size
=0, filename_size
=0;
879 SetupGetIntField( &context
, 1, &dirid
);
880 dir
= DIRID_get_string( dirid
);
882 if (dir
) dir_len
= strlenW(dir
);
884 SetupGetStringFieldW( &context
, 2, NULL
, 0, &subdir_size
);
885 SetupGetStringFieldW( &context
, 3, NULL
, 0, &filename_size
);
887 if (dir_len
&& filename_size
)
889 cmdline
= cmdline_end
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
) * (dir_len
+subdir_size
+filename_size
+1) );
891 strcpyW( cmdline_end
, dir
);
892 cmdline_end
+= dir_len
;
893 if (cmdline_end
[-1] != '\\') *cmdline_end
++ = '\\';
897 SetupGetStringFieldW( &context
, 2, cmdline_end
, subdir_size
, NULL
);
898 cmdline_end
+= subdir_size
-1;
899 if (cmdline_end
[-1] != '\\') *cmdline_end
++ = '\\';
901 SetupGetStringFieldW( &context
, 3, cmdline_end
, filename_size
, NULL
);
902 TRACE( "cmdline: %s\n", debugstr_w(cmdline
));
906 if (!cmdline
) return TRUE
;
908 initresult
= CoInitialize(NULL
);
910 if (FAILED(CoCreateInstance( &CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
911 &IID_IShellLinkW
, (LPVOID
*)&shelllink
)))
914 IShellLinkW_SetPath( shelllink
, cmdline
);
915 SHPathPrepareForWriteW( NULL
, NULL
, lnkpath
, SHPPFW_DIRCREATE
|SHPPFW_IGNOREFILENAME
);
916 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink
, &IID_IPersistFile
, (LPVOID
*)&persistfile
)))
918 TRACE( "writing link: %s\n", debugstr_w(lnkpath
) );
919 IPersistFile_Save( persistfile
, lnkpath
, FALSE
);
920 IPersistFile_Release( persistfile
);
922 IShellLinkW_Release( shelllink
);
925 if (SUCCEEDED(initresult
)) CoUninitialize();
926 HeapFree( GetProcessHeap(), 0, cmdline
);
932 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
934 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
939 /***********************************************************************
940 * iterate_section_fields
942 * Iterate over all fields of a certain key of a certain section
944 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
945 iterate_fields_func callback
, void *arg
)
947 WCHAR static_buffer
[200];
948 WCHAR
*buffer
= static_buffer
;
949 DWORD size
= sizeof(static_buffer
)/sizeof(WCHAR
);
953 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
956 UINT i
, count
= SetupGetFieldCount( &context
);
957 for (i
= 1; i
<= count
; i
++)
959 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
961 if (!callback( hinf
, buffer
, arg
))
963 WARN("callback failed for %s %s err %d\n",
964 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
968 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
972 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
977 /***********************************************************************
978 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
980 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
981 PCSTR section
, PCSTR src_root
, UINT flags
)
983 UNICODE_STRING sectionW
;
986 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
988 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
992 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
997 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
999 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1000 srcW
.Buffer
, flags
);
1001 RtlFreeUnicodeString( &srcW
);
1003 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1005 RtlFreeUnicodeString( §ionW
);
1010 /***********************************************************************
1011 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1013 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1014 PCWSTR section
, PCWSTR src_root
, UINT flags
)
1016 struct files_callback_info info
;
1019 info
.src_root
= src_root
;
1020 info
.copy_flags
= flags
;
1021 info
.layout
= hlayout
;
1022 return iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
);
1026 /***********************************************************************
1027 * SetupInstallFromInfSectionA (SETUPAPI.@)
1029 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
1030 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
1031 PSP_FILE_CALLBACK_A callback
, PVOID context
,
1032 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1034 UNICODE_STRING sectionW
, src_rootW
;
1035 struct callback_WtoA_context ctx
;
1038 src_rootW
.Buffer
= NULL
;
1039 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
1041 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1045 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1047 ctx
.orig_context
= context
;
1048 ctx
.orig_handler
= callback
;
1049 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
1050 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
1051 &ctx
, devinfo
, devinfo_data
);
1052 RtlFreeUnicodeString( §ionW
);
1054 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1056 RtlFreeUnicodeString( &src_rootW
);
1061 /***********************************************************************
1062 * SetupInstallFromInfSectionW (SETUPAPI.@)
1064 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
1065 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
1066 PSP_FILE_CALLBACK_W callback
, PVOID context
,
1067 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1072 if (flags
& SPINST_FILES
)
1074 struct files_callback_info info
;
1077 if (!(queue
= SetupOpenFileQueue())) return FALSE
;
1079 info
.src_root
= src_root
;
1080 info
.copy_flags
= copy_flags
;
1082 ret
= (iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
) &&
1083 iterate_section_fields( hinf
, section
, DelFiles
, delete_files_callback
, &info
) &&
1084 iterate_section_fields( hinf
, section
, RenFiles
, rename_files_callback
, &info
) &&
1085 SetupCommitFileQueueW( owner
, queue
, callback
, context
));
1086 SetupCloseFileQueue( queue
);
1087 if (!ret
) return FALSE
;
1089 if (flags
& SPINST_INIFILES
)
1091 if (!iterate_section_fields( hinf
, section
, UpdateInis
, update_ini_callback
, NULL
) ||
1092 !iterate_section_fields( hinf
, section
, UpdateIniFields
,
1093 update_ini_fields_callback
, NULL
))
1096 if (flags
& SPINST_INI2REG
)
1098 if (!iterate_section_fields( hinf
, section
, Ini2Reg
, ini2reg_callback
, NULL
))
1101 if (flags
& SPINST_LOGCONFIG
)
1103 if (!iterate_section_fields( hinf
, section
, LogConf
, logconf_callback
, NULL
))
1106 if (flags
& SPINST_REGSVR
)
1108 struct register_dll_info info
;
1110 info
.unregister
= FALSE
;
1111 info
.modules_size
= 0;
1112 info
.modules_count
= 0;
1113 info
.modules
= NULL
;
1114 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1116 info
.callback
= callback
;
1117 info
.callback_context
= context
;
1119 else info
.callback
= NULL
;
1121 if (iterate_section_fields( hinf
, section
, WineFakeDlls
, fake_dlls_callback
, NULL
))
1122 cleanup_fake_dlls();
1126 ret
= iterate_section_fields( hinf
, section
, RegisterDlls
, register_dlls_callback
, &info
);
1127 for (i
= 0; i
< info
.modules_count
; i
++) FreeLibrary( info
.modules
[i
] );
1128 HeapFree( GetProcessHeap(), 0, info
.modules
);
1129 if (!ret
) return FALSE
;
1131 if (flags
& SPINST_UNREGSVR
)
1133 struct register_dll_info info
;
1135 info
.unregister
= TRUE
;
1136 info
.modules_size
= 0;
1137 info
.modules_count
= 0;
1138 info
.modules
= NULL
;
1139 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1141 info
.callback
= callback
;
1142 info
.callback_context
= context
;
1144 else info
.callback
= NULL
;
1146 ret
= iterate_section_fields( hinf
, section
, UnregisterDlls
, register_dlls_callback
, &info
);
1147 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_REGISTRY
)
1153 struct registry_callback_info info
;
1155 info
.default_root
= key_root
;
1157 if (!iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
))
1159 info
.delete = FALSE
;
1160 if (!iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
))
1163 if (flags
& SPINST_BITREG
)
1165 if (!iterate_section_fields( hinf
, section
, BitReg
, bitreg_callback
, NULL
))
1168 if (flags
& SPINST_PROFILEITEMS
)
1170 if (!iterate_section_fields( hinf
, section
, ProfileItems
, profile_items_callback
, NULL
))
1173 if (flags
& SPINST_COPYINF
)
1175 if (!iterate_section_fields( hinf
, section
, CopyINF
, copy_inf_callback
, NULL
))
1183 /***********************************************************************
1184 * InstallHinfSectionW (SETUPAPI.@)
1186 * NOTE: 'cmdline' is <section> <mode> <path> from
1187 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1189 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
1192 static const WCHAR nt_platformW
[] = {'.','n','t','x','8','6',0};
1193 #elif defined(__x86_64)
1194 static const WCHAR nt_platformW
[] = {'.','n','t','a','m','d','6','4',0};
1195 #else /* FIXME: other platforms */
1196 static const WCHAR nt_platformW
[] = {'.','n','t',0};
1198 static const WCHAR nt_genericW
[] = {'.','n','t',0};
1199 static const WCHAR servicesW
[] = {'.','S','e','r','v','i','c','e','s',0};
1201 WCHAR
*s
, *path
, section
[MAX_PATH
+ (sizeof(nt_platformW
) + sizeof(servicesW
)) / sizeof(WCHAR
)];
1202 void *callback_context
;
1206 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
1208 lstrcpynW( section
, cmdline
, MAX_PATH
);
1210 if (!(s
= strchrW( section
, ' ' ))) return;
1212 while (*s
== ' ') s
++;
1215 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1216 if (!(s
= strchrW( s
, ' ' ))) return;
1217 while (*s
== ' ') s
++;
1220 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
1221 if (hinf
== INVALID_HANDLE_VALUE
) return;
1223 if (!(GetVersion() & 0x80000000))
1227 /* check for <section>.ntx86 (or corresponding name for the current platform)
1228 * and then <section>.nt */
1229 s
= section
+ strlenW(section
);
1230 memcpy( s
, nt_platformW
, sizeof(nt_platformW
) );
1231 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
)))
1233 memcpy( s
, nt_genericW
, sizeof(nt_genericW
) );
1234 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
))) *s
= 0;
1236 if (*s
) TRACE( "using section %s instead\n", debugstr_w(section
) );
1239 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
1240 SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
, SP_COPY_NEWER
,
1241 SetupDefaultQueueCallbackW
, callback_context
,
1243 SetupTermDefaultQueueCallback( callback_context
);
1244 strcatW( section
, servicesW
);
1245 SetupInstallServicesFromInfSectionW( hinf
, section
, 0 );
1246 SetupCloseInfFile( hinf
);
1248 /* FIXME: should check the mode and maybe reboot */
1249 /* there isn't much point in doing that since we */
1250 /* don't yet handle deferred file copies anyway. */
1251 if (mode
& 7) TRACE( "should consider reboot, mode %u\n", mode
);
1255 /***********************************************************************
1256 * InstallHinfSectionA (SETUPAPI.@)
1258 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
1260 UNICODE_STRING cmdlineW
;
1262 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
1264 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
1265 RtlFreeUnicodeString( &cmdlineW
);
1270 /***********************************************************************
1273 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1275 static BOOL
add_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, const WCHAR
*section
, DWORD flags
)
1277 struct registry_callback_info info
;
1280 SERVICE_DESCRIPTIONW descr
;
1281 WCHAR
*display_name
, *start_name
, *load_order
, *binary_path
;
1282 INT service_type
= 0, start_type
= 0, error_control
= 0;
1286 /* first the mandatory fields */
1288 if (!SetupFindFirstLineW( hinf
, section
, ServiceType
, &context
) ||
1289 !SetupGetIntField( &context
, 1, &service_type
))
1291 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1294 if (!SetupFindFirstLineW( hinf
, section
, StartType
, &context
) ||
1295 !SetupGetIntField( &context
, 1, &start_type
))
1297 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1300 if (!SetupFindFirstLineW( hinf
, section
, ErrorControl
, &context
) ||
1301 !SetupGetIntField( &context
, 1, &error_control
))
1303 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1306 if (!(binary_path
= dup_section_line_field( hinf
, section
, ServiceBinary
, 1 )))
1308 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1312 /* now the optional fields */
1314 display_name
= dup_section_line_field( hinf
, section
, DisplayName
, 1 );
1315 start_name
= dup_section_line_field( hinf
, section
, StartName
, 1 );
1316 load_order
= dup_section_line_field( hinf
, section
, LoadOrderGroup
, 1 );
1317 descr
.lpDescription
= dup_section_line_field( hinf
, section
, Description
, 1 );
1319 /* FIXME: Dependencies field */
1320 /* FIXME: Security field */
1322 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %x\n",
1323 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1324 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
), flags
);
1326 service
= CreateServiceW( scm
, name
, display_name
, SERVICE_ALL_ACCESS
,
1327 service_type
, start_type
, error_control
, binary_path
,
1328 load_order
, NULL
, NULL
, start_name
, NULL
);
1331 if (descr
.lpDescription
) ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1335 if (GetLastError() != ERROR_SERVICE_EXISTS
) goto done
;
1336 service
= OpenServiceW( scm
, name
, SERVICE_QUERY_CONFIG
|SERVICE_CHANGE_CONFIG
|SERVICE_START
);
1337 if (!service
) goto done
;
1339 if (flags
& (SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
|
1340 SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
))
1342 QUERY_SERVICE_CONFIGW
*config
= NULL
;
1344 if (!QueryServiceConfigW( service
, NULL
, 0, &size
) &&
1345 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1346 config
= HeapAlloc( GetProcessHeap(), 0, size
);
1347 if (config
&& QueryServiceConfigW( service
, config
, size
, &size
))
1349 if (flags
& SPSVCINST_NOCLOBBER_STARTTYPE
) start_type
= config
->dwStartType
;
1350 if (flags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) error_control
= config
->dwErrorControl
;
1351 if (flags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
)
1353 HeapFree( GetProcessHeap(), 0, display_name
);
1354 display_name
= strdupW( config
->lpDisplayName
);
1356 if (flags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
)
1358 HeapFree( GetProcessHeap(), 0, load_order
);
1359 load_order
= strdupW( config
->lpLoadOrderGroup
);
1362 HeapFree( GetProcessHeap(), 0, config
);
1364 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1365 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1366 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
) );
1368 ChangeServiceConfigW( service
, service_type
, start_type
, error_control
, binary_path
,
1369 load_order
, NULL
, NULL
, start_name
, NULL
, display_name
);
1371 if (!(flags
& SPSVCINST_NOCLOBBER_DESCRIPTION
))
1372 ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1375 /* execute the AddReg, DelReg and BitReg entries */
1377 info
.default_root
= 0;
1378 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, ServicesKey
, &hkey
))
1380 RegOpenKeyW( hkey
, name
, &info
.default_root
);
1381 RegCloseKey( hkey
);
1383 if (info
.default_root
)
1386 iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
);
1387 info
.delete = FALSE
;
1388 iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
);
1389 RegCloseKey( info
.default_root
);
1391 iterate_section_fields( hinf
, section
, BitReg
, bitreg_callback
, NULL
);
1393 if (flags
& SPSVCINST_STARTSERVICE
) StartServiceW( service
, 0, NULL
);
1394 CloseServiceHandle( service
);
1397 if (!service
) WARN( "failed err %u\n", GetLastError() );
1398 HeapFree( GetProcessHeap(), 0, binary_path
);
1399 HeapFree( GetProcessHeap(), 0, display_name
);
1400 HeapFree( GetProcessHeap(), 0, start_name
);
1401 HeapFree( GetProcessHeap(), 0, load_order
);
1402 HeapFree( GetProcessHeap(), 0, descr
.lpDescription
);
1403 return service
!= 0;
1407 /***********************************************************************
1410 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1412 static BOOL
del_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, DWORD flags
)
1416 SERVICE_STATUS status
;
1418 if (!(service
= OpenServiceW( scm
, name
, SERVICE_STOP
| DELETE
)))
1420 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
) return TRUE
;
1421 WARN( "cannot open %s err %u\n", debugstr_w(name
), GetLastError() );
1424 if (flags
& SPSVCINST_STOPSERVICE
) ControlService( service
, SERVICE_CONTROL_STOP
, &status
);
1425 TRACE( "deleting %s\n", debugstr_w(name
) );
1426 ret
= DeleteService( service
);
1427 CloseServiceHandle( service
);
1432 /***********************************************************************
1433 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1435 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF hinf
, PCWSTR section
, DWORD flags
)
1437 WCHAR service_name
[MAX_INF_STRING_LENGTH
];
1438 WCHAR service_section
[MAX_INF_STRING_LENGTH
];
1442 BOOL ok
, ret
= FALSE
;
1444 if (!(scm
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_ALL_ACCESS
))) return FALSE
;
1446 if (!(ok
= SetupFindFirstLineW( hinf
, section
, AddService
, &context
)))
1447 SetLastError( ERROR_SECTION_NOT_FOUND
);
1450 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1452 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1453 if (!SetupGetStringFieldW( &context
, 3, service_section
, MAX_INF_STRING_LENGTH
, NULL
))
1455 if (!(ret
= add_service( scm
, hinf
, service_name
, service_section
, section_flags
| flags
)))
1457 ok
= SetupFindNextMatchLineW( &context
, AddService
, &context
);
1460 if (!(ok
= SetupFindFirstLineW( hinf
, section
, DelService
, &context
)))
1461 SetLastError( ERROR_SECTION_NOT_FOUND
);
1464 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1466 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1467 if (!(ret
= del_service( scm
, hinf
, service_name
, section_flags
| flags
))) goto done
;
1468 ok
= SetupFindNextMatchLineW( &context
, AddService
, &context
);
1470 if (ret
) SetLastError( ERROR_SUCCESS
);
1472 CloseServiceHandle( scm
);
1477 /***********************************************************************
1478 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1480 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF Inf
, PCSTR SectionName
, DWORD Flags
)
1482 UNICODE_STRING SectionNameW
;
1485 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW
, SectionName
))
1487 ret
= SetupInstallServicesFromInfSectionW( Inf
, SectionNameW
.Buffer
, Flags
);
1488 RtlFreeUnicodeString( &SectionNameW
);
1491 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1497 /***********************************************************************
1498 * SetupGetInfFileListA (SETUPAPI.@)
1500 BOOL WINAPI
SetupGetInfFileListA(PCSTR dir
, DWORD style
, PSTR buffer
,
1501 DWORD insize
, PDWORD outsize
)
1503 UNICODE_STRING dirW
;
1504 PWSTR bufferW
= NULL
;
1506 DWORD outsizeA
, outsizeW
;
1509 RtlCreateUnicodeStringFromAsciiz( &dirW
, dir
);
1514 bufferW
= HeapAlloc( GetProcessHeap(), 0, insize
* sizeof( WCHAR
));
1516 ret
= SetupGetInfFileListW( dirW
.Buffer
, style
, bufferW
, insize
, &outsizeW
);
1520 outsizeA
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, outsizeW
,
1521 buffer
, insize
, NULL
, NULL
);
1522 if ( outsize
) *outsize
= outsizeA
;
1525 HeapFree( GetProcessHeap(), 0, bufferW
);
1526 RtlFreeUnicodeString( &dirW
);
1531 /***********************************************************************
1532 * SetupGetInfFileListW (SETUPAPI.@)
1534 BOOL WINAPI
SetupGetInfFileListW(PCWSTR dir
, DWORD style
, PWSTR buffer
,
1535 DWORD insize
, PDWORD outsize
)
1537 static WCHAR inf
[] = {'\\','*','.','i','n','f',0 };
1538 WCHAR
*filter
, *fullname
= NULL
, *ptr
= buffer
;
1539 DWORD dir_len
, name_len
= 20, size
;
1540 WIN32_FIND_DATAW finddata
;
1542 if (style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1543 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1545 FIXME( "unknown inf_style(s) 0x%x\n",
1546 style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1547 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
));
1548 if( outsize
) *outsize
= 1;
1551 if ((style
& ( INF_STYLE_OLDNT
| INF_STYLE_WIN4
)) == INF_STYLE_NONE
)
1553 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1554 if( outsize
) *outsize
= 1;
1557 if (style
& ( INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1558 FIXME("ignored inf_style(s) %s %s\n",
1559 ( style
& INF_STYLE_CACHE_ENABLE
) ? "INF_STYLE_CACHE_ENABLE" : "",
1560 ( style
& INF_STYLE_CACHE_DISABLE
) ? "INF_STYLE_CACHE_DISABLE" : "");
1565 dir_len
= strlenW( dir
);
1566 if ( !dir_len
) return FALSE
;
1567 msize
= ( 7 + dir_len
) * sizeof( WCHAR
); /* \\*.inf\0 */
1568 filter
= HeapAlloc( GetProcessHeap(), 0, msize
);
1571 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1574 strcpyW( filter
, dir
);
1575 if ( '\\' == filter
[dir_len
- 1] )
1576 filter
[--dir_len
] = 0;
1578 att
= GetFileAttributesW( filter
);
1579 if (att
!= INVALID_FILE_ATTRIBUTES
&& !(att
& FILE_ATTRIBUTE_DIRECTORY
))
1581 HeapFree( GetProcessHeap(), 0, filter
);
1582 SetLastError( ERROR_DIRECTORY
);
1588 WCHAR infdir
[] = {'\\','i','n','f',0 };
1590 dir_len
= GetWindowsDirectoryW( NULL
, 0 );
1591 msize
= ( 7 + 4 + dir_len
) * sizeof( WCHAR
);
1592 filter
= HeapAlloc( GetProcessHeap(), 0, msize
);
1595 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1598 GetWindowsDirectoryW( filter
, msize
);
1599 strcatW( filter
, infdir
);
1601 strcatW( filter
, inf
);
1603 hdl
= FindFirstFileW( filter
, &finddata
);
1604 if ( hdl
== INVALID_HANDLE_VALUE
)
1606 if( outsize
) *outsize
= 1;
1607 HeapFree( GetProcessHeap(), 0, filter
);
1613 static const WCHAR key
[] =
1614 {'S','i','g','n','a','t','u','r','e',0 };
1615 static const WCHAR section
[] =
1616 {'V','e','r','s','i','o','n',0 };
1617 static const WCHAR sig_win4_1
[] =
1618 {'$','C','h','i','c','a','g','o','$',0 };
1619 static const WCHAR sig_win4_2
[] =
1620 {'$','W','I','N','D','O','W','S',' ','N','T','$',0 };
1621 WCHAR signature
[ MAX_PATH
];
1623 DWORD len
= strlenW( finddata
.cFileName
);
1624 if (!fullname
|| ( name_len
< len
))
1626 name_len
= ( name_len
< len
) ? len
: name_len
;
1627 HeapFree( GetProcessHeap(), 0, fullname
);
1628 fullname
= HeapAlloc( GetProcessHeap(), 0,
1629 ( 2 + dir_len
+ name_len
) * sizeof( WCHAR
));
1632 HeapFree( GetProcessHeap(), 0, filter
);
1633 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1636 strcpyW( fullname
, filter
);
1638 fullname
[ dir_len
+ 1] = 0; /* keep '\\' */
1639 strcatW( fullname
, finddata
.cFileName
);
1640 if (!GetPrivateProfileStringW( section
, key
, NULL
, signature
, MAX_PATH
, fullname
))
1642 if( INF_STYLE_OLDNT
& style
)
1643 valid
= strcmpiW( sig_win4_1
, signature
) &&
1644 strcmpiW( sig_win4_2
, signature
);
1645 if( INF_STYLE_WIN4
& style
)
1646 valid
= valid
|| !strcmpiW( sig_win4_1
, signature
) ||
1647 !strcmpiW( sig_win4_2
, signature
);
1650 size
+= 1 + strlenW( finddata
.cFileName
);
1651 if( ptr
&& insize
>= size
)
1653 strcpyW( ptr
, finddata
.cFileName
);
1654 ptr
+= 1 + strlenW( finddata
.cFileName
);
1659 while( FindNextFileW( hdl
, &finddata
));
1662 HeapFree( GetProcessHeap(), 0, fullname
);
1663 HeapFree( GetProcessHeap(), 0, filter
);
1664 if( outsize
) *outsize
= size
;