mmdevapi/tests: Add tests for volume control interfaces.
[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
553     msi_free( folder->ResolvedTarget );
554     folder->ResolvedTarget = strdupW( path );
555     msi_clean_path( folder->ResolvedTarget );
556     msi_set_property( package->db, folder->Directory, folder->ResolvedTarget );
557
558     LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
559     {
560         child = fl->folder;
561         msi_resolve_target_folder( package, child->Directory, FALSE );
562     }
563 }
564
565 UINT MSI_SetTargetPathW( MSIPACKAGE *package, LPCWSTR szFolder, LPCWSTR szFolderPath )
566 {
567     DWORD attrib, len;
568     MSIFOLDER *folder;
569     MSIFILE *file;
570
571     TRACE("%p %s %s\n", package, debugstr_w(szFolder), debugstr_w(szFolderPath));
572
573     attrib = GetFileAttributesW(szFolderPath);
574     /* native MSI tests writeability by making temporary files at each drive */
575     if (attrib != INVALID_FILE_ATTRIBUTES &&
576         (attrib & FILE_ATTRIBUTE_OFFLINE || attrib & FILE_ATTRIBUTE_READONLY))
577     {
578         return ERROR_FUNCTION_FAILED;
579     }
580     if (!(folder = msi_get_loaded_folder( package, szFolder ))) return ERROR_DIRECTORY;
581
582     len = strlenW( szFolderPath );
583     if (len && szFolderPath[len - 1] != '\\')
584     {
585         WCHAR *path = msi_alloc( (len + 2) * sizeof(WCHAR) );
586         memcpy( path, szFolderPath, len * sizeof(WCHAR) );
587         path[len] = '\\';
588         path[len + 1] = 0;
589         set_target_path( package, folder, path );
590         msi_free( path );
591     }
592     else set_target_path( package, folder, szFolderPath );
593
594     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
595     {
596         const WCHAR *dir;
597         MSICOMPONENT *comp = file->Component;
598
599         if (!comp->Enabled || (comp->assembly && !comp->assembly->application)) continue;
600
601         dir = msi_get_target_folder( package, comp->Directory );
602         msi_free( file->TargetPath );
603         file->TargetPath = msi_build_directory_name( 2, dir, file->FileName );
604     }
605     return ERROR_SUCCESS;
606 }
607
608 /***********************************************************************
609  * MsiSetTargetPathW  (MSI.@)
610  */
611 UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
612                              LPCWSTR szFolderPath)
613 {
614     MSIPACKAGE *package;
615     UINT ret;
616
617     TRACE("%s %s\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
618
619     if ( !szFolder || !szFolderPath )
620         return ERROR_INVALID_PARAMETER;
621
622     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
623     if (!package)
624     {
625         HRESULT hr;
626         BSTR folder, path;
627         IWineMsiRemotePackage *remote_package;
628
629         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
630         if (!remote_package)
631             return ERROR_INVALID_HANDLE;
632
633         folder = SysAllocString( szFolder );
634         path = SysAllocString( szFolderPath );
635         if (!folder || !path)
636         {
637             SysFreeString(folder);
638             SysFreeString(path);
639             IWineMsiRemotePackage_Release( remote_package );
640             return ERROR_OUTOFMEMORY;
641         }
642
643         hr = IWineMsiRemotePackage_SetTargetPath( remote_package, folder, path );
644
645         SysFreeString(folder);
646         SysFreeString(path);
647         IWineMsiRemotePackage_Release( remote_package );
648
649         if (FAILED(hr))
650         {
651             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
652                 return HRESULT_CODE(hr);
653
654             return ERROR_FUNCTION_FAILED;
655         }
656
657         return ERROR_SUCCESS;
658     }
659
660     ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
661     msiobj_release( &package->hdr );
662     return ret;
663 }
664
665 /***********************************************************************
666  *           MsiGetMode    (MSI.@)
667  *
668  * Returns an internal installer state (if it is running in a mode iRunMode)
669  *
670  * PARAMS
671  *   hInstall    [I]  Handle to the installation
672  *   hRunMode    [I]  Checking run mode
673  *        MSIRUNMODE_ADMIN             Administrative mode
674  *        MSIRUNMODE_ADVERTISE         Advertisement mode
675  *        MSIRUNMODE_MAINTENANCE       Maintenance mode
676  *        MSIRUNMODE_ROLLBACKENABLED   Rollback is enabled
677  *        MSIRUNMODE_LOGENABLED        Log file is writing
678  *        MSIRUNMODE_OPERATIONS        Operations in progress??
679  *        MSIRUNMODE_REBOOTATEND       We need to reboot after installation completed
680  *        MSIRUNMODE_REBOOTNOW         We need to reboot to continue the installation
681  *        MSIRUNMODE_CABINET           Files from cabinet are installed
682  *        MSIRUNMODE_SOURCESHORTNAMES  Long names in source files is suppressed
683  *        MSIRUNMODE_TARGETSHORTNAMES  Long names in destination files is suppressed
684  *        MSIRUNMODE_RESERVED11        Reserved
685  *        MSIRUNMODE_WINDOWS9X         Running under Windows95/98
686  *        MSIRUNMODE_ZAWENABLED        Demand installation is supported
687  *        MSIRUNMODE_RESERVED14        Reserved
688  *        MSIRUNMODE_RESERVED15        Reserved
689  *        MSIRUNMODE_SCHEDULED         called from install script
690  *        MSIRUNMODE_ROLLBACK          called from rollback script
691  *        MSIRUNMODE_COMMIT            called from commit script
692  *
693  * RETURNS
694  *    In the state: TRUE
695  *    Not in the state: FALSE
696  *
697  */
698 BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode)
699 {
700     MSIPACKAGE *package;
701     BOOL r = FALSE;
702
703     TRACE("%d %d\n", hInstall, iRunMode);
704
705     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
706     if (!package)
707     {
708         BOOL ret;
709         HRESULT hr;
710         IWineMsiRemotePackage *remote_package;
711
712         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
713         if (!remote_package)
714             return FALSE;
715
716         hr = IWineMsiRemotePackage_GetMode(remote_package, iRunMode, &ret);
717         IWineMsiRemotePackage_Release(remote_package);
718
719         if (hr == S_OK)
720             return ret;
721
722         return FALSE;
723     }
724
725     switch (iRunMode)
726     {
727     case MSIRUNMODE_WINDOWS9X:
728         if (GetVersion() & 0x80000000)
729             r = TRUE;
730         break;
731
732     case MSIRUNMODE_OPERATIONS:
733     case MSIRUNMODE_RESERVED11:
734     case MSIRUNMODE_RESERVED14:
735     case MSIRUNMODE_RESERVED15:
736         break;
737
738     case MSIRUNMODE_SCHEDULED:
739         r = package->scheduled_action_running;
740         break;
741
742     case MSIRUNMODE_ROLLBACK:
743         r = package->rollback_action_running;
744         break;
745
746     case MSIRUNMODE_COMMIT:
747         r = package->commit_action_running;
748         break;
749
750     case MSIRUNMODE_MAINTENANCE:
751         r = msi_get_property_int( package->db, szInstalled, 0 ) != 0;
752         break;
753
754     case MSIRUNMODE_REBOOTATEND:
755         r = package->need_reboot;
756         break;
757
758     case MSIRUNMODE_LOGENABLED:
759         r = (package->log_file != INVALID_HANDLE_VALUE);
760         break;
761
762     default:
763         FIXME("unimplemented run mode: %d\n", iRunMode);
764         r = TRUE;
765     }
766
767     msiobj_release( &package->hdr );
768     return r;
769 }
770
771 /***********************************************************************
772  *           MsiSetMode    (MSI.@)
773  */
774 UINT WINAPI MsiSetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode, BOOL fState)
775 {
776     MSIPACKAGE *package;
777     UINT r;
778
779     TRACE("%d %d %d\n", hInstall, iRunMode, fState);
780
781     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
782     if (!package)
783     {
784         HRESULT hr;
785         IWineMsiRemotePackage *remote_package;
786
787         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
788         if (!remote_package)
789             return FALSE;
790
791         hr = IWineMsiRemotePackage_SetMode( remote_package, iRunMode, fState );
792         IWineMsiRemotePackage_Release( remote_package );
793
794         if (FAILED(hr))
795         {
796             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
797                 return HRESULT_CODE(hr);
798
799             return ERROR_FUNCTION_FAILED;
800         }
801
802         return ERROR_SUCCESS;
803     }
804
805     switch (iRunMode)
806     {
807     case MSIRUNMODE_REBOOTATEND:
808         package->need_reboot = 1;
809         r = ERROR_SUCCESS;
810         break;
811
812     case MSIRUNMODE_REBOOTNOW:
813         FIXME("unimplemented run mode: %d\n", iRunMode);
814         r = ERROR_FUNCTION_FAILED;
815         break;
816
817     default:
818         r = ERROR_ACCESS_DENIED;
819     }
820
821     msiobj_release( &package->hdr );
822     return r;
823 }
824
825 /***********************************************************************
826  * MsiSetFeatureStateA (MSI.@)
827  *
828  * According to the docs, when this is called it immediately recalculates
829  * all the component states as well
830  */
831 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
832                                 INSTALLSTATE iState)
833 {
834     LPWSTR szwFeature = NULL;
835     UINT rc;
836
837     szwFeature = strdupAtoW(szFeature);
838
839     if (!szwFeature)
840         return ERROR_FUNCTION_FAILED;
841    
842     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
843
844     msi_free(szwFeature);
845
846     return rc;
847 }
848
849 /* update component state based on a feature change */
850 void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
851 {
852     INSTALLSTATE newstate;
853     ComponentList *cl;
854
855     newstate = feature->ActionRequest;
856     if (newstate == INSTALLSTATE_ABSENT) newstate = INSTALLSTATE_UNKNOWN;
857
858     LIST_FOR_EACH_ENTRY(cl, &feature->Components, ComponentList, entry)
859     {
860         MSICOMPONENT *component = cl->component;
861
862         if (!component->Enabled) continue;
863
864         TRACE("Modifying (%d): Component %s (Installed %d, Action %d, Request %d)\n",
865             newstate, debugstr_w(component->Component), component->Installed,
866             component->Action, component->ActionRequest);
867
868         if (newstate == INSTALLSTATE_LOCAL)
869         {
870             component->Action = INSTALLSTATE_LOCAL;
871             component->ActionRequest = INSTALLSTATE_LOCAL;
872         }
873         else
874         {
875             ComponentList *clist;
876             MSIFEATURE *f;
877
878             component->hasLocalFeature = FALSE;
879
880             component->Action = newstate;
881             component->ActionRequest = newstate;
882             /* if any other feature wants it local we need to set it local */
883             LIST_FOR_EACH_ENTRY(f, &package->features, MSIFEATURE, entry)
884             {
885                 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
886                      f->ActionRequest != INSTALLSTATE_SOURCE )
887                 {
888                     continue;
889                 }
890                 LIST_FOR_EACH_ENTRY(clist, &f->Components, ComponentList, entry)
891                 {
892                     if (clist->component == component &&
893                         (f->ActionRequest == INSTALLSTATE_LOCAL ||
894                          f->ActionRequest == INSTALLSTATE_SOURCE))
895                     {
896                         TRACE("Saved by %s\n", debugstr_w(f->Feature));
897                         component->hasLocalFeature = TRUE;
898
899                         if (component->Attributes & msidbComponentAttributesOptional)
900                         {
901                             if (f->Attributes & msidbFeatureAttributesFavorSource)
902                             {
903                                 component->Action = INSTALLSTATE_SOURCE;
904                                 component->ActionRequest = INSTALLSTATE_SOURCE;
905                             }
906                             else
907                             {
908                                 component->Action = INSTALLSTATE_LOCAL;
909                                 component->ActionRequest = INSTALLSTATE_LOCAL;
910                             }
911                         }
912                         else if (component->Attributes & msidbComponentAttributesSourceOnly)
913                         {
914                             component->Action = INSTALLSTATE_SOURCE;
915                             component->ActionRequest = INSTALLSTATE_SOURCE;
916                         }
917                         else
918                         {
919                             component->Action = INSTALLSTATE_LOCAL;
920                             component->ActionRequest = INSTALLSTATE_LOCAL;
921                         }
922                     }
923                 }
924             }
925         }
926         TRACE("Result (%d): Component %s (Installed %d, Action %d, Request %d)\n",
927             newstate, debugstr_w(component->Component), component->Installed,
928             component->Action, component->ActionRequest);
929     }
930 }
931
932 UINT WINAPI MSI_SetFeatureStateW( MSIPACKAGE *package, LPCWSTR szFeature, INSTALLSTATE iState )
933 {
934     UINT rc = ERROR_SUCCESS;
935     MSIFEATURE *feature, *child;
936
937     TRACE("%s %i\n", debugstr_w(szFeature), iState);
938
939     feature = msi_get_loaded_feature( package, szFeature );
940     if (!feature)
941         return ERROR_UNKNOWN_FEATURE;
942
943     if (iState == INSTALLSTATE_ADVERTISED && 
944         feature->Attributes & msidbFeatureAttributesDisallowAdvertise)
945         return ERROR_FUNCTION_FAILED;
946
947     feature->ActionRequest = iState;
948
949     ACTION_UpdateComponentStates( package, feature );
950
951     /* update all the features that are children of this feature */
952     LIST_FOR_EACH_ENTRY( child, &package->features, MSIFEATURE, entry )
953     {
954         if (child->Feature_Parent && !strcmpW( szFeature, child->Feature_Parent ))
955             MSI_SetFeatureStateW(package, child->Feature, iState);
956     }
957     
958     return rc;
959 }
960
961 /***********************************************************************
962  * MsiSetFeatureStateW (MSI.@)
963  */
964 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
965                                 INSTALLSTATE iState)
966 {
967     MSIPACKAGE* package;
968     UINT rc = ERROR_SUCCESS;
969
970     TRACE("%s %i\n",debugstr_w(szFeature), iState);
971
972     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
973     if (!package)
974     {
975         HRESULT hr;
976         BSTR feature;
977         IWineMsiRemotePackage *remote_package;
978
979         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
980         if (!remote_package)
981             return ERROR_INVALID_HANDLE;
982
983         feature = SysAllocString(szFeature);
984         if (!feature)
985         {
986             IWineMsiRemotePackage_Release(remote_package);
987             return ERROR_OUTOFMEMORY;
988         }
989
990         hr = IWineMsiRemotePackage_SetFeatureState(remote_package, feature, iState);
991
992         SysFreeString(feature);
993         IWineMsiRemotePackage_Release(remote_package);
994
995         if (FAILED(hr))
996         {
997             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
998                 return HRESULT_CODE(hr);
999
1000             return ERROR_FUNCTION_FAILED;
1001         }
1002
1003         return ERROR_SUCCESS;
1004     }
1005
1006     rc = MSI_SetFeatureStateW(package,szFeature,iState);
1007
1008     msiobj_release( &package->hdr );
1009     return rc;
1010 }
1011
1012 /***********************************************************************
1013 * MsiGetFeatureStateA   (MSI.@)
1014 */
1015 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
1016                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1017 {
1018     LPWSTR szwFeature = NULL;
1019     UINT rc;
1020     
1021     szwFeature = strdupAtoW(szFeature);
1022
1023     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
1024
1025     msi_free( szwFeature);
1026
1027     return rc;
1028 }
1029
1030 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPCWSTR szFeature,
1031                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1032 {
1033     MSIFEATURE *feature;
1034
1035     feature = msi_get_loaded_feature(package,szFeature);
1036     if (!feature)
1037         return ERROR_UNKNOWN_FEATURE;
1038
1039     if (piInstalled)
1040         *piInstalled = feature->Installed;
1041
1042     if (piAction)
1043         *piAction = feature->ActionRequest;
1044
1045     TRACE("returning %i %i\n", feature->Installed, feature->ActionRequest);
1046
1047     return ERROR_SUCCESS;
1048 }
1049
1050 /***********************************************************************
1051 * MsiGetFeatureStateW   (MSI.@)
1052 */
1053 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
1054                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1055 {
1056     MSIPACKAGE* package;
1057     UINT ret;
1058
1059     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled, piAction);
1060
1061     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1062     if (!package)
1063     {
1064         HRESULT hr;
1065         BSTR feature;
1066         IWineMsiRemotePackage *remote_package;
1067
1068         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1069         if (!remote_package)
1070             return ERROR_INVALID_HANDLE;
1071
1072         feature = SysAllocString(szFeature);
1073         if (!feature)
1074         {
1075             IWineMsiRemotePackage_Release(remote_package);
1076             return ERROR_OUTOFMEMORY;
1077         }
1078
1079         hr = IWineMsiRemotePackage_GetFeatureState(remote_package, feature,
1080                                                    piInstalled, piAction);
1081
1082         SysFreeString(feature);
1083         IWineMsiRemotePackage_Release(remote_package);
1084
1085         if (FAILED(hr))
1086         {
1087             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1088                 return HRESULT_CODE(hr);
1089
1090             return ERROR_FUNCTION_FAILED;
1091         }
1092
1093         return ERROR_SUCCESS;
1094     }
1095
1096     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
1097     msiobj_release( &package->hdr );
1098     return ret;
1099 }
1100
1101 /***********************************************************************
1102 * MsiGetFeatureCostA   (MSI.@)
1103 */
1104 UINT WINAPI MsiGetFeatureCostA(MSIHANDLE hInstall, LPCSTR szFeature,
1105                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1106 {
1107     LPWSTR szwFeature = NULL;
1108     UINT rc;
1109
1110     szwFeature = strdupAtoW(szFeature);
1111
1112     rc = MsiGetFeatureCostW(hInstall, szwFeature, iCostTree, iState, piCost);
1113
1114     msi_free(szwFeature);
1115
1116     return rc;
1117 }
1118
1119 static INT feature_cost( MSIFEATURE *feature )
1120 {
1121     INT cost = 0;
1122     MSICOMPONENT *comp;
1123
1124     LIST_FOR_EACH_ENTRY( comp, &feature->Components, MSICOMPONENT, entry )
1125     {
1126         cost += comp->Cost;
1127     }
1128     return cost;
1129 }
1130
1131 UINT MSI_GetFeatureCost( MSIPACKAGE *package, MSIFEATURE *feature, MSICOSTTREE tree,
1132                          INSTALLSTATE state, LPINT cost )
1133 {
1134     TRACE("%s, %u, %d, %p\n", debugstr_w(feature->Feature), tree, state, cost);
1135
1136     *cost = 0;
1137     switch (tree)
1138     {
1139     case MSICOSTTREE_CHILDREN:
1140     {
1141         MSIFEATURE *child;
1142
1143         LIST_FOR_EACH_ENTRY( child, &feature->Children, MSIFEATURE, entry )
1144         {
1145             if (child->ActionRequest == state)
1146                 *cost += feature_cost( child );
1147         }
1148         break;
1149     }
1150     case MSICOSTTREE_PARENTS:
1151     {
1152         const WCHAR *feature_parent = feature->Feature_Parent;
1153         for (;;)
1154         {
1155             MSIFEATURE *parent = msi_get_loaded_feature( package, feature_parent );
1156             if (!parent)
1157                 break;
1158
1159             if (parent->ActionRequest == state)
1160                 *cost += feature_cost( parent );
1161
1162             feature_parent = parent->Feature_Parent;
1163         }
1164         break;
1165     }
1166     case MSICOSTTREE_SELFONLY:
1167         if (feature->ActionRequest == state)
1168             *cost = feature_cost( feature );
1169         break;
1170
1171     default:
1172         WARN("unhandled cost tree %u\n", tree);
1173         break;
1174     }
1175
1176     *cost /= 512;
1177     return ERROR_SUCCESS;
1178 }
1179
1180 /***********************************************************************
1181 * MsiGetFeatureCostW   (MSI.@)
1182 */
1183 UINT WINAPI MsiGetFeatureCostW(MSIHANDLE hInstall, LPCWSTR szFeature,
1184                   MSICOSTTREE iCostTree, INSTALLSTATE iState, LPINT piCost)
1185 {
1186     MSIPACKAGE *package;
1187     MSIFEATURE *feature;
1188     UINT ret;
1189
1190     TRACE("(%d %s %i %i %p)\n", hInstall, debugstr_w(szFeature),
1191           iCostTree, iState, piCost);
1192
1193     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1194     if (!package)
1195     {
1196         HRESULT hr;
1197         BSTR feature;
1198         IWineMsiRemotePackage *remote_package;
1199
1200         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1201         if (!remote_package)
1202             return ERROR_INVALID_HANDLE;
1203
1204         feature = SysAllocString(szFeature);
1205         if (!feature)
1206         {
1207             IWineMsiRemotePackage_Release(remote_package);
1208             return ERROR_OUTOFMEMORY;
1209         }
1210
1211         hr = IWineMsiRemotePackage_GetFeatureCost(remote_package, feature,
1212                                                   iCostTree, iState, piCost);
1213
1214         SysFreeString(feature);
1215         IWineMsiRemotePackage_Release(remote_package);
1216
1217         if (FAILED(hr))
1218         {
1219             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1220                 return HRESULT_CODE(hr);
1221
1222             return ERROR_FUNCTION_FAILED;
1223         }
1224
1225         return ERROR_SUCCESS;
1226     }
1227
1228     feature = msi_get_loaded_feature(package, szFeature);
1229
1230     if (feature)
1231         ret = MSI_GetFeatureCost(package, feature, iCostTree, iState, piCost);
1232     else
1233         ret = ERROR_UNKNOWN_FEATURE;
1234
1235     msiobj_release( &package->hdr );
1236     return ret;
1237 }
1238
1239 /***********************************************************************
1240  * MsiSetComponentStateA (MSI.@)
1241  */
1242 UINT WINAPI MsiSetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1243                                   INSTALLSTATE iState)
1244 {
1245     UINT rc;
1246     LPWSTR szwComponent = strdupAtoW(szComponent);
1247
1248     rc = MsiSetComponentStateW(hInstall, szwComponent, iState);
1249
1250     msi_free(szwComponent);
1251
1252     return rc;
1253 }
1254
1255 /***********************************************************************
1256  * MsiGetComponentStateA (MSI.@)
1257  */
1258 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPCSTR szComponent,
1259                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1260 {
1261     LPWSTR szwComponent= NULL;
1262     UINT rc;
1263     
1264     szwComponent= strdupAtoW(szComponent);
1265
1266     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
1267
1268     msi_free( szwComponent);
1269
1270     return rc;
1271 }
1272
1273 static UINT MSI_SetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1274                                    INSTALLSTATE iState)
1275 {
1276     MSICOMPONENT *comp;
1277
1278     TRACE("%p %s %d\n", package, debugstr_w(szComponent), iState);
1279
1280     comp = msi_get_loaded_component(package, szComponent);
1281     if (!comp)
1282         return ERROR_UNKNOWN_COMPONENT;
1283
1284     if (comp->Enabled)
1285         comp->Action = iState;
1286
1287     return ERROR_SUCCESS;
1288 }
1289
1290 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPCWSTR szComponent,
1291                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1292 {
1293     MSICOMPONENT *comp;
1294
1295     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent),
1296            piInstalled, piAction);
1297
1298     comp = msi_get_loaded_component(package,szComponent);
1299     if (!comp)
1300         return ERROR_UNKNOWN_COMPONENT;
1301
1302     if (piInstalled)
1303     {
1304         if (comp->Enabled)
1305             *piInstalled = comp->Installed;
1306         else
1307             *piInstalled = INSTALLSTATE_UNKNOWN;
1308     }
1309
1310     if (piAction)
1311     {
1312         if (comp->Enabled)
1313             *piAction = comp->Action;
1314         else
1315             *piAction = INSTALLSTATE_UNKNOWN;
1316     }
1317
1318     TRACE("states (%i, %i)\n", comp->Installed, comp->Action );
1319     return ERROR_SUCCESS;
1320 }
1321
1322 /***********************************************************************
1323  * MsiSetComponentStateW (MSI.@)
1324  */
1325 UINT WINAPI MsiSetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1326                                   INSTALLSTATE iState)
1327 {
1328     MSIPACKAGE* package;
1329     UINT ret;
1330
1331     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1332     if (!package)
1333     {
1334         HRESULT hr;
1335         BSTR component;
1336         IWineMsiRemotePackage *remote_package;
1337
1338         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1339         if (!remote_package)
1340             return ERROR_INVALID_HANDLE;
1341
1342         component = SysAllocString(szComponent);
1343         if (!component)
1344         {
1345             IWineMsiRemotePackage_Release(remote_package);
1346             return ERROR_OUTOFMEMORY;
1347         }
1348
1349         hr = IWineMsiRemotePackage_SetComponentState(remote_package, component, iState);
1350
1351         SysFreeString(component);
1352         IWineMsiRemotePackage_Release(remote_package);
1353
1354         if (FAILED(hr))
1355         {
1356             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1357                 return HRESULT_CODE(hr);
1358
1359             return ERROR_FUNCTION_FAILED;
1360         }
1361
1362         return ERROR_SUCCESS;
1363     }
1364
1365     ret = MSI_SetComponentStateW(package, szComponent, iState);
1366     msiobj_release(&package->hdr);
1367     return ret;
1368 }
1369
1370 /***********************************************************************
1371  * MsiGetComponentStateW (MSI.@)
1372  */
1373 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPCWSTR szComponent,
1374                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
1375 {
1376     MSIPACKAGE* package;
1377     UINT ret;
1378
1379     TRACE("%d %s %p %p\n", hInstall, debugstr_w(szComponent),
1380            piInstalled, piAction);
1381
1382     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1383     if (!package)
1384     {
1385         HRESULT hr;
1386         BSTR component;
1387         IWineMsiRemotePackage *remote_package;
1388
1389         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1390         if (!remote_package)
1391             return ERROR_INVALID_HANDLE;
1392
1393         component = SysAllocString(szComponent);
1394         if (!component)
1395         {
1396             IWineMsiRemotePackage_Release(remote_package);
1397             return ERROR_OUTOFMEMORY;
1398         }
1399
1400         hr = IWineMsiRemotePackage_GetComponentState(remote_package, component,
1401                                                      piInstalled, piAction);
1402
1403         SysFreeString(component);
1404         IWineMsiRemotePackage_Release(remote_package);
1405
1406         if (FAILED(hr))
1407         {
1408             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1409                 return HRESULT_CODE(hr);
1410
1411             return ERROR_FUNCTION_FAILED;
1412         }
1413
1414         return ERROR_SUCCESS;
1415     }
1416
1417     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
1418     msiobj_release( &package->hdr );
1419     return ret;
1420 }
1421
1422 /***********************************************************************
1423  * MsiGetLanguage (MSI.@)
1424  */
1425 LANGID WINAPI MsiGetLanguage(MSIHANDLE hInstall)
1426 {
1427     MSIPACKAGE* package;
1428     LANGID langid;
1429
1430     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1431     if (!package)
1432     {
1433         HRESULT hr;
1434         LANGID lang;
1435         IWineMsiRemotePackage *remote_package;
1436
1437         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1438         if (!remote_package)
1439             return ERROR_INVALID_HANDLE;
1440
1441         hr = IWineMsiRemotePackage_GetLanguage(remote_package, &lang);
1442
1443         if (SUCCEEDED(hr))
1444             return lang;
1445
1446         return 0;
1447     }
1448
1449     langid = msi_get_property_int( package->db, szProductLanguage, 0 );
1450     msiobj_release( &package->hdr );
1451     return langid;
1452 }
1453
1454 UINT MSI_SetInstallLevel( MSIPACKAGE *package, int iInstallLevel )
1455 {
1456     static const WCHAR fmt[] = { '%','d',0 };
1457     WCHAR level[6];
1458     UINT r;
1459
1460     TRACE("%p %i\n", package, iInstallLevel);
1461
1462     if (iInstallLevel > 32767)
1463         return ERROR_INVALID_PARAMETER;
1464
1465     if (iInstallLevel < 1)
1466         return MSI_SetFeatureStates( package );
1467
1468     sprintfW( level, fmt, iInstallLevel );
1469     r = msi_set_property( package->db, szInstallLevel, level );
1470     if ( r == ERROR_SUCCESS )
1471         r = MSI_SetFeatureStates( package );
1472
1473     return r;
1474 }
1475
1476 /***********************************************************************
1477  * MsiSetInstallLevel (MSI.@)
1478  */
1479 UINT WINAPI MsiSetInstallLevel(MSIHANDLE hInstall, int iInstallLevel)
1480 {
1481     MSIPACKAGE* package;
1482     UINT r;
1483
1484     TRACE("%d %i\n", hInstall, iInstallLevel);
1485
1486     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
1487     if (!package)
1488     {
1489         HRESULT hr;
1490         IWineMsiRemotePackage *remote_package;
1491
1492         remote_package = (IWineMsiRemotePackage *)msi_get_remote(hInstall);
1493         if (!remote_package)
1494             return ERROR_INVALID_HANDLE;
1495
1496         hr = IWineMsiRemotePackage_SetInstallLevel(remote_package, iInstallLevel);
1497
1498         IWineMsiRemotePackage_Release(remote_package);
1499
1500         if (FAILED(hr))
1501         {
1502             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1503                 return HRESULT_CODE(hr);
1504
1505             return ERROR_FUNCTION_FAILED;
1506         }
1507
1508         return ERROR_SUCCESS;
1509     }
1510
1511     r = MSI_SetInstallLevel( package, iInstallLevel );
1512
1513     msiobj_release( &package->hdr );
1514
1515     return r;
1516 }
1517
1518 /***********************************************************************
1519  * MsiGetFeatureValidStatesW (MSI.@)
1520  */
1521 UINT WINAPI MsiGetFeatureValidStatesW(MSIHANDLE hInstall, LPCWSTR szFeature,
1522                   LPDWORD pInstallState)
1523 {
1524     if(pInstallState) *pInstallState = 1<<INSTALLSTATE_LOCAL;
1525     FIXME("%d %s %p stub returning %d\n",
1526         hInstall, debugstr_w(szFeature), pInstallState, pInstallState ? *pInstallState : 0);
1527
1528     return ERROR_SUCCESS;
1529 }
1530
1531 /***********************************************************************
1532  * MsiGetFeatureValidStatesA (MSI.@)
1533  */
1534 UINT WINAPI MsiGetFeatureValidStatesA(MSIHANDLE hInstall, LPCSTR szFeature,
1535                   LPDWORD pInstallState)
1536 {
1537     UINT ret;
1538     LPWSTR szwFeature = strdupAtoW(szFeature);
1539
1540     ret = MsiGetFeatureValidStatesW(hInstall, szwFeature, pInstallState);
1541
1542     msi_free(szwFeature);
1543
1544     return ret;
1545 }