mmdevapi/tests: Fix wrong buffer unit and memory leaks.
[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 * MsiGetFeatureStateA   (MSI.@)
1032 */
1033 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1034                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1035 {
1036     LPWSTR szwFeature = NULL;
1037     UINT rc;
1038     
1039     szwFeature = strdupAtoW(szFeature);
1040
1041     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
1042
1043     msi_free( szwFeature);
1044
1045     return rc;
1046 }
1047
1048 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1049                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1050 {
1051     MSIFEATURE *feature;
1052
1053     feature = msi_get_loaded_feature(package,szFeature);
1054     if (!feature)
1055         return ERROR_UNKNOWN_FEATURE;
1056
1057     if (piInstalled)
1058         *piInstalled = feature->Installed;
1059
1060     if (piAction)
1061         *piAction = feature->ActionRequest;
1062
1063     TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1064
1065     return ERROR_SUCCESS;
1066 }
1067
1068 /***********************************************************************
1069 * MsiGetFeatureStateW   (MSI.@)
1070 */
1071 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1072                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1073 {
1074     MSIPACKAGE* package;
1075     UINT ret;
1076
1077     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1078
1079     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1080     if (!package)
1081     {
1082         HRESULT hr;
1083         BSTR feature;
1084         IWineMsiRemotePackage *remote_package;
1085
1086         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1087         if (!remote_package)
1088             return ERROR_INVALID_HANDLE;
1089
1090         feature = SysAllocString(szFeature);
1091         if (!feature)
1092         {
1093             IWineMsiRemotePackage_Release(remote_package);
1094             return ERROR_OUTOFMEMORY;
1095         }
1096
1097         hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
1098                                                    piInstalled, piAction);
1099
1100         SysFreeString(feature);
1101         IWineMsiRemotePackage_Release(remote_package);
1102
1103         if (FAILED(hr))
1104         {
1105             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1106                 return HRESULT_CODE(hr);
1107
1108             return ERROR_FUNCTION_FAILED;
1109         }
1110
1111         return ERROR_SUCCESS;
1112     }
1113
1114     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
1115     msiobj_release( &package->hdr );
1116     return ret;
1117 }
1118
1119 /***********************************************************************
1120 * MsiGetFeatureCostA   (MSI.@)
1121 */
1122 UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1123                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1124 {
1125     LPWSTR szwFeature = NULL;
1126     UINT rc;
1127
1128     szwFeature = strdupAtoW(szFeature);
1129
1130     rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);
1131
1132     msi_free(szwFeature);
1133
1134     return rc;
1135 }
1136
1137 static INT feature_cost( MSIFEATURE *feature )
1138 {
1139     INT cost = 0;
1140     MSICOMPONENT *comp;
1141
1142     LIST_FOR_EACH_ENTRY( comp, &feature->Components, MSICOMPONENT, entry )
1143     {
1144         cost += comp->Cost;
1145     }
1146     return cost;
1147 }
1148
1149 UINT MSI_GetFeatureCost( MSIPACKAGE *package, MSIFEATURE *feature, MSICOSTTREE tree,
1150                          INSTALLSTATE state, LPINT cost )
1151 {
1152     TRACE("%s, %u, %d, %p\n", debugstr_w(feature->Feature), tree, state, cost);
1153
1154     *cost = 0;
1155     switch (tree)
1156     {
1157     case MSICOSTTREE_CHILDREN:
1158     {
1159         MSIFEATURE *child;
1160
1161         LIST_FOR_EACH_ENTRY( child, &feature->Children, MSIFEATURE, entry )
1162         {
1163             if (child->ActionRequest == state)
1164                 *cost += feature_cost( child );
1165         }
1166         break;
1167     }
1168     case MSICOSTTREE_PARENTS:
1169     {
1170         const WCHAR *feature_parent = feature->Feature_Parent;
1171         for (;;)
1172         {
1173             MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1174             if (!parent)
1175                 break;
1176
1177             if (parent->ActionRequest == state)
1178                 *cost += feature_cost( parent );
1179
1180             feature_parent = parent->Feature_Parent;
1181         }
1182         break;
1183     }
1184     case MSICOSTTREE_SELFONLY:
1185         if (feature->ActionRequest == state)
1186             *cost = feature_cost( feature );
1187         break;
1188
1189     default:
1190         WARN("unhandled cost tree %u\n", tree);
1191         break;
1192     }
1193
1194     *cost /= 512;
1195     return ERROR_SUCCESS;
1196 }
1197
1198 /***********************************************************************
1199 * MsiGetFeatureCostW   (MSI.@)
1200 */
1201 UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1202                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1203 {
1204     MSIPACKAGE *package;
1205     MSIFEATURE *feature;
1206     UINT ret;
1207
1208     TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1209           iCostTree, iState, piCost);
1210
1211     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1212     if (!package)
1213     {
1214         HRESULT hr;
1215         BSTR feature;
1216         IWineMsiRemotePackage *remote_package;
1217
1218         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1219         if (!remote_package)
1220             return ERROR_INVALID_HANDLE;
1221
1222         feature = SysAllocString(szFeature);
1223         if (!feature)
1224         {
1225             IWineMsiRemotePackage_Release(remote_package);
1226             return ERROR_OUTOFMEMORY;
1227         }
1228
1229         hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
1230                                                   iCostTree, iState, piCost);
1231
1232         SysFreeString(feature);
1233         IWineMsiRemotePackage_Release(remote_package);
1234
1235         if (FAILED(hr))
1236         {
1237             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1238                 return HRESULT_CODE(hr);
1239
1240             return ERROR_FUNCTION_FAILED;
1241         }
1242
1243         return ERROR_SUCCESS;
1244     }
1245
1246     feature = msi_get_loaded_feature(package, szFeature);
1247
1248     if (feature)
1249         ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
1250     else
1251         ret = ERROR_UNKNOWN_FEATURE;
1252
1253     msiobj_release( &package->hdr );
1254     return ret;
1255 }
1256
1257 /***********************************************************************
1258  * MsiSetComponentStateA (MSI.@)
1259  */
1260 UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1261                                   INSTALLSTATE iState)
1262 {
1263     UINT rc;
1264     LPWSTR szwComponent = strdupAtoW(szComponent);
1265
1266     rc = MsiSetComponentStateW(hInstall, szwComponent, iState);
1267
1268     msi_free(szwComponent);
1269
1270     return rc;
1271 }
1272
1273 /***********************************************************************
1274  * MsiGetComponentStateA (MSI.@)
1275  */
1276 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1277                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1278 {
1279     LPWSTR szwComponent= NULL;
1280     UINT rc;
1281     
1282     szwComponent= strdupAtoW(szComponent);
1283
1284     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
1285
1286     msi_free( szwComponent);
1287
1288     return rc;
1289 }
1290
1291 static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1292                                    INSTALLSTATE iState)
1293 {
1294     MSICOMPONENT *comp;
1295
1296     TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);
1297
1298     comp = msi_get_loaded_component(package, szComponent);
1299     if (!comp)
1300         return ERROR_UNKNOWN_COMPONENT;
1301
1302     if (comp->Enabled)
1303         comp->Action = iState;
1304
1305     return ERROR_SUCCESS;
1306 }
1307
1308 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1309                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1310 {
1311     MSICOMPONENT *comp;
1312
1313     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
1314            piInstalled, piAction);
1315
1316     comp = msi_get_loaded_component(package,szComponent);
1317     if (!comp)
1318         return ERROR_UNKNOWN_COMPONENT;
1319
1320     if (piInstalled)
1321     {
1322         if (comp->Enabled)
1323             *piInstalled = comp->Installed;
1324         else
1325             *piInstalled = INSTALLSTATE_UNKNOWN;
1326     }
1327
1328     if (piAction)
1329     {
1330         if (comp->Enabled)
1331             *piAction = comp->Action;
1332         else
1333             *piAction = INSTALLSTATE_UNKNOWN;
1334     }
1335
1336     TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1337     return ERROR_SUCCESS;
1338 }
1339
1340 /***********************************************************************
1341  * MsiSetComponentStateW (MSI.@)
1342  */
1343 UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1344                                   INSTALLSTATE iState)
1345 {
1346     MSIPACKAGE* package;
1347     UINT ret;
1348
1349     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1350     if (!package)
1351     {
1352         HRESULT hr;
1353         BSTR component;
1354         IWineMsiRemotePackage *remote_package;
1355
1356         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1357         if (!remote_package)
1358             return ERROR_INVALID_HANDLE;
1359
1360         component = SysAllocString(szComponent);
1361         if (!component)
1362         {
1363             IWineMsiRemotePackage_Release(remote_package);
1364             return ERROR_OUTOFMEMORY;
1365         }
1366
1367         hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1368
1369         SysFreeString(component);
1370         IWineMsiRemotePackage_Release(remote_package);
1371
1372         if (FAILED(hr))
1373         {
1374             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1375                 return HRESULT_CODE(hr);
1376
1377             return ERROR_FUNCTION_FAILED;
1378         }
1379
1380         return ERROR_SUCCESS;
1381     }
1382
1383     ret = MSI_SetComponentStateW(package, szComponent, iState);
1384     msiobj_release(&package->hdr);
1385     return ret;
1386 }
1387
1388 /***********************************************************************
1389  * MsiGetComponentStateW (MSI.@)
1390  */
1391 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1392                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1393 {
1394     MSIPACKAGE* package;
1395     UINT ret;
1396
1397     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1398            piInstalled, piAction);
1399
1400     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1401     if (!package)
1402     {
1403         HRESULT hr;
1404         BSTR component;
1405         IWineMsiRemotePackage *remote_package;
1406
1407         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1408         if (!remote_package)
1409             return ERROR_INVALID_HANDLE;
1410
1411         component = SysAllocString(szComponent);
1412         if (!component)
1413         {
1414             IWineMsiRemotePackage_Release(remote_package);
1415             return ERROR_OUTOFMEMORY;
1416         }
1417
1418         hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1419                                                      piInstalled, piAction);
1420
1421         SysFreeString(component);
1422         IWineMsiRemotePackage_Release(remote_package);
1423
1424         if (FAILED(hr))
1425         {
1426             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1427                 return HRESULT_CODE(hr);
1428
1429             return ERROR_FUNCTION_FAILED;
1430         }
1431
1432         return ERROR_SUCCESS;
1433     }
1434
1435     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
1436     msiobj_release( &package->hdr );
1437     return ret;
1438 }
1439
1440 /***********************************************************************
1441  * MsiGetLanguage (MSI.@)
1442  */
1443 LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
1444 {
1445     MSIPACKAGE* package;
1446     LANGID langid;
1447
1448     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1449     if (!package)
1450     {
1451         HRESULT hr;
1452         LANGID lang;
1453         IWineMsiRemotePackage *remote_package;
1454
1455         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1456         if (!remote_package)
1457             return ERROR_INVALID_HANDLE;
1458
1459         hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);
1460
1461         if (SUCCEEDED(hr))
1462             return lang;
1463
1464         return 0;
1465     }
1466
1467     langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1468     msiobj_release( &package->hdr );
1469     return langid;
1470 }
1471
1472 UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1473 {
1474     static const WCHAR fmt[] = { '%','d',0 };
1475     WCHAR level[6];
1476     UINT r;
1477
1478     TRACE("%p %i\n", package, iInstallLevel);
1479
1480     if (iInstallLevel > 32767)
1481         return ERROR_INVALID_PARAMETER;
1482
1483     if (iInstallLevel < 1)
1484         return MSI_SetFeatureStates( package );
1485
1486     sprintfW( level, fmt, iInstallLevel );
1487     r = msi_set_property( package->db, szInstallLevel, level );
1488     if ( r == ERROR_SUCCESS )
1489         r = MSI_SetFeatureStates( package );
1490
1491     return r;
1492 }
1493
1494 /***********************************************************************
1495  * MsiSetInstallLevel (MSI.@)
1496  */
1497 UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
1498 {
1499     MSIPACKAGE* package;
1500     UINT r;
1501
1502     TRACE("%d %i\n", hInstall, iInstallLevel);
1503
1504     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1505     if (!package)
1506     {
1507         HRESULT hr;
1508         IWineMsiRemotePackage *remote_package;
1509
1510         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1511         if (!remote_package)
1512             return ERROR_INVALID_HANDLE;
1513
1514         hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);
1515
1516         IWineMsiRemotePackage_Release(remote_package);
1517
1518         if (FAILED(hr))
1519         {
1520             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1521                 return HRESULT_CODE(hr);
1522
1523             return ERROR_FUNCTION_FAILED;
1524         }
1525
1526         return ERROR_SUCCESS;
1527     }
1528
1529     r = MSI_SetInstallLevel( package, iInstallLevel );
1530
1531     msiobj_release( &package->hdr );
1532
1533     return r;
1534 }
1535
1536 /***********************************************************************
1537  * MsiGetFeatureValidStatesW (MSI.@)
1538  */
1539 UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1540                   LPDWORD pInstallState)
1541 {
1542     if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1543     FIXME("%d %s %p stub returning %d\n",
1544         hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);
1545
1546     return ERROR_SUCCESS;
1547 }
1548
1549 /***********************************************************************
1550  * MsiGetFeatureValidStatesA (MSI.@)
1551  */
1552 UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1553                   LPDWORD pInstallState)
1554 {
1555     UINT ret;
1556     LPWSTR szwFeature = strdupAtoW(szFeature);
1557
1558     ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);
1559
1560     msi_free(szwFeature);
1561
1562     return ret;
1563 }