rpcrt4: Don't try to bind to a null handle.
[wine] / dlls / msi / install.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /* Msi top level apis directly related to installs */
22
23 #define COBJMACROS
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msidefs.h"
33 #include "objbase.h"
34 #include "oleauto.h"
35
36 #include "msipriv.h"
37 #include "msiserver.h"
38 #include "wine/unicode.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42 /***********************************************************************
43  * MsiDoActionA       (MSI.@)
44  */
45 UINT WINAPI MsiDoActionA( MSIHANDLE hInstall, LPCSTR szAction )
46 {
47     LPWSTR szwAction;
48     UINT ret;
49
50     TRACE("%s\n", debugstr_a(szAction));
51
52     szwAction = strdupAtoW(szAction);
53     if (szAction && !szwAction)
54         return ERROR_FUNCTION_FAILED; 
55
56     ret = MsiDoActionW( hInstall, szwAction );
57     msi_free( szwAction );
58     return ret;
59 }
60
61 /***********************************************************************
62  * MsiDoActionW       (MSI.@)
63  */
64 UINT WINAPI MsiDoActionW( MSIHANDLE hInstall, LPCWSTR szAction )
65 {
66     MSIPACKAGE *package;
67     UINT ret;
68
69     TRACE("%s\n",debugstr_w(szAction));
70
71     if (!szAction)
72         return ERROR_INVALID_PARAMETER;
73
74     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
75     if (!package)
76     {
77         HRESULT hr;
78         BSTR action;
79         IWineMsiRemotePackage *remote_package;
80
81         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
82         if (!remote_package)
83             return ERROR_INVALID_HANDLE;
84
85         action = SysAllocString( szAction );
86         if (!action)
87         {
88             IWineMsiRemotePackage_Release( remote_package );
89             return ERROR_OUTOFMEMORY;
90         }
91
92         hr = IWineMsiRemotePackage_DoAction( remote_package, action );
93
94         SysFreeString( action );
95         IWineMsiRemotePackage_Release( remote_package );
96
97         if (FAILED(hr))
98         {
99             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
100                 return HRESULT_CODE(hr);
101
102             return ERROR_FUNCTION_FAILED;
103         }
104
105         return ERROR_SUCCESS;
106     }
107  
108     ret = ACTION_PerformUIAction( package, szAction, -1 );
109     msiobj_release( &package->hdr );
110
111     return ret;
112 }
113
114 /***********************************************************************
115  * MsiSequenceA       (MSI.@)
116  */
117 UINT WINAPI MsiSequenceA( MSIHANDLE hInstall, LPCSTR szTable, INT iSequenceMode )
118 {
119     LPWSTR szwTable;
120     UINT ret;
121
122     TRACE("%s\n", debugstr_a(szTable));
123
124     szwTable = strdupAtoW(szTable);
125     if (szTable && !szwTable)
126         return ERROR_FUNCTION_FAILED; 
127
128     ret = MsiSequenceW( hInstall, szwTable, iSequenceMode );
129     msi_free( szwTable );
130     return ret;
131 }
132
133 /***********************************************************************
134  * MsiSequenceW       (MSI.@)
135  */
136 UINT WINAPI MsiSequenceW( MSIHANDLE hInstall, LPCWSTR szTable, INT iSequenceMode )
137 {
138     MSIPACKAGE *package;
139     UINT ret;
140
141     TRACE("%s\n", debugstr_w(szTable));
142
143     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
144     if (!package)
145     {
146         HRESULT hr;
147         BSTR table;
148         IWineMsiRemotePackage *remote_package;
149
150         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
151         if (!remote_package)
152             return ERROR_INVALID_HANDLE;
153
154         table = SysAllocString( szTable );
155         if (!table)
156         {
157             IWineMsiRemotePackage_Release( remote_package );
158             return ERROR_OUTOFMEMORY;
159         }
160
161         hr = IWineMsiRemotePackage_Sequence( remote_package, table, iSequenceMode );
162
163         SysFreeString( table );
164         IWineMsiRemotePackage_Release( remote_package );
165
166         if (FAILED(hr))
167         {
168             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
169                 return HRESULT_CODE(hr);
170
171             return ERROR_FUNCTION_FAILED;
172         }
173
174         return ERROR_SUCCESS;
175     }
176
177     ret = MSI_Sequence( package, szTable, iSequenceMode );
178     msiobj_release( &package->hdr );
179  
180     return ret;
181 }
182
183 UINT msi_strcpy_to_awstring( LPCWSTR str, awstring *awbuf, DWORD *sz )
184 {
185     UINT len, r = ERROR_SUCCESS;
186
187     if (awbuf->str.w && !sz )
188         return ERROR_INVALID_PARAMETER;
189
190     if (!sz)
191         return r;
192  
193     if (awbuf->unicode)
194     {
195         len = lstrlenW( str );
196         if (awbuf->str.w) 
197             lstrcpynW( awbuf->str.w, str, *sz );
198     }
199     else
200     {
201         len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
202         if (len)
203             len--;
204         WideCharToMultiByte( CP_ACP, 0, str, -1, awbuf->str.a, *sz, NULL, NULL );
205         if ( awbuf->str.a && *sz && (len >= *sz) )
206             awbuf->str.a[*sz - 1] = 0;
207     }
208
209     if (awbuf->str.w && len >= *sz)
210         r = ERROR_MORE_DATA;
211     *sz = len;
212     return r;
213 }
214
215 const WCHAR *msi_get_target_folder( MSIPACKAGE *package, const WCHAR *name )
216 {
217     MSIFOLDER *folder = msi_get_loaded_folder( package, name );
218     if (folder) return folder->ResolvedTarget;
219     return NULL;
220 }
221
222 /***********************************************************************
223  * MsiGetTargetPath   (internal)
224  */
225 static UINT MSI_GetTargetPath( MSIHANDLE hInstall, LPCWSTR szFolder,
226                                awstring *szPathBuf, LPDWORD pcchPathBuf )
227 {
228     MSIPACKAGE *package;
229     const WCHAR *path;
230     UINT r = ERROR_FUNCTION_FAILED;
231
232     if (!szFolder)
233         return ERROR_INVALID_PARAMETER;
234
235     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
236     if (!package)
237     {
238         HRESULT hr;
239         IWineMsiRemotePackage *remote_package;
240         LPWSTR value = NULL;
241         BSTR folder;
242         DWORD len;
243
244         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
245         if (!remote_package)
246             return ERROR_INVALID_HANDLE;
247
248         folder = SysAllocString( szFolder );
249         if (!folder)
250         {
251             IWineMsiRemotePackage_Release( remote_package );
252             return ERROR_OUTOFMEMORY;
253         }
254
255         len = 0;
256         hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder, NULL, &len );
257         if (FAILED(hr))
258             goto done;
259
260         len++;
261         value = msi_alloc(len * sizeof(WCHAR));
262         if (!value)
263         {
264             r = ERROR_OUTOFMEMORY;
265             goto done;
266         }
267
268         hr = IWineMsiRemotePackage_GetTargetPath( remote_package, folder, value, &len );
269         if (FAILED(hr))
270             goto done;
271
272         r = msi_strcpy_to_awstring( value, szPathBuf, pcchPathBuf );
273
274 done:
275         IWineMsiRemotePackage_Release( remote_package );
276         SysFreeString( folder );
277         msi_free( value );
278
279         if (FAILED(hr))
280         {
281             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
282                 return HRESULT_CODE(hr);
283
284             return ERROR_FUNCTION_FAILED;
285         }
286
287         return r;
288     }
289
290     path = msi_get_target_folder( package, szFolder );
291     msiobj_release( &package->hdr );
292
293     if (!path)
294         return ERROR_DIRECTORY;
295
296     r = msi_strcpy_to_awstring( path, szPathBuf, pcchPathBuf );
297     return r;
298 }
299
300 /***********************************************************************
301  * MsiGetTargetPathA        (MSI.@)
302  */
303 UINT WINAPI MsiGetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder, 
304                                LPSTR szPathBuf, LPDWORD pcchPathBuf )
305 {
306     LPWSTR szwFolder;
307     awstring path;
308     UINT r;
309
310     TRACE("%s %p %p\n", debugstr_a(szFolder), szPathBuf, pcchPathBuf);
311
312     szwFolder = strdupAtoW(szFolder);
313     if (szFolder && !szwFolder)
314         return ERROR_FUNCTION_FAILED; 
315
316     path.unicode = FALSE;
317     path.str.a = szPathBuf;
318
319     r = MSI_GetTargetPath( hInstall, szwFolder, &path, pcchPathBuf );
320
321     msi_free( szwFolder );
322
323     return r;
324 }
325
326 /***********************************************************************
327  * MsiGetTargetPathW        (MSI.@)
328  */
329 UINT WINAPI MsiGetTargetPathW( MSIHANDLE hInstall, LPCWSTR szFolder,
330                                LPWSTR szPathBuf, LPDWORD pcchPathBuf )
331 {
332     awstring path;
333
334     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf);
335
336     path.unicode = TRUE;
337     path.str.w = szPathBuf;
338
339     return MSI_GetTargetPath( hInstall, szFolder, &path, pcchPathBuf );
340 }
341
342 static WCHAR *get_source_root( MSIDATABASE *db )
343 {
344     WCHAR *path, *p;
345
346     if ((path = msi_dup_property( db, szSourceDir ))) return path;
347     if ((path = msi_dup_property( db, szDatabase )))
348     {
349         if ((p = strrchrW( path, '\\' ))) p[1] = 0;
350     }
351     return path;
352 }
353
354 WCHAR *msi_resolve_source_folder( MSIPACKAGE *package, const WCHAR *name, MSIFOLDER **folder )
355 {
356     MSIFOLDER *f;
357     LPWSTR p, path = NULL, parent;
358
359     TRACE("working to resolve %s\n", debugstr_w(name));
360
361     if (!strcmpW( name, szSourceDir )) name = szTargetDir;
362     if (!(f = msi_get_loaded_folder( package, name ))) return NULL;
363
364     /* special resolving for root dir */
365     if (!strcmpW( name, szTargetDir ) && !f->ResolvedSource)
366     {
367         f->ResolvedSource = get_source_root( package->db );
368     }
369     if (folder) *folder = f;
370     if (f->ResolvedSource)
371     {
372         path = strdupW( f->ResolvedSource );
373         TRACE("   already resolved to %s\n", debugstr_w(path));
374         return path;
375     }
376     if (!f->Parent) return path;
377     parent = f->Parent;
378     TRACE(" ! parent is %s\n", debugstr_w(parent));
379
380     p = msi_resolve_source_folder( package, parent, NULL );
381
382     if (package->WordCount & msidbSumInfoSourceTypeCompressed)
383         path = get_source_root( package->db );
384     else if (package->WordCount & msidbSumInfoSourceTypeSFN)
385         path = msi_build_directory_name( 3, p, f->SourceShortPath, NULL );
386     else
387         path = msi_build_directory_name( 3, p, f->SourceLongPath, NULL );
388
389     TRACE("-> %s\n", debugstr_w(path));
390     f->ResolvedSource = strdupW( path );
391     msi_free( p );
392
393     return path;
394 }
395
396 /***********************************************************************
397  * MSI_GetSourcePath   (internal)
398  */
399 static UINT MSI_GetSourcePath( MSIHANDLE hInstall, LPCWSTR szFolder,
400                                awstring *szPathBuf, LPDWORD pcchPathBuf )
401 {
402     MSIPACKAGE *package;
403     LPWSTR path;
404     UINT r = ERROR_FUNCTION_FAILED;
405
406     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );
407
408     if (!szFolder)
409         return ERROR_INVALID_PARAMETER;
410
411     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
412     if (!package)
413     {
414         HRESULT hr;
415         IWineMsiRemotePackage *remote_package;
416         LPWSTR value = NULL;
417         BSTR folder;
418         DWORD len;
419
420         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
421         if (!remote_package)
422             return ERROR_INVALID_HANDLE;
423
424         folder = SysAllocString( szFolder );
425         if (!folder)
426         {
427             IWineMsiRemotePackage_Release( remote_package );
428             return ERROR_OUTOFMEMORY;
429         }
430
431         len = 0;
432         hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder, NULL, &len );
433         if (FAILED(hr))
434             goto done;
435
436         len++;
437         value = msi_alloc(len * sizeof(WCHAR));
438         if (!value)
439         {
440             r = ERROR_OUTOFMEMORY;
441             goto done;
442         }
443
444         hr = IWineMsiRemotePackage_GetSourcePath( remote_package, folder, value, &len );
445         if (FAILED(hr))
446             goto done;
447
448         r = msi_strcpy_to_awstring( value, szPathBuf, pcchPathBuf );
449
450 done:
451         IWineMsiRemotePackage_Release( remote_package );
452         SysFreeString( folder );
453         msi_free( value );
454
455         if (FAILED(hr))
456         {
457             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
458                 return HRESULT_CODE(hr);
459
460             return ERROR_FUNCTION_FAILED;
461         }
462
463         return r;
464     }
465
466     if (szPathBuf->str.w && !pcchPathBuf )
467     {
468         msiobj_release( &package->hdr );
469         return ERROR_INVALID_PARAMETER;
470     }
471
472     path = msi_resolve_source_folder( package, szFolder, NULL );
473     msiobj_release( &package->hdr );
474
475     TRACE("path = %s\n", debugstr_w(path));
476     if (!path)
477         return ERROR_DIRECTORY;
478
479     r = msi_strcpy_to_awstring( path, szPathBuf, pcchPathBuf );
480     msi_free( path );
481     return r;
482 }
483
484 /***********************************************************************
485  * MsiGetSourcePathA     (MSI.@)
486  */
487 UINT WINAPI MsiGetSourcePathA( MSIHANDLE hInstall, LPCSTR szFolder, 
488                                LPSTR szPathBuf, LPDWORD pcchPathBuf )
489 {
490     LPWSTR folder;
491     awstring str;
492     UINT r;
493
494     TRACE("%s %p %p\n", szFolder, debugstr_a(szPathBuf), pcchPathBuf);
495
496     str.unicode = FALSE;
497     str.str.a = szPathBuf;
498
499     folder = strdupAtoW( szFolder );
500     r = MSI_GetSourcePath( hInstall, folder, &str, pcchPathBuf );
501     msi_free( folder );
502
503     return r;
504 }
505
506 /***********************************************************************
507  * MsiGetSourcePathW     (MSI.@)
508  */
509 UINT WINAPI MsiGetSourcePathW( MSIHANDLE hInstall, LPCWSTR szFolder,
510                                LPWSTR szPathBuf, LPDWORD pcchPathBuf )
511 {
512     awstring str;
513
514     TRACE("%s %p %p\n", debugstr_w(szFolder), szPathBuf, pcchPathBuf );
515
516     str.unicode = TRUE;
517     str.str.w = szPathBuf;
518
519     return MSI_GetSourcePath( hInstall, szFolder, &str, pcchPathBuf );
520 }
521
522 /***********************************************************************
523  * MsiSetTargetPathA  (MSI.@)
524  */
525 UINT WINAPI MsiSetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder,
526                                LPCSTR szFolderPath )
527 {
528     LPWSTR szwFolder = NULL, szwFolderPath = NULL;
529     UINT rc = ERROR_OUTOFMEMORY;
530
531     if ( !szFolder || !szFolderPath )
532         return ERROR_INVALID_PARAMETER;
533
534     szwFolder = strdupAtoW(szFolder);
535     szwFolderPath = strdupAtoW(szFolderPath);
536     if (!szwFolder || !szwFolderPath)
537         goto end;
538
539     rc = MsiSetTargetPathW( hInstall, szwFolder, szwFolderPath );
540
541 end:
542     msi_free(szwFolder);
543     msi_free(szwFolderPath);
544
545     return rc;
546 }
547
548 static void set_target_path( MSIPACKAGE *package, MSIFOLDER *folder, const WCHAR *path )
549 {
550     FolderList *fl;
551     MSIFOLDER *child;
552     WCHAR *target_path;
553
554     if (!(target_path = strdupW( path ))) return;
555     msi_clean_path( target_path );
556     if (strcmpW( target_path, folder->ResolvedTarget ))
557     {
558         msi_free( folder->ResolvedTarget );
559         folder->ResolvedTarget = target_path;
560         msi_set_property( package->db, folder->Directory, folder->ResolvedTarget );
561
562         LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
563         {
564             child = fl->folder;
565             msi_resolve_target_folder( package, child->Directory, FALSE );
566         }
567     }
568     else msi_free( target_path );
569 }
570
571 UINT MSI_SetTargetPathW( MSIPACKAGE *package, LPCWSTR szFolder, LPCWSTR szFolderPath )
572 {
573     DWORD attrib, len;
574     MSIFOLDER *folder;
575     MSIFILE *file;
576
577     TRACE("%p %s %s\n", package, debugstr_w(szFolder), debugstr_w(szFolderPath));
578
579     attrib = GetFileAttributesW(szFolderPath);
580     /* native MSI tests writeability by making temporary files at each drive */
581     if (attrib != INVALID_FILE_ATTRIBUTES &&
582         (attrib & FILE_ATTRIBUTE_OFFLINE || attrib & FILE_ATTRIBUTE_READONLY))
583     {
584         return ERROR_FUNCTION_FAILED;
585     }
586     if (!(folder = msi_get_loaded_folder( package, szFolder ))) return ERROR_DIRECTORY;
587
588     len = strlenW( szFolderPath );
589     if (len && szFolderPath[len - 1] != '\\')
590     {
591         WCHAR *path = msi_alloc( (len + 2) * sizeof(WCHAR) );
592         memcpy( path, szFolderPath, len * sizeof(WCHAR) );
593         path[len] = '\\';
594         path[len + 1] = 0;
595         set_target_path( package, folder, path );
596         msi_free( path );
597     }
598     else set_target_path( package, folder, szFolderPath );
599
600     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
601     {
602         const WCHAR *dir;
603         MSICOMPONENT *comp = file->Component;
604
605         if (!comp->Enabled || (comp->assembly && !comp->assembly->application)) continue;
606
607         dir = msi_get_target_folder( package, comp->Directory );
608         msi_free( file->TargetPath );
609         file->TargetPath = msi_build_directory_name( 2, dir, file->FileName );
610     }
611     return ERROR_SUCCESS;
612 }
613
614 /***********************************************************************
615  * MsiSetTargetPathW  (MSI.@)
616  */
617 UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
618                              LPCWSTR szFolderPath)
619 {
620     MSIPACKAGE *package;
621     UINT ret;
622
623     TRACE("%s %s\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
624
625     if ( !szFolder || !szFolderPath )
626         return ERROR_INVALID_PARAMETER;
627
628     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
629     if (!package)
630     {
631         HRESULT hr;
632         BSTR folder, path;
633         IWineMsiRemotePackage *remote_package;
634
635         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
636         if (!remote_package)
637             return ERROR_INVALID_HANDLE;
638
639         folder = SysAllocString( szFolder );
640         path = SysAllocString( szFolderPath );
641         if (!folder || !path)
642         {
643             SysFreeString(folder);
644             SysFreeString(path);
645             IWineMsiRemotePackage_Release( remote_package );
646             return ERROR_OUTOFMEMORY;
647         }
648
649         hr = IWineMsiRemotePackage_SetTargetPath( remote_package, folder, path );
650
651         SysFreeString(folder);
652         SysFreeString(path);
653         IWineMsiRemotePackage_Release( remote_package );
654
655         if (FAILED(hr))
656         {
657             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
658                 return HRESULT_CODE(hr);
659
660             return ERROR_FUNCTION_FAILED;
661         }
662
663         return ERROR_SUCCESS;
664     }
665
666     ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
667     msiobj_release( &package->hdr );
668     return ret;
669 }
670
671 /***********************************************************************
672  *           MsiGetMode    (MSI.@)
673  *
674  * Returns an internal installer state (if it is running in a mode iRunMode)
675  *
676  * PARAMS
677  *   hInstall    [I]  Handle to the installation
678  *   hRunMode    [I]  Checking run mode
679  *        MSIRUNMODE_ADMIN             Administrative mode
680  *        MSIRUNMODE_ADVERTISE         Advertisement mode
681  *        MSIRUNMODE_MAINTENANCE       Maintenance mode
682  *        MSIRUNMODE_ROLLBACKENABLED   Rollback is enabled
683  *        MSIRUNMODE_LOGENABLED        Log file is writing
684  *        MSIRUNMODE_OPERATIONS        Operations in progress??
685  *        MSIRUNMODE_REBOOTATEND       We need to reboot after installation completed
686  *        MSIRUNMODE_REBOOTNOW         We need to reboot to continue the installation
687  *        MSIRUNMODE_CABINET           Files from cabinet are installed
688  *        MSIRUNMODE_SOURCESHORTNAMES  Long names in source files is suppressed
689  *        MSIRUNMODE_TARGETSHORTNAMES  Long names in destination files is suppressed
690  *        MSIRUNMODE_RESERVED11        Reserved
691  *        MSIRUNMODE_WINDOWS9X         Running under Windows95/98
692  *        MSIRUNMODE_ZAWENABLED        Demand installation is supported
693  *        MSIRUNMODE_RESERVED14        Reserved
694  *        MSIRUNMODE_RESERVED15        Reserved
695  *        MSIRUNMODE_SCHEDULED         called from install script
696  *        MSIRUNMODE_ROLLBACK          called from rollback script
697  *        MSIRUNMODE_COMMIT            called from commit script
698  *
699  * RETURNS
700  *    In the state: TRUE
701  *    Not in the state: FALSE
702  *
703  */
704 BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode)
705 {
706     MSIPACKAGE *package;
707     BOOL r = FALSE;
708
709     TRACE("%d %d\n", hInstall, iRunMode);
710
711     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
712     if (!package)
713     {
714         BOOL ret;
715         HRESULT hr;
716         IWineMsiRemotePackage *remote_package;
717
718         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
719         if (!remote_package)
720             return FALSE;
721
722         hr = IWineMsiRemotePackage_GetMode(remote_package, iRunMode, &ret);
723         IWineMsiRemotePackage_Release(remote_package);
724
725         if (hr == S_OK)
726             return ret;
727
728         return FALSE;
729     }
730
731     switch (iRunMode)
732     {
733     case MSIRUNMODE_ADMIN:
734         FIXME("no support for administrative installs\n");
735         break;
736
737     case MSIRUNMODE_ADVERTISE:
738         FIXME("no support for advertised installs\n");
739         break;
740
741     case MSIRUNMODE_WINDOWS9X:
742         if (GetVersion() & 0x80000000)
743             r = TRUE;
744         break;
745
746     case MSIRUNMODE_OPERATIONS:
747     case MSIRUNMODE_RESERVED11:
748     case MSIRUNMODE_RESERVED14:
749     case MSIRUNMODE_RESERVED15:
750         break;
751
752     case MSIRUNMODE_SCHEDULED:
753         r = package->scheduled_action_running;
754         break;
755
756     case MSIRUNMODE_ROLLBACK:
757         r = package->rollback_action_running;
758         break;
759
760     case MSIRUNMODE_COMMIT:
761         r = package->commit_action_running;
762         break;
763
764     case MSIRUNMODE_MAINTENANCE:
765         r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
766         break;
767
768     case MSIRUNMODE_ROLLBACKENABLED:
769         r = msi_get_property_int( package->db, szRollbackDisabled, 0 ) == 0;
770         break;
771
772     case MSIRUNMODE_REBOOTATEND:
773         r = package->need_reboot;
774         break;
775
776     case MSIRUNMODE_LOGENABLED:
777         r = (package->log_file != INVALID_HANDLE_VALUE);
778         break;
779
780     default:
781         FIXME("unimplemented run mode: %d\n", iRunMode);
782         r = TRUE;
783     }
784
785     msiobj_release( &package->hdr );
786     return r;
787 }
788
789 /***********************************************************************
790  *           MsiSetMode    (MSI.@)
791  */
792 UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
793 {
794     MSIPACKAGE *package;
795     UINT r;
796
797     TRACE("%d %d %d\n", hInstall, iRunMode, fState);
798
799     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
800     if (!package)
801     {
802         HRESULT hr;
803         IWineMsiRemotePackage *remote_package;
804
805         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
806         if (!remote_package)
807             return FALSE;
808
809         hr = IWineMsiRemotePackage_SetMode( remote_package, iRunMode, fState );
810         IWineMsiRemotePackage_Release( remote_package );
811
812         if (FAILED(hr))
813         {
814             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
815                 return HRESULT_CODE(hr);
816
817             return ERROR_FUNCTION_FAILED;
818         }
819
820         return ERROR_SUCCESS;
821     }
822
823     switch (iRunMode)
824     {
825     case MSIRUNMODE_REBOOTATEND:
826         package->need_reboot = 1;
827         r = ERROR_SUCCESS;
828         break;
829
830     case MSIRUNMODE_REBOOTNOW:
831         FIXME("unimplemented run mode: %d\n", iRunMode);
832         r = ERROR_FUNCTION_FAILED;
833         break;
834
835     default:
836         r = ERROR_ACCESS_DENIED;
837     }
838
839     msiobj_release( &package->hdr );
840     return r;
841 }
842
843 /***********************************************************************
844  * MsiSetFeatureStateA (MSI.@)
845  *
846  * According to the docs, when this is called it immediately recalculates
847  * all the component states as well
848  */
849 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
850                                 INSTALLSTATE iState)
851 {
852     LPWSTR szwFeature = NULL;
853     UINT rc;
854
855     szwFeature = strdupAtoW(szFeature);
856
857     if (!szwFeature)
858         return ERROR_FUNCTION_FAILED;
859    
860     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
861
862     msi_free(szwFeature);
863
864     return rc;
865 }
866
867 /* update component state based on a feature change */
868 void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
869 {
870     INSTALLSTATE newstate;
871     ComponentList *cl;
872
873     newstate = feature->ActionRequest;
874     if (newstate == INSTALLSTATE_ABSENT) newstate = INSTALLSTATE_UNKNOWN;
875
876     LIST_FOR_EACH_ENTRY(cl, &feature->Components, ComponentList, entry)
877     {
878         MSICOMPONENT *component = cl->component;
879
880         if (!component->Enabled) continue;
881
882         TRACE("Modifying (%d): Component %s (Installed %d, Action %d, Request %d)\n",
883             newstate, debugstr_w(component->Component), component->Installed,
884             component->Action, component->ActionRequest);
885
886         if (newstate == INSTALLSTATE_LOCAL)
887         {
888             component->Action = INSTALLSTATE_LOCAL;
889             component->ActionRequest = INSTALLSTATE_LOCAL;
890         }
891         else
892         {
893             ComponentList *clist;
894             MSIFEATURE *f;
895
896             component->hasLocalFeature = FALSE;
897
898             component->Action = newstate;
899             component->ActionRequest = newstate;
900             /* if any other feature wants it local we need to set it local */
901             LIST_FOR_EACH_ENTRY(f, &package->features, MSIFEATURE, entry)
902             {
903                 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
904                      f->ActionRequest != INSTALLSTATE_SOURCE )
905                 {
906                     continue;
907                 }
908                 LIST_FOR_EACH_ENTRY(clist, &f->Components, ComponentList, entry)
909                 {
910                     if (clist->component == component &&
911                         (f->ActionRequest == INSTALLSTATE_LOCAL ||
912                          f->ActionRequest == INSTALLSTATE_SOURCE))
913                     {
914                         TRACE("Saved by %s\n", debugstr_w(f->Feature));
915                         component->hasLocalFeature = TRUE;
916
917                         if (component->Attributes & msidbComponentAttributesOptional)
918                         {
919                             if (f->Attributes & msidbFeatureAttributesFavorSource)
920                             {
921                                 component->Action = INSTALLSTATE_SOURCE;
922                                 component->ActionRequest = INSTALLSTATE_SOURCE;
923                             }
924                             else
925                             {
926                                 component->Action = INSTALLSTATE_LOCAL;
927                                 component->ActionRequest = INSTALLSTATE_LOCAL;
928                             }
929                         }
930                         else if (component->Attributes & msidbComponentAttributesSourceOnly)
931                         {
932                             component->Action = INSTALLSTATE_SOURCE;
933                             component->ActionRequest = INSTALLSTATE_SOURCE;
934                         }
935                         else
936                         {
937                             component->Action = INSTALLSTATE_LOCAL;
938                             component->ActionRequest = INSTALLSTATE_LOCAL;
939                         }
940                     }
941                 }
942             }
943         }
944         TRACE("Result (%d): Component %s (Installed %d, Action %d, Request %d)\n",
945             newstate, debugstr_w(component->Component), component->Installed,
946             component->Action, component->ActionRequest);
947     }
948 }
949
950 UINT WINAPI MSI_SetFeatureStateW( MSIPACKAGE *package, LPCWSTR szFeature, INSTALLSTATE iState )
951 {
952     UINT rc = ERROR_SUCCESS;
953     MSIFEATURE *feature, *child;
954
955     TRACE("%s %i\n", debugstr_w(szFeature), iState);
956
957     feature = msi_get_loaded_feature( package, szFeature );
958     if (!feature)
959         return ERROR_UNKNOWN_FEATURE;
960
961     if (iState == INSTALLSTATE_ADVERTISED && 
962         feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
963         return ERROR_FUNCTION_FAILED;
964
965     feature->ActionRequest = iState;
966
967     ACTION_UpdateComponentStates( package, feature );
968
969     /* update all the features that are children of this feature */
970     LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
971     {
972         if (child->Feature_Parent && !strcmpW( szFeature, child->Feature_Parent ))
973             MSI_SetFeatureStateW(package, child->Feature, iState);
974     }
975     
976     return rc;
977 }
978
979 /***********************************************************************
980  * MsiSetFeatureStateW (MSI.@)
981  */
982 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
983                                 INSTALLSTATE iState)
984 {
985     MSIPACKAGE* package;
986     UINT rc = ERROR_SUCCESS;
987
988     TRACE("%s %i\n",debugstr_w(szFeature), iState);
989
990     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
991     if (!package)
992     {
993         HRESULT hr;
994         BSTR feature;
995         IWineMsiRemotePackage *remote_package;
996
997         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
998         if (!remote_package)
999             return ERROR_INVALID_HANDLE;
1000
1001         feature = SysAllocString(szFeature);
1002         if (!feature)
1003         {
1004             IWineMsiRemotePackage_Release(remote_package);
1005             return ERROR_OUTOFMEMORY;
1006         }
1007
1008         hr = IWineMsiRemotePackage_SetFeatureState(remote_package, feature, iState);
1009
1010         SysFreeString(feature);
1011         IWineMsiRemotePackage_Release(remote_package);
1012
1013         if (FAILED(hr))
1014         {
1015             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1016                 return HRESULT_CODE(hr);
1017
1018             return ERROR_FUNCTION_FAILED;
1019         }
1020
1021         return ERROR_SUCCESS;
1022     }
1023
1024     rc = MSI_SetFeatureStateW(package,szFeature,iState);
1025
1026     msiobj_release( &package->hdr );
1027     return rc;
1028 }
1029
1030 /***********************************************************************
1031 * MsiSetFeatureAttributesA   (MSI.@)
1032 */
1033 UINT WINAPI MsiSetFeatureAttributesA( MSIHANDLE handle, LPCSTR feature, DWORD attrs )
1034 {
1035     UINT r;
1036     WCHAR *featureW = NULL;
1037
1038     TRACE("%u, %s, 0x%08x\n", handle, debugstr_a(feature), attrs);
1039
1040     if (feature && !(featureW = strdupAtoW( feature ))) return ERROR_OUTOFMEMORY;
1041
1042     r = MsiSetFeatureAttributesW( handle, featureW, attrs );
1043     msi_free( featureW );
1044     return r;
1045 }
1046
1047 static DWORD unmap_feature_attributes( DWORD attrs )
1048 {
1049     DWORD ret = 0;
1050
1051     if (attrs & INSTALLFEATUREATTRIBUTE_FAVORLOCAL)             ret = msidbFeatureAttributesFavorLocal;
1052     if (attrs & INSTALLFEATUREATTRIBUTE_FAVORSOURCE)            ret |= msidbFeatureAttributesFavorSource;
1053     if (attrs & INSTALLFEATUREATTRIBUTE_FOLLOWPARENT)           ret |= msidbFeatureAttributesFollowParent;
1054     if (attrs & INSTALLFEATUREATTRIBUTE_FAVORADVERTISE)         ret |= msidbFeatureAttributesFavorAdvertise;
1055     if (attrs & INSTALLFEATUREATTRIBUTE_DISALLOWADVERTISE)      ret |= msidbFeatureAttributesDisallowAdvertise;
1056     if (attrs & INSTALLFEATUREATTRIBUTE_NOUNSUPPORTEDADVERTISE) ret |= msidbFeatureAttributesNoUnsupportedAdvertise;
1057     return ret;
1058 }
1059
1060 /***********************************************************************
1061 * MsiSetFeatureAttributesW   (MSI.@)
1062 */
1063 UINT WINAPI MsiSetFeatureAttributesW( MSIHANDLE handle, LPCWSTR name, DWORD attrs )
1064 {
1065     MSIPACKAGE *package;
1066     MSIFEATURE *feature;
1067     WCHAR *costing;
1068
1069     TRACE("%u, %s, 0x%08x\n", handle, debugstr_w(name), attrs);
1070
1071     if (!name || !name[0]) return ERROR_UNKNOWN_FEATURE;
1072
1073     if (!(package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE )))
1074         return ERROR_INVALID_HANDLE;
1075
1076     costing = msi_dup_property( package->db, szCostingComplete );
1077     if (!costing || !strcmpW( costing, szOne ))
1078     {
1079         msi_free( costing );
1080         msiobj_release( &package->hdr );
1081         return ERROR_FUNCTION_FAILED;
1082     }
1083     msi_free( costing );
1084     if (!(feature = msi_get_loaded_feature( package, name )))
1085     {
1086         msiobj_release( &package->hdr );
1087         return ERROR_UNKNOWN_FEATURE;
1088     }
1089     feature->Attributes = unmap_feature_attributes( attrs );
1090     msiobj_release( &package->hdr );
1091     return ERROR_SUCCESS;
1092 }
1093
1094 /***********************************************************************
1095 * MsiGetFeatureStateA   (MSI.@)
1096 */
1097 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1098                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1099 {
1100     LPWSTR szwFeature = NULL;
1101     UINT rc;
1102     
1103     if (szFeature && !(szwFeature = strdupAtoW(szFeature))) return ERROR_OUTOFMEMORY;
1104
1105     rc = MsiGetFeatureStateW(hInstall, szwFeature, piInstalled, piAction);
1106     msi_free( szwFeature);
1107     return rc;
1108 }
1109
1110 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1111                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1112 {
1113     MSIFEATURE *feature;
1114
1115     feature = msi_get_loaded_feature(package,szFeature);
1116     if (!feature)
1117         return ERROR_UNKNOWN_FEATURE;
1118
1119     if (piInstalled)
1120         *piInstalled = feature->Installed;
1121
1122     if (piAction)
1123         *piAction = feature->ActionRequest;
1124
1125     TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1126
1127     return ERROR_SUCCESS;
1128 }
1129
1130 /***********************************************************************
1131 * MsiGetFeatureStateW   (MSI.@)
1132 */
1133 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1134                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1135 {
1136     MSIPACKAGE* package;
1137     UINT ret;
1138
1139     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1140
1141     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1142     if (!package)
1143     {
1144         HRESULT hr;
1145         BSTR feature;
1146         IWineMsiRemotePackage *remote_package;
1147
1148         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1149         if (!remote_package)
1150             return ERROR_INVALID_HANDLE;
1151
1152         feature = SysAllocString(szFeature);
1153         if (!feature)
1154         {
1155             IWineMsiRemotePackage_Release(remote_package);
1156             return ERROR_OUTOFMEMORY;
1157         }
1158
1159         hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
1160                                                    piInstalled, piAction);
1161
1162         SysFreeString(feature);
1163         IWineMsiRemotePackage_Release(remote_package);
1164
1165         if (FAILED(hr))
1166         {
1167             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1168                 return HRESULT_CODE(hr);
1169
1170             return ERROR_FUNCTION_FAILED;
1171         }
1172
1173         return ERROR_SUCCESS;
1174     }
1175
1176     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
1177     msiobj_release( &package->hdr );
1178     return ret;
1179 }
1180
1181 /***********************************************************************
1182 * MsiGetFeatureCostA   (MSI.@)
1183 */
1184 UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1185                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1186 {
1187     LPWSTR szwFeature = NULL;
1188     UINT rc;
1189
1190     szwFeature = strdupAtoW(szFeature);
1191
1192     rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);
1193
1194     msi_free(szwFeature);
1195
1196     return rc;
1197 }
1198
1199 static INT feature_cost( MSIFEATURE *feature )
1200 {
1201     INT cost = 0;
1202     MSICOMPONENT *comp;
1203
1204     LIST_FOR_EACH_ENTRY( comp, &feature->Components, MSICOMPONENT, entry )
1205     {
1206         cost += comp->Cost;
1207     }
1208     return cost;
1209 }
1210
1211 UINT MSI_GetFeatureCost( MSIPACKAGE *package, MSIFEATURE *feature, MSICOSTTREE tree,
1212                          INSTALLSTATE state, LPINT cost )
1213 {
1214     TRACE("%s, %u, %d, %p\n", debugstr_w(feature->Feature), tree, state, cost);
1215
1216     *cost = 0;
1217     switch (tree)
1218     {
1219     case MSICOSTTREE_CHILDREN:
1220     {
1221         MSIFEATURE *child;
1222
1223         LIST_FOR_EACH_ENTRY( child, &feature->Children, MSIFEATURE, entry )
1224         {
1225             if (child->ActionRequest == state)
1226                 *cost += feature_cost( child );
1227         }
1228         break;
1229     }
1230     case MSICOSTTREE_PARENTS:
1231     {
1232         const WCHAR *feature_parent = feature->Feature_Parent;
1233         for (;;)
1234         {
1235             MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1236             if (!parent)
1237                 break;
1238
1239             if (parent->ActionRequest == state)
1240                 *cost += feature_cost( parent );
1241
1242             feature_parent = parent->Feature_Parent;
1243         }
1244         break;
1245     }
1246     case MSICOSTTREE_SELFONLY:
1247         if (feature->ActionRequest == state)
1248             *cost = feature_cost( feature );
1249         break;
1250
1251     default:
1252         WARN("unhandled cost tree %u\n", tree);
1253         break;
1254     }
1255
1256     *cost /= 512;
1257     return ERROR_SUCCESS;
1258 }
1259
1260 /***********************************************************************
1261 * MsiGetFeatureCostW   (MSI.@)
1262 */
1263 UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1264                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1265 {
1266     MSIPACKAGE *package;
1267     MSIFEATURE *feature;
1268     UINT ret;
1269
1270     TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1271           iCostTree, iState, piCost);
1272
1273     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1274     if (!package)
1275     {
1276         HRESULT hr;
1277         BSTR feature;
1278         IWineMsiRemotePackage *remote_package;
1279
1280         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1281         if (!remote_package)
1282             return ERROR_INVALID_HANDLE;
1283
1284         feature = SysAllocString(szFeature);
1285         if (!feature)
1286         {
1287             IWineMsiRemotePackage_Release(remote_package);
1288             return ERROR_OUTOFMEMORY;
1289         }
1290
1291         hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
1292                                                   iCostTree, iState, piCost);
1293
1294         SysFreeString(feature);
1295         IWineMsiRemotePackage_Release(remote_package);
1296
1297         if (FAILED(hr))
1298         {
1299             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1300                 return HRESULT_CODE(hr);
1301
1302             return ERROR_FUNCTION_FAILED;
1303         }
1304
1305         return ERROR_SUCCESS;
1306     }
1307
1308     feature = msi_get_loaded_feature(package, szFeature);
1309
1310     if (feature)
1311         ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
1312     else
1313         ret = ERROR_UNKNOWN_FEATURE;
1314
1315     msiobj_release( &package->hdr );
1316     return ret;
1317 }
1318
1319 /***********************************************************************
1320 * MsiGetFeatureInfoA   (MSI.@)
1321 */
1322 UINT WINAPI MsiGetFeatureInfoA( MSIHANDLE handle, LPCSTR feature, LPDWORD attrs,
1323                                 LPSTR title, LPDWORD title_len, LPSTR help, LPDWORD help_len )
1324 {
1325     UINT r;
1326     WCHAR *titleW = NULL, *helpW = NULL, *featureW = NULL;
1327
1328     TRACE("%u, %s, %p, %p, %p, %p, %p\n", handle, debugstr_a(feature), attrs, title,
1329           title_len, help, help_len);
1330
1331     if (feature && !(featureW = strdupAtoW( feature ))) return ERROR_OUTOFMEMORY;
1332
1333     if (title && title_len && !(titleW = msi_alloc( *title_len * sizeof(WCHAR) )))
1334     {
1335         msi_free( featureW );
1336         return ERROR_OUTOFMEMORY;
1337     }
1338     if (help && help_len && !(helpW = msi_alloc( *help_len * sizeof(WCHAR) )))
1339     {
1340         msi_free( featureW );
1341         msi_free( titleW );
1342         return ERROR_OUTOFMEMORY;
1343     }
1344     r = MsiGetFeatureInfoW( handle, featureW, attrs, titleW, title_len, helpW, help_len );
1345     if (r == ERROR_SUCCESS)
1346     {
1347         if (titleW) WideCharToMultiByte( CP_ACP, 0, titleW, -1, title, *title_len + 1, NULL, NULL );
1348         if (helpW) WideCharToMultiByte( CP_ACP, 0, helpW, -1, help, *help_len + 1, NULL, NULL );
1349     }
1350     msi_free( titleW );
1351     msi_free( helpW );
1352     msi_free( featureW );
1353     return r;
1354 }
1355
1356 static DWORD map_feature_attributes( DWORD attrs )
1357 {
1358     DWORD ret = 0;
1359
1360     if (attrs == msidbFeatureAttributesFavorLocal)            ret |= INSTALLFEATUREATTRIBUTE_FAVORLOCAL;
1361     if (attrs & msidbFeatureAttributesFavorSource)            ret |= INSTALLFEATUREATTRIBUTE_FAVORSOURCE;
1362     if (attrs & msidbFeatureAttributesFollowParent)           ret |= INSTALLFEATUREATTRIBUTE_FOLLOWPARENT;
1363     if (attrs & msidbFeatureAttributesFavorAdvertise)         ret |= INSTALLFEATUREATTRIBUTE_FAVORADVERTISE;
1364     if (attrs & msidbFeatureAttributesDisallowAdvertise)      ret |= INSTALLFEATUREATTRIBUTE_DISALLOWADVERTISE;
1365     if (attrs & msidbFeatureAttributesNoUnsupportedAdvertise) ret |= INSTALLFEATUREATTRIBUTE_NOUNSUPPORTEDADVERTISE;
1366     return ret;
1367 }
1368
1369 static UINT MSI_GetFeatureInfo( MSIPACKAGE *package, LPCWSTR name, LPDWORD attrs,
1370                                 LPWSTR title, LPDWORD title_len, LPWSTR help, LPDWORD help_len )
1371 {
1372     UINT r = ERROR_SUCCESS;
1373     MSIFEATURE *feature = msi_get_loaded_feature( package, name );
1374     int len;
1375
1376     if (!feature) return ERROR_UNKNOWN_FEATURE;
1377     if (attrs) *attrs = map_feature_attributes( feature->Attributes );
1378     if (title_len)
1379     {
1380         if (feature->Title) len = strlenW( feature->Title );
1381         else len = 0;
1382         if (*title_len <= len)
1383         {
1384             *title_len = len;
1385             if (title) r = ERROR_MORE_DATA;
1386         }
1387         else if (title)
1388         {
1389             if (feature->Title) strcpyW( title, feature->Title );
1390             else *title = 0;
1391             *title_len = len;
1392         }
1393     }
1394     if (help_len)
1395     {
1396         if (feature->Description) len = strlenW( feature->Description );
1397         else len = 0;
1398         if (*help_len <= len)
1399         {
1400             *help_len = len;
1401             if (help) r = ERROR_MORE_DATA;
1402         }
1403         else if (help)
1404         {
1405             if (feature->Description) strcpyW( help, feature->Description );
1406             else *help = 0;
1407             *help_len = len;
1408         }
1409     }
1410     return r;
1411 }
1412
1413 /***********************************************************************
1414 * MsiGetFeatureInfoW   (MSI.@)
1415 */
1416 UINT WINAPI MsiGetFeatureInfoW( MSIHANDLE handle, LPCWSTR feature, LPDWORD attrs,
1417                                 LPWSTR title, LPDWORD title_len, LPWSTR help, LPDWORD help_len )
1418 {
1419     UINT r;
1420     MSIPACKAGE *package;
1421
1422     TRACE("%u, %s, %p, %p, %p, %p, %p\n", handle, debugstr_w(feature), attrs, title,
1423           title_len, help, help_len);
1424
1425     if (!feature) return ERROR_INVALID_PARAMETER;
1426
1427     if (!(package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE )))
1428         return ERROR_INVALID_HANDLE;
1429
1430     /* features may not have been loaded yet */
1431     msi_load_all_components( package );
1432     msi_load_all_features( package );
1433
1434     r = MSI_GetFeatureInfo( package, feature, attrs, title, title_len, help, help_len );
1435     msiobj_release( &package->hdr );
1436     return r;
1437 }
1438
1439 /***********************************************************************
1440  * MsiSetComponentStateA (MSI.@)
1441  */
1442 UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1443                                   INSTALLSTATE iState)
1444 {
1445     UINT rc;
1446     LPWSTR szwComponent = strdupAtoW(szComponent);
1447
1448     rc = MsiSetComponentStateW(hInstall, szwComponent, iState);
1449
1450     msi_free(szwComponent);
1451
1452     return rc;
1453 }
1454
1455 /***********************************************************************
1456  * MsiGetComponentStateA (MSI.@)
1457  */
1458 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1459                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1460 {
1461     LPWSTR szwComponent= NULL;
1462     UINT rc;
1463     
1464     szwComponent= strdupAtoW(szComponent);
1465
1466     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
1467
1468     msi_free( szwComponent);
1469
1470     return rc;
1471 }
1472
1473 static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1474                                    INSTALLSTATE iState)
1475 {
1476     MSICOMPONENT *comp;
1477
1478     TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);
1479
1480     comp = msi_get_loaded_component(package, szComponent);
1481     if (!comp)
1482         return ERROR_UNKNOWN_COMPONENT;
1483
1484     if (comp->Enabled)
1485         comp->Action = iState;
1486
1487     return ERROR_SUCCESS;
1488 }
1489
1490 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1491                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1492 {
1493     MSICOMPONENT *comp;
1494
1495     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
1496            piInstalled, piAction);
1497
1498     comp = msi_get_loaded_component(package,szComponent);
1499     if (!comp)
1500         return ERROR_UNKNOWN_COMPONENT;
1501
1502     if (piInstalled)
1503     {
1504         if (comp->Enabled)
1505             *piInstalled = comp->Installed;
1506         else
1507             *piInstalled = INSTALLSTATE_UNKNOWN;
1508     }
1509
1510     if (piAction)
1511     {
1512         if (comp->Enabled)
1513             *piAction = comp->Action;
1514         else
1515             *piAction = INSTALLSTATE_UNKNOWN;
1516     }
1517
1518     TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1519     return ERROR_SUCCESS;
1520 }
1521
1522 /***********************************************************************
1523  * MsiSetComponentStateW (MSI.@)
1524  */
1525 UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1526                                   INSTALLSTATE iState)
1527 {
1528     MSIPACKAGE* package;
1529     UINT ret;
1530
1531     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1532     if (!package)
1533     {
1534         HRESULT hr;
1535         BSTR component;
1536         IWineMsiRemotePackage *remote_package;
1537
1538         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1539         if (!remote_package)
1540             return ERROR_INVALID_HANDLE;
1541
1542         component = SysAllocString(szComponent);
1543         if (!component)
1544         {
1545             IWineMsiRemotePackage_Release(remote_package);
1546             return ERROR_OUTOFMEMORY;
1547         }
1548
1549         hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1550
1551         SysFreeString(component);
1552         IWineMsiRemotePackage_Release(remote_package);
1553
1554         if (FAILED(hr))
1555         {
1556             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1557                 return HRESULT_CODE(hr);
1558
1559             return ERROR_FUNCTION_FAILED;
1560         }
1561
1562         return ERROR_SUCCESS;
1563     }
1564
1565     ret = MSI_SetComponentStateW(package, szComponent, iState);
1566     msiobj_release(&package->hdr);
1567     return ret;
1568 }
1569
1570 /***********************************************************************
1571  * MsiGetComponentStateW (MSI.@)
1572  */
1573 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1574                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1575 {
1576     MSIPACKAGE* package;
1577     UINT ret;
1578
1579     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1580            piInstalled, piAction);
1581
1582     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1583     if (!package)
1584     {
1585         HRESULT hr;
1586         BSTR component;
1587         IWineMsiRemotePackage *remote_package;
1588
1589         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1590         if (!remote_package)
1591             return ERROR_INVALID_HANDLE;
1592
1593         component = SysAllocString(szComponent);
1594         if (!component)
1595         {
1596             IWineMsiRemotePackage_Release(remote_package);
1597             return ERROR_OUTOFMEMORY;
1598         }
1599
1600         hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1601                                                      piInstalled, piAction);
1602
1603         SysFreeString(component);
1604         IWineMsiRemotePackage_Release(remote_package);
1605
1606         if (FAILED(hr))
1607         {
1608             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1609                 return HRESULT_CODE(hr);
1610
1611             return ERROR_FUNCTION_FAILED;
1612         }
1613
1614         return ERROR_SUCCESS;
1615     }
1616
1617     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
1618     msiobj_release( &package->hdr );
1619     return ret;
1620 }
1621
1622 /***********************************************************************
1623  * MsiGetLanguage (MSI.@)
1624  */
1625 LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
1626 {
1627     MSIPACKAGE* package;
1628     LANGID langid;
1629
1630     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1631     if (!package)
1632     {
1633         HRESULT hr;
1634         LANGID lang;
1635         IWineMsiRemotePackage *remote_package;
1636
1637         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1638         if (!remote_package)
1639             return ERROR_INVALID_HANDLE;
1640
1641         hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);
1642
1643         if (SUCCEEDED(hr))
1644             return lang;
1645
1646         return 0;
1647     }
1648
1649     langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1650     msiobj_release( &package->hdr );
1651     return langid;
1652 }
1653
1654 UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1655 {
1656     static const WCHAR fmt[] = { '%','d',0 };
1657     WCHAR level[6];
1658     UINT r;
1659
1660     TRACE("%p %i\n", package, iInstallLevel);
1661
1662     if (iInstallLevel > 32767)
1663         return ERROR_INVALID_PARAMETER;
1664
1665     if (iInstallLevel < 1)
1666         return MSI_SetFeatureStates( package );
1667
1668     sprintfW( level, fmt, iInstallLevel );
1669     r = msi_set_property( package->db, szInstallLevel, level );
1670     if ( r == ERROR_SUCCESS )
1671         r = MSI_SetFeatureStates( package );
1672
1673     return r;
1674 }
1675
1676 /***********************************************************************
1677  * MsiSetInstallLevel (MSI.@)
1678  */
1679 UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
1680 {
1681     MSIPACKAGE* package;
1682     UINT r;
1683
1684     TRACE("%d %i\n", hInstall, iInstallLevel);
1685
1686     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1687     if (!package)
1688     {
1689         HRESULT hr;
1690         IWineMsiRemotePackage *remote_package;
1691
1692         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1693         if (!remote_package)
1694             return ERROR_INVALID_HANDLE;
1695
1696         hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);
1697
1698         IWineMsiRemotePackage_Release(remote_package);
1699
1700         if (FAILED(hr))
1701         {
1702             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1703                 return HRESULT_CODE(hr);
1704
1705             return ERROR_FUNCTION_FAILED;
1706         }
1707
1708         return ERROR_SUCCESS;
1709     }
1710
1711     r = MSI_SetInstallLevel( package, iInstallLevel );
1712
1713     msiobj_release( &package->hdr );
1714
1715     return r;
1716 }
1717
1718 /***********************************************************************
1719  * MsiGetFeatureValidStatesW (MSI.@)
1720  */
1721 UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1722                   LPDWORD pInstallState)
1723 {
1724     if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1725     FIXME("%d %s %p stub returning %d\n",
1726         hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);
1727
1728     return ERROR_SUCCESS;
1729 }
1730
1731 /***********************************************************************
1732  * MsiGetFeatureValidStatesA (MSI.@)
1733  */
1734 UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1735                   LPDWORD pInstallState)
1736 {
1737     UINT ret;
1738     LPWSTR szwFeature = strdupAtoW(szFeature);
1739
1740     ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);
1741
1742     msi_free(szwFeature);
1743
1744     return ret;
1745 }