2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
40 #include "setupapi_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
45 /* info passed to callback functions dealing with files */
46 struct files_callback_info
54 /* info passed to callback functions dealing with the registry */
55 struct registry_callback_info
61 /* info passed to callback functions dealing with registering dlls */
62 struct register_dll_info
64 PSP_FILE_CALLBACK_W callback
;
65 PVOID callback_context
;
72 typedef BOOL (*iterate_fields_func
)( HINF hinf
, PCWSTR field
, void *arg
);
75 /***********************************************************************
78 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
80 static WCHAR
*get_field_string( INFCONTEXT
*context
, DWORD index
, WCHAR
*buffer
,
81 WCHAR
*static_buffer
, DWORD
*size
)
85 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
86 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
88 /* now grow the buffer */
89 if (buffer
!= static_buffer
) free( buffer
);
90 if (!(buffer
= malloc( required
* sizeof(WCHAR
) ))) return NULL
;
92 if (SetupGetStringFieldW( context
, index
, buffer
, *size
, &required
)) return buffer
;
94 if (buffer
!= static_buffer
) free( buffer
);
99 /***********************************************************************
100 * dup_section_line_field
102 * Retrieve the contents of a field in a newly-allocated buffer.
104 static WCHAR
*dup_section_line_field( HINF hinf
, const WCHAR
*section
, const WCHAR
*line
, DWORD index
)
110 if (!SetupFindFirstLineW( hinf
, section
, line
, &context
)) return NULL
;
111 if (!SetupGetStringFieldW( &context
, index
, NULL
, 0, &size
)) return NULL
;
112 if (!(buffer
= malloc( size
* sizeof(WCHAR
) ))) return NULL
;
113 if (!SetupGetStringFieldW( &context
, index
, buffer
, size
, NULL
)) buffer
[0] = 0;
117 static void get_inf_src_path( HINF hinf
, WCHAR
*path
)
119 const WCHAR
*inf_path
= PARSER_get_inf_filename( hinf
);
120 WCHAR pnf_path
[MAX_PATH
];
123 wcscpy( pnf_path
, inf_path
);
124 PathRemoveExtensionW( pnf_path
);
125 PathAddExtensionW( pnf_path
, L
".pnf" );
126 if ((pnf
= _wfopen( pnf_path
, L
"r" )))
128 if (fgetws( path
, MAX_PATH
, pnf
) && !wcscmp( path
, PNF_HEADER
))
130 fgetws( path
, MAX_PATH
, pnf
);
131 TRACE("using original source path %s\n", debugstr_w(path
));
137 wcscpy( path
, inf_path
);
140 /***********************************************************************
141 * copy_files_callback
143 * Called once for each CopyFiles entry in a given section.
145 static BOOL
copy_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
148 struct files_callback_info
*info
= arg
;
149 WCHAR src_root
[MAX_PATH
], *p
;
153 const WCHAR
*build_dir
= _wgetenv( L
"WINEBUILDDIR" );
154 const WCHAR
*data_dir
= _wgetenv( L
"WINEDATADIR" );
156 if ((build_dir
|| data_dir
) && SetupFindFirstLineW( hinf
, L
"WineSourceDirs", field
, &context
))
158 lstrcpyW( src_root
, build_dir
? build_dir
: data_dir
);
159 src_root
[1] = '\\'; /* change \??\ to \\?\ */
160 p
= src_root
+ lstrlenW(src_root
);
162 if (!build_dir
|| !SetupGetStringFieldW( &context
, 2, p
, MAX_PATH
- (p
- src_root
), NULL
))
164 if (!SetupGetStringFieldW( &context
, 1, p
, MAX_PATH
- (p
- src_root
), NULL
)) p
[-1] = 0;
169 get_inf_src_path( hinf
, src_root
);
170 if ((p
= wcsrchr( src_root
, '\\' ))) *p
= 0;
174 if (field
[0] == '@') /* special case: copy single file */
175 SetupQueueDefaultCopyW( info
->queue
, info
->layout
? info
->layout
: hinf
,
176 info
->src_root
? info
->src_root
: src_root
, field
+1, field
+1, info
->copy_flags
);
178 SetupQueueCopySectionW( info
->queue
, info
->src_root
? info
->src_root
: src_root
,
179 info
->layout
? info
->layout
: hinf
, hinf
, field
, info
->copy_flags
);
184 /***********************************************************************
185 * delete_files_callback
187 * Called once for each DelFiles entry in a given section.
189 static BOOL
delete_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
191 struct files_callback_info
*info
= arg
;
192 SetupQueueDeleteSectionW( info
->queue
, hinf
, 0, field
);
197 /***********************************************************************
198 * rename_files_callback
200 * Called once for each RenFiles entry in a given section.
202 static BOOL
rename_files_callback( HINF hinf
, PCWSTR field
, void *arg
)
204 struct files_callback_info
*info
= arg
;
205 SetupQueueRenameSectionW( info
->queue
, hinf
, 0, field
);
210 /***********************************************************************
213 * Retrieve the registry root key from its name.
215 static HKEY
get_root_key( const WCHAR
*name
, HKEY def_root
)
217 if (!wcsicmp( name
, L
"HKCR" )) return HKEY_CLASSES_ROOT
;
218 if (!wcsicmp( name
, L
"HKCU" )) return HKEY_CURRENT_USER
;
219 if (!wcsicmp( name
, L
"HKLM" )) return HKEY_LOCAL_MACHINE
;
220 if (!wcsicmp( name
, L
"HKU" )) return HKEY_USERS
;
221 if (!wcsicmp( name
, L
"HKR" )) return def_root
;
226 /***********************************************************************
227 * append_multi_sz_value
229 * Append a multisz string to a multisz registry value.
231 static bool append_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*strings
,
234 DWORD size
, type
, total
;
238 if ((ret
= RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)))
240 if (ret
!= ERROR_FILE_NOT_FOUND
)
242 ERR( "failed to query value %s, error %lu\n", debugstr_w(value
), ret
);
247 if ((ret
= RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)strings
, str_size
* sizeof(WCHAR
) )))
249 ERR( "failed to set value %s, error %lu\n", debugstr_w(value
), ret
);
257 if (type
!= REG_MULTI_SZ
)
259 WARN( "value %s exists but has wrong type %#lx\n", debugstr_w(value
), type
);
260 SetLastError( ERROR_INVALID_DATA
);
264 if (!(buffer
= malloc( (size
+ str_size
) * sizeof(WCHAR
) ))) return false;
265 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
))
271 /* compare each string against all the existing ones */
275 int len
= lstrlenW(strings
) + 1;
277 for (p
= buffer
; *p
; p
+= lstrlenW(p
) + 1)
278 if (!wcsicmp( p
, strings
)) break;
280 if (!*p
) /* not found, need to append it */
282 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
284 total
+= len
* sizeof(WCHAR
);
290 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
291 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
);
299 /***********************************************************************
300 * delete_multi_sz_value
302 * Remove a string from a multisz registry value.
304 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
307 WCHAR
*buffer
, *src
, *dst
;
309 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
310 if (type
!= REG_MULTI_SZ
) return;
311 /* allocate double the size, one for value before and one for after */
312 if (!(buffer
= malloc( size
* 2 * sizeof(WCHAR
) ))) return;
313 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
318 int len
= lstrlenW(src
) + 1;
319 if (wcsicmp( src
, string
))
321 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
327 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
329 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
330 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
331 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
338 /***********************************************************************
341 * Perform an add/delete registry operation depending on the flags.
343 static BOOL
do_reg_operation( HKEY hkey
, const WCHAR
*value
, INFCONTEXT
*context
, INT flags
)
347 if (flags
& (FLG_ADDREG_DELREG_BIT
| FLG_ADDREG_DELVAL
)) /* deletion */
349 if (*value
&& !(flags
& FLG_DELREG_KEYONLY_COMMON
))
351 if ((flags
& FLG_DELREG_MULTI_SZ_DELSTRING
) == FLG_DELREG_MULTI_SZ_DELSTRING
)
355 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
) || !size
) return TRUE
;
356 if (!(str
= malloc( size
* sizeof(WCHAR
) ))) return FALSE
;
357 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
358 delete_multi_sz_value( hkey
, value
, str
);
361 else RegDeleteValueW( hkey
, value
);
365 RegDeleteTreeW( hkey
, NULL
);
371 if (flags
& (FLG_ADDREG_KEYONLY
|FLG_ADDREG_KEYONLY_COMMON
)) return TRUE
;
373 if (flags
& (FLG_ADDREG_NOCLOBBER
|FLG_ADDREG_OVERWRITEONLY
))
375 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
376 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
)) return TRUE
;
377 if (!exists
&& (flags
& FLG_ADDREG_OVERWRITEONLY
)) return TRUE
;
380 switch(flags
& FLG_ADDREG_TYPE_MASK
)
382 case FLG_ADDREG_TYPE_SZ
: type
= REG_SZ
; break;
383 case FLG_ADDREG_TYPE_MULTI_SZ
: type
= REG_MULTI_SZ
; break;
384 case FLG_ADDREG_TYPE_EXPAND_SZ
: type
= REG_EXPAND_SZ
; break;
385 case FLG_ADDREG_TYPE_BINARY
: type
= REG_BINARY
; break;
386 case FLG_ADDREG_TYPE_DWORD
: type
= REG_DWORD
; break;
387 case FLG_ADDREG_TYPE_NONE
: type
= REG_NONE
; break;
388 default: type
= flags
>> 16; break;
391 if (!(flags
& FLG_ADDREG_BINVALUETYPE
) ||
392 (type
== REG_DWORD
&& SetupGetFieldCount(context
) == 5))
396 if (type
== REG_MULTI_SZ
)
398 if (!SetupGetMultiSzFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
401 if (!(str
= malloc( size
* sizeof(WCHAR
) ))) return FALSE
;
402 SetupGetMultiSzFieldW( context
, 5, str
, size
, NULL
);
404 if (flags
& FLG_ADDREG_APPEND
)
406 if (!str
) return TRUE
;
407 if (!append_multi_sz_value( hkey
, value
, str
, size
))
415 /* else fall through to normal string handling */
419 if (!SetupGetStringFieldW( context
, 5, NULL
, 0, &size
)) size
= 0;
422 if (!(str
= malloc( size
* sizeof(WCHAR
) ))) return FALSE
;
423 SetupGetStringFieldW( context
, 5, str
, size
, NULL
);
424 if (type
== REG_LINK
) size
--; /* no terminating null for symlinks */
428 if (type
== REG_DWORD
)
430 DWORD dw
= str
? wcstoul( str
, NULL
, 0 ) : 0;
431 TRACE( "setting dword %s to %lx\n", debugstr_w(value
), dw
);
432 RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)&dw
, sizeof(dw
) );
436 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(str
) );
437 if (str
) RegSetValueExW( hkey
, value
, 0, type
, (BYTE
*)str
, size
* sizeof(WCHAR
) );
438 else RegSetValueExW( hkey
, value
, 0, type
, (const BYTE
*)L
"", sizeof(WCHAR
) );
443 else /* get the binary data */
447 if (!SetupGetBinaryField( context
, 5, NULL
, 0, &size
)) size
= 0;
450 if (!(data
= malloc( size
))) return FALSE
;
451 TRACE( "setting binary data %s len %ld\n", debugstr_w(value
), size
);
452 SetupGetBinaryField( context
, 5, data
, size
, NULL
);
454 RegSetValueExW( hkey
, value
, 0, type
, data
, size
);
461 /***********************************************************************
464 * Called once for each AddReg and DelReg entry in a given section.
466 static BOOL
registry_callback( HINF hinf
, PCWSTR field
, void *arg
)
468 struct registry_callback_info
*info
= arg
;
472 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
474 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
477 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
481 if (!SetupGetStringFieldW( &context
, 1, buffer
, ARRAY_SIZE( buffer
), NULL
))
483 if (!(root_key
= get_root_key( buffer
, info
->default_root
)))
487 if (!SetupGetStringFieldW( &context
, 2, buffer
, ARRAY_SIZE( buffer
), NULL
))
491 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
495 if (flags
& FLG_ADDREG_DELREG_BIT
) continue; /* ignore this entry */
499 if (!flags
) flags
= FLG_ADDREG_DELREG_BIT
;
500 else if (!(flags
& FLG_ADDREG_DELREG_BIT
)) continue; /* ignore this entry */
502 /* Wine extension: magic support for symlinks */
503 if (flags
>> 16 == REG_LINK
) options
= REG_OPTION_OPEN_LINK
| REG_OPTION_CREATE_LINK
;
505 if (info
->delete || (flags
& FLG_ADDREG_OVERWRITEONLY
))
507 if (RegOpenKeyExW( root_key
, buffer
, options
, MAXIMUM_ALLOWED
, &hkey
))
508 continue; /* ignore if it doesn't exist */
512 DWORD res
= RegCreateKeyExW( root_key
, buffer
, 0, NULL
, options
,
513 MAXIMUM_ALLOWED
, NULL
, &hkey
, NULL
);
514 if (res
== ERROR_ALREADY_EXISTS
&& (options
& REG_OPTION_CREATE_LINK
))
515 res
= RegCreateKeyExW( root_key
, buffer
, 0, NULL
, REG_OPTION_OPEN_LINK
,
516 MAXIMUM_ALLOWED
, NULL
, &hkey
, NULL
);
519 ERR( "could not create key %p %s\n", root_key
, debugstr_w(buffer
) );
523 TRACE( "key %p %s\n", root_key
, debugstr_w(buffer
) );
526 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
530 if (!do_reg_operation( hkey
, buffer
, &context
, flags
))
541 /***********************************************************************
544 * Register or unregister a dll.
546 static BOOL
do_register_dll( struct register_dll_info
*info
, const WCHAR
*path
,
547 INT flags
, INT timeout
, const WCHAR
*args
)
551 SP_REGISTER_CONTROL_STATUSW status
;
552 IMAGE_NT_HEADERS
*nt
;
554 status
.cbSize
= sizeof(status
);
555 status
.FileName
= path
;
556 status
.FailureCode
= SPREG_SUCCESS
;
557 status
.Win32Error
= ERROR_SUCCESS
;
561 switch(info
->callback( info
->callback_context
, SPFILENOTIFY_STARTREGISTRATION
,
562 (UINT_PTR
)&status
, !info
->unregister
))
565 SetLastError( ERROR_OPERATION_ABORTED
);
574 if (!(module
= LoadLibraryExW( path
, 0, LOAD_WITH_ALTERED_SEARCH_PATH
)))
576 WARN( "could not load %s\n", debugstr_w(path
) );
577 status
.FailureCode
= SPREG_LOADLIBRARY
;
578 status
.Win32Error
= GetLastError();
582 if ((nt
= RtlImageNtHeader( module
)) && !(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
584 /* file is an executable, not a dll */
585 STARTUPINFOW startup
;
586 PROCESS_INFORMATION process_info
;
591 FreeLibrary( module
);
593 if (!args
) args
= L
"/RegServer";
594 len
= lstrlenW(path
) + lstrlenW(args
) + 4;
595 cmd_line
= malloc( len
* sizeof(WCHAR
) );
596 swprintf( cmd_line
, len
, L
"\"%s\" %s", path
, args
);
597 memset( &startup
, 0, sizeof(startup
) );
598 startup
.cb
= sizeof(startup
);
599 TRACE( "executing %s\n", debugstr_w(cmd_line
) );
600 res
= CreateProcessW( path
, cmd_line
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &process_info
);
604 status
.FailureCode
= SPREG_LOADLIBRARY
;
605 status
.Win32Error
= GetLastError();
608 CloseHandle( process_info
.hThread
);
610 if (WaitForSingleObject( process_info
.hProcess
, timeout
*1000 ) == WAIT_TIMEOUT
)
612 /* timed out, kill the process */
613 TerminateProcess( process_info
.hProcess
, 1 );
614 status
.FailureCode
= SPREG_TIMEOUT
;
615 status
.Win32Error
= ERROR_TIMEOUT
;
617 CloseHandle( process_info
.hProcess
);
621 if (flags
& FLG_REGSVR_DLLREGISTER
)
623 const char *entry_point
= info
->unregister
? "DllUnregisterServer" : "DllRegisterServer";
624 HRESULT (WINAPI
*func
)(void) = (void *)GetProcAddress( module
, entry_point
);
628 status
.FailureCode
= SPREG_GETPROCADDR
;
629 status
.Win32Error
= GetLastError();
633 TRACE( "calling %s in %s\n", entry_point
, debugstr_w(path
) );
638 WARN( "calling %s in %s returned error %lx\n", entry_point
, debugstr_w(path
), res
);
639 status
.FailureCode
= SPREG_REGSVR
;
640 status
.Win32Error
= res
;
645 if (flags
& FLG_REGSVR_DLLINSTALL
)
647 HRESULT (WINAPI
*func
)(BOOL
,LPCWSTR
) = (void *)GetProcAddress( module
, "DllInstall" );
651 status
.FailureCode
= SPREG_GETPROCADDR
;
652 status
.Win32Error
= GetLastError();
656 TRACE( "calling DllInstall(%d,%s) in %s\n",
657 !info
->unregister
, debugstr_w(args
), debugstr_w(path
) );
658 res
= func( !info
->unregister
, args
);
662 WARN( "calling DllInstall in %s returned error %lx\n", debugstr_w(path
), res
);
663 status
.FailureCode
= SPREG_REGSVR
;
664 status
.Win32Error
= res
;
672 if (info
->modules_count
>= info
->modules_size
)
674 int new_size
= max( 32, info
->modules_size
* 2 );
675 HMODULE
*new = realloc( info
->modules
, new_size
* sizeof(*new) );
678 info
->modules_size
= new_size
;
682 if (info
->modules_count
< info
->modules_size
) info
->modules
[info
->modules_count
++] = module
;
683 else FreeLibrary( module
);
685 if (info
->callback
) info
->callback( info
->callback_context
, SPFILENOTIFY_ENDREGISTRATION
,
686 (UINT_PTR
)&status
, !info
->unregister
);
691 /***********************************************************************
692 * register_dlls_callback
694 * Called once for each RegisterDlls entry in a given section.
696 static BOOL
register_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
698 struct register_dll_info
*info
= arg
;
701 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
703 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
705 WCHAR
*path
, *args
, *p
;
706 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
710 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
713 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
715 if (!(p
= realloc( path
, (lstrlenW(path
) + lstrlenW(buffer
) + 2) * sizeof(WCHAR
) )))
719 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
720 lstrcpyW( p
, buffer
);
723 if (!SetupGetIntField( &context
, 4, &flags
)) flags
= 0;
726 if (!SetupGetIntField( &context
, 5, &timeout
)) timeout
= 60;
728 /* get command line */
730 if (SetupGetStringFieldW( &context
, 6, buffer
, ARRAY_SIZE( buffer
), NULL
))
733 ret
= do_register_dll( info
, path
, flags
, timeout
, args
);
742 /***********************************************************************
745 * Called once for each WineFakeDlls entry in a given section.
747 static BOOL
fake_dlls_callback( HINF hinf
, PCWSTR field
, void *arg
)
750 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
752 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
755 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
758 if (!(path
= PARSER_get_dest_dir( &context
))) continue;
761 if (!SetupGetStringFieldW( &context
, 3, buffer
, ARRAY_SIZE( buffer
), NULL
))
763 if (!(p
= realloc( path
, (lstrlenW(path
) + lstrlenW(buffer
) + 2) * sizeof(WCHAR
) )))
767 if (p
== path
|| p
[-1] != '\\') *p
++ = '\\';
768 lstrcpyW( p
, buffer
);
771 if (SetupGetStringFieldW( &context
, 4, buffer
, ARRAY_SIZE( buffer
), NULL
))
772 p
= buffer
; /* otherwise use target base name as default source */
774 create_fake_dll( path
, p
); /* ignore errors */
782 /***********************************************************************
783 * update_ini_callback
785 * Called once for each UpdateInis entry in a given section.
787 static BOOL
update_ini_callback( HINF hinf
, PCWSTR field
, void *arg
)
791 BOOL ok
= SetupFindFirstLineW( hinf
, field
, NULL
, &context
);
793 for (; ok
; ok
= SetupFindNextLine( &context
, &context
))
795 WCHAR buffer
[MAX_INF_STRING_LENGTH
];
796 WCHAR filename
[MAX_INF_STRING_LENGTH
];
797 WCHAR section
[MAX_INF_STRING_LENGTH
];
798 WCHAR entry
[MAX_INF_STRING_LENGTH
];
799 WCHAR string
[MAX_INF_STRING_LENGTH
];
802 if (!SetupGetStringFieldW( &context
, 1, filename
, ARRAY_SIZE( filename
), NULL
))
805 if (!SetupGetStringFieldW( &context
, 2, section
, ARRAY_SIZE( section
), NULL
))
808 if (!SetupGetStringFieldW( &context
, 4, buffer
, ARRAY_SIZE( buffer
), NULL
))
811 divider
= wcschr(buffer
,'=');
815 lstrcpyW(entry
,buffer
);
817 lstrcpyW(string
,divider
);
821 lstrcpyW(entry
,buffer
);
825 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry
),
826 debugstr_w(string
),debugstr_w(section
),debugstr_w(filename
));
827 WritePrivateProfileStringW(section
,entry
,string
,filename
);
833 static BOOL
update_ini_fields_callback( HINF hinf
, PCWSTR field
, void *arg
)
835 FIXME( "should update ini fields %s\n", debugstr_w(field
) );
839 static BOOL
ini2reg_callback( HINF hinf
, PCWSTR field
, void *arg
)
841 FIXME( "should do ini2reg %s\n", debugstr_w(field
) );
845 static BOOL
logconf_callback( HINF hinf
, PCWSTR field
, void *arg
)
847 FIXME( "should do logconf %s\n", debugstr_w(field
) );
851 static BOOL
bitreg_callback( HINF hinf
, PCWSTR field
, void *arg
)
853 FIXME( "should do bitreg %s\n", debugstr_w(field
) );
857 static BOOL
profile_items_callback( HINF hinf
, PCWSTR field
, void *arg
)
859 WCHAR lnkpath
[MAX_PATH
];
860 LPWSTR cmdline
=NULL
, lnkpath_end
;
862 INFCONTEXT name_context
, context
;
865 TRACE( "(%s)\n", debugstr_w(field
) );
867 if (SetupFindFirstLineW( hinf
, field
, L
"Name", &name_context
))
869 SetupGetIntField( &name_context
, 2, &attrs
);
870 if (attrs
& ~FLG_PROFITEM_GROUP
) FIXME( "unhandled attributes: %x\n", attrs
);
874 /* calculate filename */
875 SHGetFolderPathW( NULL
, CSIDL_COMMON_PROGRAMS
, NULL
, SHGFP_TYPE_CURRENT
, lnkpath
);
876 lnkpath_end
= lnkpath
+ lstrlenW(lnkpath
);
877 if (lnkpath_end
[-1] != '\\') *lnkpath_end
++ = '\\';
879 if (!(attrs
& FLG_PROFITEM_GROUP
) && SetupFindFirstLineW( hinf
, field
, L
"SubDir", &context
))
883 if (!SetupGetStringFieldW( &context
, 1, lnkpath_end
, (lnkpath
+MAX_PATH
)-lnkpath_end
, &subdir_size
))
886 lnkpath_end
+= subdir_size
- 1;
887 if (lnkpath_end
[-1] != '\\') *lnkpath_end
++ = '\\';
890 if (!SetupGetStringFieldW( &name_context
, 1, lnkpath_end
, (lnkpath
+MAX_PATH
)-lnkpath_end
, &name_size
))
893 lnkpath_end
+= name_size
- 1;
895 if (attrs
& FLG_PROFITEM_GROUP
)
897 SHPathPrepareForWriteW( NULL
, NULL
, lnkpath
, SHPPFW_DIRCREATE
);
901 IShellLinkW
* shelllink
=NULL
;
902 IPersistFile
* persistfile
=NULL
;
903 HRESULT initresult
=E_FAIL
;
905 if (lnkpath
+MAX_PATH
< lnkpath_end
+ 5) return TRUE
;
906 lstrcpyW( lnkpath_end
, L
".lnk" );
908 TRACE( "link path: %s\n", debugstr_w(lnkpath
) );
910 /* calculate command line */
911 if (SetupFindFirstLineW( hinf
, field
, L
"CmdLine", &context
))
913 unsigned int dir_len
=0;
914 DWORD subdir_size
=0, filename_size
=0;
919 SetupGetIntField( &context
, 1, &dirid
);
920 dir
= DIRID_get_string( dirid
);
922 if (dir
) dir_len
= lstrlenW(dir
);
924 SetupGetStringFieldW( &context
, 2, NULL
, 0, &subdir_size
);
925 SetupGetStringFieldW( &context
, 3, NULL
, 0, &filename_size
);
927 if (dir_len
&& filename_size
)
929 cmdline
= cmdline_end
= malloc( sizeof(WCHAR
) * (dir_len
+ subdir_size
+ filename_size
+ 1) );
931 lstrcpyW( cmdline_end
, dir
);
932 cmdline_end
+= dir_len
;
933 if (cmdline_end
[-1] != '\\') *cmdline_end
++ = '\\';
937 SetupGetStringFieldW( &context
, 2, cmdline_end
, subdir_size
, NULL
);
938 cmdline_end
+= subdir_size
-1;
939 if (cmdline_end
[-1] != '\\') *cmdline_end
++ = '\\';
941 SetupGetStringFieldW( &context
, 3, cmdline_end
, filename_size
, NULL
);
942 TRACE( "cmdline: %s\n", debugstr_w(cmdline
));
946 if (!cmdline
) return TRUE
;
948 initresult
= CoInitialize(NULL
);
950 if (FAILED(CoCreateInstance( &CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
951 &IID_IShellLinkW
, (LPVOID
*)&shelllink
)))
954 IShellLinkW_SetPath( shelllink
, cmdline
);
955 SHPathPrepareForWriteW( NULL
, NULL
, lnkpath
, SHPPFW_DIRCREATE
|SHPPFW_IGNOREFILENAME
);
956 if (SUCCEEDED(IShellLinkW_QueryInterface( shelllink
, &IID_IPersistFile
, (LPVOID
*)&persistfile
)))
958 TRACE( "writing link: %s\n", debugstr_w(lnkpath
) );
959 IPersistFile_Save( persistfile
, lnkpath
, FALSE
);
960 IPersistFile_Release( persistfile
);
962 IShellLinkW_Release( shelllink
);
965 if (SUCCEEDED(initresult
)) CoUninitialize();
972 static BOOL
copy_inf_callback( HINF hinf
, PCWSTR field
, void *arg
)
974 FIXME( "should do copy inf %s\n", debugstr_w(field
) );
979 /***********************************************************************
980 * iterate_section_fields
982 * Iterate over all fields of a certain key of a certain section
984 static BOOL
iterate_section_fields( HINF hinf
, PCWSTR section
, PCWSTR key
,
985 iterate_fields_func callback
, void *arg
)
987 WCHAR static_buffer
[200];
988 WCHAR
*buffer
= static_buffer
;
989 DWORD size
= ARRAY_SIZE( static_buffer
);
993 BOOL ok
= SetupFindFirstLineW( hinf
, section
, key
, &context
);
996 UINT i
, count
= SetupGetFieldCount( &context
);
997 for (i
= 1; i
<= count
; i
++)
999 if (!(buffer
= get_field_string( &context
, i
, buffer
, static_buffer
, &size
)))
1001 if (!callback( hinf
, buffer
, arg
))
1003 WARN("callback failed for %s %s err %ld\n",
1004 debugstr_w(section
), debugstr_w(buffer
), GetLastError() );
1008 ok
= SetupFindNextMatchLineW( &context
, key
, &context
);
1012 if (buffer
!= static_buffer
) free( buffer
);
1017 /***********************************************************************
1018 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
1020 BOOL WINAPI
SetupInstallFilesFromInfSectionA( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1021 PCSTR section
, PCSTR src_root
, UINT flags
)
1023 UNICODE_STRING sectionW
;
1026 if (!RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1028 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1032 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1036 UNICODE_STRING srcW
;
1037 if (RtlCreateUnicodeStringFromAsciiz( &srcW
, src_root
))
1039 ret
= SetupInstallFilesFromInfSectionW( hinf
, hlayout
, queue
, sectionW
.Buffer
,
1040 srcW
.Buffer
, flags
);
1041 RtlFreeUnicodeString( &srcW
);
1043 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1045 RtlFreeUnicodeString( §ionW
);
1050 /***********************************************************************
1051 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
1053 BOOL WINAPI
SetupInstallFilesFromInfSectionW( HINF hinf
, HINF hlayout
, HSPFILEQ queue
,
1054 PCWSTR section
, PCWSTR src_root
, UINT flags
)
1056 struct files_callback_info info
;
1059 info
.src_root
= src_root
;
1060 info
.copy_flags
= flags
;
1061 info
.layout
= hlayout
;
1062 return iterate_section_fields( hinf
, section
, L
"CopyFiles", copy_files_callback
, &info
) &&
1063 iterate_section_fields( hinf
, section
, L
"DelFiles", delete_files_callback
, &info
) &&
1064 iterate_section_fields( hinf
, section
, L
"RenFiles", rename_files_callback
, &info
);
1068 /***********************************************************************
1069 * SetupInstallFromInfSectionA (SETUPAPI.@)
1071 BOOL WINAPI
SetupInstallFromInfSectionA( HWND owner
, HINF hinf
, PCSTR section
, UINT flags
,
1072 HKEY key_root
, PCSTR src_root
, UINT copy_flags
,
1073 PSP_FILE_CALLBACK_A callback
, PVOID context
,
1074 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1076 UNICODE_STRING sectionW
, src_rootW
;
1077 struct callback_WtoA_context ctx
;
1080 src_rootW
.Buffer
= NULL
;
1081 if (src_root
&& !RtlCreateUnicodeStringFromAsciiz( &src_rootW
, src_root
))
1083 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1087 if (RtlCreateUnicodeStringFromAsciiz( §ionW
, section
))
1089 ctx
.orig_context
= context
;
1090 ctx
.orig_handler
= callback
;
1091 ret
= SetupInstallFromInfSectionW( owner
, hinf
, sectionW
.Buffer
, flags
, key_root
,
1092 src_rootW
.Buffer
, copy_flags
, QUEUE_callback_WtoA
,
1093 &ctx
, devinfo
, devinfo_data
);
1094 RtlFreeUnicodeString( §ionW
);
1096 else SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1098 RtlFreeUnicodeString( &src_rootW
);
1103 /***********************************************************************
1104 * SetupInstallFromInfSectionW (SETUPAPI.@)
1106 BOOL WINAPI
SetupInstallFromInfSectionW( HWND owner
, HINF hinf
, PCWSTR section
, UINT flags
,
1107 HKEY key_root
, PCWSTR src_root
, UINT copy_flags
,
1108 PSP_FILE_CALLBACK_W callback
, PVOID context
,
1109 HDEVINFO devinfo
, PSP_DEVINFO_DATA devinfo_data
)
1114 if (flags
& SPINST_REGSVR
)
1116 if (iterate_section_fields( hinf
, section
, L
"WineFakeDlls", fake_dlls_callback
, NULL
))
1117 cleanup_fake_dlls();
1121 if (flags
& SPINST_FILES
)
1125 if (!(queue
= SetupOpenFileQueue())) return FALSE
;
1126 ret
= (SetupInstallFilesFromInfSectionW( hinf
, NULL
, queue
, section
, src_root
, copy_flags
) &&
1127 SetupCommitFileQueueW( owner
, queue
, callback
, context
));
1128 SetupCloseFileQueue( queue
);
1129 if (!ret
) return FALSE
;
1131 if (flags
& SPINST_INIFILES
)
1133 if (!iterate_section_fields( hinf
, section
, L
"UpdateInis", update_ini_callback
, NULL
) ||
1134 !iterate_section_fields( hinf
, section
, L
"UpdateIniFields",
1135 update_ini_fields_callback
, NULL
))
1138 if (flags
& SPINST_INI2REG
)
1140 if (!iterate_section_fields( hinf
, section
, L
"Ini2Reg", ini2reg_callback
, NULL
))
1143 if (flags
& SPINST_LOGCONFIG
)
1145 if (!iterate_section_fields( hinf
, section
, L
"LogConf", logconf_callback
, NULL
))
1148 if (flags
& SPINST_REGSVR
)
1150 struct register_dll_info info
= { .unregister
= FALSE
};
1153 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1155 info
.callback
= callback
;
1156 info
.callback_context
= context
;
1159 hr
= CoInitialize(NULL
);
1161 ret
= iterate_section_fields( hinf
, section
, L
"RegisterDlls", register_dlls_callback
, &info
);
1162 for (i
= 0; i
< info
.modules_count
; i
++) FreeLibrary( info
.modules
[i
] );
1167 free( info
.modules
);
1168 if (!ret
) return FALSE
;
1170 if (flags
& SPINST_UNREGSVR
)
1172 struct register_dll_info info
= { .unregister
= TRUE
};
1175 if (flags
& SPINST_REGISTERCALLBACKAWARE
)
1177 info
.callback
= callback
;
1178 info
.callback_context
= context
;
1181 hr
= CoInitialize(NULL
);
1183 ret
= iterate_section_fields( hinf
, section
, L
"UnregisterDlls", register_dlls_callback
, &info
);
1184 for (i
= 0; i
< info
.modules_count
; i
++) FreeLibrary( info
.modules
[i
] );
1189 free( info
.modules
);
1190 if (!ret
) return FALSE
;
1192 if (flags
& SPINST_REGISTRY
)
1194 struct registry_callback_info info
;
1196 info
.default_root
= key_root
;
1198 if (!iterate_section_fields( hinf
, section
, L
"DelReg", registry_callback
, &info
))
1200 info
.delete = FALSE
;
1201 if (!iterate_section_fields( hinf
, section
, L
"AddReg", registry_callback
, &info
))
1204 if (flags
& SPINST_BITREG
)
1206 if (!iterate_section_fields( hinf
, section
, L
"BitReg", bitreg_callback
, NULL
))
1209 if (flags
& SPINST_PROFILEITEMS
)
1211 if (!iterate_section_fields( hinf
, section
, L
"ProfileItems", profile_items_callback
, NULL
))
1214 if (flags
& SPINST_COPYINF
)
1216 if (!iterate_section_fields( hinf
, section
, L
"CopyINF", copy_inf_callback
, NULL
))
1220 SetLastError(ERROR_SUCCESS
);
1225 /***********************************************************************
1226 * InstallHinfSectionW (SETUPAPI.@)
1228 * NOTE: 'cmdline' is <section> <mode> <path> from
1229 * RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection <section> <mode> <path>
1231 void WINAPI
InstallHinfSectionW( HWND hwnd
, HINSTANCE handle
, LPCWSTR cmdline
, INT show
)
1234 static const WCHAR nt_platformW
[] = L
".ntx86";
1235 #elif defined(__x86_64__)
1236 static const WCHAR nt_platformW
[] = L
".ntamd64";
1237 #elif defined(__arm__)
1238 static const WCHAR nt_platformW
[] = L
".ntarm";
1239 #elif defined(__aarch64__)
1240 static const WCHAR nt_platformW
[] = L
".ntarm64";
1241 #else /* FIXME: other platforms */
1242 static const WCHAR nt_platformW
[] = L
".nt";
1245 WCHAR
*s
, *path
, section
[MAX_PATH
+ ARRAY_SIZE( nt_platformW
) + ARRAY_SIZE( L
".Services" )];
1246 void *callback_context
;
1250 TRACE("hwnd %p, handle %p, cmdline %s\n", hwnd
, handle
, debugstr_w(cmdline
));
1252 lstrcpynW( section
, cmdline
, MAX_PATH
);
1254 if (!(s
= wcschr( section
, ' ' ))) return;
1256 while (*s
== ' ') s
++;
1257 mode
= wcstol( s
, NULL
, 10 );
1259 /* quoted paths are not allowed on native, the rest of the command line is taken as the path */
1260 if (!(s
= wcschr( s
, ' ' ))) return;
1261 while (*s
== ' ') s
++;
1264 hinf
= SetupOpenInfFileW( path
, NULL
, INF_STYLE_WIN4
, NULL
);
1265 if (hinf
== INVALID_HANDLE_VALUE
) return;
1267 if (!(GetVersion() & 0x80000000))
1271 /* check for <section>.ntx86 (or corresponding name for the current platform)
1272 * and then <section>.nt */
1273 s
= section
+ lstrlenW(section
);
1274 lstrcpyW( s
, nt_platformW
);
1275 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
)))
1277 lstrcpyW( s
, L
".nt" );
1278 if (!(SetupFindFirstLineW( hinf
, section
, NULL
, &context
))) *s
= 0;
1280 if (*s
) TRACE( "using section %s instead\n", debugstr_w(section
) );
1283 callback_context
= SetupInitDefaultQueueCallback( hwnd
);
1284 SetupInstallFromInfSectionW( hwnd
, hinf
, section
, SPINST_ALL
, NULL
, NULL
, SP_COPY_NEWER
,
1285 SetupDefaultQueueCallbackW
, callback_context
,
1287 SetupTermDefaultQueueCallback( callback_context
);
1288 lstrcatW( section
, L
".Services" );
1289 SetupInstallServicesFromInfSectionW( hinf
, section
, 0 );
1290 SetupCloseInfFile( hinf
);
1292 /* FIXME: should check the mode and maybe reboot */
1293 /* there isn't much point in doing that since we */
1294 /* don't yet handle deferred file copies anyway. */
1295 if (mode
& 7) TRACE( "should consider reboot, mode %u\n", mode
);
1299 /***********************************************************************
1300 * InstallHinfSectionA (SETUPAPI.@)
1302 void WINAPI
InstallHinfSectionA( HWND hwnd
, HINSTANCE handle
, LPCSTR cmdline
, INT show
)
1304 UNICODE_STRING cmdlineW
;
1306 if (RtlCreateUnicodeStringFromAsciiz( &cmdlineW
, cmdline
))
1308 InstallHinfSectionW( hwnd
, handle
, cmdlineW
.Buffer
, show
);
1309 RtlFreeUnicodeString( &cmdlineW
);
1314 /***********************************************************************
1317 * Create a new service. Helper for SetupInstallServicesFromInfSectionW.
1319 static BOOL
add_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, const WCHAR
*section
, DWORD flags
)
1321 struct registry_callback_info info
;
1324 SERVICE_DESCRIPTIONW descr
;
1325 WCHAR
*display_name
, *start_name
, *load_order
, *binary_path
;
1326 INT service_type
= 0, start_type
= 0, error_control
= 0;
1330 /* first the mandatory fields */
1332 if (!SetupFindFirstLineW( hinf
, section
, L
"ServiceType", &context
) ||
1333 !SetupGetIntField( &context
, 1, &service_type
))
1335 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1338 if (!SetupFindFirstLineW( hinf
, section
, L
"StartType", &context
) ||
1339 !SetupGetIntField( &context
, 1, &start_type
))
1341 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1344 if (!SetupFindFirstLineW( hinf
, section
, L
"ErrorControl", &context
) ||
1345 !SetupGetIntField( &context
, 1, &error_control
))
1347 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1350 if (!(binary_path
= dup_section_line_field( hinf
, section
, L
"ServiceBinary", 1 )))
1352 SetLastError( ERROR_BAD_SERVICE_INSTALLSECT
);
1356 /* now the optional fields */
1358 display_name
= dup_section_line_field( hinf
, section
, L
"DisplayName", 1 );
1359 start_name
= dup_section_line_field( hinf
, section
, L
"StartName", 1 );
1360 load_order
= dup_section_line_field( hinf
, section
, L
"LoadOrderGroup", 1 );
1361 descr
.lpDescription
= dup_section_line_field( hinf
, section
, L
"Description", 1 );
1363 /* FIXME: Dependencies field */
1364 /* FIXME: Security field */
1366 TRACE( "service %s display %s type %x start %x error %x binary %s order %s startname %s flags %lx\n",
1367 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1368 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
), flags
);
1370 service
= CreateServiceW( scm
, name
, display_name
, SERVICE_ALL_ACCESS
,
1371 service_type
, start_type
, error_control
, binary_path
,
1372 load_order
, NULL
, NULL
, start_name
, NULL
);
1375 if (descr
.lpDescription
) ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1379 if (GetLastError() != ERROR_SERVICE_EXISTS
) goto done
;
1380 service
= OpenServiceW( scm
, name
, SERVICE_QUERY_CONFIG
|SERVICE_CHANGE_CONFIG
|SERVICE_START
);
1381 if (!service
) goto done
;
1383 if (flags
& (SPSVCINST_NOCLOBBER_DISPLAYNAME
| SPSVCINST_NOCLOBBER_STARTTYPE
|
1384 SPSVCINST_NOCLOBBER_ERRORCONTROL
| SPSVCINST_NOCLOBBER_LOADORDERGROUP
))
1386 QUERY_SERVICE_CONFIGW
*config
= NULL
;
1388 if (!QueryServiceConfigW( service
, NULL
, 0, &size
) &&
1389 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1390 config
= malloc( size
);
1391 if (config
&& QueryServiceConfigW( service
, config
, size
, &size
))
1393 if (flags
& SPSVCINST_NOCLOBBER_STARTTYPE
) start_type
= config
->dwStartType
;
1394 if (flags
& SPSVCINST_NOCLOBBER_ERRORCONTROL
) error_control
= config
->dwErrorControl
;
1395 if (flags
& SPSVCINST_NOCLOBBER_DISPLAYNAME
)
1397 free( display_name
);
1398 display_name
= wcsdup( config
->lpDisplayName
);
1400 if (flags
& SPSVCINST_NOCLOBBER_LOADORDERGROUP
)
1403 load_order
= wcsdup( config
->lpLoadOrderGroup
);
1408 TRACE( "changing %s display %s type %x start %x error %x binary %s loadorder %s startname %s\n",
1409 debugstr_w(name
), debugstr_w(display_name
), service_type
, start_type
, error_control
,
1410 debugstr_w(binary_path
), debugstr_w(load_order
), debugstr_w(start_name
) );
1412 ChangeServiceConfigW( service
, service_type
, start_type
, error_control
, binary_path
,
1413 load_order
, NULL
, NULL
, start_name
, NULL
, display_name
);
1415 if (!(flags
& SPSVCINST_NOCLOBBER_DESCRIPTION
))
1416 ChangeServiceConfig2W( service
, SERVICE_CONFIG_DESCRIPTION
, &descr
);
1419 /* execute the AddReg, DelReg and BitReg entries */
1421 info
.default_root
= 0;
1422 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, L
"System\\CurrentControlSet\\Services", &hkey
))
1424 RegOpenKeyW( hkey
, name
, &info
.default_root
);
1425 RegCloseKey( hkey
);
1427 if (info
.default_root
)
1430 iterate_section_fields( hinf
, section
, L
"DelReg", registry_callback
, &info
);
1431 info
.delete = FALSE
;
1432 iterate_section_fields( hinf
, section
, L
"AddReg", registry_callback
, &info
);
1433 RegCloseKey( info
.default_root
);
1435 iterate_section_fields( hinf
, section
, L
"BitReg", bitreg_callback
, NULL
);
1437 if (flags
& SPSVCINST_STARTSERVICE
) StartServiceW( service
, 0, NULL
);
1438 CloseServiceHandle( service
);
1441 if (!service
) WARN( "failed err %lu\n", GetLastError() );
1442 free( binary_path
);
1443 free( display_name
);
1446 free( descr
.lpDescription
);
1447 return service
!= 0;
1451 /***********************************************************************
1454 * Delete service. Helper for SetupInstallServicesFromInfSectionW.
1456 static BOOL
del_service( SC_HANDLE scm
, HINF hinf
, const WCHAR
*name
, DWORD flags
)
1460 SERVICE_STATUS status
;
1462 if (!(service
= OpenServiceW( scm
, name
, SERVICE_STOP
| DELETE
)))
1464 if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST
) return TRUE
;
1465 WARN( "cannot open %s err %lu\n", debugstr_w(name
), GetLastError() );
1468 if (flags
& SPSVCINST_STOPSERVICE
) ControlService( service
, SERVICE_CONTROL_STOP
, &status
);
1469 TRACE( "deleting %s\n", debugstr_w(name
) );
1470 ret
= DeleteService( service
);
1471 CloseServiceHandle( service
);
1476 /***********************************************************************
1477 * SetupInstallServicesFromInfSectionW (SETUPAPI.@)
1479 BOOL WINAPI
SetupInstallServicesFromInfSectionW( HINF hinf
, PCWSTR section
, DWORD flags
)
1481 WCHAR service_name
[MAX_INF_STRING_LENGTH
];
1482 WCHAR service_section
[MAX_INF_STRING_LENGTH
];
1488 if (!SetupFindFirstLineW( hinf
, section
, NULL
, &context
))
1490 SetLastError( ERROR_SECTION_NOT_FOUND
);
1493 if (!(scm
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_ALL_ACCESS
))) return FALSE
;
1495 if (SetupFindFirstLineW( hinf
, section
, L
"AddService", &context
))
1499 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1501 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1502 if (!SetupGetStringFieldW( &context
, 3, service_section
, MAX_INF_STRING_LENGTH
, NULL
))
1504 if (!(ret
= add_service( scm
, hinf
, service_name
, service_section
, section_flags
| flags
)))
1506 } while (SetupFindNextMatchLineW( &context
, L
"AddService", &context
));
1509 if (SetupFindFirstLineW( hinf
, section
, L
"DelService", &context
))
1513 if (!SetupGetStringFieldW( &context
, 1, service_name
, MAX_INF_STRING_LENGTH
, NULL
))
1515 if (!SetupGetIntField( &context
, 2, §ion_flags
)) section_flags
= 0;
1516 if (!(ret
= del_service( scm
, hinf
, service_name
, section_flags
| flags
))) goto done
;
1517 } while (SetupFindNextMatchLineW( &context
, L
"AddService", &context
));
1519 if (ret
) SetLastError( ERROR_SUCCESS
);
1521 CloseServiceHandle( scm
);
1526 /***********************************************************************
1527 * SetupInstallServicesFromInfSectionA (SETUPAPI.@)
1529 BOOL WINAPI
SetupInstallServicesFromInfSectionA( HINF Inf
, PCSTR SectionName
, DWORD Flags
)
1531 UNICODE_STRING SectionNameW
;
1534 if (RtlCreateUnicodeStringFromAsciiz( &SectionNameW
, SectionName
))
1536 ret
= SetupInstallServicesFromInfSectionW( Inf
, SectionNameW
.Buffer
, Flags
);
1537 RtlFreeUnicodeString( &SectionNameW
);
1540 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1546 /***********************************************************************
1547 * SetupGetInfFileListA (SETUPAPI.@)
1549 BOOL WINAPI
SetupGetInfFileListA(PCSTR dir
, DWORD style
, PSTR buffer
,
1550 DWORD insize
, PDWORD outsize
)
1552 UNICODE_STRING dirW
;
1553 PWSTR bufferW
= NULL
;
1555 DWORD outsizeA
, outsizeW
;
1558 RtlCreateUnicodeStringFromAsciiz( &dirW
, dir
);
1563 bufferW
= malloc( insize
* sizeof( WCHAR
));
1565 ret
= SetupGetInfFileListW( dirW
.Buffer
, style
, bufferW
, insize
, &outsizeW
);
1569 outsizeA
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, outsizeW
,
1570 buffer
, insize
, NULL
, NULL
);
1571 if ( outsize
) *outsize
= outsizeA
;
1575 RtlFreeUnicodeString( &dirW
);
1580 /***********************************************************************
1581 * SetupGetInfFileListW (SETUPAPI.@)
1583 BOOL WINAPI
SetupGetInfFileListW(PCWSTR dir
, DWORD style
, PWSTR buffer
,
1584 DWORD insize
, PDWORD outsize
)
1586 WCHAR
*filter
, *fullname
= NULL
, *ptr
= buffer
;
1587 DWORD dir_len
, name_len
= 20, size
;
1588 WIN32_FIND_DATAW finddata
;
1590 if (style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1591 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1593 FIXME( "unknown inf_style(s) 0x%lx\n",
1594 style
& ~( INF_STYLE_OLDNT
| INF_STYLE_WIN4
|
1595 INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
));
1596 if( outsize
) *outsize
= 1;
1599 if ((style
& ( INF_STYLE_OLDNT
| INF_STYLE_WIN4
)) == INF_STYLE_NONE
)
1601 FIXME( "inf_style INF_STYLE_NONE not handled\n" );
1602 if( outsize
) *outsize
= 1;
1605 if (style
& ( INF_STYLE_CACHE_ENABLE
| INF_STYLE_CACHE_DISABLE
))
1606 FIXME("ignored inf_style(s) %s %s\n",
1607 ( style
& INF_STYLE_CACHE_ENABLE
) ? "INF_STYLE_CACHE_ENABLE" : "",
1608 ( style
& INF_STYLE_CACHE_DISABLE
) ? "INF_STYLE_CACHE_DISABLE" : "");
1613 dir_len
= lstrlenW( dir
);
1614 if ( !dir_len
) return FALSE
;
1615 msize
= ( 7 + dir_len
) * sizeof( WCHAR
); /* \\*.inf\0 */
1616 filter
= malloc( msize
);
1619 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1622 lstrcpyW( filter
, dir
);
1623 if ( '\\' == filter
[dir_len
- 1] )
1624 filter
[--dir_len
] = 0;
1626 att
= GetFileAttributesW( filter
);
1627 if (att
!= INVALID_FILE_ATTRIBUTES
&& !(att
& FILE_ATTRIBUTE_DIRECTORY
))
1630 SetLastError( ERROR_DIRECTORY
);
1637 dir_len
= GetWindowsDirectoryW( NULL
, 0 );
1638 msize
= ( 7 + 4 + dir_len
) * sizeof( WCHAR
);
1639 filter
= malloc( msize
);
1642 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1645 GetWindowsDirectoryW( filter
, msize
);
1646 lstrcatW( filter
, L
"\\inf" );
1648 lstrcatW( filter
, L
"\\*.inf" );
1650 hdl
= FindFirstFileW( filter
, &finddata
);
1651 if ( hdl
== INVALID_HANDLE_VALUE
)
1653 if( outsize
) *outsize
= 1;
1660 WCHAR signature
[ MAX_PATH
];
1662 DWORD len
= lstrlenW( finddata
.cFileName
);
1663 if (!fullname
|| ( name_len
< len
))
1665 name_len
= ( name_len
< len
) ? len
: name_len
;
1667 fullname
= malloc( (2 + dir_len
+ name_len
) * sizeof( WCHAR
) );
1672 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1675 lstrcpyW( fullname
, filter
);
1677 fullname
[ dir_len
+ 1] = 0; /* keep '\\' */
1678 lstrcatW( fullname
, finddata
.cFileName
);
1679 if (!GetPrivateProfileStringW( L
"Version", L
"Signature", NULL
, signature
, MAX_PATH
, fullname
))
1681 if( INF_STYLE_OLDNT
& style
)
1682 valid
= wcsicmp( L
"$Chicago$", signature
) && wcsicmp( L
"$WINDOWS NT$", signature
);
1683 if( INF_STYLE_WIN4
& style
)
1684 valid
= valid
|| !wcsicmp( L
"$Chicago$", signature
) ||
1685 !wcsicmp( L
"$WINDOWS NT$", signature
);
1688 size
+= 1 + lstrlenW( finddata
.cFileName
);
1689 if( ptr
&& insize
>= size
)
1691 lstrcpyW( ptr
, finddata
.cFileName
);
1692 ptr
+= 1 + lstrlenW( finddata
.cFileName
);
1697 while( FindNextFileW( hdl
, &finddata
));
1702 if( outsize
) *outsize
= size
;