2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
5 * 2005-2006 Hervé Poussineau (hpoussin@reactos.org)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "setupapi_private.h"
25 #include <ndk/cmfuncs.h>
27 /* Unicode constants */
28 static const WCHAR BackSlash
[] = {'\\',0};
29 static const WCHAR GroupOrderListKey
[] = {'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','G','r','o','u','p','O','r','d','e','r','L','i','s','t',0};
30 static const WCHAR InfDirectory
[] = {'i','n','f','\\',0};
31 static const WCHAR OemFileMask
[] = {'o','e','m','*','.','i','n','f',0};
32 static const WCHAR OemFileSpecification
[] = {'o','e','m','%','l','u','.','i','n','f',0};
33 static const WCHAR DotLnk
[] = {'.','l','n','k',0};
34 static const WCHAR DotServices
[] = {'.','S','e','r','v','i','c','e','s',0};
36 static const WCHAR DependenciesKey
[] = {'D','e','p','e','n','d','e','n','c','i','e','s',0};
37 static const WCHAR DescriptionKey
[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
38 static const WCHAR DisplayNameKey
[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
39 static const WCHAR ErrorControlKey
[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
40 static const WCHAR LoadOrderGroupKey
[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
41 static const WCHAR SecurityKey
[] = {'S','e','c','u','r','i','t','y',0};
42 static const WCHAR ServiceBinaryKey
[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
43 static const WCHAR ServiceTypeKey
[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
44 static const WCHAR StartTypeKey
[] = {'S','t','a','r','t','T','y','p','e',0};
46 static const WCHAR Name
[] = {'N','a','m','e',0};
47 static const WCHAR CmdLine
[] = {'C','m','d','L','i','n','e',0};
48 static const WCHAR SubDir
[] = {'S','u','b','D','i','r',0};
49 static const WCHAR WorkingDir
[] = {'W','o','r','k','i','n','g','D','i','r',0};
50 static const WCHAR IconPath
[] = {'I','c','o','n','P','a','t','h',0};
51 static const WCHAR IconIndex
[] = {'I','c','o','n','I','n','d','e','x',0};
52 static const WCHAR HotKey
[] = {'H','o','t','K','e','y',0};
53 static const WCHAR InfoTip
[] = {'I','n','f','o','T','i','p',0};
54 static const WCHAR DisplayResource
[] = {'D','i','s','p','l','a','y','R','e','s','o','u','r','c','e',0};
56 /* info passed to callback functions dealing with files */
57 struct files_callback_info
65 /* info passed to callback functions dealing with the registry */
66 struct registry_callback_info
72 /* info passed to callback functions dealing with registering dlls */
73 struct register_dll_info
75 PSP_FILE_CALLBACK_W callback
;
76 PVOID callback_context
;
80 /* info passed to callback functions dealing with Needs directives */
81 struct needs_callback_info
93 PSP_DEVINFO_DATA devinfo_data
;
98 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
99 static BOOL
GetLineText( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, PWSTR
*value
);
100 typedef HRESULT (WINAPI
*COINITIALIZE
)(IN LPVOID pvReserved
);
101 typedef HRESULT (WINAPI
*COCREATEINSTANCE
)(IN REFCLSID rclsid
, IN LPUNKNOWN pUnkOuter
, IN DWORD dwClsContext
, IN REFIID riid
, OUT LPVOID
*ppv
);
102 typedef HRESULT (WINAPI
*COUNINITIALIZE
)(VOID
);
104 /* Unicode constants */
105 static const WCHAR AddService
[] = {'A','d','d','S','e','r','v','i','c','e',0};
106 static const WCHAR CopyFiles
[] = {'C','o','p','y','F','i','l','e','s',0};
107 static const WCHAR DelFiles
[] = {'D','e','l','F','i','l','e','s',0};
108 static const WCHAR RenFiles
[] = {'R','e','n','F','i','l','e','s',0};
109 static const WCHAR Ini2Reg
[] = {'I','n','i','2','R','e','g',0};
110 static const WCHAR LogConf
[] = {'L','o','g','C','o','n','f',0};
111 static const WCHAR AddReg
[] = {'A','d','d','R','e','g',0};
112 static const WCHAR DelReg
[] = {'D','e','l','R','e','g',0};
113 static const WCHAR BitReg
[] = {'B','i','t','R','e','g',0};
114 static const WCHAR UpdateInis
[] = {'U','p','d','a','t','e','I','n','i','s',0};
115 static const WCHAR CopyINF
[] = {'C','o','p','y','I','N','F',0};
116 static const WCHAR UpdateIniFields
[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
117 static const WCHAR RegisterDlls
[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
118 static const WCHAR UnregisterDlls
[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
119 static const WCHAR ProfileItems
[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
120 static const WCHAR Include
[] = {'I','n','c','l','u','d','e',0};
121 static const WCHAR Needs
[] = {'N','e','e','d','s',0};
122 static const WCHAR DotSecurity
[] = {'.','S','e','c','u','r','i','t','y',0};
124 static const WCHAR WineFakeDlls
[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
128 /***********************************************************************
131 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
133 static WCHAR
*get_field_string( INFCONTEXT
*context
, DWORD index
, WCHAR
*buffer
,
134 WCHAR
*static_buffer
, DWORD
*size
)
138 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
139 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
141 /* now grow the buffer */
142 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
143 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
*sizeof(WCHAR
) ))) return NULL
;
145 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
147 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
152 /***********************************************************************
153 * copy_files_callback
155 * Called once for each CopyFiles entry in a given section.
157 static BOOL
copy_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
159 struct files_callback_info
*info
= arg
;
161 if (field
[0] == '@') /* special case: copy single file */
162 SetupQueueDefaultCopyW( info
->queue
, info
->layout
? info
->layout
: hinf
, info
->src_root
, NULL
, field
+1, info
->copy_flags
);
164 SetupQueueCopySectionW( info
->queue
, info
->src_root
, info
->layout
? info
->layout
: hinf
, hinf
, field
, info
->copy_flags
);
169 /***********************************************************************
170 * delete_files_callback
172 * Called once for each DelFiles entry in a given section.
174 static BOOL
delete_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
176 struct files_callback_info
*info
= arg
;
177 SetupQueueDeleteSectionW( info
->queue
, hinf
, 0, field
);
182 /***********************************************************************
183 * rename_files_callback
185 * Called once for each RenFiles entry in a given section.
187 static BOOL
rename_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
189 struct files_callback_info
*info
= arg
;
190 SetupQueueRenameSectionW( info
->queue
, hinf
, 0, field
);
195 /***********************************************************************
198 * Retrieve the registry root key from its name.
200 static HKEY
get_root_key( const WCHAR
*name
, HKEY def_root
)
202 static const WCHAR HKCR
[] = {'H','K','C','R',0};
203 static const WCHAR HKCU
[] = {'H','K','C','U',0};
204 static const WCHAR HKLM
[] = {'H','K','L','M',0};
205 static const WCHAR HKU
[] = {'H','K','U',0};
206 static const WCHAR HKR
[] = {'H','K','R',0};
208 if (!strcmpiW( name
, HKCR
)) return HKEY_CLASSES_ROOT
;
209 if (!strcmpiW( name
, HKCU
)) return HKEY_CURRENT_USER
;
210 if (!strcmpiW( name
, HKLM
)) return HKEY_LOCAL_MACHINE
;
211 if (!strcmpiW( name
, HKU
)) return HKEY_USERS
;
212 if (!strcmpiW( name
, HKR
)) return def_root
;
217 /***********************************************************************
218 * append_multi_sz_value
220 * Append a multisz string to a multisz registry value.
222 static void append_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*strings
,
225 DWORD size
, type
, total
;
228 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
229 if (type
!= REG_MULTI_SZ
) return;
231 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
232 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
234 /* compare each string against all the existing ones */
238 int len
= strlenW(strings
) + 1;
240 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
241 if (!strcmpiW( p
, strings
)) break;
243 if (!*p
) /* not found, need to append it */
245 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
253 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
254 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
+ sizeof(WCHAR
) );
257 HeapFree( GetProcessHeap(), 0, buffer
);
261 /***********************************************************************
262 * delete_multi_sz_value
264 * Remove a string from a multisz registry value.
266 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
269 WCHAR
*buffer
, *src
, *dst
;
271 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
272 if (type
!= REG_MULTI_SZ
) return;
273 /* allocate double the size, one for value before and one for after */
274 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
275 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
280 int len
= strlenW(src
) + 1;
281 if (strcmpiW( src
, string
))
283 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
289 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
291 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
292 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
293 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
296 HeapFree( GetProcessHeap(), 0, buffer
);
300 /***********************************************************************
303 * Perform an add/delete registry operation depending on the flags.
305 static BOOL
do_reg_operation( HKEY hkey
, const WCHAR
*value
, INFCONTEXT
*context
, INT flags
)
309 if (flags
& (FLG_ADDREG_DELREG_BIT
| FLG_ADDREG_DELVAL
)) /* deletion */
311 if (*value
&& !(flags
& FLG_DELREG_KEYONLY_COMMON
))
313 if ((flags
& FLG_DELREG_MULTI_SZ_DELSTRING
) == FLG_DELREG_MULTI_SZ_DELSTRING
)
317 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
) || !size
) return TRUE
;
318 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
319 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
320 delete_multi_sz_value( hkey
, value
, str
);
321 HeapFree( GetProcessHeap(), 0, str
);
323 else RegDeleteValueW( hkey
, value
);
325 else NtDeleteKey( hkey
);
329 if (flags
& (FLG_ADDREG_KEYONLY
|FLG_ADDREG_KEYONLY_COMMON
)) return TRUE
;
331 if (flags
& (FLG_ADDREG_NOCLOBBER
|FLG_ADDREG_OVERWRITEONLY
))
333 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
334 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
)) return TRUE
;
335 if (!exists
&& (flags
& FLG_ADDREG_OVERWRITEONLY
)) return TRUE
;
338 switch(flags
& FLG_ADDREG_TYPE_MASK
)
340 case FLG_ADDREG_TYPE_SZ
: type
= REG_SZ
; break;
341 case FLG_ADDREG_TYPE_MULTI_SZ
: type
= REG_MULTI_SZ
; break;
342 case FLG_ADDREG_TYPE_EXPAND_SZ
: type
= REG_EXPAND_SZ
; break;
343 case FLG_ADDREG_TYPE_BINARY
: type
= REG_BINARY
; break;
344 case FLG_ADDREG_TYPE_DWORD
: type
= REG_DWORD
; break;
345 case FLG_ADDREG_TYPE_NONE
: type
= REG_NONE
; break;
346 default: type
= flags
>> 16; break;
349 if (!(flags
& FLG_ADDREG_BINVALUETYPE
) ||
350 (type
== REG_DWORD
&& SetupGetFieldCount(context
) == 5))
352 static const WCHAR empty
;
355 if (type
== REG_MULTI_SZ
)
357 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
360 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
361 SetupGetMultiSzFieldW( context
, 5, str
, size
, NULL
);
363 if (flags
& FLG_ADDREG_APPEND
)
365 if (!str
) return TRUE
;
366 append_multi_sz_value( hkey
, value
, str
, size
);
367 HeapFree( GetProcessHeap(), 0, str
);
370 /* else fall through to normal string handling */
374 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
377 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
378 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
382 if (type
== REG_DWORD
)
384 DWORD dw
= str
? strtoulW( str
, NULL
, 0 ) : 0;
385 TRACE( "setting dword %s to %x\n", debugstr_w(value
), dw
);
386 RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&dw
, sizeof(dw
) );
390 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(str
) );
391 if (str
) RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)str
, size
* sizeof(WCHAR
) );
392 else RegSetValueExW( hkey
, value
, 0, type
, (const BYTE
*)&empty
, sizeof(WCHAR
) );
394 HeapFree( GetProcessHeap(), 0, str
);
397 else /* get the binary data */
401 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
404 if (!(data
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
405 TRACE( "setting binary data %s len %d\n", debugstr_w(value
), size
);
406 SetupGetBinaryField( context
, 5, data
, size
, NULL
);
408 RegSetValueExW( hkey
, value
, 0, type
, data
, size
);
409 HeapFree( GetProcessHeap(), 0, data
);
415 /***********************************************************************
418 * Called once for each AddReg and DelReg entry in a given section.
420 static BOOL
registry_callback( HINF hinf
, PCWSTR field
, void *arg
)
422 struct registry_callback_info
*info
= arg
;
423 LPWSTR security_key
, security_descriptor
;
424 INFCONTEXT context
, security_context
;
425 PSECURITY_DESCRIPTOR sd
= NULL
;
426 SECURITY_ATTRIBUTES security_attributes
= { 0, };
430 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
434 /* Check for .Security section */
435 security_key
= MyMalloc( (strlenW( field
) + strlenW( DotSecurity
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
) );
438 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
441 strcpyW( security_key
, field
);
442 strcatW( security_key
, DotSecurity
);
443 ok
= SetupFindFirstLineW( hinf
, security_key
, NULL
, &security_context
);
444 MyFree(security_key
);
447 if (!SetupGetLineTextW( &security_context
, NULL
, NULL
, NULL
, NULL
, 0, &required
))
449 security_descriptor
= MyMalloc( required
* sizeof(WCHAR
) );
450 if (!security_descriptor
)
452 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
455 if (!SetupGetLineTextW( &security_context
, NULL
, NULL
, NULL
, security_descriptor
, required
, NULL
))
457 ok
= ConvertStringSecurityDescriptorToSecurityDescriptorW( security_descriptor
, SDDL_REVISION_1
, &sd
, NULL
);
458 MyFree( security_descriptor
);
461 security_attributes
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
462 security_attributes
.lpSecurityDescriptor
= sd
;
465 for (ok
= TRUE
; ok
; ok
= SetupFindNextLine( &context
, &context
))
467 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
471 if (!SetupGetStringFieldW( &context
, 1, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
473 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
477 if (!SetupGetStringFieldW( &context
, 2, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
481 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
485 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
489 if (!flags
) flags
= FLG_ADDREG_DELREG_BIT
;
490 else if (!(flags
& FLG_ADDREG_DELREG_BIT
)) continue; /* ignore this entry */
493 if (info
->delete || (flags
& FLG_ADDREG_OVERWRITEONLY
))
495 if (RegOpenKeyW( root_key
, buffer
, &hkey
)) continue; /* ignore if it doesn't exist */
497 else if (RegCreateKeyExW( root_key
, buffer
, 0, NULL
, 0, MAXIMUM_ALLOWED
,
498 sd
? &security_attributes
: NULL
, &hkey
, NULL
))
500 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
503 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
506 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
510 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
512 if (hkey
!= root_key
) RegCloseKey( hkey
);
513 if (sd
) LocalFree( sd
);
516 if (hkey
!= root_key
) RegCloseKey( hkey
);
518 if (sd
) LocalFree( sd
);
523 /***********************************************************************
526 * Register or unregister a dll.
528 static BOOL
do_register_dll( const struct register_dll_info
*info
, const WCHAR
*path
,
529 INT flags
, INT timeout
, const WCHAR
*args
)
533 SP_REGISTER_CONTROL_STATUSW status
;
535 IMAGE_NT_HEADERS
*nt
;
538 status
.cbSize
= sizeof(status
);
539 status
.FileName
= path
;
540 status
.FailureCode
= SPREG_SUCCESS
;
541 status
.Win32Error
= ERROR_SUCCESS
;
545 switch(info
->callback( info
->callback_context
, SPFILENOTIFY_STARTREGISTRATION
,
546 (UINT_PTR
)&status
, !info
->unregister
))
549 SetLastError( ERROR_OPERATION_ABORTED
);
558 if (!(module
= LoadLibraryExW( path
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)))
560 WARN( "could not load %s\n", debugstr_w(path
) );
561 status
.FailureCode
= SPREG_LOADLIBRARY
;
562 status
.Win32Error
= GetLastError();
567 if ((nt
= RtlImageNtHeader( module
)) && !(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
569 /* file is an executable, not a dll */
570 STARTUPINFOW startup
;
571 PROCESS_INFORMATION info
;
574 static const WCHAR format
[] = {'"','%','s','"',' ','%','s',0};
575 static const WCHAR default_args
[] = {'/','R','e','g','S','e','r','v','e','r',0};
577 FreeLibrary( module
);
579 if (!args
) args
= default_args
;
580 cmd_line
= HeapAlloc( GetProcessHeap(), 0, (strlenW(path
) + strlenW(args
) + 4) * sizeof(WCHAR
) );
581 sprintfW( cmd_line
, format
, path
, args
);
582 memset( &startup
, 0, sizeof(startup
) );
583 startup
.cb
= sizeof(startup
);
584 TRACE( "executing %s\n", debugstr_w(cmd_line
) );
585 res
= CreateProcessW( NULL
, cmd_line
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
586 HeapFree( GetProcessHeap(), 0, cmd_line
);
589 status
.FailureCode
= SPREG_LOADLIBRARY
;
590 status
.Win32Error
= GetLastError();
593 CloseHandle( info
.hThread
);
595 if (WaitForSingleObject( info
.hProcess
, timeout
*1000 ) == WAIT_TIMEOUT
)
597 /* timed out, kill the process */
598 TerminateProcess( info
.hProcess
, 1 );
599 status
.FailureCode
= SPREG_TIMEOUT
;
600 status
.Win32Error
= ERROR_TIMEOUT
;
602 CloseHandle( info
.hProcess
);
605 #endif // __WINESRC__
607 if (flags
& FLG_REGSVR_DLLREGISTER
)
609 const char *entry_point
= info
->unregister
? "DllUnregisterServer" : "DllRegisterServer";
610 HRESULT (WINAPI
*func
)(void) = (void *)GetProcAddress( module
, entry_point
);
614 status
.FailureCode
= SPREG_GETPROCADDR
;
615 status
.Win32Error
= GetLastError();
619 TRACE( "calling %s in %s\n", entry_point
, debugstr_w(path
) );
624 WARN( "calling %s in %s returned error %x\n", entry_point
, debugstr_w(path
), res
);
625 status
.FailureCode
= SPREG_REGSVR
;
626 status
.Win32Error
= res
;
631 if (flags
& FLG_REGSVR_DLLINSTALL
)
633 HRESULT (WINAPI
*func
)(BOOL
,LPCWSTR
) = (void *)GetProcAddress( module
, "DllInstall" );
637 status
.FailureCode
= SPREG_GETPROCADDR
;
638 status
.Win32Error
= GetLastError();
642 TRACE( "calling DllInstall(%d,%s) in %s\n",
643 !info
->unregister
, debugstr_w(args
), debugstr_w(path
) );
644 res
= func( !info
->unregister
, args
);
648 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path
), res
);
649 status
.FailureCode
= SPREG_REGSVR
;
650 status
.Win32Error
= res
;
656 if (module
) FreeLibrary( module
);
657 if (info
->callback
) info
->callback( info
->callback_context
, SPFILENOTIFY_ENDREGISTRATION
,
658 (UINT_PTR
)&status
, !info
->unregister
);
663 /***********************************************************************
664 * register_dlls_callback
666 * Called once for each RegisterDlls entry in a given section.
668 static BOOL
register_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
670 struct register_dll_info
*info
= arg
;
673 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
675 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
677 WCHAR
*path
, *args
, *p
;
678 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
682 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
685 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
687 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
688 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
691 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
692 strcpyW( p
, buffer
);
695 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
698 if (!SetupGetIntField( &context
, 5, &timeout
)) timeout
= 60;
700 /* get command line */
702 if (SetupGetStringFieldW( &context
, 6, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
705 ret
= do_register_dll( info
, path
, flags
, timeout
, args
);
708 HeapFree( GetProcessHeap(), 0, path
);
715 /***********************************************************************
718 * Called once for each WineFakeDlls entry in a given section.
720 static BOOL
fake_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
724 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
726 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
729 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
732 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
735 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
737 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
738 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
741 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
742 strcpyW( p
, buffer
);
745 if (SetupGetStringFieldW( &context
, 4, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
746 p
= buffer
; /* otherwise use target base name as default source */
748 create_fake_dll( path
, p
); /* ignore errors */
751 HeapFree( GetProcessHeap(), 0, path
);
756 #endif // __WINESRC__
758 /***********************************************************************
759 * update_ini_callback
761 * Called once for each UpdateInis entry in a given section.
763 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
767 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
769 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
771 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
772 WCHAR filename
[MAX_INF_STRING_LENGTH
];
773 WCHAR section
[MAX_INF_STRING_LENGTH
];
774 WCHAR entry
[MAX_INF_STRING_LENGTH
];
775 WCHAR string
[MAX_INF_STRING_LENGTH
];
778 if (!SetupGetStringFieldW( &context
, 1, filename
,
779 sizeof(filename
)/sizeof(WCHAR
), NULL
))
782 if (!SetupGetStringFieldW( &context
, 2, section
,
783 sizeof(section
)/sizeof(WCHAR
), NULL
))
786 if (!SetupGetStringFieldW( &context
, 4, buffer
,
787 sizeof(buffer
)/sizeof(WCHAR
), NULL
))
790 divider
= strchrW(buffer
,'=');
794 strcpyW(entry
,buffer
);
796 strcpyW(string
,divider
);
800 strcpyW(entry
,buffer
);
804 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
805 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
806 WritePrivateProfileStringW(section
,entry
,string
,filename
);
812 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
814 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
818 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
820 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
824 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
826 FIXME( "should do logconf %s\n", debugstr_w(field
) );
830 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
832 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
836 static BOOL
Concatenate(int DirId
, LPCWSTR SubDirPart
, LPCWSTR NamePart
, LPWSTR
*pFullName
)
838 DWORD dwRequired
= 0;
844 Dir
= DIRID_get_string(DirId
);
846 dwRequired
+= wcslen(Dir
) + 1;
848 dwRequired
+= wcslen(SubDirPart
) + 1;
850 dwRequired
+= wcslen(NamePart
);
851 dwRequired
= dwRequired
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
853 FullName
= MyMalloc(dwRequired
);
856 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
859 FullName
[0] = UNICODE_NULL
;
863 wcscat(FullName
, Dir
);
864 if (FullName
[wcslen(FullName
) - 1] != '\\')
865 wcscat(FullName
, BackSlash
);
869 wcscat(FullName
, SubDirPart
);
870 if (FullName
[wcslen(FullName
) - 1] != '\\')
871 wcscat(FullName
, BackSlash
);
874 wcscat(FullName
, NamePart
);
876 *pFullName
= FullName
;
880 /***********************************************************************
881 * profile_items_callback
883 * Called once for each ProfileItems entry in a given section.
886 profile_items_callback(
888 IN PCWSTR SectionName
,
892 LPWSTR LinkSubDir
= NULL
, LinkName
= NULL
;
893 INT LinkAttributes
= 0;
896 INT CSIDL
= CSIDL_COMMON_PROGRAMS
;
897 LPWSTR FileSubDir
= NULL
;
899 LPWSTR SubDirPart
= NULL
, NamePart
= NULL
;
900 LPWSTR FullLinkName
= NULL
, FullFileName
= NULL
, FullWorkingDir
= NULL
, FullIconName
= NULL
;
902 LPWSTR lpHotKey
= NULL
, lpInfoTip
= NULL
;
903 LPWSTR DisplayName
= NULL
;
904 INT DisplayResId
= 0;
906 DWORD Index
, Required
;
910 HMODULE hOle32
= NULL
;
911 COINITIALIZE pCoInitialize
;
912 COCREATEINSTANCE pCoCreateInstance
;
913 COUNINITIALIZE pCoUninitialize
;
916 TRACE("hInf %p, SectionName %s, Arg %p\n",
917 hInf
, debugstr_w(SectionName
), Arg
);
919 /* Read 'Name' entry */
920 if (!SetupFindFirstLineW(hInf
, SectionName
, Name
, &Context
))
922 if (!GetStringField(&Context
, 1, &LinkName
))
924 if (SetupGetFieldCount(&Context
) >= 2)
926 if (!SetupGetIntField(&Context
, 2, &LinkAttributes
))
929 if (SetupGetFieldCount(&Context
) >= 3)
931 if (!SetupGetIntField(&Context
, 3, &LinkFolder
))
935 /* Read 'CmdLine' entry */
936 if (!SetupFindFirstLineW(hInf
, SectionName
, CmdLine
, &Context
))
939 if (!SetupGetIntField(&Context
, Index
++, &FileDirId
))
941 if (SetupGetFieldCount(&Context
) >= 3)
943 if (!GetStringField(&Context
, Index
++, &FileSubDir
))
946 if (!GetStringField(&Context
, Index
++, &NamePart
))
948 if (!Concatenate(FileDirId
, FileSubDir
, NamePart
, &FullFileName
))
953 /* Read 'SubDir' entry */
954 if ((LinkAttributes
& FLG_PROFITEM_GROUP
) == 0 && SetupFindFirstLineW(hInf
, SectionName
, SubDir
, &Context
))
956 if (!GetStringField(&Context
, 1, &LinkSubDir
))
960 /* Read 'WorkingDir' entry */
961 if (SetupFindFirstLineW(hInf
, SectionName
, WorkingDir
, &Context
))
963 if (!SetupGetIntField(&Context
, 1, &DirId
))
965 if (SetupGetFieldCount(&Context
) >= 2)
967 if (!GetStringField(&Context
, 2, &SubDirPart
))
970 if (!Concatenate(DirId
, SubDirPart
, NULL
, &FullWorkingDir
))
977 if (!Concatenate(FileDirId
, FileSubDir
, NULL
, &FullWorkingDir
))
981 /* Read 'IconPath' entry */
982 if (SetupFindFirstLineW(hInf
, SectionName
, IconPath
, &Context
))
985 if (!SetupGetIntField(&Context
, Index
++, &DirId
))
987 if (SetupGetFieldCount(&Context
) >= 3)
989 if (!GetStringField(&Context
, Index
++, &SubDirPart
))
992 if (!GetStringField(&Context
, Index
, &NamePart
))
994 if (!Concatenate(DirId
, SubDirPart
, NamePart
, &FullIconName
))
998 SubDirPart
= NamePart
= NULL
;
1002 FullIconName
= pSetupDuplicateString(FullFileName
);
1007 /* Read 'IconIndex' entry */
1008 if (SetupFindFirstLineW(hInf
, SectionName
, IconIndex
, &Context
))
1010 if (!SetupGetIntField(&Context
, 1, &IconIdx
))
1014 /* Read 'HotKey' and 'InfoTip' entries */
1015 GetLineText(hInf
, SectionName
, HotKey
, &lpHotKey
);
1016 GetLineText(hInf
, SectionName
, InfoTip
, &lpInfoTip
);
1018 /* Read 'DisplayResource' entry */
1019 if (SetupFindFirstLineW(hInf
, SectionName
, DisplayResource
, &Context
))
1021 if (!GetStringField(&Context
, 1, &DisplayName
))
1023 if (!SetupGetIntField(&Context
, 2, &DisplayResId
))
1028 TRACE("Link is %s\\%s, attributes 0x%x\n", debugstr_w(LinkSubDir
), debugstr_w(LinkName
), LinkAttributes
);
1029 TRACE("File is %s\n", debugstr_w(FullFileName
));
1030 TRACE("Working dir %s\n", debugstr_w(FullWorkingDir
));
1031 TRACE("Icon is %s, %d\n", debugstr_w(FullIconName
), IconIdx
);
1032 TRACE("Hotkey %s\n", debugstr_w(lpHotKey
));
1033 TRACE("InfoTip %s\n", debugstr_w(lpInfoTip
));
1034 TRACE("Display %s, %d\n", DisplayName
, DisplayResId
);
1036 /* Load ole32.dll */
1037 hOle32
= LoadLibraryA("ole32.dll");
1040 pCoInitialize
= (COINITIALIZE
)GetProcAddress(hOle32
, "CoInitialize");
1043 pCoCreateInstance
= (COCREATEINSTANCE
)GetProcAddress(hOle32
, "CoCreateInstance");
1044 if (!pCoCreateInstance
)
1046 pCoUninitialize
= (COUNINITIALIZE
)GetProcAddress(hOle32
, "CoUninitialize");
1047 if (!pCoUninitialize
)
1050 /* Create shortcut */
1051 hr
= pCoInitialize(NULL
);
1054 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
1055 SetLastError(HRESULT_CODE(hr
));
1057 SetLastError(E_FAIL
);
1060 hr
= pCoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLinkW
, (LPVOID
*)&psl
);
1063 /* Fill link properties */
1064 hr
= IShellLinkW_SetPath(psl
, FullFileName
);
1066 hr
= IShellLinkW_SetArguments(psl
, L
"");
1068 hr
= IShellLinkW_SetWorkingDirectory(psl
, FullWorkingDir
);
1070 hr
= IShellLinkW_SetIconLocation(psl
, FullIconName
, IconIdx
);
1071 if (SUCCEEDED(hr
) && lpHotKey
)
1072 FIXME("Need to store hotkey %s in shell link\n", debugstr_w(lpHotKey
));
1073 if (SUCCEEDED(hr
) && lpInfoTip
)
1074 hr
= IShellLinkW_SetDescription(psl
, lpInfoTip
);
1075 if (SUCCEEDED(hr
) && DisplayName
)
1076 FIXME("Need to store display name %s, %d in shell link\n", debugstr_w(DisplayName
), DisplayResId
);
1079 hr
= IShellLinkW_QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
1082 Required
= (MAX_PATH
+ wcslen(LinkSubDir
) + 1 + wcslen(LinkName
)) * sizeof(WCHAR
);
1083 FullLinkName
= MyMalloc(Required
);
1088 if (LinkAttributes
& (FLG_PROFITEM_DELETE
| FLG_PROFITEM_GROUP
))
1089 FIXME("Need to handle FLG_PROFITEM_DELETE and FLG_PROFITEM_GROUP\n");
1090 if (LinkAttributes
& FLG_PROFITEM_CSIDL
)
1092 else if (LinkAttributes
& FLG_PROFITEM_CURRENTUSER
)
1093 CSIDL
= CSIDL_PROGRAMS
;
1095 if (SHGetSpecialFolderPathW(
1101 if (FullLinkName
[wcslen(FullLinkName
) - 1] != '\\')
1102 wcscat(FullLinkName
, BackSlash
);
1105 wcscat(FullLinkName
, LinkSubDir
);
1106 if (FullLinkName
[wcslen(FullLinkName
) - 1] != '\\')
1107 wcscat(FullLinkName
, BackSlash
);
1109 wcscat(FullLinkName
, LinkName
);
1110 wcscat(FullLinkName
, DotLnk
);
1111 hr
= IPersistFile_Save(ppf
, FullLinkName
, TRUE
);
1114 hr
= HRESULT_FROM_WIN32(GetLastError());
1116 IPersistFile_Release(ppf
);
1119 IShellLinkW_Release(psl
);
1126 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
1127 SetLastError(HRESULT_CODE(hr
));
1129 SetLastError(E_FAIL
);
1138 MyFree(FullFileName
);
1139 MyFree(FullWorkingDir
);
1140 MyFree(FullIconName
);
1141 MyFree(FullLinkName
);
1144 MyFree(DisplayName
);
1146 FreeLibrary(hOle32
);
1148 TRACE("Returning %d\n", ret
);
1152 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
1154 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
1159 /***********************************************************************
1160 * iterate_section_fields
1162 * Iterate over all fields of a certain key of a certain section
1164 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
1165 iterate_fields_func callback
, void *arg
)
1167 WCHAR static_buffer
[200];
1168 WCHAR
*buffer
= static_buffer
;
1169 DWORD size
= sizeof(static_buffer
)/sizeof(WCHAR
);
1173 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
1176 UINT i
, count
= SetupGetFieldCount( &context
);
1177 for (i
= 1; i
<= count
; i
++)
1179 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
1181 if (!callback( hinf
, buffer
, arg
))
1183 WARN("callback failed for %s %s err %d\n",
1184 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
1188 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
1192 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
1197 /***********************************************************************
1198 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
1200 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1201 PCSTR section
, PCSTR src_root
, UINT flags
)
1203 UNICODE_STRING sectionW
;
1206 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1208 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1212 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1216 UNICODE_STRING srcW
;
1217 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
1219 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1220 srcW
.Buffer
, flags
);
1221 RtlFreeUnicodeString( &srcW
);
1223 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1225 RtlFreeUnicodeString( §ionW
);
1230 /***********************************************************************
1231 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1233 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1234 PCWSTR section
, PCWSTR src_root
, UINT flags
)
1236 struct files_callback_info info
;
1239 info
.src_root
= src_root
;
1240 info
.copy_flags
= flags
;
1241 info
.layout
= hlayout
;
1242 return iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
);
1246 /***********************************************************************
1247 * SetupInstallFromInfSectionA (SETUPAPI.@)
1249 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
1250 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
1251 PSP_FILE_CALLBACK_A callback
, PVOID context
,
1252 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1254 UNICODE_STRING sectionW
, src_rootW
;
1255 struct callback_WtoA_context ctx
;
1258 src_rootW
.Buffer
= NULL
;
1259 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
1261 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1265 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1267 ctx
.orig_context
= context
;
1268 ctx
.orig_handler
= callback
;
1269 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
1270 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
1271 &ctx
, devinfo
, devinfo_data
);
1272 RtlFreeUnicodeString( §ionW
);
1274 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1276 RtlFreeUnicodeString( &src_rootW
);
1281 /***********************************************************************
1284 * Called once for each Include entry in a given section.
1286 static BOOL
include_callback( HINF hinf
, PCWSTR field
, void *arg
)
1288 return SetupOpenAppendInfFileW( field
, hinf
, NULL
);
1292 /***********************************************************************
1295 * Called once for each Needs entry in a given section.
1297 static BOOL
needs_callback( HINF hinf
, PCWSTR field
, void *arg
)
1299 struct needs_callback_info
*info
= arg
;
1304 return SetupInstallFromInfSectionW(info
->owner
, *(HINF
*)hinf
, field
, info
->flags
,
1305 info
->key_root
, info
->src_root
, info
->copy_flags
, info
->callback
,
1306 info
->context
, info
->devinfo
, info
->devinfo_data
);
1308 return SetupInstallServicesFromInfSectionExW(*(HINF
*)hinf
, field
, info
->flags
,
1309 info
->devinfo
, info
->devinfo_data
, info
->reserved1
, info
->reserved2
);
1311 ERR("Unknown info type %u\n", info
->type
);
1317 /***********************************************************************
1318 * SetupInstallFromInfSectionW (SETUPAPI.@)
1320 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
1321 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
1322 PSP_FILE_CALLBACK_W callback
, PVOID context
,
1323 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1325 struct needs_callback_info needs_info
;
1327 /* Parse 'Include' and 'Needs' directives */
1328 iterate_section_fields( hinf
, section
, Include
, include_callback
, NULL
);
1329 needs_info
.type
= 0;
1330 needs_info
.owner
= owner
;
1331 needs_info
.flags
= flags
;
1332 needs_info
.key_root
= key_root
;
1333 needs_info
.src_root
= src_root
;
1334 needs_info
.copy_flags
= copy_flags
;
1335 needs_info
.callback
= callback
;
1336 needs_info
.context
= context
;
1337 needs_info
.devinfo
= devinfo
;
1338 needs_info
.devinfo_data
= devinfo_data
;
1339 iterate_section_fields( hinf
, section
, Needs
, needs_callback
, &needs_info
);
1341 if (flags
& SPINST_FILES
)
1343 SP_DEVINSTALL_PARAMS_W install_params
;
1344 struct files_callback_info info
;
1345 HSPFILEQ queue
= NULL
;
1346 BOOL use_custom_queue
;
1349 install_params
.cbSize
= sizeof(SP_DEVINSTALL_PARAMS
);
1350 use_custom_queue
= SetupDiGetDeviceInstallParamsW(devinfo
, devinfo_data
, &install_params
) && (install_params
.Flags
& DI_NOVCP
);
1351 if (!use_custom_queue
&& ((queue
= SetupOpenFileQueue()) == (HSPFILEQ
)INVALID_HANDLE_VALUE
))
1353 info
.queue
= use_custom_queue
? install_params
.FileQueue
: queue
;
1354 info
.src_root
= src_root
;
1355 info
.copy_flags
= copy_flags
;
1357 ret
= (iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
) &&
1358 iterate_section_fields( hinf
, section
, DelFiles
, delete_files_callback
, &info
) &&
1359 iterate_section_fields( hinf
, section
, RenFiles
, rename_files_callback
, &info
));
1360 if (!use_custom_queue
)
1363 ret
= SetupCommitFileQueueW( owner
, queue
, callback
, context
);
1364 SetupCloseFileQueue( queue
);
1366 if (!ret
) return FALSE
;
1368 if (flags
& SPINST_INIFILES
)
1370 if (!iterate_section_fields( hinf
, section
, UpdateInis
, update_ini_callback
, NULL
) ||
1371 !iterate_section_fields( hinf
, section
, UpdateIniFields
,
1372 update_ini_fields_callback
, NULL
))
1375 if (flags
& SPINST_INI2REG
)
1377 if (!iterate_section_fields( hinf
, section
, Ini2Reg
, ini2reg_callback
, NULL
))
1380 if (flags
& SPINST_LOGCONFIG
)
1382 if (!iterate_section_fields( hinf
, section
, LogConf
, logconf_callback
, NULL
))
1385 if (flags
& SPINST_REGSVR
)
1387 struct register_dll_info info
;
1389 info
.unregister
= FALSE
;
1390 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1392 info
.callback
= callback
;
1393 info
.callback_context
= context
;
1395 else info
.callback
= NULL
;
1397 if (!iterate_section_fields( hinf
, section
, RegisterDlls
, register_dlls_callback
, &info
))
1401 if (!iterate_section_fields( hinf
, section
, WineFakeDlls
, fake_dlls_callback
, NULL
))
1403 #endif // __WINESRC__
1405 if (flags
& SPINST_UNREGSVR
)
1407 struct register_dll_info info
;
1409 info
.unregister
= TRUE
;
1410 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1412 info
.callback
= callback
;
1413 info
.callback_context
= context
;
1415 else info
.callback
= NULL
;
1417 if (!iterate_section_fields( hinf
, section
, UnregisterDlls
, register_dlls_callback
, &info
))
1420 if (flags
& SPINST_REGISTRY
)
1422 struct registry_callback_info info
;
1424 info
.default_root
= key_root
;
1426 if (!iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
))
1428 info
.delete = FALSE
;
1429 if (!iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
))
1432 if (flags
& SPINST_BITREG
)
1434 if (!iterate_section_fields( hinf
, section
, BitReg
, bitreg_callback
, NULL
))
1437 if (flags
& SPINST_PROFILEITEMS
)
1439 if (!iterate_section_fields( hinf
, section
, ProfileItems
, profile_items_callback
, NULL
))
1442 if (flags
& SPINST_COPYINF
)
1444 if (!iterate_section_fields( hinf
, section
, CopyINF
, copy_inf_callback
, NULL
))
1452 /***********************************************************************
1453 * InstallHinfSectionW (SETUPAPI.@)
1455 * NOTE: 'cmdline' is <section> <mode> <path> from
1456 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1458 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
1460 WCHAR
*s
, *path
, section
[MAX_PATH
];
1461 void *callback_context
= NULL
;
1462 DWORD SectionNameLength
;
1464 HINF hinf
= INVALID_HANDLE_VALUE
;
1465 BOOL bRebootRequired
= FALSE
;
1468 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
1470 lstrcpynW( section
, cmdline
, MAX_PATH
);
1472 if (!(s
= strchrW( section
, ' ' ))) return;
1474 while (*s
== ' ') s
++;
1477 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1478 if (!(s
= strchrW( s
, ' ' ))) return;
1479 while (*s
== ' ') s
++;
1484 FIXME("default path of the installation not changed\n");
1488 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
1489 if (hinf
== INVALID_HANDLE_VALUE
)
1491 WARN("SetupOpenInfFileW(%s) failed (Error %u)\n", path
, GetLastError());
1495 ret
= SetupDiGetActualSectionToInstallW(
1496 hinf
, section
, section
, sizeof(section
)/sizeof(section
[0]), &SectionNameLength
, NULL
);
1499 WARN("SetupDiGetActualSectionToInstallW() failed (Error %u)\n", GetLastError());
1502 if (SectionNameLength
> MAX_PATH
- strlenW(DotServices
))
1504 WARN("Section name '%s' too long\n", section
);
1508 /* Copy files and add registry entries */
1509 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
1510 ret
= SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
,
1511 SP_COPY_NEWER
| SP_COPY_IN_USE_NEEDS_REBOOT
,
1512 SetupDefaultQueueCallbackW
, callback_context
,
1516 WARN("SetupInstallFromInfSectionW() failed (Error %u)\n", GetLastError());
1519 /* FIXME: need to check if some files were in use and need reboot
1523 /* Install services */
1524 wcscat(section
, DotServices
);
1525 ret
= SetupInstallServicesFromInfSectionW( hinf
, section
, 0 );
1526 if (!ret
&& GetLastError() == ERROR_SECTION_NOT_FOUND
)
1530 WARN("SetupInstallServicesFromInfSectionW() failed (Error %u)\n", GetLastError());
1533 else if (GetLastError() == ERROR_SUCCESS_REBOOT_REQUIRED
)
1535 bRebootRequired
= TRUE
;
1538 /* Check if we need to reboot */
1546 ExitWindowsEx(EWX_REBOOT
, SHTDN_REASON_MAJOR_APPLICATION
|
1547 SHTDN_REASON_MINOR_INSTALLATION
| SHTDN_REASON_FLAG_PLANNED
);
1550 /* Query user before rebooting */
1551 SetupPromptReboot(NULL
, hwnd
, FALSE
);
1554 /* Reboot if necessary */
1555 if (bRebootRequired
)
1557 ExitWindowsEx(EWX_REBOOT
, SHTDN_REASON_MAJOR_APPLICATION
|
1558 SHTDN_REASON_MINOR_INSTALLATION
| SHTDN_REASON_FLAG_PLANNED
);
1562 /* If necessary, query user before rebooting */
1563 if (bRebootRequired
)
1565 SetupPromptReboot(NULL
, hwnd
, FALSE
);
1573 if ( callback_context
)
1574 SetupTermDefaultQueueCallback( callback_context
);
1575 if ( hinf
!= INVALID_HANDLE_VALUE
)
1576 SetupCloseInfFile( hinf
);
1580 /***********************************************************************
1581 * InstallHinfSectionA (SETUPAPI.@)
1583 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
1585 UNICODE_STRING cmdlineW
;
1587 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
1589 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
1590 RtlFreeUnicodeString( &cmdlineW
);
1594 /***********************************************************************
1595 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1597 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF Inf
, PCWSTR SectionName
, DWORD Flags
)
1599 return SetupInstallServicesFromInfSectionExW( Inf
, SectionName
, Flags
,
1600 NULL
, NULL
, NULL
, NULL
);
1603 /***********************************************************************
1604 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1606 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF Inf
, PCSTR SectionName
, DWORD Flags
)
1608 return SetupInstallServicesFromInfSectionExA( Inf
, SectionName
, Flags
,
1609 NULL
, NULL
, NULL
, NULL
);
1612 /***********************************************************************
1613 * SetupInstallServicesFromInfSectionExA (SETUPAPI.@)
1615 BOOL WINAPI
SetupInstallServicesFromInfSectionExA( HINF hinf
, PCSTR sectionname
, DWORD flags
, HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
, PVOID reserved1
, PVOID reserved2
)
1617 UNICODE_STRING sectionnameW
;
1620 if (RtlCreateUnicodeStringFromAsciiz( §ionnameW
, sectionname
))
1622 ret
= SetupInstallServicesFromInfSectionExW( hinf
, sectionnameW
.Buffer
, flags
, devinfo
, devinfo_data
, reserved1
, reserved2
);
1623 RtlFreeUnicodeString( §ionnameW
);
1626 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1632 static BOOL
GetLineText( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, PWSTR
*value
)
1639 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, NULL
, 0, &required
)
1640 && GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1643 buf
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) );
1646 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1650 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, buf
, required
, &required
) )
1652 HeapFree( GetProcessHeap(), 0, buf
);
1661 static BOOL
GetIntField( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, INT
*value
)
1666 if (! GetLineText( hinf
, section_name
, key_name
, &buffer
) )
1669 res
= wcstol( buffer
, &end
, 0 );
1670 if (end
!= buffer
&& !*end
)
1672 HeapFree(GetProcessHeap(), 0, buffer
);
1678 HeapFree(GetProcessHeap(), 0, buffer
);
1679 SetLastError( ERROR_INVALID_DATA
);
1685 BOOL
GetStringField( PINFCONTEXT context
, DWORD index
, PWSTR
*value
)
1690 ret
= SetupGetStringFieldW(
1697 else if (RequiredSize
== 0)
1703 /* We got the needed size for the buffer */
1704 *value
= MyMalloc(RequiredSize
* sizeof(WCHAR
));
1707 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1710 ret
= SetupGetStringFieldW(
1713 *value
, RequiredSize
, NULL
);
1720 static VOID
FixupServiceBinaryPath(
1721 IN DWORD ServiceType
,
1722 IN OUT LPWSTR
*ServiceBinary
)
1725 WCHAR ReactosDir
[MAX_PATH
];
1726 DWORD RosDirLength
, ServiceLength
, Win32Length
;
1728 GetWindowsDirectoryW(ReactosDir
, MAX_PATH
);
1729 RosDirLength
= strlenW(ReactosDir
);
1730 ServiceLength
= strlenW(*ServiceBinary
);
1732 /* Check and fix two things:
1733 1. Get rid of C:\ReactOS and use relative
1735 2. Add %SystemRoot% for Win32 services */
1737 if (ServiceLength
< RosDirLength
)
1740 if (!wcsnicmp(*ServiceBinary
, ReactosDir
, RosDirLength
))
1742 /* Yes, the first part is the C:\ReactOS\, just skip it */
1743 MoveMemory(*ServiceBinary
, *ServiceBinary
+ RosDirLength
+ 1,
1744 (ServiceLength
- RosDirLength
) * sizeof(WCHAR
));
1746 /* Handle Win32-services differently */
1747 if (ServiceType
& SERVICE_WIN32
)
1749 Win32Length
= (ServiceLength
-
1750 RosDirLength
- 1 + 13) * sizeof(WCHAR
);
1751 /* -1 to not count the separator after C:\ReactOS
1752 wcslen(L"%SystemRoot%\\") = 13*sizeof(wchar_t) */
1753 Buffer
= MyMalloc(Win32Length
);
1755 wcscpy(Buffer
, L
"%SystemRoot%\\");
1756 wcscat(Buffer
, *ServiceBinary
);
1757 MyFree(*ServiceBinary
);
1759 *ServiceBinary
= Buffer
;
1764 static BOOL
InstallOneService(
1765 struct DeviceInfoSet
*list
,
1767 IN LPCWSTR ServiceSection
,
1768 IN LPCWSTR ServiceName
,
1769 IN UINT ServiceFlags
)
1771 SC_HANDLE hSCManager
= NULL
;
1772 SC_HANDLE hService
= NULL
;
1773 LPDWORD GroupOrder
= NULL
;
1774 LPQUERY_SERVICE_CONFIGW ServiceConfig
= NULL
;
1775 HKEY hServicesKey
, hServiceKey
;
1779 HKEY hGroupOrderListKey
= NULL
;
1780 LPWSTR ServiceBinary
= NULL
;
1781 LPWSTR LoadOrderGroup
= NULL
;
1782 LPWSTR DisplayName
= NULL
;
1783 LPWSTR Description
= NULL
;
1784 LPWSTR Dependencies
= NULL
;
1785 LPWSTR SecurityDescriptor
= NULL
;
1786 PSECURITY_DESCRIPTOR sd
= NULL
;
1787 INT ServiceType
, StartType
, ErrorControl
;
1789 DWORD tagId
= (DWORD
)-1;
1792 if (!GetIntField(hInf
, ServiceSection
, ServiceTypeKey
, &ServiceType
))
1794 if (!GetIntField(hInf
, ServiceSection
, StartTypeKey
, &StartType
))
1796 if (!GetIntField(hInf
, ServiceSection
, ErrorControlKey
, &ErrorControl
))
1798 useTag
= (ServiceType
== SERVICE_BOOT_START
|| ServiceType
== SERVICE_SYSTEM_START
);
1800 hSCManager
= OpenSCManagerW(NULL
, SERVICES_ACTIVE_DATABASE
, SC_MANAGER_CREATE_SERVICE
);
1801 if (hSCManager
== NULL
)
1804 if (!GetLineText(hInf
, ServiceSection
, ServiceBinaryKey
, &ServiceBinary
))
1807 /* Adjust binary path according to the service type */
1808 FixupServiceBinaryPath(ServiceType
, &ServiceBinary
);
1810 /* Don't check return value, as these fields are optional and
1811 * GetLineText initialize output parameter even on failure */
1812 GetLineText(hInf
, ServiceSection
, LoadOrderGroupKey
, &LoadOrderGroup
);
1813 GetLineText(hInf
, ServiceSection
, DisplayNameKey
, &DisplayName
);
1814 GetLineText(hInf
, ServiceSection
, DescriptionKey
, &Description
);
1815 GetLineText(hInf
, ServiceSection
, DependenciesKey
, &Dependencies
);
1817 /* If there is no group, we must not request a tag */
1818 if (!LoadOrderGroup
|| !*LoadOrderGroup
)
1821 hService
= OpenServiceW(
1824 DELETE
| SERVICE_QUERY_CONFIG
| SERVICE_CHANGE_CONFIG
| WRITE_DAC
);
1825 if (hService
== NULL
&& GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST
)
1828 if (hService
&& (ServiceFlags
& SPSVCINST_DELETEEVENTLOGENTRY
))
1830 ret
= DeleteService(hService
);
1831 if (!ret
&& GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE
)
1835 if (hService
== NULL
)
1837 /* Create new service */
1838 hService
= CreateServiceW(
1848 useTag
? &tagId
: NULL
,
1851 if (hService
== NULL
)
1857 /* Read current configuration */
1858 if (!QueryServiceConfigW(hService
, NULL
, 0, &bufferSize
))
1860 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1862 ServiceConfig
= MyMalloc(bufferSize
);
1865 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1868 if (!QueryServiceConfigW(hService
, ServiceConfig
, bufferSize
, &bufferSize
))
1871 tagId
= ServiceConfig
->dwTagId
;
1873 /* Update configuration */
1874 ret
= ChangeServiceConfigW(
1877 (ServiceFlags
& SPSVCINST_NOCLOBBER_STARTTYPE
) ? SERVICE_NO_CHANGE
: StartType
,
1878 (ServiceFlags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) ? SERVICE_NO_CHANGE
: ErrorControl
,
1880 (ServiceFlags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
&& ServiceConfig
->lpLoadOrderGroup
) ? NULL
: LoadOrderGroup
,
1881 useTag
? &tagId
: NULL
,
1882 (ServiceFlags
& SPSVCINST_NOCLOBBER_DEPENDENCIES
&& ServiceConfig
->lpDependencies
) ? NULL
: Dependencies
,
1884 (ServiceFlags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
&& ServiceConfig
->lpDisplayName
) ? NULL
: DisplayName
);
1890 if (GetLineText(hInf
, ServiceSection
, SecurityKey
, &SecurityDescriptor
))
1892 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(SecurityDescriptor
, SDDL_REVISION_1
, &sd
, NULL
);
1895 ret
= SetServiceObjectSecurity(hService
, DACL_SECURITY_INFORMATION
, sd
);
1900 /* FIXME: use Description and SPSVCINST_NOCLOBBER_DESCRIPTION */
1904 /* Add the tag to SYSTEM\CurrentControlSet\Control\GroupOrderList key */
1905 LPCWSTR lpLoadOrderGroup
;
1908 lpLoadOrderGroup
= LoadOrderGroup
;
1909 if ((ServiceFlags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
) && ServiceConfig
&& ServiceConfig
->lpLoadOrderGroup
)
1910 lpLoadOrderGroup
= ServiceConfig
->lpLoadOrderGroup
;
1913 list
? list
->HKLM
: HKEY_LOCAL_MACHINE
,
1915 &hGroupOrderListKey
);
1916 if (rc
!= ERROR_SUCCESS
)
1921 rc
= RegQueryValueExW(hGroupOrderListKey
, lpLoadOrderGroup
, NULL
, &dwRegType
, NULL
, &bufferSize
);
1922 if (rc
== ERROR_FILE_NOT_FOUND
)
1923 bufferSize
= sizeof(DWORD
);
1924 else if (rc
!= ERROR_SUCCESS
)
1929 else if (dwRegType
!= REG_BINARY
|| bufferSize
== 0 || bufferSize
% sizeof(DWORD
) != 0)
1931 SetLastError(ERROR_GEN_FAILURE
);
1934 /* Allocate buffer to store existing data + the new tag */
1935 GroupOrder
= MyMalloc(bufferSize
+ sizeof(DWORD
));
1938 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1941 if (rc
== ERROR_SUCCESS
)
1943 /* Read existing data */
1944 rc
= RegQueryValueExW(
1951 if (rc
!= ERROR_SUCCESS
)
1956 if (ServiceFlags
& SPSVCINST_TAGTOFRONT
)
1957 memmove(&GroupOrder
[2], &GroupOrder
[1], bufferSize
- sizeof(DWORD
));
1964 if (ServiceFlags
& SPSVCINST_TAGTOFRONT
)
1965 GroupOrder
[1] = tagId
;
1967 GroupOrder
[bufferSize
/ sizeof(DWORD
)] = tagId
;
1969 rc
= RegSetValueExW(
1975 bufferSize
+ sizeof(DWORD
));
1976 if (rc
!= ERROR_SUCCESS
)
1983 /* Handle AddReg and DelReg */
1985 list
? list
->HKLM
: HKEY_LOCAL_MACHINE
,
1986 REGSTR_PATH_SERVICES
,
1990 if (rc
!= ERROR_SUCCESS
)
1999 KEY_READ
| KEY_WRITE
,
2001 RegCloseKey(hServicesKey
);
2002 if (rc
!= ERROR_SUCCESS
)
2008 ret
= SetupInstallFromInfSectionW(
2020 RegCloseKey(hServiceKey
);
2023 if (hSCManager
!= NULL
)
2024 CloseServiceHandle(hSCManager
);
2025 if (hService
!= NULL
)
2026 CloseServiceHandle(hService
);
2027 if (hGroupOrderListKey
!= NULL
)
2028 RegCloseKey(hGroupOrderListKey
);
2031 MyFree(ServiceConfig
);
2032 MyFree(ServiceBinary
);
2033 MyFree(LoadOrderGroup
);
2034 MyFree(DisplayName
);
2035 MyFree(Description
);
2036 MyFree(Dependencies
);
2037 MyFree(SecurityDescriptor
);
2040 TRACE("Returning %d\n", ret
);
2045 /***********************************************************************
2046 * SetupInstallServicesFromInfSectionExW (SETUPAPI.@)
2048 BOOL WINAPI
SetupInstallServicesFromInfSectionExW( HINF hinf
, PCWSTR sectionname
, DWORD flags
, HDEVINFO DeviceInfoSet
, PSP_DEVINFO_DATA DeviceInfoData
, PVOID reserved1
, PVOID reserved2
)
2050 struct DeviceInfoSet
*list
= NULL
;
2053 TRACE("%p, %s, 0x%lx, %p, %p, %p, %p\n", hinf
, debugstr_w(sectionname
),
2054 flags
, DeviceInfoSet
, DeviceInfoData
, reserved1
, reserved2
);
2057 SetLastError(ERROR_INVALID_PARAMETER
);
2058 else if (flags
& ~(SPSVCINST_TAGTOFRONT
| SPSVCINST_DELETEEVENTLOGENTRY
| SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
| SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
| SPSVCINST_NOCLOBBER_DEPENDENCIES
| SPSVCINST_STOPSERVICE
))
2060 TRACE("Unknown flags: 0x%08lx\n", flags
& ~(SPSVCINST_TAGTOFRONT
| SPSVCINST_DELETEEVENTLOGENTRY
| SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
| SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
| SPSVCINST_NOCLOBBER_DEPENDENCIES
| SPSVCINST_STOPSERVICE
));
2061 SetLastError(ERROR_INVALID_FLAGS
);
2063 else if (DeviceInfoSet
== (HDEVINFO
)INVALID_HANDLE_VALUE
)
2064 SetLastError(ERROR_INVALID_HANDLE
);
2065 else if (DeviceInfoSet
&& (list
= (struct DeviceInfoSet
*)DeviceInfoSet
)->magic
!= SETUP_DEVICE_INFO_SET_MAGIC
)
2066 SetLastError(ERROR_INVALID_HANDLE
);
2067 else if (DeviceInfoData
&& DeviceInfoData
->cbSize
!= sizeof(SP_DEVINFO_DATA
))
2068 SetLastError(ERROR_INVALID_USER_BUFFER
);
2069 else if (reserved1
!= NULL
|| reserved2
!= NULL
)
2070 SetLastError(ERROR_INVALID_PARAMETER
);
2073 struct needs_callback_info needs_info
;
2074 LPWSTR ServiceName
= NULL
;
2075 LPWSTR ServiceSection
= NULL
;
2077 INFCONTEXT ContextService
;
2078 BOOL bNeedReboot
= FALSE
;
2080 /* Parse 'Include' and 'Needs' directives */
2081 iterate_section_fields( hinf
, sectionname
, Include
, include_callback
, NULL
);
2082 needs_info
.type
= 1;
2083 needs_info
.flags
= flags
;
2084 needs_info
.devinfo
= DeviceInfoSet
;
2085 needs_info
.devinfo_data
= DeviceInfoData
;
2086 needs_info
.reserved1
= reserved1
;
2087 needs_info
.reserved2
= reserved2
;
2088 iterate_section_fields( hinf
, sectionname
, Needs
, needs_callback
, &needs_info
);
2090 if (flags
& SPSVCINST_STOPSERVICE
)
2092 FIXME("Stopping the device not implemented\n");
2093 /* This may lead to require a reboot */
2094 /* bNeedReboot = TRUE; */
2096 SERVICE_STATUS ServiceStatus
;
2097 ret
= ControlService(hService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
2098 if (!ret
&& GetLastError() != ERROR_SERVICE_NOT_ACTIVE
)
2100 if (ServiceStatus
.dwCurrentState
!= SERVICE_STOP_PENDING
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
)
2102 SetLastError(ERROR_INSTALL_SERVICE_FAILURE
);
2106 flags
&= ~SPSVCINST_STOPSERVICE
;
2109 ret
= SetupFindFirstLineW(hinf
, sectionname
, AddService
, &ContextService
);
2112 if (!GetStringField(&ContextService
, 1, &ServiceName
))
2115 ret
= SetupGetIntField(
2117 2, /* Field index */
2121 /* The field may be empty. Ignore the error */
2125 if (!GetStringField(&ContextService
, 3, &ServiceSection
))
2128 ret
= InstallOneService(list
, hinf
, ServiceSection
, ServiceName
, (ServiceFlags
& ~SPSVCINST_ASSOCSERVICE
) | flags
);
2132 if (ServiceFlags
& SPSVCINST_ASSOCSERVICE
)
2134 ret
= SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet
, DeviceInfoData
, SPDRP_SERVICE
, (LPBYTE
)ServiceName
, (strlenW(ServiceName
) + 1) * sizeof(WCHAR
));
2140 HeapFree(GetProcessHeap(), 0, ServiceName
);
2141 HeapFree(GetProcessHeap(), 0, ServiceSection
);
2142 ServiceName
= ServiceSection
= NULL
;
2143 ret
= SetupFindNextMatchLineW(&ContextService
, AddService
, &ContextService
);
2147 SetLastError(ERROR_SUCCESS_REBOOT_REQUIRED
);
2149 SetLastError(ERROR_SUCCESS
);
2153 TRACE("Returning %d\n", ret
);
2158 /***********************************************************************
2159 * SetupCopyOEMInfA (SETUPAPI.@)
2161 BOOL WINAPI
SetupCopyOEMInfA(
2162 IN PCSTR SourceInfFileName
,
2163 IN PCSTR OEMSourceMediaLocation
,
2164 IN DWORD OEMSourceMediaType
,
2166 OUT PSTR DestinationInfFileName OPTIONAL
,
2167 IN DWORD DestinationInfFileNameSize
,
2168 OUT PDWORD RequiredSize OPTIONAL
,
2169 OUT PSTR
* DestinationInfFileNameComponent OPTIONAL
)
2171 PWSTR SourceInfFileNameW
= NULL
;
2172 PWSTR OEMSourceMediaLocationW
= NULL
;
2173 PWSTR DestinationInfFileNameW
= NULL
;
2174 PWSTR DestinationInfFileNameComponentW
= NULL
;
2178 TRACE("%s %s 0x%lx 0x%lx %p 0%lu %p %p\n",
2179 SourceInfFileName
, OEMSourceMediaLocation
, OEMSourceMediaType
,
2180 CopyStyle
, DestinationInfFileName
, DestinationInfFileNameSize
,
2181 RequiredSize
, DestinationInfFileNameComponent
);
2183 if (!DestinationInfFileName
&& DestinationInfFileNameSize
> 0)
2184 SetLastError(ERROR_INVALID_PARAMETER
);
2185 else if (!(SourceInfFileNameW
= pSetupMultiByteToUnicode(SourceInfFileName
, CP_ACP
)))
2186 SetLastError(ERROR_INVALID_PARAMETER
);
2187 else if (OEMSourceMediaType
!= SPOST_NONE
&& !(OEMSourceMediaLocationW
= pSetupMultiByteToUnicode(OEMSourceMediaLocation
, CP_ACP
)))
2188 SetLastError(ERROR_INVALID_PARAMETER
);
2191 if (DestinationInfFileNameSize
!= 0)
2193 DestinationInfFileNameW
= MyMalloc(DestinationInfFileNameSize
* sizeof(WCHAR
));
2194 if (!DestinationInfFileNameW
)
2196 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2201 ret
= SetupCopyOEMInfW(
2203 OEMSourceMediaLocationW
,
2206 DestinationInfFileNameW
,
2207 DestinationInfFileNameSize
,
2209 DestinationInfFileNameComponent
? &DestinationInfFileNameComponentW
: NULL
);
2212 if (RequiredSize
) *RequiredSize
= size
;
2216 if (DestinationInfFileNameSize
!= 0)
2218 if (WideCharToMultiByte(CP_ACP
, 0, DestinationInfFileNameW
, -1,
2219 DestinationInfFileName
, DestinationInfFileNameSize
, NULL
, NULL
) == 0)
2221 DestinationInfFileName
[0] = '\0';
2225 if (DestinationInfFileNameComponent
)
2227 if (DestinationInfFileNameComponentW
)
2228 *DestinationInfFileNameComponent
= &DestinationInfFileName
[DestinationInfFileNameComponentW
- DestinationInfFileNameW
];
2230 *DestinationInfFileNameComponent
= NULL
;
2236 MyFree(SourceInfFileNameW
);
2237 MyFree(OEMSourceMediaLocationW
);
2238 MyFree(DestinationInfFileNameW
);
2239 TRACE("Returning %d\n", ret
);
2240 if (ret
) SetLastError(ERROR_SUCCESS
);
2244 static int compare_files( HANDLE file1
, HANDLE file2
)
2251 while( ReadFile(file1
, buffer1
, sizeof(buffer1
), &size1
, NULL
) &&
2252 ReadFile(file2
, buffer2
, sizeof(buffer2
), &size2
, NULL
) )
2256 return size1
> size2
? 1 : -1;
2259 ret
= memcmp( buffer1
, buffer2
, size1
);
2267 /***********************************************************************
2268 * SetupCopyOEMInfW (SETUPAPI.@)
2270 BOOL WINAPI
SetupCopyOEMInfW(
2271 IN PCWSTR SourceInfFileName
,
2272 IN PCWSTR OEMSourceMediaLocation
,
2273 IN DWORD OEMSourceMediaType
,
2275 OUT PWSTR DestinationInfFileName OPTIONAL
,
2276 IN DWORD DestinationInfFileNameSize
,
2277 OUT PDWORD RequiredSize OPTIONAL
,
2278 OUT PWSTR
* DestinationInfFileNameComponent OPTIONAL
)
2282 TRACE("%s %s 0x%lx 0x%lx %p 0%lu %p %p\n",
2283 debugstr_w(SourceInfFileName
), debugstr_w(OEMSourceMediaLocation
), OEMSourceMediaType
,
2284 CopyStyle
, DestinationInfFileName
, DestinationInfFileNameSize
,
2285 RequiredSize
, DestinationInfFileNameComponent
);
2287 if (!SourceInfFileName
)
2288 SetLastError(ERROR_INVALID_PARAMETER
);
2289 else if (OEMSourceMediaType
!= SPOST_NONE
&& OEMSourceMediaType
!= SPOST_PATH
&& OEMSourceMediaType
!= SPOST_URL
)
2290 SetLastError(ERROR_INVALID_PARAMETER
);
2291 else if (CopyStyle
& ~(SP_COPY_DELETESOURCE
| SP_COPY_REPLACEONLY
| SP_COPY_NOOVERWRITE
| SP_COPY_OEMINF_CATALOG_ONLY
))
2293 TRACE("Unknown flags: 0x%08lx\n", CopyStyle
& ~(SP_COPY_DELETESOURCE
| SP_COPY_REPLACEONLY
| SP_COPY_NOOVERWRITE
| SP_COPY_OEMINF_CATALOG_ONLY
));
2294 SetLastError(ERROR_INVALID_FLAGS
);
2296 else if (!DestinationInfFileName
&& DestinationInfFileNameSize
> 0)
2297 SetLastError(ERROR_INVALID_PARAMETER
);
2298 else if (CopyStyle
& SP_COPY_OEMINF_CATALOG_ONLY
)
2300 FIXME("CopyStyle 0x%x not supported\n", SP_COPY_OEMINF_CATALOG_ONLY
);
2301 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2305 HANDLE hSearch
= INVALID_HANDLE_VALUE
;
2306 WIN32_FIND_DATAW FindFileData
;
2308 DWORD NextFreeNumber
= 0;
2310 LPWSTR pFullFileName
= NULL
;
2311 LPWSTR pFileName
; /* Pointer into pFullFileName buffer */
2312 HANDLE hSourceFile
= INVALID_HANDLE_VALUE
;
2314 if (OEMSourceMediaType
== SPOST_PATH
|| OEMSourceMediaType
== SPOST_URL
)
2315 FIXME("OEMSourceMediaType 0x%lx ignored\n", OEMSourceMediaType
);
2317 /* Check if source file exists, and open it */
2318 if (strchrW(SourceInfFileName
, '\\' ) || strchrW(SourceInfFileName
, '/' ))
2322 if (!(len
= GetFullPathNameW(SourceInfFileName
, 0, NULL
, NULL
)))
2324 if (!(path
= MyMalloc(len
* sizeof(WCHAR
))))
2326 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2329 GetFullPathNameW(SourceInfFileName
, len
, path
, NULL
);
2330 hSourceFile
= CreateFileW(
2331 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2332 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2333 NULL
, OPEN_EXISTING
, 0, NULL
);
2336 else /* try Windows directory */
2339 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
2340 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
2342 len
= GetWindowsDirectoryW(NULL
, 0) + strlenW(SourceInfFileName
) + 12;
2343 if (!(path
= MyMalloc(len
* sizeof(WCHAR
))))
2345 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2348 GetWindowsDirectoryW(path
, len
);
2349 p
= path
+ strlenW(path
);
2351 strcatW(p
, SourceInfFileName
);
2352 hSourceFile
= CreateFileW(
2353 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2354 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2355 NULL
, OPEN_EXISTING
, 0, NULL
);
2356 if (hSourceFile
== INVALID_HANDLE_VALUE
)
2358 strcpyW(p
, System32
);
2359 strcatW(p
, SourceInfFileName
);
2360 hSourceFile
= CreateFileW(
2361 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2362 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2363 NULL
, OPEN_EXISTING
, 0, NULL
);
2367 if (hSourceFile
== INVALID_HANDLE_VALUE
)
2369 SetLastError(ERROR_FILE_NOT_FOUND
);
2373 /* Prepare .inf file specification */
2374 len
= MAX_PATH
+ 1 + strlenW(InfDirectory
) + 13;
2375 pFullFileName
= MyMalloc(len
* sizeof(WCHAR
));
2378 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2381 len
= GetSystemWindowsDirectoryW(pFullFileName
, MAX_PATH
);
2382 if (len
== 0 || len
> MAX_PATH
)
2384 if (pFullFileName
[strlenW(pFullFileName
) - 1] != '\\')
2385 strcatW(pFullFileName
, BackSlash
);
2386 strcatW(pFullFileName
, InfDirectory
);
2387 pFileName
= &pFullFileName
[strlenW(pFullFileName
)];
2389 /* Search if the specified .inf file already exists in %WINDIR%\Inf */
2390 AlreadyExists
= FALSE
;
2391 strcpyW(pFileName
, OemFileMask
);
2392 hSearch
= FindFirstFileW(pFullFileName
, &FindFileData
);
2393 if (hSearch
!= INVALID_HANDLE_VALUE
)
2395 LARGE_INTEGER SourceFileSize
;
2397 if (GetFileSizeEx(hSourceFile
, &SourceFileSize
))
2401 LARGE_INTEGER DestFileSize
;
2404 strcpyW(pFileName
, FindFileData
.cFileName
);
2405 hDestFile
= CreateFileW(
2406 pFullFileName
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2407 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2408 NULL
, OPEN_EXISTING
, 0, NULL
);
2409 if (hDestFile
!= INVALID_HANDLE_VALUE
)
2411 if (GetFileSizeEx(hDestFile
, &DestFileSize
)
2412 && DestFileSize
.QuadPart
== SourceFileSize
.QuadPart
2413 && !compare_files(hSourceFile
, hDestFile
))
2415 TRACE("%s already exists as %s\n",
2416 debugstr_w(SourceInfFileName
), debugstr_w(pFileName
));
2417 AlreadyExists
= TRUE
;
2420 } while (!AlreadyExists
&& FindNextFileW(hSearch
, &FindFileData
));
2423 hSearch
= INVALID_HANDLE_VALUE
;
2426 if (!AlreadyExists
&& CopyStyle
& SP_COPY_REPLACEONLY
)
2428 /* FIXME: set DestinationInfFileName, RequiredSize, DestinationInfFileNameComponent */
2429 SetLastError(ERROR_FILE_NOT_FOUND
);
2432 else if (AlreadyExists
&& (CopyStyle
& SP_COPY_NOOVERWRITE
))
2434 DWORD Size
= strlenW(pFileName
) + 1;
2437 *RequiredSize
= Size
;
2438 if (DestinationInfFileNameSize
== 0)
2439 SetLastError(ERROR_FILE_EXISTS
);
2440 else if (DestinationInfFileNameSize
< Size
)
2441 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2444 SetLastError(ERROR_FILE_EXISTS
);
2445 strcpyW(DestinationInfFileName
, pFileName
);
2450 /* Search the number to give to OEM??.INF */
2451 strcpyW(pFileName
, OemFileMask
);
2452 hSearch
= FindFirstFileW(pFullFileName
, &FindFileData
);
2453 if (hSearch
== INVALID_HANDLE_VALUE
)
2455 if (GetLastError() != ERROR_FILE_NOT_FOUND
)
2462 DWORD CurrentNumber
;
2463 if (swscanf(FindFileData
.cFileName
, OemFileSpecification
, &CurrentNumber
) == 1
2464 && CurrentNumber
<= 99999)
2466 if (CurrentNumber
>= NextFreeNumber
)
2467 NextFreeNumber
= CurrentNumber
+ 1;
2469 } while (FindNextFileW(hSearch
, &FindFileData
));
2472 if (NextFreeNumber
> 99999)
2474 ERR("Too much custom .inf files\n");
2475 SetLastError(ERROR_GEN_FAILURE
);
2479 /* Create the full path: %WINDIR%\Inf\OEM{XXXXX}.inf */
2480 sprintfW(pFileName
, OemFileSpecification
, NextFreeNumber
);
2481 TRACE("Next available file is %s\n", debugstr_w(pFileName
));
2483 if (!CopyFileW(SourceInfFileName
, pFullFileName
, TRUE
))
2485 TRACE("CopyFileW() failed with error 0x%lx\n", GetLastError());
2489 len
= strlenW(pFullFileName
) + 1;
2491 *RequiredSize
= len
;
2492 if (DestinationInfFileName
)
2494 if (DestinationInfFileNameSize
>= len
)
2496 strcpyW(DestinationInfFileName
, pFullFileName
);
2497 if (DestinationInfFileNameComponent
)
2498 *DestinationInfFileNameComponent
= &DestinationInfFileName
[pFileName
- pFullFileName
];
2502 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2507 if (CopyStyle
& SP_COPY_DELETESOURCE
)
2509 if (!DeleteFileW(SourceInfFileName
))
2511 TRACE("DeleteFileW() failed with error 0x%lx\n", GetLastError());
2519 if (hSourceFile
!= INVALID_HANDLE_VALUE
)
2520 CloseHandle(hSourceFile
);
2521 if (hSearch
!= INVALID_HANDLE_VALUE
)
2523 MyFree(pFullFileName
);
2526 TRACE("Returning %d\n", ret
);
2527 if (ret
) SetLastError(ERROR_SUCCESS
);