shell32: COM cleanup for the IShellView2 iface.
[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         r = FALSE;
736         break;
737
738     case MSIRUNMODE_WINDOWS9X:
739         if (GetVersion() & 0x80000000)
740             r = TRUE;
741         break;
742
743     case MSIRUNMODE_OPERATIONS:
744     case MSIRUNMODE_RESERVED11:
745     case MSIRUNMODE_RESERVED14:
746     case MSIRUNMODE_RESERVED15:
747         break;
748
749     case MSIRUNMODE_SCHEDULED:
750         r = package->scheduled_action_running;
751         break;
752
753     case MSIRUNMODE_ROLLBACK:
754         r = package->rollback_action_running;
755         break;
756
757     case MSIRUNMODE_COMMIT:
758         r = package->commit_action_running;
759         break;
760
761     case MSIRUNMODE_MAINTENANCE:
762         r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
763         break;
764
765     case MSIRUNMODE_ROLLBACKENABLED:
766         r = msi_get_property_int( package->db, szRollbackDisabled, 0 ) == 0;
767         break;
768
769     case MSIRUNMODE_REBOOTATEND:
770         r = package->need_reboot;
771         break;
772
773     case MSIRUNMODE_LOGENABLED:
774         r = (package->log_file != INVALID_HANDLE_VALUE);
775         break;
776
777     default:
778         FIXME("unimplemented run mode: %d\n", iRunMode);
779         r = TRUE;
780     }
781
782     msiobj_release( &package->hdr );
783     return r;
784 }
785
786 /***********************************************************************
787  *           MsiSetMode    (MSI.@)
788  */
789 UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
790 {
791     MSIPACKAGE *package;
792     UINT r;
793
794     TRACE("%d %d %d\n", hInstall, iRunMode, fState);
795
796     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
797     if (!package)
798     {
799         HRESULT hr;
800         IWineMsiRemotePackage *remote_package;
801
802         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
803         if (!remote_package)
804             return FALSE;
805
806         hr = IWineMsiRemotePackage_SetMode( remote_package, iRunMode, fState );
807         IWineMsiRemotePackage_Release( remote_package );
808
809         if (FAILED(hr))
810         {
811             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
812                 return HRESULT_CODE(hr);
813
814             return ERROR_FUNCTION_FAILED;
815         }
816
817         return ERROR_SUCCESS;
818     }
819
820     switch (iRunMode)
821     {
822     case MSIRUNMODE_REBOOTATEND:
823         package->need_reboot = 1;
824         r = ERROR_SUCCESS;
825         break;
826
827     case MSIRUNMODE_REBOOTNOW:
828         FIXME("unimplemented run mode: %d\n", iRunMode);
829         r = ERROR_FUNCTION_FAILED;
830         break;
831
832     default:
833         r = ERROR_ACCESS_DENIED;
834     }
835
836     msiobj_release( &package->hdr );
837     return r;
838 }
839
840 /***********************************************************************
841  * MsiSetFeatureStateA (MSI.@)
842  *
843  * According to the docs, when this is called it immediately recalculates
844  * all the component states as well
845  */
846 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
847                                 INSTALLSTATE iState)
848 {
849     LPWSTR szwFeature = NULL;
850     UINT rc;
851
852     szwFeature = strdupAtoW(szFeature);
853
854     if (!szwFeature)
855         return ERROR_FUNCTION_FAILED;
856    
857     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
858
859     msi_free(szwFeature);
860
861     return rc;
862 }
863
864 /* update component state based on a feature change */
865 void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
866 {
867     INSTALLSTATE newstate;
868     ComponentList *cl;
869
870     newstate = feature->ActionRequest;
871     if (newstate == INSTALLSTATE_ABSENT) newstate = INSTALLSTATE_UNKNOWN;
872
873     LIST_FOR_EACH_ENTRY(cl, &feature->Components, ComponentList, entry)
874     {
875         MSICOMPONENT *component = cl->component;
876
877         if (!component->Enabled) continue;
878
879         TRACE("Modifying (%d): Component %s (Installed %d, Action %d, Request %d)\n",
880             newstate, debugstr_w(component->Component), component->Installed,
881             component->Action, component->ActionRequest);
882
883         if (newstate == INSTALLSTATE_LOCAL)
884         {
885             component->Action = INSTALLSTATE_LOCAL;
886             component->ActionRequest = INSTALLSTATE_LOCAL;
887         }
888         else
889         {
890             ComponentList *clist;
891             MSIFEATURE *f;
892
893             component->hasLocalFeature = FALSE;
894
895             component->Action = newstate;
896             component->ActionRequest = newstate;
897             /* if any other feature wants it local we need to set it local */
898             LIST_FOR_EACH_ENTRY(f, &package->features, MSIFEATURE, entry)
899             {
900                 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
901                      f->ActionRequest != INSTALLSTATE_SOURCE )
902                 {
903                     continue;
904                 }
905                 LIST_FOR_EACH_ENTRY(clist, &f->Components, ComponentList, entry)
906                 {
907                     if (clist->component == component &&
908                         (f->ActionRequest == INSTALLSTATE_LOCAL ||
909                          f->ActionRequest == INSTALLSTATE_SOURCE))
910                     {
911                         TRACE("Saved by %s\n", debugstr_w(f->Feature));
912                         component->hasLocalFeature = TRUE;
913
914                         if (component->Attributes & msidbComponentAttributesOptional)
915                         {
916                             if (f->Attributes & msidbFeatureAttributesFavorSource)
917                             {
918                                 component->Action = INSTALLSTATE_SOURCE;
919                                 component->ActionRequest = INSTALLSTATE_SOURCE;
920                             }
921                             else
922                             {
923                                 component->Action = INSTALLSTATE_LOCAL;
924                                 component->ActionRequest = INSTALLSTATE_LOCAL;
925                             }
926                         }
927                         else if (component->Attributes & msidbComponentAttributesSourceOnly)
928                         {
929                             component->Action = INSTALLSTATE_SOURCE;
930                             component->ActionRequest = INSTALLSTATE_SOURCE;
931                         }
932                         else
933                         {
934                             component->Action = INSTALLSTATE_LOCAL;
935                             component->ActionRequest = INSTALLSTATE_LOCAL;
936                         }
937                     }
938                 }
939             }
940         }
941         TRACE("Result (%d): Component %s (Installed %d, Action %d, Request %d)\n",
942             newstate, debugstr_w(component->Component), component->Installed,
943             component->Action, component->ActionRequest);
944     }
945 }
946
947 UINT WINAPI MSI_SetFeatureStateW( MSIPACKAGE *package, LPCWSTR szFeature, INSTALLSTATE iState )
948 {
949     UINT rc = ERROR_SUCCESS;
950     MSIFEATURE *feature, *child;
951
952     TRACE("%s %i\n", debugstr_w(szFeature), iState);
953
954     feature = msi_get_loaded_feature( package, szFeature );
955     if (!feature)
956         return ERROR_UNKNOWN_FEATURE;
957
958     if (iState == INSTALLSTATE_ADVERTISED && 
959         feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
960         return ERROR_FUNCTION_FAILED;
961
962     feature->ActionRequest = iState;
963
964     ACTION_UpdateComponentStates( package, feature );
965
966     /* update all the features that are children of this feature */
967     LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
968     {
969         if (child->Feature_Parent && !strcmpW( szFeature, child->Feature_Parent ))
970             MSI_SetFeatureStateW(package, child->Feature, iState);
971     }
972     
973     return rc;
974 }
975
976 /***********************************************************************
977  * MsiSetFeatureStateW (MSI.@)
978  */
979 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
980                                 INSTALLSTATE iState)
981 {
982     MSIPACKAGE* package;
983     UINT rc = ERROR_SUCCESS;
984
985     TRACE("%s %i\n",debugstr_w(szFeature), iState);
986
987     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
988     if (!package)
989     {
990         HRESULT hr;
991         BSTR feature;
992         IWineMsiRemotePackage *remote_package;
993
994         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
995         if (!remote_package)
996             return ERROR_INVALID_HANDLE;
997
998         feature = SysAllocString(szFeature);
999         if (!feature)
1000         {
1001             IWineMsiRemotePackage_Release(remote_package);
1002             return ERROR_OUTOFMEMORY;
1003         }
1004
1005         hr = IWineMsiRemotePackage_SetFeatureState(remote_package, feature, iState);
1006
1007         SysFreeString(feature);
1008         IWineMsiRemotePackage_Release(remote_package);
1009
1010         if (FAILED(hr))
1011         {
1012             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1013                 return HRESULT_CODE(hr);
1014
1015             return ERROR_FUNCTION_FAILED;
1016         }
1017
1018         return ERROR_SUCCESS;
1019     }
1020
1021     rc = MSI_SetFeatureStateW(package,szFeature,iState);
1022
1023     msiobj_release( &package->hdr );
1024     return rc;
1025 }
1026
1027 /***********************************************************************
1028 * MsiGetFeatureStateA   (MSI.@)
1029 */
1030 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1031                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1032 {
1033     LPWSTR szwFeature = NULL;
1034     UINT rc;
1035     
1036     szwFeature = strdupAtoW(szFeature);
1037
1038     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
1039
1040     msi_free( szwFeature);
1041
1042     return rc;
1043 }
1044
1045 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1046                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1047 {
1048     MSIFEATURE *feature;
1049
1050     feature = msi_get_loaded_feature(package,szFeature);
1051     if (!feature)
1052         return ERROR_UNKNOWN_FEATURE;
1053
1054     if (piInstalled)
1055         *piInstalled = feature->Installed;
1056
1057     if (piAction)
1058         *piAction = feature->ActionRequest;
1059
1060     TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1061
1062     return ERROR_SUCCESS;
1063 }
1064
1065 /***********************************************************************
1066 * MsiGetFeatureStateW   (MSI.@)
1067 */
1068 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1069                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1070 {
1071     MSIPACKAGE* package;
1072     UINT ret;
1073
1074     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1075
1076     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1077     if (!package)
1078     {
1079         HRESULT hr;
1080         BSTR feature;
1081         IWineMsiRemotePackage *remote_package;
1082
1083         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1084         if (!remote_package)
1085             return ERROR_INVALID_HANDLE;
1086
1087         feature = SysAllocString(szFeature);
1088         if (!feature)
1089         {
1090             IWineMsiRemotePackage_Release(remote_package);
1091             return ERROR_OUTOFMEMORY;
1092         }
1093
1094         hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
1095                                                    piInstalled, piAction);
1096
1097         SysFreeString(feature);
1098         IWineMsiRemotePackage_Release(remote_package);
1099
1100         if (FAILED(hr))
1101         {
1102             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1103                 return HRESULT_CODE(hr);
1104
1105             return ERROR_FUNCTION_FAILED;
1106         }
1107
1108         return ERROR_SUCCESS;
1109     }
1110
1111     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
1112     msiobj_release( &package->hdr );
1113     return ret;
1114 }
1115
1116 /***********************************************************************
1117 * MsiGetFeatureCostA   (MSI.@)
1118 */
1119 UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1120                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1121 {
1122     LPWSTR szwFeature = NULL;
1123     UINT rc;
1124
1125     szwFeature = strdupAtoW(szFeature);
1126
1127     rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);
1128
1129     msi_free(szwFeature);
1130
1131     return rc;
1132 }
1133
1134 static INT feature_cost( MSIFEATURE *feature )
1135 {
1136     INT cost = 0;
1137     MSICOMPONENT *comp;
1138
1139     LIST_FOR_EACH_ENTRY( comp, &feature->Components, MSICOMPONENT, entry )
1140     {
1141         cost += comp->Cost;
1142     }
1143     return cost;
1144 }
1145
1146 UINT MSI_GetFeatureCost( MSIPACKAGE *package, MSIFEATURE *feature, MSICOSTTREE tree,
1147                          INSTALLSTATE state, LPINT cost )
1148 {
1149     TRACE("%s, %u, %d, %p\n", debugstr_w(feature->Feature), tree, state, cost);
1150
1151     *cost = 0;
1152     switch (tree)
1153     {
1154     case MSICOSTTREE_CHILDREN:
1155     {
1156         MSIFEATURE *child;
1157
1158         LIST_FOR_EACH_ENTRY( child, &feature->Children, MSIFEATURE, entry )
1159         {
1160             if (child->ActionRequest == state)
1161                 *cost += feature_cost( child );
1162         }
1163         break;
1164     }
1165     case MSICOSTTREE_PARENTS:
1166     {
1167         const WCHAR *feature_parent = feature->Feature_Parent;
1168         for (;;)
1169         {
1170             MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1171             if (!parent)
1172                 break;
1173
1174             if (parent->ActionRequest == state)
1175                 *cost += feature_cost( parent );
1176
1177             feature_parent = parent->Feature_Parent;
1178         }
1179         break;
1180     }
1181     case MSICOSTTREE_SELFONLY:
1182         if (feature->ActionRequest == state)
1183             *cost = feature_cost( feature );
1184         break;
1185
1186     default:
1187         WARN("unhandled cost tree %u\n", tree);
1188         break;
1189     }
1190
1191     *cost /= 512;
1192     return ERROR_SUCCESS;
1193 }
1194
1195 /***********************************************************************
1196 * MsiGetFeatureCostW   (MSI.@)
1197 */
1198 UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1199                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1200 {
1201     MSIPACKAGE *package;
1202     MSIFEATURE *feature;
1203     UINT ret;
1204
1205     TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1206           iCostTree, iState, piCost);
1207
1208     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1209     if (!package)
1210     {
1211         HRESULT hr;
1212         BSTR feature;
1213         IWineMsiRemotePackage *remote_package;
1214
1215         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1216         if (!remote_package)
1217             return ERROR_INVALID_HANDLE;
1218
1219         feature = SysAllocString(szFeature);
1220         if (!feature)
1221         {
1222             IWineMsiRemotePackage_Release(remote_package);
1223             return ERROR_OUTOFMEMORY;
1224         }
1225
1226         hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
1227                                                   iCostTree, iState, piCost);
1228
1229         SysFreeString(feature);
1230         IWineMsiRemotePackage_Release(remote_package);
1231
1232         if (FAILED(hr))
1233         {
1234             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1235                 return HRESULT_CODE(hr);
1236
1237             return ERROR_FUNCTION_FAILED;
1238         }
1239
1240         return ERROR_SUCCESS;
1241     }
1242
1243     feature = msi_get_loaded_feature(package, szFeature);
1244
1245     if (feature)
1246         ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
1247     else
1248         ret = ERROR_UNKNOWN_FEATURE;
1249
1250     msiobj_release( &package->hdr );
1251     return ret;
1252 }
1253
1254 /***********************************************************************
1255  * MsiSetComponentStateA (MSI.@)
1256  */
1257 UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1258                                   INSTALLSTATE iState)
1259 {
1260     UINT rc;
1261     LPWSTR szwComponent = strdupAtoW(szComponent);
1262
1263     rc = MsiSetComponentStateW(hInstall, szwComponent, iState);
1264
1265     msi_free(szwComponent);
1266
1267     return rc;
1268 }
1269
1270 /***********************************************************************
1271  * MsiGetComponentStateA (MSI.@)
1272  */
1273 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1274                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1275 {
1276     LPWSTR szwComponent= NULL;
1277     UINT rc;
1278     
1279     szwComponent= strdupAtoW(szComponent);
1280
1281     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
1282
1283     msi_free( szwComponent);
1284
1285     return rc;
1286 }
1287
1288 static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1289                                    INSTALLSTATE iState)
1290 {
1291     MSICOMPONENT *comp;
1292
1293     TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);
1294
1295     comp = msi_get_loaded_component(package, szComponent);
1296     if (!comp)
1297         return ERROR_UNKNOWN_COMPONENT;
1298
1299     if (comp->Enabled)
1300         comp->Action = iState;
1301
1302     return ERROR_SUCCESS;
1303 }
1304
1305 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1306                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1307 {
1308     MSICOMPONENT *comp;
1309
1310     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
1311            piInstalled, piAction);
1312
1313     comp = msi_get_loaded_component(package,szComponent);
1314     if (!comp)
1315         return ERROR_UNKNOWN_COMPONENT;
1316
1317     if (piInstalled)
1318     {
1319         if (comp->Enabled)
1320             *piInstalled = comp->Installed;
1321         else
1322             *piInstalled = INSTALLSTATE_UNKNOWN;
1323     }
1324
1325     if (piAction)
1326     {
1327         if (comp->Enabled)
1328             *piAction = comp->Action;
1329         else
1330             *piAction = INSTALLSTATE_UNKNOWN;
1331     }
1332
1333     TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1334     return ERROR_SUCCESS;
1335 }
1336
1337 /***********************************************************************
1338  * MsiSetComponentStateW (MSI.@)
1339  */
1340 UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1341                                   INSTALLSTATE iState)
1342 {
1343     MSIPACKAGE* package;
1344     UINT ret;
1345
1346     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1347     if (!package)
1348     {
1349         HRESULT hr;
1350         BSTR component;
1351         IWineMsiRemotePackage *remote_package;
1352
1353         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1354         if (!remote_package)
1355             return ERROR_INVALID_HANDLE;
1356
1357         component = SysAllocString(szComponent);
1358         if (!component)
1359         {
1360             IWineMsiRemotePackage_Release(remote_package);
1361             return ERROR_OUTOFMEMORY;
1362         }
1363
1364         hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1365
1366         SysFreeString(component);
1367         IWineMsiRemotePackage_Release(remote_package);
1368
1369         if (FAILED(hr))
1370         {
1371             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1372                 return HRESULT_CODE(hr);
1373
1374             return ERROR_FUNCTION_FAILED;
1375         }
1376
1377         return ERROR_SUCCESS;
1378     }
1379
1380     ret = MSI_SetComponentStateW(package, szComponent, iState);
1381     msiobj_release(&package->hdr);
1382     return ret;
1383 }
1384
1385 /***********************************************************************
1386  * MsiGetComponentStateW (MSI.@)
1387  */
1388 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1389                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1390 {
1391     MSIPACKAGE* package;
1392     UINT ret;
1393
1394     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1395            piInstalled, piAction);
1396
1397     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1398     if (!package)
1399     {
1400         HRESULT hr;
1401         BSTR component;
1402         IWineMsiRemotePackage *remote_package;
1403
1404         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1405         if (!remote_package)
1406             return ERROR_INVALID_HANDLE;
1407
1408         component = SysAllocString(szComponent);
1409         if (!component)
1410         {
1411             IWineMsiRemotePackage_Release(remote_package);
1412             return ERROR_OUTOFMEMORY;
1413         }
1414
1415         hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1416                                                      piInstalled, piAction);
1417
1418         SysFreeString(component);
1419         IWineMsiRemotePackage_Release(remote_package);
1420
1421         if (FAILED(hr))
1422         {
1423             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1424                 return HRESULT_CODE(hr);
1425
1426             return ERROR_FUNCTION_FAILED;
1427         }
1428
1429         return ERROR_SUCCESS;
1430     }
1431
1432     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
1433     msiobj_release( &package->hdr );
1434     return ret;
1435 }
1436
1437 /***********************************************************************
1438  * MsiGetLanguage (MSI.@)
1439  */
1440 LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
1441 {
1442     MSIPACKAGE* package;
1443     LANGID langid;
1444
1445     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1446     if (!package)
1447     {
1448         HRESULT hr;
1449         LANGID lang;
1450         IWineMsiRemotePackage *remote_package;
1451
1452         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1453         if (!remote_package)
1454             return ERROR_INVALID_HANDLE;
1455
1456         hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);
1457
1458         if (SUCCEEDED(hr))
1459             return lang;
1460
1461         return 0;
1462     }
1463
1464     langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1465     msiobj_release( &package->hdr );
1466     return langid;
1467 }
1468
1469 UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1470 {
1471     static const WCHAR fmt[] = { '%','d',0 };
1472     WCHAR level[6];
1473     UINT r;
1474
1475     TRACE("%p %i\n", package, iInstallLevel);
1476
1477     if (iInstallLevel > 32767)
1478         return ERROR_INVALID_PARAMETER;
1479
1480     if (iInstallLevel < 1)
1481         return MSI_SetFeatureStates( package );
1482
1483     sprintfW( level, fmt, iInstallLevel );
1484     r = msi_set_property( package->db, szInstallLevel, level );
1485     if ( r == ERROR_SUCCESS )
1486         r = MSI_SetFeatureStates( package );
1487
1488     return r;
1489 }
1490
1491 /***********************************************************************
1492  * MsiSetInstallLevel (MSI.@)
1493  */
1494 UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
1495 {
1496     MSIPACKAGE* package;
1497     UINT r;
1498
1499     TRACE("%d %i\n", hInstall, iInstallLevel);
1500
1501     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1502     if (!package)
1503     {
1504         HRESULT hr;
1505         IWineMsiRemotePackage *remote_package;
1506
1507         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1508         if (!remote_package)
1509             return ERROR_INVALID_HANDLE;
1510
1511         hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);
1512
1513         IWineMsiRemotePackage_Release(remote_package);
1514
1515         if (FAILED(hr))
1516         {
1517             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1518                 return HRESULT_CODE(hr);
1519
1520             return ERROR_FUNCTION_FAILED;
1521         }
1522
1523         return ERROR_SUCCESS;
1524     }
1525
1526     r = MSI_SetInstallLevel( package, iInstallLevel );
1527
1528     msiobj_release( &package->hdr );
1529
1530     return r;
1531 }
1532
1533 /***********************************************************************
1534  * MsiGetFeatureValidStatesW (MSI.@)
1535  */
1536 UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1537                   LPDWORD pInstallState)
1538 {
1539     if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1540     FIXME("%d %s %p stub returning %d\n",
1541         hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);
1542
1543     return ERROR_SUCCESS;
1544 }
1545
1546 /***********************************************************************
1547  * MsiGetFeatureValidStatesA (MSI.@)
1548  */
1549 UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1550                   LPDWORD pInstallState)
1551 {
1552     UINT ret;
1553     LPWSTR szwFeature = strdupAtoW(szFeature);
1554
1555     ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);
1556
1557     msi_free(szwFeature);
1558
1559     return ret;
1560 }