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"
24 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
26 /* Unicode constants */
27 static const WCHAR BackSlash
[] = {'\\',0};
28 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};
29 static const WCHAR InfDirectory
[] = {'i','n','f','\\',0};
30 static const WCHAR OemFileMask
[] = {'o','e','m','*','.','i','n','f',0};
31 static const WCHAR OemFileSpecification
[] = {'o','e','m','%','l','u','.','i','n','f',0};
32 static const WCHAR DotLnk
[] = {'.','l','n','k',0};
33 static const WCHAR DotServices
[] = {'.','S','e','r','v','i','c','e','s',0};
35 static const WCHAR DependenciesKey
[] = {'D','e','p','e','n','d','e','n','c','i','e','s',0};
36 static const WCHAR DescriptionKey
[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
37 static const WCHAR DisplayNameKey
[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
38 static const WCHAR ErrorControlKey
[] = {'E','r','r','o','r','C','o','n','t','r','o','l',0};
39 static const WCHAR LoadOrderGroupKey
[] = {'L','o','a','d','O','r','d','e','r','G','r','o','u','p',0};
40 static const WCHAR SecurityKey
[] = {'S','e','c','u','r','i','t','y',0};
41 static const WCHAR ServiceBinaryKey
[] = {'S','e','r','v','i','c','e','B','i','n','a','r','y',0};
42 static const WCHAR ServiceTypeKey
[] = {'S','e','r','v','i','c','e','T','y','p','e',0};
43 static const WCHAR StartTypeKey
[] = {'S','t','a','r','t','T','y','p','e',0};
45 static const WCHAR Name
[] = {'N','a','m','e',0};
46 static const WCHAR CmdLine
[] = {'C','m','d','L','i','n','e',0};
47 static const WCHAR SubDir
[] = {'S','u','b','D','i','r',0};
48 static const WCHAR WorkingDir
[] = {'W','o','r','k','i','n','g','D','i','r',0};
49 static const WCHAR IconPath
[] = {'I','c','o','n','P','a','t','h',0};
50 static const WCHAR IconIndex
[] = {'I','c','o','n','I','n','d','e','x',0};
51 static const WCHAR HotKey
[] = {'H','o','t','K','e','y',0};
52 static const WCHAR InfoTip
[] = {'I','n','f','o','T','i','p',0};
53 static const WCHAR DisplayResource
[] = {'D','i','s','p','l','a','y','R','e','s','o','u','r','c','e',0};
55 /* info passed to callback functions dealing with files */
56 struct files_callback_info
64 /* info passed to callback functions dealing with the registry */
65 struct registry_callback_info
71 /* info passed to callback functions dealing with registering dlls */
72 struct register_dll_info
74 PSP_FILE_CALLBACK_W callback
;
75 PVOID callback_context
;
79 /* info passed to callback functions dealing with Needs directives */
80 struct needs_callback_info
92 PSP_DEVINFO_DATA devinfo_data
;
97 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
98 static BOOL
GetLineText( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, PWSTR
*value
);
99 typedef HRESULT (WINAPI
*COINITIALIZE
)(IN LPVOID pvReserved
);
100 typedef HRESULT (WINAPI
*COCREATEINSTANCE
)(IN REFCLSID rclsid
, IN LPUNKNOWN pUnkOuter
, IN DWORD dwClsContext
, IN REFIID riid
, OUT LPVOID
*ppv
);
101 typedef HRESULT (WINAPI
*COUNINITIALIZE
)(VOID
);
103 /* Unicode constants */
104 static const WCHAR AddService
[] = {'A','d','d','S','e','r','v','i','c','e',0};
105 static const WCHAR CopyFiles
[] = {'C','o','p','y','F','i','l','e','s',0};
106 static const WCHAR DelFiles
[] = {'D','e','l','F','i','l','e','s',0};
107 static const WCHAR RenFiles
[] = {'R','e','n','F','i','l','e','s',0};
108 static const WCHAR Ini2Reg
[] = {'I','n','i','2','R','e','g',0};
109 static const WCHAR LogConf
[] = {'L','o','g','C','o','n','f',0};
110 static const WCHAR AddReg
[] = {'A','d','d','R','e','g',0};
111 static const WCHAR DelReg
[] = {'D','e','l','R','e','g',0};
112 static const WCHAR BitReg
[] = {'B','i','t','R','e','g',0};
113 static const WCHAR UpdateInis
[] = {'U','p','d','a','t','e','I','n','i','s',0};
114 static const WCHAR CopyINF
[] = {'C','o','p','y','I','N','F',0};
115 static const WCHAR UpdateIniFields
[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
116 static const WCHAR RegisterDlls
[] = {'R','e','g','i','s','t','e','r','D','l','l','s',0};
117 static const WCHAR UnregisterDlls
[] = {'U','n','r','e','g','i','s','t','e','r','D','l','l','s',0};
118 static const WCHAR ProfileItems
[] = {'P','r','o','f','i','l','e','I','t','e','m','s',0};
119 static const WCHAR Include
[] = {'I','n','c','l','u','d','e',0};
120 static const WCHAR Needs
[] = {'N','e','e','d','s',0};
121 static const WCHAR DotSecurity
[] = {'.','S','e','c','u','r','i','t','y',0};
123 static const WCHAR WineFakeDlls
[] = {'W','i','n','e','F','a','k','e','D','l','l','s',0};
127 /***********************************************************************
130 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
132 static WCHAR
*get_field_string( INFCONTEXT
*context
, DWORD index
, WCHAR
*buffer
,
133 WCHAR
*static_buffer
, DWORD
*size
)
137 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
138 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
140 /* now grow the buffer */
141 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
142 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, required
*sizeof(WCHAR
) ))) return NULL
;
144 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
146 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
151 /***********************************************************************
152 * copy_files_callback
154 * Called once for each CopyFiles entry in a given section.
156 static BOOL
copy_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
158 struct files_callback_info
*info
= arg
;
160 if (field
[0] == '@') /* special case: copy single file */
161 SetupQueueDefaultCopyW( info
->queue
, info
->layout
? info
->layout
: hinf
, info
->src_root
, NULL
, field
+1, info
->copy_flags
);
163 SetupQueueCopySectionW( info
->queue
, info
->src_root
, info
->layout
? info
->layout
: hinf
, hinf
, field
, info
->copy_flags
);
168 /***********************************************************************
169 * delete_files_callback
171 * Called once for each DelFiles entry in a given section.
173 static BOOL
delete_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
175 struct files_callback_info
*info
= arg
;
176 SetupQueueDeleteSectionW( info
->queue
, hinf
, 0, field
);
181 /***********************************************************************
182 * rename_files_callback
184 * Called once for each RenFiles entry in a given section.
186 static BOOL
rename_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
188 struct files_callback_info
*info
= arg
;
189 SetupQueueRenameSectionW( info
->queue
, hinf
, 0, field
);
194 /***********************************************************************
197 * Retrieve the registry root key from its name.
199 static HKEY
get_root_key( const WCHAR
*name
, HKEY def_root
)
201 static const WCHAR HKCR
[] = {'H','K','C','R',0};
202 static const WCHAR HKCU
[] = {'H','K','C','U',0};
203 static const WCHAR HKLM
[] = {'H','K','L','M',0};
204 static const WCHAR HKU
[] = {'H','K','U',0};
205 static const WCHAR HKR
[] = {'H','K','R',0};
207 if (!strcmpiW( name
, HKCR
)) return HKEY_CLASSES_ROOT
;
208 if (!strcmpiW( name
, HKCU
)) return HKEY_CURRENT_USER
;
209 if (!strcmpiW( name
, HKLM
)) return HKEY_LOCAL_MACHINE
;
210 if (!strcmpiW( name
, HKU
)) return HKEY_USERS
;
211 if (!strcmpiW( name
, HKR
)) return def_root
;
216 /***********************************************************************
217 * append_multi_sz_value
219 * Append a multisz string to a multisz registry value.
221 static void append_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*strings
,
224 DWORD size
, type
, total
;
227 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
228 if (type
!= REG_MULTI_SZ
) return;
230 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
231 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
233 /* compare each string against all the existing ones */
237 int len
= strlenW(strings
) + 1;
239 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
240 if (!strcmpiW( p
, strings
)) break;
242 if (!*p
) /* not found, need to append it */
244 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
252 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
253 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
+ sizeof(WCHAR
) );
256 HeapFree( GetProcessHeap(), 0, buffer
);
260 /***********************************************************************
261 * delete_multi_sz_value
263 * Remove a string from a multisz registry value.
265 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
268 WCHAR
*buffer
, *src
, *dst
;
270 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
271 if (type
!= REG_MULTI_SZ
) return;
272 /* allocate double the size, one for value before and one for after */
273 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
274 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
279 int len
= strlenW(src
) + 1;
280 if (strcmpiW( src
, string
))
282 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
288 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
290 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
291 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
292 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
295 HeapFree( GetProcessHeap(), 0, buffer
);
299 /***********************************************************************
302 * Perform an add/delete registry operation depending on the flags.
304 static BOOL
do_reg_operation( HKEY hkey
, const WCHAR
*value
, INFCONTEXT
*context
, INT flags
)
308 if (flags
& (FLG_ADDREG_DELREG_BIT
| FLG_ADDREG_DELVAL
)) /* deletion */
310 if (*value
&& !(flags
& FLG_DELREG_KEYONLY_COMMON
))
312 if ((flags
& FLG_DELREG_MULTI_SZ_DELSTRING
) == FLG_DELREG_MULTI_SZ_DELSTRING
)
316 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
) || !size
) return TRUE
;
317 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
318 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
319 delete_multi_sz_value( hkey
, value
, str
);
320 HeapFree( GetProcessHeap(), 0, str
);
322 else RegDeleteValueW( hkey
, value
);
324 else NtDeleteKey( hkey
);
328 if (flags
& (FLG_ADDREG_KEYONLY
|FLG_ADDREG_KEYONLY_COMMON
)) return TRUE
;
330 if (flags
& (FLG_ADDREG_NOCLOBBER
|FLG_ADDREG_OVERWRITEONLY
))
332 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
333 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
)) return TRUE
;
334 if (!exists
&& (flags
& FLG_ADDREG_OVERWRITEONLY
)) return TRUE
;
337 switch(flags
& FLG_ADDREG_TYPE_MASK
)
339 case FLG_ADDREG_TYPE_SZ
: type
= REG_SZ
; break;
340 case FLG_ADDREG_TYPE_MULTI_SZ
: type
= REG_MULTI_SZ
; break;
341 case FLG_ADDREG_TYPE_EXPAND_SZ
: type
= REG_EXPAND_SZ
; break;
342 case FLG_ADDREG_TYPE_BINARY
: type
= REG_BINARY
; break;
343 case FLG_ADDREG_TYPE_DWORD
: type
= REG_DWORD
; break;
344 case FLG_ADDREG_TYPE_NONE
: type
= REG_NONE
; break;
345 default: type
= flags
>> 16; break;
348 if (!(flags
& FLG_ADDREG_BINVALUETYPE
) ||
349 (type
== REG_DWORD
&& SetupGetFieldCount(context
) == 5))
351 static const WCHAR empty
;
354 if (type
== REG_MULTI_SZ
)
356 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
359 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
360 SetupGetMultiSzFieldW( context
, 5, str
, size
, NULL
);
362 if (flags
& FLG_ADDREG_APPEND
)
364 if (!str
) return TRUE
;
365 append_multi_sz_value( hkey
, value
, str
, size
);
366 HeapFree( GetProcessHeap(), 0, str
);
369 /* else fall through to normal string handling */
373 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
376 if (!(str
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
377 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
381 if (type
== REG_DWORD
)
383 DWORD dw
= str
? strtoulW( str
, NULL
, 0 ) : 0;
384 TRACE( "setting dword %s to %x\n", debugstr_w(value
), dw
);
385 RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&dw
, sizeof(dw
) );
389 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(str
) );
390 if (str
) RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)str
, size
* sizeof(WCHAR
) );
391 else RegSetValueExW( hkey
, value
, 0, type
, (const BYTE
*)&empty
, sizeof(WCHAR
) );
393 HeapFree( GetProcessHeap(), 0, str
);
396 else /* get the binary data */
400 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
403 if (!(data
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
404 TRACE( "setting binary data %s len %d\n", debugstr_w(value
), size
);
405 SetupGetBinaryField( context
, 5, data
, size
, NULL
);
407 RegSetValueExW( hkey
, value
, 0, type
, data
, size
);
408 HeapFree( GetProcessHeap(), 0, data
);
414 /***********************************************************************
417 * Called once for each AddReg and DelReg entry in a given section.
419 static BOOL
registry_callback( HINF hinf
, PCWSTR field
, void *arg
)
421 struct registry_callback_info
*info
= arg
;
422 LPWSTR security_key
, security_descriptor
;
423 INFCONTEXT context
, security_context
;
424 PSECURITY_DESCRIPTOR sd
= NULL
;
425 SECURITY_ATTRIBUTES security_attributes
= { 0, };
429 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
433 /* Check for .Security section */
434 security_key
= MyMalloc( (strlenW( field
) + strlenW( DotSecurity
)) * sizeof(WCHAR
) + sizeof(UNICODE_NULL
) );
437 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
440 strcpyW( security_key
, field
);
441 strcatW( security_key
, DotSecurity
);
442 ok
= SetupFindFirstLineW( hinf
, security_key
, NULL
, &security_context
);
443 MyFree(security_key
);
446 if (!SetupGetLineTextW( &security_context
, NULL
, NULL
, NULL
, NULL
, 0, &required
))
448 security_descriptor
= MyMalloc( required
* sizeof(WCHAR
) );
449 if (!security_descriptor
)
451 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
454 if (!SetupGetLineTextW( &security_context
, NULL
, NULL
, NULL
, security_descriptor
, required
, NULL
))
456 ok
= ConvertStringSecurityDescriptorToSecurityDescriptorW( security_descriptor
, SDDL_REVISION_1
, &sd
, NULL
);
457 MyFree( security_descriptor
);
460 security_attributes
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
461 security_attributes
.lpSecurityDescriptor
= sd
;
464 for (ok
= TRUE
; ok
; ok
= SetupFindNextLine( &context
, &context
))
466 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
470 if (!SetupGetStringFieldW( &context
, 1, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
472 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
476 if (!SetupGetStringFieldW( &context
, 2, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
480 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
484 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
488 if (!flags
) flags
= FLG_ADDREG_DELREG_BIT
;
489 else if (!(flags
& FLG_ADDREG_DELREG_BIT
)) continue; /* ignore this entry */
492 if (info
->delete || (flags
& FLG_ADDREG_OVERWRITEONLY
))
494 if (RegOpenKeyW( root_key
, buffer
, &hkey
)) continue; /* ignore if it doesn't exist */
496 else if (RegCreateKeyExW( root_key
, buffer
, 0, NULL
, 0, MAXIMUM_ALLOWED
,
497 sd
? &security_attributes
: NULL
, &hkey
, NULL
))
499 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
502 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
505 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
509 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
511 if (hkey
!= root_key
) RegCloseKey( hkey
);
512 if (sd
) LocalFree( sd
);
515 if (hkey
!= root_key
) RegCloseKey( hkey
);
517 if (sd
) LocalFree( sd
);
522 /***********************************************************************
525 * Register or unregister a dll.
527 static BOOL
do_register_dll( const struct register_dll_info
*info
, const WCHAR
*path
,
528 INT flags
, INT timeout
, const WCHAR
*args
)
532 SP_REGISTER_CONTROL_STATUSW status
;
534 IMAGE_NT_HEADERS
*nt
;
537 status
.cbSize
= sizeof(status
);
538 status
.FileName
= path
;
539 status
.FailureCode
= SPREG_SUCCESS
;
540 status
.Win32Error
= ERROR_SUCCESS
;
544 switch(info
->callback( info
->callback_context
, SPFILENOTIFY_STARTREGISTRATION
,
545 (UINT_PTR
)&status
, !info
->unregister
))
548 SetLastError( ERROR_OPERATION_ABORTED
);
557 if (!(module
= LoadLibraryExW( path
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)))
559 WARN( "could not load %s\n", debugstr_w(path
) );
560 status
.FailureCode
= SPREG_LOADLIBRARY
;
561 status
.Win32Error
= GetLastError();
566 if ((nt
= RtlImageNtHeader( module
)) && !(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
568 /* file is an executable, not a dll */
569 STARTUPINFOW startup
;
570 PROCESS_INFORMATION info
;
573 static const WCHAR format
[] = {'"','%','s','"',' ','%','s',0};
574 static const WCHAR default_args
[] = {'/','R','e','g','S','e','r','v','e','r',0};
576 FreeLibrary( module
);
578 if (!args
) args
= default_args
;
579 cmd_line
= HeapAlloc( GetProcessHeap(), 0, (strlenW(path
) + strlenW(args
) + 4) * sizeof(WCHAR
) );
580 sprintfW( cmd_line
, format
, path
, args
);
581 memset( &startup
, 0, sizeof(startup
) );
582 startup
.cb
= sizeof(startup
);
583 TRACE( "executing %s\n", debugstr_w(cmd_line
) );
584 res
= CreateProcessW( NULL
, cmd_line
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
585 HeapFree( GetProcessHeap(), 0, cmd_line
);
588 status
.FailureCode
= SPREG_LOADLIBRARY
;
589 status
.Win32Error
= GetLastError();
592 CloseHandle( info
.hThread
);
594 if (WaitForSingleObject( info
.hProcess
, timeout
*1000 ) == WAIT_TIMEOUT
)
596 /* timed out, kill the process */
597 TerminateProcess( info
.hProcess
, 1 );
598 status
.FailureCode
= SPREG_TIMEOUT
;
599 status
.Win32Error
= ERROR_TIMEOUT
;
601 CloseHandle( info
.hProcess
);
604 #endif // __WINESRC__
606 if (flags
& FLG_REGSVR_DLLREGISTER
)
608 const char *entry_point
= info
->unregister
? "DllUnregisterServer" : "DllRegisterServer";
609 HRESULT (WINAPI
*func
)(void) = (void *)GetProcAddress( module
, entry_point
);
613 status
.FailureCode
= SPREG_GETPROCADDR
;
614 status
.Win32Error
= GetLastError();
618 TRACE( "calling %s in %s\n", entry_point
, debugstr_w(path
) );
623 WARN( "calling %s in %s returned error %x\n", entry_point
, debugstr_w(path
), res
);
624 status
.FailureCode
= SPREG_REGSVR
;
625 status
.Win32Error
= res
;
630 if (flags
& FLG_REGSVR_DLLINSTALL
)
632 HRESULT (WINAPI
*func
)(BOOL
,LPCWSTR
) = (void *)GetProcAddress( module
, "DllInstall" );
636 status
.FailureCode
= SPREG_GETPROCADDR
;
637 status
.Win32Error
= GetLastError();
641 TRACE( "calling DllInstall(%d,%s) in %s\n",
642 !info
->unregister
, debugstr_w(args
), debugstr_w(path
) );
643 res
= func( !info
->unregister
, args
);
647 WARN( "calling DllInstall in %s returned error %x\n", debugstr_w(path
), res
);
648 status
.FailureCode
= SPREG_REGSVR
;
649 status
.Win32Error
= res
;
655 if (module
) FreeLibrary( module
);
656 if (info
->callback
) info
->callback( info
->callback_context
, SPFILENOTIFY_ENDREGISTRATION
,
657 (UINT_PTR
)&status
, !info
->unregister
);
662 /***********************************************************************
663 * register_dlls_callback
665 * Called once for each RegisterDlls entry in a given section.
667 static BOOL
register_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
669 struct register_dll_info
*info
= arg
;
672 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
674 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
676 WCHAR
*path
, *args
, *p
;
677 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
681 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
684 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
686 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
687 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
690 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
691 strcpyW( p
, buffer
);
694 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
697 if (!SetupGetIntField( &context
, 5, &timeout
)) timeout
= 60;
699 /* get command line */
701 if (SetupGetStringFieldW( &context
, 6, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
704 ret
= do_register_dll( info
, path
, flags
, timeout
, args
);
707 HeapFree( GetProcessHeap(), 0, path
);
714 /***********************************************************************
717 * Called once for each WineFakeDlls entry in a given section.
719 static BOOL
fake_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
723 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
725 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
728 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
731 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
734 if (!SetupGetStringFieldW( &context
, 3, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
736 if (!(p
= HeapReAlloc( GetProcessHeap(), 0, path
,
737 (strlenW(path
) + strlenW(buffer
) + 2) * sizeof(WCHAR
) ))) goto done
;
740 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
741 strcpyW( p
, buffer
);
744 if (SetupGetStringFieldW( &context
, 4, buffer
, sizeof(buffer
)/sizeof(WCHAR
), NULL
))
745 p
= buffer
; /* otherwise use target base name as default source */
747 create_fake_dll( path
, p
); /* ignore errors */
750 HeapFree( GetProcessHeap(), 0, path
);
755 #endif // __WINESRC__
757 /***********************************************************************
758 * update_ini_callback
760 * Called once for each UpdateInis entry in a given section.
762 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
766 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
768 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
770 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
771 WCHAR filename
[MAX_INF_STRING_LENGTH
];
772 WCHAR section
[MAX_INF_STRING_LENGTH
];
773 WCHAR entry
[MAX_INF_STRING_LENGTH
];
774 WCHAR string
[MAX_INF_STRING_LENGTH
];
777 if (!SetupGetStringFieldW( &context
, 1, filename
,
778 sizeof(filename
)/sizeof(WCHAR
), NULL
))
781 if (!SetupGetStringFieldW( &context
, 2, section
,
782 sizeof(section
)/sizeof(WCHAR
), NULL
))
785 if (!SetupGetStringFieldW( &context
, 4, buffer
,
786 sizeof(buffer
)/sizeof(WCHAR
), NULL
))
789 divider
= strchrW(buffer
,'=');
793 strcpyW(entry
,buffer
);
795 strcpyW(string
,divider
);
799 strcpyW(entry
,buffer
);
803 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
804 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
805 WritePrivateProfileStringW(section
,entry
,string
,filename
);
811 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
813 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
817 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
819 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
823 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
825 FIXME( "should do logconf %s\n", debugstr_w(field
) );
829 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
831 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
835 static BOOL
Concatenate(int DirId
, LPCWSTR SubDirPart
, LPCWSTR NamePart
, LPWSTR
*pFullName
)
837 DWORD dwRequired
= 0;
843 Dir
= DIRID_get_string(DirId
);
845 dwRequired
+= wcslen(Dir
) + 1;
847 dwRequired
+= wcslen(SubDirPart
) + 1;
849 dwRequired
+= wcslen(NamePart
);
850 dwRequired
= dwRequired
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
852 FullName
= MyMalloc(dwRequired
);
855 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
858 FullName
[0] = UNICODE_NULL
;
862 wcscat(FullName
, Dir
);
863 if (FullName
[wcslen(FullName
) - 1] != '\\')
864 wcscat(FullName
, BackSlash
);
868 wcscat(FullName
, SubDirPart
);
869 if (FullName
[wcslen(FullName
) - 1] != '\\')
870 wcscat(FullName
, BackSlash
);
873 wcscat(FullName
, NamePart
);
875 *pFullName
= FullName
;
879 /***********************************************************************
880 * profile_items_callback
882 * Called once for each ProfileItems entry in a given section.
885 profile_items_callback(
887 IN PCWSTR SectionName
,
891 LPWSTR LinkSubDir
= NULL
, LinkName
= NULL
;
892 INT LinkAttributes
= 0;
895 INT CSIDL
= CSIDL_COMMON_PROGRAMS
;
896 LPWSTR FileSubDir
= NULL
;
898 LPWSTR SubDirPart
= NULL
, NamePart
= NULL
;
899 LPWSTR FullLinkName
= NULL
, FullFileName
= NULL
, FullWorkingDir
= NULL
, FullIconName
= NULL
;
901 LPWSTR lpHotKey
= NULL
, lpInfoTip
= NULL
;
902 LPWSTR DisplayName
= NULL
;
903 INT DisplayResId
= 0;
905 DWORD Index
, Required
;
909 HMODULE hOle32
= NULL
;
910 COINITIALIZE pCoInitialize
;
911 COCREATEINSTANCE pCoCreateInstance
;
912 COUNINITIALIZE pCoUninitialize
;
915 TRACE("hInf %p, SectionName %s, Arg %p\n",
916 hInf
, debugstr_w(SectionName
), Arg
);
918 /* Read 'Name' entry */
919 if (!SetupFindFirstLineW(hInf
, SectionName
, Name
, &Context
))
921 if (!GetStringField(&Context
, 1, &LinkName
))
923 if (SetupGetFieldCount(&Context
) >= 2)
925 if (!SetupGetIntField(&Context
, 2, &LinkAttributes
))
928 if (SetupGetFieldCount(&Context
) >= 3)
930 if (!SetupGetIntField(&Context
, 3, &LinkFolder
))
934 /* Read 'CmdLine' entry */
935 if (!SetupFindFirstLineW(hInf
, SectionName
, CmdLine
, &Context
))
938 if (!SetupGetIntField(&Context
, Index
++, &FileDirId
))
940 if (SetupGetFieldCount(&Context
) >= 3)
942 if (!GetStringField(&Context
, Index
++, &FileSubDir
))
945 if (!GetStringField(&Context
, Index
++, &NamePart
))
947 if (!Concatenate(FileDirId
, FileSubDir
, NamePart
, &FullFileName
))
952 /* Read 'SubDir' entry */
953 if ((LinkAttributes
& FLG_PROFITEM_GROUP
) == 0 && SetupFindFirstLineW(hInf
, SectionName
, SubDir
, &Context
))
955 if (!GetStringField(&Context
, 1, &LinkSubDir
))
959 /* Read 'WorkingDir' entry */
960 if (SetupFindFirstLineW(hInf
, SectionName
, WorkingDir
, &Context
))
962 if (!SetupGetIntField(&Context
, 1, &DirId
))
964 if (SetupGetFieldCount(&Context
) >= 2)
966 if (!GetStringField(&Context
, 2, &SubDirPart
))
969 if (!Concatenate(DirId
, SubDirPart
, NULL
, &FullWorkingDir
))
976 if (!Concatenate(FileDirId
, FileSubDir
, NULL
, &FullWorkingDir
))
980 /* Read 'IconPath' entry */
981 if (SetupFindFirstLineW(hInf
, SectionName
, IconPath
, &Context
))
984 if (!SetupGetIntField(&Context
, Index
++, &DirId
))
986 if (SetupGetFieldCount(&Context
) >= 3)
988 if (!GetStringField(&Context
, Index
++, &SubDirPart
))
991 if (!GetStringField(&Context
, Index
, &NamePart
))
993 if (!Concatenate(DirId
, SubDirPart
, NamePart
, &FullIconName
))
997 SubDirPart
= NamePart
= NULL
;
1001 FullIconName
= pSetupDuplicateString(FullFileName
);
1006 /* Read 'IconIndex' entry */
1007 if (SetupFindFirstLineW(hInf
, SectionName
, IconIndex
, &Context
))
1009 if (!SetupGetIntField(&Context
, 1, &IconIdx
))
1013 /* Read 'HotKey' and 'InfoTip' entries */
1014 GetLineText(hInf
, SectionName
, HotKey
, &lpHotKey
);
1015 GetLineText(hInf
, SectionName
, InfoTip
, &lpInfoTip
);
1017 /* Read 'DisplayResource' entry */
1018 if (SetupFindFirstLineW(hInf
, SectionName
, DisplayResource
, &Context
))
1020 if (!GetStringField(&Context
, 1, &DisplayName
))
1022 if (!SetupGetIntField(&Context
, 2, &DisplayResId
))
1027 TRACE("Link is %s\\%s, attributes 0x%x\n", debugstr_w(LinkSubDir
), debugstr_w(LinkName
), LinkAttributes
);
1028 TRACE("File is %s\n", debugstr_w(FullFileName
));
1029 TRACE("Working dir %s\n", debugstr_w(FullWorkingDir
));
1030 TRACE("Icon is %s, %d\n", debugstr_w(FullIconName
), IconIdx
);
1031 TRACE("Hotkey %s\n", debugstr_w(lpHotKey
));
1032 TRACE("InfoTip %s\n", debugstr_w(lpInfoTip
));
1033 TRACE("Display %s, %d\n", DisplayName
, DisplayResId
);
1035 /* Load ole32.dll */
1036 hOle32
= LoadLibraryA("ole32.dll");
1039 pCoInitialize
= (COINITIALIZE
)GetProcAddress(hOle32
, "CoInitialize");
1042 pCoCreateInstance
= (COCREATEINSTANCE
)GetProcAddress(hOle32
, "CoCreateInstance");
1043 if (!pCoCreateInstance
)
1045 pCoUninitialize
= (COUNINITIALIZE
)GetProcAddress(hOle32
, "CoUninitialize");
1046 if (!pCoUninitialize
)
1049 /* Create shortcut */
1050 hr
= pCoInitialize(NULL
);
1053 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
1054 SetLastError(HRESULT_CODE(hr
));
1056 SetLastError(E_FAIL
);
1059 hr
= pCoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLinkW
, (LPVOID
*)&psl
);
1062 /* Fill link properties */
1063 hr
= IShellLinkW_SetPath(psl
, FullFileName
);
1065 hr
= IShellLinkW_SetArguments(psl
, L
"");
1067 hr
= IShellLinkW_SetWorkingDirectory(psl
, FullWorkingDir
);
1069 hr
= IShellLinkW_SetIconLocation(psl
, FullIconName
, IconIdx
);
1070 if (SUCCEEDED(hr
) && lpHotKey
)
1071 FIXME("Need to store hotkey %s in shell link\n", debugstr_w(lpHotKey
));
1072 if (SUCCEEDED(hr
) && lpInfoTip
)
1073 hr
= IShellLinkW_SetDescription(psl
, lpInfoTip
);
1074 if (SUCCEEDED(hr
) && DisplayName
)
1075 FIXME("Need to store display name %s, %d in shell link\n", debugstr_w(DisplayName
), DisplayResId
);
1078 hr
= IShellLinkW_QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
1081 Required
= (MAX_PATH
+ wcslen(LinkSubDir
) + 1 + wcslen(LinkName
)) * sizeof(WCHAR
);
1082 FullLinkName
= MyMalloc(Required
);
1087 if (LinkAttributes
& (FLG_PROFITEM_DELETE
| FLG_PROFITEM_GROUP
))
1088 FIXME("Need to handle FLG_PROFITEM_DELETE and FLG_PROFITEM_GROUP\n");
1089 if (LinkAttributes
& FLG_PROFITEM_CSIDL
)
1091 else if (LinkAttributes
& FLG_PROFITEM_CURRENTUSER
)
1092 CSIDL
= CSIDL_PROGRAMS
;
1094 if (SHGetSpecialFolderPathW(
1100 if (FullLinkName
[wcslen(FullLinkName
) - 1] != '\\')
1101 wcscat(FullLinkName
, BackSlash
);
1104 wcscat(FullLinkName
, LinkSubDir
);
1105 if (FullLinkName
[wcslen(FullLinkName
) - 1] != '\\')
1106 wcscat(FullLinkName
, BackSlash
);
1108 wcscat(FullLinkName
, LinkName
);
1109 wcscat(FullLinkName
, DotLnk
);
1110 hr
= IPersistFile_Save(ppf
, FullLinkName
, TRUE
);
1113 hr
= HRESULT_FROM_WIN32(GetLastError());
1115 IPersistFile_Release(ppf
);
1118 IShellLinkW_Release(psl
);
1125 if (HRESULT_FACILITY(hr
) == FACILITY_WIN32
)
1126 SetLastError(HRESULT_CODE(hr
));
1128 SetLastError(E_FAIL
);
1137 MyFree(FullFileName
);
1138 MyFree(FullWorkingDir
);
1139 MyFree(FullIconName
);
1140 MyFree(FullLinkName
);
1143 MyFree(DisplayName
);
1145 FreeLibrary(hOle32
);
1147 TRACE("Returning %d\n", ret
);
1151 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
1153 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
1158 /***********************************************************************
1159 * iterate_section_fields
1161 * Iterate over all fields of a certain key of a certain section
1163 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
1164 iterate_fields_func callback
, void *arg
)
1166 WCHAR static_buffer
[200];
1167 WCHAR
*buffer
= static_buffer
;
1168 DWORD size
= sizeof(static_buffer
)/sizeof(WCHAR
);
1172 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
1175 UINT i
, count
= SetupGetFieldCount( &context
);
1176 for (i
= 1; i
<= count
; i
++)
1178 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
1180 if (!callback( hinf
, buffer
, arg
))
1182 WARN("callback failed for %s %s err %d\n",
1183 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
1187 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
1191 if (buffer
!= static_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
1196 /***********************************************************************
1197 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
1199 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1200 PCSTR section
, PCSTR src_root
, UINT flags
)
1202 UNICODE_STRING sectionW
;
1205 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1207 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1211 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1215 UNICODE_STRING srcW
;
1216 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
1218 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1219 srcW
.Buffer
, flags
);
1220 RtlFreeUnicodeString( &srcW
);
1222 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1224 RtlFreeUnicodeString( §ionW
);
1229 /***********************************************************************
1230 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1232 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1233 PCWSTR section
, PCWSTR src_root
, UINT flags
)
1235 struct files_callback_info info
;
1238 info
.src_root
= src_root
;
1239 info
.copy_flags
= flags
;
1240 info
.layout
= hlayout
;
1241 return iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
);
1245 /***********************************************************************
1246 * SetupInstallFromInfSectionA (SETUPAPI.@)
1248 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
1249 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
1250 PSP_FILE_CALLBACK_A callback
, PVOID context
,
1251 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1253 UNICODE_STRING sectionW
, src_rootW
;
1254 struct callback_WtoA_context ctx
;
1257 src_rootW
.Buffer
= NULL
;
1258 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
1260 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1264 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1266 ctx
.orig_context
= context
;
1267 ctx
.orig_handler
= callback
;
1268 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
1269 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
1270 &ctx
, devinfo
, devinfo_data
);
1271 RtlFreeUnicodeString( §ionW
);
1273 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1275 RtlFreeUnicodeString( &src_rootW
);
1280 /***********************************************************************
1283 * Called once for each Include entry in a given section.
1285 static BOOL
include_callback( HINF hinf
, PCWSTR field
, void *arg
)
1287 return SetupOpenAppendInfFileW( field
, hinf
, NULL
);
1291 /***********************************************************************
1294 * Called once for each Needs entry in a given section.
1296 static BOOL
needs_callback( HINF hinf
, PCWSTR field
, void *arg
)
1298 struct needs_callback_info
*info
= arg
;
1303 return SetupInstallFromInfSectionW(info
->owner
, *(HINF
*)hinf
, field
, info
->flags
,
1304 info
->key_root
, info
->src_root
, info
->copy_flags
, info
->callback
,
1305 info
->context
, info
->devinfo
, info
->devinfo_data
);
1307 return SetupInstallServicesFromInfSectionExW(*(HINF
*)hinf
, field
, info
->flags
,
1308 info
->devinfo
, info
->devinfo_data
, info
->reserved1
, info
->reserved2
);
1310 ERR("Unknown info type %u\n", info
->type
);
1316 /***********************************************************************
1317 * SetupInstallFromInfSectionW (SETUPAPI.@)
1319 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
1320 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
1321 PSP_FILE_CALLBACK_W callback
, PVOID context
,
1322 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1324 struct needs_callback_info needs_info
;
1326 /* Parse 'Include' and 'Needs' directives */
1327 iterate_section_fields( hinf
, section
, Include
, include_callback
, NULL
);
1328 needs_info
.type
= 0;
1329 needs_info
.owner
= owner
;
1330 needs_info
.flags
= flags
;
1331 needs_info
.key_root
= key_root
;
1332 needs_info
.src_root
= src_root
;
1333 needs_info
.copy_flags
= copy_flags
;
1334 needs_info
.callback
= callback
;
1335 needs_info
.context
= context
;
1336 needs_info
.devinfo
= devinfo
;
1337 needs_info
.devinfo_data
= devinfo_data
;
1338 iterate_section_fields( hinf
, section
, Needs
, needs_callback
, &needs_info
);
1340 if (flags
& SPINST_FILES
)
1342 SP_DEVINSTALL_PARAMS_W install_params
;
1343 struct files_callback_info info
;
1344 HSPFILEQ queue
= NULL
;
1345 BOOL use_custom_queue
;
1348 install_params
.cbSize
= sizeof(SP_DEVINSTALL_PARAMS
);
1349 use_custom_queue
= SetupDiGetDeviceInstallParamsW(devinfo
, devinfo_data
, &install_params
) && (install_params
.Flags
& DI_NOVCP
);
1350 if (!use_custom_queue
&& ((queue
= SetupOpenFileQueue()) == (HSPFILEQ
)INVALID_HANDLE_VALUE
))
1352 info
.queue
= use_custom_queue
? install_params
.FileQueue
: queue
;
1353 info
.src_root
= src_root
;
1354 info
.copy_flags
= copy_flags
;
1356 ret
= (iterate_section_fields( hinf
, section
, CopyFiles
, copy_files_callback
, &info
) &&
1357 iterate_section_fields( hinf
, section
, DelFiles
, delete_files_callback
, &info
) &&
1358 iterate_section_fields( hinf
, section
, RenFiles
, rename_files_callback
, &info
));
1359 if (!use_custom_queue
)
1362 ret
= SetupCommitFileQueueW( owner
, queue
, callback
, context
);
1363 SetupCloseFileQueue( queue
);
1365 if (!ret
) return FALSE
;
1367 if (flags
& SPINST_INIFILES
)
1369 if (!iterate_section_fields( hinf
, section
, UpdateInis
, update_ini_callback
, NULL
) ||
1370 !iterate_section_fields( hinf
, section
, UpdateIniFields
,
1371 update_ini_fields_callback
, NULL
))
1374 if (flags
& SPINST_INI2REG
)
1376 if (!iterate_section_fields( hinf
, section
, Ini2Reg
, ini2reg_callback
, NULL
))
1379 if (flags
& SPINST_LOGCONFIG
)
1381 if (!iterate_section_fields( hinf
, section
, LogConf
, logconf_callback
, NULL
))
1384 if (flags
& SPINST_REGSVR
)
1386 struct register_dll_info info
;
1388 info
.unregister
= FALSE
;
1389 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1391 info
.callback
= callback
;
1392 info
.callback_context
= context
;
1394 else info
.callback
= NULL
;
1396 if (!iterate_section_fields( hinf
, section
, RegisterDlls
, register_dlls_callback
, &info
))
1400 if (!iterate_section_fields( hinf
, section
, WineFakeDlls
, fake_dlls_callback
, NULL
))
1402 #endif // __WINESRC__
1404 if (flags
& SPINST_UNREGSVR
)
1406 struct register_dll_info info
;
1408 info
.unregister
= TRUE
;
1409 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1411 info
.callback
= callback
;
1412 info
.callback_context
= context
;
1414 else info
.callback
= NULL
;
1416 if (!iterate_section_fields( hinf
, section
, UnregisterDlls
, register_dlls_callback
, &info
))
1419 if (flags
& SPINST_REGISTRY
)
1421 struct registry_callback_info info
;
1423 info
.default_root
= key_root
;
1425 if (!iterate_section_fields( hinf
, section
, DelReg
, registry_callback
, &info
))
1427 info
.delete = FALSE
;
1428 if (!iterate_section_fields( hinf
, section
, AddReg
, registry_callback
, &info
))
1431 if (flags
& SPINST_BITREG
)
1433 if (!iterate_section_fields( hinf
, section
, BitReg
, bitreg_callback
, NULL
))
1436 if (flags
& SPINST_PROFILEITEMS
)
1438 if (!iterate_section_fields( hinf
, section
, ProfileItems
, profile_items_callback
, NULL
))
1441 if (flags
& SPINST_COPYINF
)
1443 if (!iterate_section_fields( hinf
, section
, CopyINF
, copy_inf_callback
, NULL
))
1451 /***********************************************************************
1452 * InstallHinfSectionW (SETUPAPI.@)
1454 * NOTE: 'cmdline' is <section> <mode> <path> from
1455 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1457 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
1459 WCHAR
*s
, *path
, section
[MAX_PATH
];
1460 void *callback_context
= NULL
;
1461 DWORD SectionNameLength
;
1463 HINF hinf
= INVALID_HANDLE_VALUE
;
1464 BOOL bRebootRequired
= FALSE
;
1467 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
1469 lstrcpynW( section
, cmdline
, MAX_PATH
);
1471 if (!(s
= strchrW( section
, ' ' ))) return;
1473 while (*s
== ' ') s
++;
1476 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1477 if (!(s
= strchrW( s
, ' ' ))) return;
1478 while (*s
== ' ') s
++;
1483 FIXME("default path of the installation not changed\n");
1487 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
1488 if (hinf
== INVALID_HANDLE_VALUE
)
1490 WARN("SetupOpenInfFileW(%s) failed (Error %u)\n", path
, GetLastError());
1494 ret
= SetupDiGetActualSectionToInstallW(
1495 hinf
, section
, section
, sizeof(section
)/sizeof(section
[0]), &SectionNameLength
, NULL
);
1498 WARN("SetupDiGetActualSectionToInstallW() failed (Error %u)\n", GetLastError());
1501 if (SectionNameLength
> MAX_PATH
- strlenW(DotServices
))
1503 WARN("Section name '%s' too long\n", section
);
1507 /* Copy files and add registry entries */
1508 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
1509 ret
= SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
,
1510 SP_COPY_NEWER
| SP_COPY_IN_USE_NEEDS_REBOOT
,
1511 SetupDefaultQueueCallbackW
, callback_context
,
1515 WARN("SetupInstallFromInfSectionW() failed (Error %u)\n", GetLastError());
1518 /* FIXME: need to check if some files were in use and need reboot
1522 /* Install services */
1523 wcscat(section
, DotServices
);
1524 ret
= SetupInstallServicesFromInfSectionW( hinf
, section
, 0 );
1525 if (!ret
&& GetLastError() == ERROR_SECTION_NOT_FOUND
)
1529 WARN("SetupInstallServicesFromInfSectionW() failed (Error %u)\n", GetLastError());
1532 else if (GetLastError() == ERROR_SUCCESS_REBOOT_REQUIRED
)
1534 bRebootRequired
= TRUE
;
1537 /* Check if we need to reboot */
1545 ExitWindowsEx(EWX_REBOOT
, SHTDN_REASON_MAJOR_APPLICATION
|
1546 SHTDN_REASON_MINOR_INSTALLATION
| SHTDN_REASON_FLAG_PLANNED
);
1549 /* Query user before rebooting */
1550 SetupPromptReboot(NULL
, hwnd
, FALSE
);
1553 /* Reboot if necessary */
1554 if (bRebootRequired
)
1556 ExitWindowsEx(EWX_REBOOT
, SHTDN_REASON_MAJOR_APPLICATION
|
1557 SHTDN_REASON_MINOR_INSTALLATION
| SHTDN_REASON_FLAG_PLANNED
);
1561 /* If necessary, query user before rebooting */
1562 if (bRebootRequired
)
1564 SetupPromptReboot(NULL
, hwnd
, FALSE
);
1572 if ( callback_context
)
1573 SetupTermDefaultQueueCallback( callback_context
);
1574 if ( hinf
!= INVALID_HANDLE_VALUE
)
1575 SetupCloseInfFile( hinf
);
1579 /***********************************************************************
1580 * InstallHinfSectionA (SETUPAPI.@)
1582 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
1584 UNICODE_STRING cmdlineW
;
1586 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
1588 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
1589 RtlFreeUnicodeString( &cmdlineW
);
1593 /***********************************************************************
1594 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1596 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF Inf
, PCWSTR SectionName
, DWORD Flags
)
1598 return SetupInstallServicesFromInfSectionExW( Inf
, SectionName
, Flags
,
1599 NULL
, NULL
, NULL
, NULL
);
1602 /***********************************************************************
1603 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1605 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF Inf
, PCSTR SectionName
, DWORD Flags
)
1607 return SetupInstallServicesFromInfSectionExA( Inf
, SectionName
, Flags
,
1608 NULL
, NULL
, NULL
, NULL
);
1611 /***********************************************************************
1612 * SetupInstallServicesFromInfSectionExA (SETUPAPI.@)
1614 BOOL WINAPI
SetupInstallServicesFromInfSectionExA( HINF hinf
, PCSTR sectionname
, DWORD flags
, HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
, PVOID reserved1
, PVOID reserved2
)
1616 UNICODE_STRING sectionnameW
;
1619 if (RtlCreateUnicodeStringFromAsciiz( §ionnameW
, sectionname
))
1621 ret
= SetupInstallServicesFromInfSectionExW( hinf
, sectionnameW
.Buffer
, flags
, devinfo
, devinfo_data
, reserved1
, reserved2
);
1622 RtlFreeUnicodeString( §ionnameW
);
1625 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1631 static BOOL
GetLineText( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, PWSTR
*value
)
1638 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, NULL
, 0, &required
)
1639 && GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1642 buf
= HeapAlloc( GetProcessHeap(), 0, required
* sizeof(WCHAR
) );
1645 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1649 if (! SetupGetLineTextW( NULL
, hinf
, section_name
, key_name
, buf
, required
, &required
) )
1651 HeapFree( GetProcessHeap(), 0, buf
);
1660 static BOOL
GetIntField( HINF hinf
, PCWSTR section_name
, PCWSTR key_name
, INT
*value
)
1665 if (! GetLineText( hinf
, section_name
, key_name
, &buffer
) )
1668 res
= wcstol( buffer
, &end
, 0 );
1669 if (end
!= buffer
&& !*end
)
1671 HeapFree(GetProcessHeap(), 0, buffer
);
1677 HeapFree(GetProcessHeap(), 0, buffer
);
1678 SetLastError( ERROR_INVALID_DATA
);
1684 BOOL
GetStringField( PINFCONTEXT context
, DWORD index
, PWSTR
*value
)
1689 ret
= SetupGetStringFieldW(
1696 else if (RequiredSize
== 0)
1702 /* We got the needed size for the buffer */
1703 *value
= MyMalloc(RequiredSize
* sizeof(WCHAR
));
1706 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1709 ret
= SetupGetStringFieldW(
1712 *value
, RequiredSize
, NULL
);
1719 static VOID
FixupServiceBinaryPath(
1720 IN DWORD ServiceType
,
1721 IN OUT LPWSTR
*ServiceBinary
)
1724 WCHAR ReactosDir
[MAX_PATH
];
1725 DWORD RosDirLength
, ServiceLength
, Win32Length
;
1727 GetWindowsDirectoryW(ReactosDir
, MAX_PATH
);
1728 RosDirLength
= strlenW(ReactosDir
);
1729 ServiceLength
= strlenW(*ServiceBinary
);
1731 /* Check and fix two things:
1732 1. Get rid of C:\ReactOS and use relative
1734 2. Add %SystemRoot% for Win32 services */
1736 if (ServiceLength
< RosDirLength
)
1739 if (!wcsnicmp(*ServiceBinary
, ReactosDir
, RosDirLength
))
1741 /* Yes, the first part is the C:\ReactOS\, just skip it */
1742 MoveMemory(*ServiceBinary
, *ServiceBinary
+ RosDirLength
+ 1,
1743 (ServiceLength
- RosDirLength
) * sizeof(WCHAR
));
1745 /* Handle Win32-services differently */
1746 if (ServiceType
& SERVICE_WIN32
)
1748 Win32Length
= (ServiceLength
-
1749 RosDirLength
- 1 + 13) * sizeof(WCHAR
);
1750 /* -1 to not count the separator after C:\ReactOS
1751 wcslen(L"%SystemRoot%\\") = 13*sizeof(wchar_t) */
1752 Buffer
= MyMalloc(Win32Length
);
1754 wcscpy(Buffer
, L
"%SystemRoot%\\");
1755 wcscat(Buffer
, *ServiceBinary
);
1756 MyFree(*ServiceBinary
);
1758 *ServiceBinary
= Buffer
;
1763 static BOOL
InstallOneService(
1764 struct DeviceInfoSet
*list
,
1766 IN LPCWSTR ServiceSection
,
1767 IN LPCWSTR ServiceName
,
1768 IN UINT ServiceFlags
)
1770 SC_HANDLE hSCManager
= NULL
;
1771 SC_HANDLE hService
= NULL
;
1772 LPDWORD GroupOrder
= NULL
;
1773 LPQUERY_SERVICE_CONFIGW ServiceConfig
= NULL
;
1774 HKEY hServicesKey
, hServiceKey
;
1778 HKEY hGroupOrderListKey
= NULL
;
1779 LPWSTR ServiceBinary
= NULL
;
1780 LPWSTR LoadOrderGroup
= NULL
;
1781 LPWSTR DisplayName
= NULL
;
1782 LPWSTR Description
= NULL
;
1783 LPWSTR Dependencies
= NULL
;
1784 LPWSTR SecurityDescriptor
= NULL
;
1785 PSECURITY_DESCRIPTOR sd
= NULL
;
1786 INT ServiceType
, StartType
, ErrorControl
;
1788 DWORD tagId
= (DWORD
)-1;
1791 if (!GetIntField(hInf
, ServiceSection
, ServiceTypeKey
, &ServiceType
))
1793 if (!GetIntField(hInf
, ServiceSection
, StartTypeKey
, &StartType
))
1795 if (!GetIntField(hInf
, ServiceSection
, ErrorControlKey
, &ErrorControl
))
1797 useTag
= (ServiceType
== SERVICE_BOOT_START
|| ServiceType
== SERVICE_SYSTEM_START
);
1799 hSCManager
= OpenSCManagerW(NULL
, SERVICES_ACTIVE_DATABASE
, SC_MANAGER_CREATE_SERVICE
);
1800 if (hSCManager
== NULL
)
1803 if (!GetLineText(hInf
, ServiceSection
, ServiceBinaryKey
, &ServiceBinary
))
1806 /* Adjust binary path according to the service type */
1807 FixupServiceBinaryPath(ServiceType
, &ServiceBinary
);
1809 /* Don't check return value, as these fields are optional and
1810 * GetLineText initialize output parameter even on failure */
1811 GetLineText(hInf
, ServiceSection
, LoadOrderGroupKey
, &LoadOrderGroup
);
1812 GetLineText(hInf
, ServiceSection
, DisplayNameKey
, &DisplayName
);
1813 GetLineText(hInf
, ServiceSection
, DescriptionKey
, &Description
);
1814 GetLineText(hInf
, ServiceSection
, DependenciesKey
, &Dependencies
);
1816 /* If there is no group, we must not request a tag */
1817 if (!LoadOrderGroup
|| !*LoadOrderGroup
)
1820 hService
= OpenServiceW(
1823 DELETE
| SERVICE_QUERY_CONFIG
| SERVICE_CHANGE_CONFIG
| WRITE_DAC
);
1824 if (hService
== NULL
&& GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST
)
1827 if (hService
&& (ServiceFlags
& SPSVCINST_DELETEEVENTLOGENTRY
))
1829 ret
= DeleteService(hService
);
1830 if (!ret
&& GetLastError() != ERROR_SERVICE_MARKED_FOR_DELETE
)
1834 if (hService
== NULL
)
1836 /* Create new service */
1837 hService
= CreateServiceW(
1847 useTag
? &tagId
: NULL
,
1850 if (hService
== NULL
)
1856 /* Read current configuration */
1857 if (!QueryServiceConfigW(hService
, NULL
, 0, &bufferSize
))
1859 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
1861 ServiceConfig
= MyMalloc(bufferSize
);
1864 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1867 if (!QueryServiceConfigW(hService
, ServiceConfig
, bufferSize
, &bufferSize
))
1870 tagId
= ServiceConfig
->dwTagId
;
1872 /* Update configuration */
1873 ret
= ChangeServiceConfigW(
1876 (ServiceFlags
& SPSVCINST_NOCLOBBER_STARTTYPE
) ? SERVICE_NO_CHANGE
: StartType
,
1877 (ServiceFlags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) ? SERVICE_NO_CHANGE
: ErrorControl
,
1879 (ServiceFlags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
&& ServiceConfig
->lpLoadOrderGroup
) ? NULL
: LoadOrderGroup
,
1880 useTag
? &tagId
: NULL
,
1881 (ServiceFlags
& SPSVCINST_NOCLOBBER_DEPENDENCIES
&& ServiceConfig
->lpDependencies
) ? NULL
: Dependencies
,
1883 (ServiceFlags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
&& ServiceConfig
->lpDisplayName
) ? NULL
: DisplayName
);
1889 if (GetLineText(hInf
, ServiceSection
, SecurityKey
, &SecurityDescriptor
))
1891 ret
= ConvertStringSecurityDescriptorToSecurityDescriptorW(SecurityDescriptor
, SDDL_REVISION_1
, &sd
, NULL
);
1894 ret
= SetServiceObjectSecurity(hService
, DACL_SECURITY_INFORMATION
, sd
);
1899 /* FIXME: use Description and SPSVCINST_NOCLOBBER_DESCRIPTION */
1903 /* Add the tag to SYSTEM\CurrentControlSet\Control\GroupOrderList key */
1904 LPCWSTR lpLoadOrderGroup
;
1907 lpLoadOrderGroup
= LoadOrderGroup
;
1908 if ((ServiceFlags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
) && ServiceConfig
&& ServiceConfig
->lpLoadOrderGroup
)
1909 lpLoadOrderGroup
= ServiceConfig
->lpLoadOrderGroup
;
1912 list
? list
->HKLM
: HKEY_LOCAL_MACHINE
,
1914 &hGroupOrderListKey
);
1915 if (rc
!= ERROR_SUCCESS
)
1920 rc
= RegQueryValueExW(hGroupOrderListKey
, lpLoadOrderGroup
, NULL
, &dwRegType
, NULL
, &bufferSize
);
1921 if (rc
== ERROR_FILE_NOT_FOUND
)
1922 bufferSize
= sizeof(DWORD
);
1923 else if (rc
!= ERROR_SUCCESS
)
1928 else if (dwRegType
!= REG_BINARY
|| bufferSize
== 0 || bufferSize
% sizeof(DWORD
) != 0)
1930 SetLastError(ERROR_GEN_FAILURE
);
1933 /* Allocate buffer to store existing data + the new tag */
1934 GroupOrder
= MyMalloc(bufferSize
+ sizeof(DWORD
));
1937 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1940 if (rc
== ERROR_SUCCESS
)
1942 /* Read existing data */
1943 rc
= RegQueryValueExW(
1950 if (rc
!= ERROR_SUCCESS
)
1955 if (ServiceFlags
& SPSVCINST_TAGTOFRONT
)
1956 memmove(&GroupOrder
[2], &GroupOrder
[1], bufferSize
- sizeof(DWORD
));
1963 if (ServiceFlags
& SPSVCINST_TAGTOFRONT
)
1964 GroupOrder
[1] = tagId
;
1966 GroupOrder
[bufferSize
/ sizeof(DWORD
)] = tagId
;
1968 rc
= RegSetValueExW(
1974 bufferSize
+ sizeof(DWORD
));
1975 if (rc
!= ERROR_SUCCESS
)
1982 /* Handle AddReg and DelReg */
1984 list
? list
->HKLM
: HKEY_LOCAL_MACHINE
,
1985 REGSTR_PATH_SERVICES
,
1989 if (rc
!= ERROR_SUCCESS
)
1998 KEY_READ
| KEY_WRITE
,
2000 RegCloseKey(hServicesKey
);
2001 if (rc
!= ERROR_SUCCESS
)
2007 ret
= SetupInstallFromInfSectionW(
2019 RegCloseKey(hServiceKey
);
2022 if (hSCManager
!= NULL
)
2023 CloseServiceHandle(hSCManager
);
2024 if (hService
!= NULL
)
2025 CloseServiceHandle(hService
);
2026 if (hGroupOrderListKey
!= NULL
)
2027 RegCloseKey(hGroupOrderListKey
);
2030 MyFree(ServiceConfig
);
2031 MyFree(ServiceBinary
);
2032 MyFree(LoadOrderGroup
);
2033 MyFree(DisplayName
);
2034 MyFree(Description
);
2035 MyFree(Dependencies
);
2036 MyFree(SecurityDescriptor
);
2039 TRACE("Returning %d\n", ret
);
2044 /***********************************************************************
2045 * SetupInstallServicesFromInfSectionExW (SETUPAPI.@)
2047 BOOL WINAPI
SetupInstallServicesFromInfSectionExW( HINF hinf
, PCWSTR sectionname
, DWORD flags
, HDEVINFO DeviceInfoSet
, PSP_DEVINFO_DATA DeviceInfoData
, PVOID reserved1
, PVOID reserved2
)
2049 struct DeviceInfoSet
*list
= NULL
;
2052 TRACE("%p, %s, 0x%lx, %p, %p, %p, %p\n", hinf
, debugstr_w(sectionname
),
2053 flags
, DeviceInfoSet
, DeviceInfoData
, reserved1
, reserved2
);
2056 SetLastError(ERROR_INVALID_PARAMETER
);
2057 else if (flags
& ~(SPSVCINST_TAGTOFRONT
| SPSVCINST_DELETEEVENTLOGENTRY
| SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
| SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
| SPSVCINST_NOCLOBBER_DEPENDENCIES
| SPSVCINST_STOPSERVICE
))
2059 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
));
2060 SetLastError(ERROR_INVALID_FLAGS
);
2062 else if (DeviceInfoSet
== (HDEVINFO
)INVALID_HANDLE_VALUE
)
2063 SetLastError(ERROR_INVALID_HANDLE
);
2064 else if (DeviceInfoSet
&& (list
= (struct DeviceInfoSet
*)DeviceInfoSet
)->magic
!= SETUP_DEVICE_INFO_SET_MAGIC
)
2065 SetLastError(ERROR_INVALID_HANDLE
);
2066 else if (DeviceInfoData
&& DeviceInfoData
->cbSize
!= sizeof(SP_DEVINFO_DATA
))
2067 SetLastError(ERROR_INVALID_USER_BUFFER
);
2068 else if (reserved1
!= NULL
|| reserved2
!= NULL
)
2069 SetLastError(ERROR_INVALID_PARAMETER
);
2072 struct needs_callback_info needs_info
;
2073 LPWSTR ServiceName
= NULL
;
2074 LPWSTR ServiceSection
= NULL
;
2076 INFCONTEXT ContextService
;
2077 BOOL bNeedReboot
= FALSE
;
2079 /* Parse 'Include' and 'Needs' directives */
2080 iterate_section_fields( hinf
, sectionname
, Include
, include_callback
, NULL
);
2081 needs_info
.type
= 1;
2082 needs_info
.flags
= flags
;
2083 needs_info
.devinfo
= DeviceInfoSet
;
2084 needs_info
.devinfo_data
= DeviceInfoData
;
2085 needs_info
.reserved1
= reserved1
;
2086 needs_info
.reserved2
= reserved2
;
2087 iterate_section_fields( hinf
, sectionname
, Needs
, needs_callback
, &needs_info
);
2089 if (flags
& SPSVCINST_STOPSERVICE
)
2091 FIXME("Stopping the device not implemented\n");
2092 /* This may lead to require a reboot */
2093 /* bNeedReboot = TRUE; */
2095 SERVICE_STATUS ServiceStatus
;
2096 ret
= ControlService(hService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
2097 if (!ret
&& GetLastError() != ERROR_SERVICE_NOT_ACTIVE
)
2099 if (ServiceStatus
.dwCurrentState
!= SERVICE_STOP_PENDING
&& ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
)
2101 SetLastError(ERROR_INSTALL_SERVICE_FAILURE
);
2105 flags
&= ~SPSVCINST_STOPSERVICE
;
2108 ret
= SetupFindFirstLineW(hinf
, sectionname
, AddService
, &ContextService
);
2111 if (!GetStringField(&ContextService
, 1, &ServiceName
))
2114 ret
= SetupGetIntField(
2116 2, /* Field index */
2120 /* The field may be empty. Ignore the error */
2124 if (!GetStringField(&ContextService
, 3, &ServiceSection
))
2127 ret
= InstallOneService(list
, hinf
, ServiceSection
, ServiceName
, (ServiceFlags
& ~SPSVCINST_ASSOCSERVICE
) | flags
);
2131 if (ServiceFlags
& SPSVCINST_ASSOCSERVICE
)
2133 ret
= SetupDiSetDeviceRegistryPropertyW(DeviceInfoSet
, DeviceInfoData
, SPDRP_SERVICE
, (LPBYTE
)ServiceName
, (strlenW(ServiceName
) + 1) * sizeof(WCHAR
));
2139 HeapFree(GetProcessHeap(), 0, ServiceName
);
2140 HeapFree(GetProcessHeap(), 0, ServiceSection
);
2141 ServiceName
= ServiceSection
= NULL
;
2142 ret
= SetupFindNextMatchLineW(&ContextService
, AddService
, &ContextService
);
2146 SetLastError(ERROR_SUCCESS_REBOOT_REQUIRED
);
2148 SetLastError(ERROR_SUCCESS
);
2152 TRACE("Returning %d\n", ret
);
2157 /***********************************************************************
2158 * SetupCopyOEMInfA (SETUPAPI.@)
2160 BOOL WINAPI
SetupCopyOEMInfA(
2161 IN PCSTR SourceInfFileName
,
2162 IN PCSTR OEMSourceMediaLocation
,
2163 IN DWORD OEMSourceMediaType
,
2165 OUT PSTR DestinationInfFileName OPTIONAL
,
2166 IN DWORD DestinationInfFileNameSize
,
2167 OUT PDWORD RequiredSize OPTIONAL
,
2168 OUT PSTR
* DestinationInfFileNameComponent OPTIONAL
)
2170 PWSTR SourceInfFileNameW
= NULL
;
2171 PWSTR OEMSourceMediaLocationW
= NULL
;
2172 PWSTR DestinationInfFileNameW
= NULL
;
2173 PWSTR DestinationInfFileNameComponentW
= NULL
;
2176 TRACE("%s %s 0x%lx 0x%lx %p 0%lu %p %p\n",
2177 SourceInfFileName
, OEMSourceMediaLocation
, OEMSourceMediaType
,
2178 CopyStyle
, DestinationInfFileName
, DestinationInfFileNameSize
,
2179 RequiredSize
, DestinationInfFileNameComponent
);
2181 if (!DestinationInfFileName
&& DestinationInfFileNameSize
> 0)
2182 SetLastError(ERROR_INVALID_PARAMETER
);
2183 else if (!(SourceInfFileNameW
= pSetupMultiByteToUnicode(SourceInfFileName
, CP_ACP
)))
2184 SetLastError(ERROR_INVALID_PARAMETER
);
2185 else if (OEMSourceMediaType
!= SPOST_NONE
&& !(OEMSourceMediaLocationW
= pSetupMultiByteToUnicode(OEMSourceMediaLocation
, CP_ACP
)))
2186 SetLastError(ERROR_INVALID_PARAMETER
);
2189 if (DestinationInfFileNameSize
!= 0)
2191 DestinationInfFileNameW
= MyMalloc(DestinationInfFileNameSize
* sizeof(WCHAR
));
2192 if (!DestinationInfFileNameW
)
2194 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2199 ret
= SetupCopyOEMInfW(
2201 OEMSourceMediaLocationW
,
2204 DestinationInfFileNameW
,
2205 DestinationInfFileNameSize
,
2207 DestinationInfFileNameComponent
? &DestinationInfFileNameComponentW
: NULL
);
2211 if (DestinationInfFileNameSize
!= 0)
2213 if (WideCharToMultiByte(CP_ACP
, 0, DestinationInfFileNameW
, -1,
2214 DestinationInfFileName
, DestinationInfFileNameSize
, NULL
, NULL
) == 0)
2216 DestinationInfFileName
[0] = '\0';
2220 if (DestinationInfFileNameComponent
)
2222 if (DestinationInfFileNameComponentW
)
2223 *DestinationInfFileNameComponent
= &DestinationInfFileName
[DestinationInfFileNameComponentW
- DestinationInfFileNameW
];
2225 *DestinationInfFileNameComponent
= NULL
;
2231 MyFree(SourceInfFileNameW
);
2232 MyFree(OEMSourceMediaLocationW
);
2233 MyFree(DestinationInfFileNameW
);
2234 TRACE("Returning %d\n", ret
);
2235 if (ret
) SetLastError(ERROR_SUCCESS
);
2239 static int compare_files( HANDLE file1
, HANDLE file2
)
2246 while( ReadFile(file1
, buffer1
, sizeof(buffer1
), &size1
, NULL
) &&
2247 ReadFile(file2
, buffer2
, sizeof(buffer2
), &size2
, NULL
) )
2251 return size1
> size2
? 1 : -1;
2254 ret
= memcmp( buffer1
, buffer2
, size1
);
2262 /***********************************************************************
2263 * SetupCopyOEMInfW (SETUPAPI.@)
2265 BOOL WINAPI
SetupCopyOEMInfW(
2266 IN PCWSTR SourceInfFileName
,
2267 IN PCWSTR OEMSourceMediaLocation
,
2268 IN DWORD OEMSourceMediaType
,
2270 OUT PWSTR DestinationInfFileName OPTIONAL
,
2271 IN DWORD DestinationInfFileNameSize
,
2272 OUT PDWORD RequiredSize OPTIONAL
,
2273 OUT PWSTR
* DestinationInfFileNameComponent OPTIONAL
)
2277 TRACE("%s %s 0x%lx 0x%lx %p 0%lu %p %p\n",
2278 debugstr_w(SourceInfFileName
), debugstr_w(OEMSourceMediaLocation
), OEMSourceMediaType
,
2279 CopyStyle
, DestinationInfFileName
, DestinationInfFileNameSize
,
2280 RequiredSize
, DestinationInfFileNameComponent
);
2282 if (!SourceInfFileName
)
2283 SetLastError(ERROR_INVALID_PARAMETER
);
2284 else if (OEMSourceMediaType
!= SPOST_NONE
&& OEMSourceMediaType
!= SPOST_PATH
&& OEMSourceMediaType
!= SPOST_URL
)
2285 SetLastError(ERROR_INVALID_PARAMETER
);
2286 else if (OEMSourceMediaType
!= SPOST_NONE
&& !OEMSourceMediaLocation
)
2287 SetLastError(ERROR_INVALID_PARAMETER
);
2288 else if (CopyStyle
& ~(SP_COPY_DELETESOURCE
| SP_COPY_REPLACEONLY
| SP_COPY_NOOVERWRITE
| SP_COPY_OEMINF_CATALOG_ONLY
))
2290 TRACE("Unknown flags: 0x%08lx\n", CopyStyle
& ~(SP_COPY_DELETESOURCE
| SP_COPY_REPLACEONLY
| SP_COPY_NOOVERWRITE
| SP_COPY_OEMINF_CATALOG_ONLY
));
2291 SetLastError(ERROR_INVALID_FLAGS
);
2293 else if (!DestinationInfFileName
&& DestinationInfFileNameSize
> 0)
2294 SetLastError(ERROR_INVALID_PARAMETER
);
2295 else if (CopyStyle
& SP_COPY_OEMINF_CATALOG_ONLY
)
2297 FIXME("CopyStyle 0x%x not supported\n", SP_COPY_OEMINF_CATALOG_ONLY
);
2298 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2302 HANDLE hSearch
= INVALID_HANDLE_VALUE
;
2303 WIN32_FIND_DATAW FindFileData
;
2305 DWORD NextFreeNumber
= 0;
2307 LPWSTR pFullFileName
= NULL
;
2308 LPWSTR pFileName
; /* Pointer into pFullFileName buffer */
2309 HANDLE hSourceFile
= INVALID_HANDLE_VALUE
;
2311 if (OEMSourceMediaType
== SPOST_PATH
|| OEMSourceMediaType
== SPOST_URL
)
2312 FIXME("OEMSourceMediaType 0x%lx ignored\n", OEMSourceMediaType
);
2314 /* Check if source file exists, and open it */
2315 if (strchrW(SourceInfFileName
, '\\' ) || strchrW(SourceInfFileName
, '/' ))
2319 if (!(len
= GetFullPathNameW(SourceInfFileName
, 0, NULL
, NULL
)))
2321 if (!(path
= MyMalloc(len
* sizeof(WCHAR
))))
2323 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2326 GetFullPathNameW(SourceInfFileName
, len
, path
, NULL
);
2327 hSourceFile
= CreateFileW(
2328 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2329 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2330 NULL
, OPEN_EXISTING
, 0, NULL
);
2333 else /* try Windows directory */
2336 static const WCHAR Inf
[] = {'\\','i','n','f','\\',0};
2337 static const WCHAR System32
[] = {'\\','s','y','s','t','e','m','3','2','\\',0};
2339 len
= GetWindowsDirectoryW(NULL
, 0) + strlenW(SourceInfFileName
) + 12;
2340 if (!(path
= MyMalloc(len
* sizeof(WCHAR
))))
2342 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2345 GetWindowsDirectoryW(path
, len
);
2346 p
= path
+ strlenW(path
);
2348 strcatW(p
, SourceInfFileName
);
2349 hSourceFile
= CreateFileW(
2350 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2351 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2352 NULL
, OPEN_EXISTING
, 0, NULL
);
2353 if (hSourceFile
== INVALID_HANDLE_VALUE
)
2355 strcpyW(p
, System32
);
2356 strcatW(p
, SourceInfFileName
);
2357 hSourceFile
= CreateFileW(
2358 path
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2359 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2360 NULL
, OPEN_EXISTING
, 0, NULL
);
2364 if (hSourceFile
== INVALID_HANDLE_VALUE
)
2366 SetLastError(ERROR_FILE_NOT_FOUND
);
2370 /* Prepare .inf file specification */
2371 len
= MAX_PATH
+ 1 + strlenW(InfDirectory
) + 13;
2372 pFullFileName
= MyMalloc(len
* sizeof(WCHAR
));
2375 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2378 len
= GetSystemWindowsDirectoryW(pFullFileName
, MAX_PATH
);
2379 if (len
== 0 || len
> MAX_PATH
)
2381 if (pFullFileName
[strlenW(pFullFileName
) - 1] != '\\')
2382 strcatW(pFullFileName
, BackSlash
);
2383 strcatW(pFullFileName
, InfDirectory
);
2384 pFileName
= &pFullFileName
[strlenW(pFullFileName
)];
2386 /* Search if the specified .inf file already exists in %WINDIR%\Inf */
2387 AlreadyExists
= FALSE
;
2388 strcpyW(pFileName
, OemFileMask
);
2389 hSearch
= FindFirstFileW(pFullFileName
, &FindFileData
);
2390 if (hSearch
!= INVALID_HANDLE_VALUE
)
2392 LARGE_INTEGER SourceFileSize
;
2394 if (GetFileSizeEx(hSourceFile
, &SourceFileSize
))
2398 LARGE_INTEGER DestFileSize
;
2401 strcpyW(pFileName
, FindFileData
.cFileName
);
2402 hDestFile
= CreateFileW(
2403 pFullFileName
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
2404 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
2405 NULL
, OPEN_EXISTING
, 0, NULL
);
2406 if (hDestFile
!= INVALID_HANDLE_VALUE
)
2408 if (GetFileSizeEx(hDestFile
, &DestFileSize
)
2409 && DestFileSize
.QuadPart
== SourceFileSize
.QuadPart
2410 && !compare_files(hSourceFile
, hDestFile
))
2412 TRACE("%s already exists as %s\n",
2413 debugstr_w(SourceInfFileName
), debugstr_w(pFileName
));
2414 AlreadyExists
= TRUE
;
2417 } while (!AlreadyExists
&& FindNextFileW(hSearch
, &FindFileData
));
2420 hSearch
= INVALID_HANDLE_VALUE
;
2423 if (!AlreadyExists
&& CopyStyle
& SP_COPY_REPLACEONLY
)
2425 /* FIXME: set DestinationInfFileName, RequiredSize, DestinationInfFileNameComponent */
2426 SetLastError(ERROR_FILE_NOT_FOUND
);
2429 else if (AlreadyExists
&& (CopyStyle
& SP_COPY_NOOVERWRITE
))
2431 DWORD Size
= strlenW(pFileName
) + 1;
2434 *RequiredSize
= Size
;
2435 if (DestinationInfFileNameSize
== 0)
2436 SetLastError(ERROR_FILE_EXISTS
);
2437 else if (DestinationInfFileNameSize
< Size
)
2438 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2441 SetLastError(ERROR_FILE_EXISTS
);
2442 strcpyW(DestinationInfFileName
, pFileName
);
2447 /* Search the number to give to OEM??.INF */
2448 strcpyW(pFileName
, OemFileMask
);
2449 hSearch
= FindFirstFileW(pFullFileName
, &FindFileData
);
2450 if (hSearch
== INVALID_HANDLE_VALUE
)
2452 if (GetLastError() != ERROR_FILE_NOT_FOUND
)
2459 DWORD CurrentNumber
;
2460 if (swscanf(FindFileData
.cFileName
, OemFileSpecification
, &CurrentNumber
) == 1
2461 && CurrentNumber
<= 99999)
2463 if (CurrentNumber
>= NextFreeNumber
)
2464 NextFreeNumber
= CurrentNumber
+ 1;
2466 } while (FindNextFileW(hSearch
, &FindFileData
));
2469 if (NextFreeNumber
> 99999)
2471 ERR("Too much custom .inf files\n");
2472 SetLastError(ERROR_GEN_FAILURE
);
2476 /* Create the full path: %WINDIR%\Inf\OEM{XXXXX}.inf */
2477 sprintfW(pFileName
, OemFileSpecification
, NextFreeNumber
);
2478 TRACE("Next available file is %s\n", debugstr_w(pFileName
));
2480 if (!CopyFileW(SourceInfFileName
, pFullFileName
, TRUE
))
2482 TRACE("CopyFileW() failed with error 0x%lx\n", GetLastError());
2486 len
= strlenW(pFullFileName
) + 1;
2488 *RequiredSize
= len
;
2489 if (DestinationInfFileName
)
2491 if (DestinationInfFileNameSize
>= len
)
2493 strcpyW(DestinationInfFileName
, pFullFileName
);
2494 if (DestinationInfFileNameComponent
)
2495 *DestinationInfFileNameComponent
= &DestinationInfFileName
[pFileName
- pFullFileName
];
2499 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
2504 if (CopyStyle
& SP_COPY_DELETESOURCE
)
2506 if (!DeleteFileW(SourceInfFileName
))
2508 TRACE("DeleteFileW() failed with error 0x%lx\n", GetLastError());
2516 if (hSourceFile
!= INVALID_HANDLE_VALUE
)
2517 CloseHandle(hSourceFile
);
2518 if (hSearch
!= INVALID_HANDLE_VALUE
)
2520 MyFree(pFullFileName
);
2523 TRACE("Returning %d\n", ret
);
2524 if (ret
) SetLastError(ERROR_SUCCESS
);