e6d5b6a5f232a7a35a238435e272d7b4265af4e4
[reactos.git] / reactos / dll / win32 / msi / package.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2004 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 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #define COBJMACROS
28
29 #include <stdarg.h>
30 #include <windef.h>
31 #include <winbase.h>
32 #include <winreg.h>
33 //#include "winnls.h"
34 #include <shlwapi.h>
35 #include <wingdi.h>
36 #include <wincon.h>
37 #include <wine/debug.h>
38 //#include "msi.h"
39 //#include "msiquery.h"
40 //#include "objidl.h"
41 //#include "wincrypt.h"
42 //#include "winuser.h"
43 #include <wininet.h>
44 //#include "winver.h"
45 //#include "urlmon.h"
46 #include <shlobj.h>
47 #include <wine/unicode.h>
48 //#include "objbase.h"
49 //#include "msidefs.h"
50 #include <sddl.h>
51
52 #include "msipriv.h"
53 #include <msiserver.h>
54
55 WINE_DEFAULT_DEBUG_CHANNEL(msi);
56
57 static void remove_tracked_tempfiles( MSIPACKAGE *package )
58 {
59 struct list *item, *cursor;
60
61 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
62 {
63 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
64
65 list_remove( &temp->entry );
66 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
67 DeleteFileW( temp->Path );
68 msi_free( temp->Path );
69 msi_free( temp );
70 }
71 }
72
73 static void free_feature( MSIFEATURE *feature )
74 {
75 struct list *item, *cursor;
76
77 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
78 {
79 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
80 list_remove( &fl->entry );
81 msi_free( fl );
82 }
83
84 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
85 {
86 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
87 list_remove( &cl->entry );
88 msi_free( cl );
89 }
90 msi_free( feature->Feature );
91 msi_free( feature->Feature_Parent );
92 msi_free( feature->Directory );
93 msi_free( feature->Description );
94 msi_free( feature->Title );
95 msi_free( feature );
96 }
97
98 static void free_folder( MSIFOLDER *folder )
99 {
100 struct list *item, *cursor;
101
102 LIST_FOR_EACH_SAFE( item, cursor, &folder->children )
103 {
104 FolderList *fl = LIST_ENTRY( item, FolderList, entry );
105 list_remove( &fl->entry );
106 msi_free( fl );
107 }
108 msi_free( folder->Parent );
109 msi_free( folder->Directory );
110 msi_free( folder->TargetDefault );
111 msi_free( folder->SourceLongPath );
112 msi_free( folder->SourceShortPath );
113 msi_free( folder->ResolvedTarget );
114 msi_free( folder->ResolvedSource );
115 msi_free( folder );
116 }
117
118 static void free_extension( MSIEXTENSION *ext )
119 {
120 struct list *item, *cursor;
121
122 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
123 {
124 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
125
126 list_remove( &verb->entry );
127 msi_free( verb->Verb );
128 msi_free( verb->Command );
129 msi_free( verb->Argument );
130 msi_free( verb );
131 }
132
133 msi_free( ext->Extension );
134 msi_free( ext->ProgIDText );
135 msi_free( ext );
136 }
137
138 static void free_assembly( MSIASSEMBLY *assembly )
139 {
140 msi_free( assembly->feature );
141 msi_free( assembly->manifest );
142 msi_free( assembly->application );
143 msi_free( assembly->display_name );
144 if (assembly->tempdir) RemoveDirectoryW( assembly->tempdir );
145 msi_free( assembly->tempdir );
146 msi_free( assembly );
147 }
148
149 void msi_free_action_script( MSIPACKAGE *package, UINT script )
150 {
151 UINT i;
152 for (i = 0; i < package->script->ActionCount[script]; i++)
153 msi_free( package->script->Actions[script][i] );
154
155 msi_free( package->script->Actions[script] );
156 package->script->Actions[script] = NULL;
157 package->script->ActionCount[script] = 0;
158 }
159
160 static void free_package_structures( MSIPACKAGE *package )
161 {
162 struct list *item, *cursor;
163
164 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
165 {
166 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
167 list_remove( &feature->entry );
168 free_feature( feature );
169 }
170
171 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
172 {
173 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
174 list_remove( &folder->entry );
175 free_folder( folder );
176 }
177
178 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
179 {
180 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
181
182 list_remove( &comp->entry );
183 msi_free( comp->Component );
184 msi_free( comp->ComponentId );
185 msi_free( comp->Directory );
186 msi_free( comp->Condition );
187 msi_free( comp->KeyPath );
188 msi_free( comp->FullKeypath );
189 if (comp->assembly) free_assembly( comp->assembly );
190 msi_free( comp );
191 }
192
193 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
194 {
195 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
196
197 list_remove( &file->entry );
198 msi_free( file->File );
199 msi_free( file->FileName );
200 msi_free( file->ShortName );
201 msi_free( file->LongName );
202 msi_free( file->Version );
203 msi_free( file->Language );
204 msi_free( file->TargetPath );
205 msi_free( file );
206 }
207
208 /* clean up extension, progid, class and verb structures */
209 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
210 {
211 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
212
213 list_remove( &cls->entry );
214 msi_free( cls->clsid );
215 msi_free( cls->Context );
216 msi_free( cls->Description );
217 msi_free( cls->FileTypeMask );
218 msi_free( cls->IconPath );
219 msi_free( cls->DefInprocHandler );
220 msi_free( cls->DefInprocHandler32 );
221 msi_free( cls->Argument );
222 msi_free( cls->ProgIDText );
223 msi_free( cls );
224 }
225
226 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
227 {
228 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
229
230 list_remove( &ext->entry );
231 free_extension( ext );
232 }
233
234 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
235 {
236 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
237
238 list_remove( &progid->entry );
239 msi_free( progid->ProgID );
240 msi_free( progid->Description );
241 msi_free( progid->IconPath );
242 msi_free( progid );
243 }
244
245 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
246 {
247 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
248
249 list_remove( &mt->entry );
250 msi_free( mt->suffix );
251 msi_free( mt->clsid );
252 msi_free( mt->ContentType );
253 msi_free( mt );
254 }
255
256 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
257 {
258 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
259
260 list_remove( &appid->entry );
261 msi_free( appid->AppID );
262 msi_free( appid->RemoteServerName );
263 msi_free( appid->LocalServer );
264 msi_free( appid->ServiceParameters );
265 msi_free( appid->DllSurrogate );
266 msi_free( appid );
267 }
268
269 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
270 {
271 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
272
273 list_remove( &info->entry );
274 msi_free( info->value );
275 msi_free( info );
276 }
277
278 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
279 {
280 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
281
282 list_remove( &info->entry );
283 msi_free( info->volume_label );
284 msi_free( info->disk_prompt );
285 msi_free( info );
286 }
287
288 if (package->script)
289 {
290 INT i;
291 UINT j;
292
293 for (i = 0; i < SCRIPT_MAX; i++)
294 msi_free_action_script( package, i );
295
296 for (j = 0; j < package->script->UniqueActionsCount; j++)
297 msi_free( package->script->UniqueActions[j] );
298
299 msi_free( package->script->UniqueActions );
300 msi_free( package->script );
301 }
302
303 LIST_FOR_EACH_SAFE( item, cursor, &package->binaries )
304 {
305 MSIBINARY *binary = LIST_ENTRY( item, MSIBINARY, entry );
306
307 list_remove( &binary->entry );
308 if (binary->module)
309 FreeLibrary( binary->module );
310 if (!DeleteFileW( binary->tmpfile ))
311 ERR("failed to delete %s (%u)\n", debugstr_w(binary->tmpfile), GetLastError());
312 msi_free( binary->source );
313 msi_free( binary->tmpfile );
314 msi_free( binary );
315 }
316
317 LIST_FOR_EACH_SAFE( item, cursor, &package->cabinet_streams )
318 {
319 MSICABINETSTREAM *cab = LIST_ENTRY( item, MSICABINETSTREAM, entry );
320
321 list_remove( &cab->entry );
322 IStorage_Release( cab->storage );
323 msi_free( cab->stream );
324 msi_free( cab );
325 }
326
327 LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
328 {
329 MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
330
331 list_remove( &patch->entry );
332 if (patch->delete_on_close && !DeleteFileW( patch->localfile ))
333 {
334 ERR("failed to delete %s (%u)\n", debugstr_w(patch->localfile), GetLastError());
335 }
336 msi_free_patchinfo( patch );
337 }
338
339 msi_free( package->BaseURL );
340 msi_free( package->PackagePath );
341 msi_free( package->ProductCode );
342 msi_free( package->ActionFormat );
343 msi_free( package->LastAction );
344 msi_free( package->langids );
345
346 remove_tracked_tempfiles(package);
347
348 /* cleanup control event subscriptions */
349 ControlEvent_CleanupSubscriptions( package );
350 }
351
352 static void MSI_FreePackage( MSIOBJECTHDR *arg)
353 {
354 MSIPACKAGE *package = (MSIPACKAGE *)arg;
355
356 msi_destroy_assembly_caches( package );
357
358 if( package->dialog )
359 msi_dialog_destroy( package->dialog );
360
361 msiobj_release( &package->db->hdr );
362 free_package_structures(package);
363 CloseHandle( package->log_file );
364
365 if (package->delete_on_close) DeleteFileW( package->localfile );
366 msi_free( package->localfile );
367 }
368
369 static UINT create_temp_property_table(MSIPACKAGE *package)
370 {
371 static const WCHAR query[] = {
372 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
373 '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
374 '`','_','P','r','o','p','e','r','t','y','`',' ',
375 'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
376 'T','E','M','P','O','R','A','R','Y',',',' ',
377 '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
378 'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
379 ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
380 '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
381 MSIQUERY *view;
382 UINT rc;
383
384 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
385 if (rc != ERROR_SUCCESS)
386 return rc;
387
388 rc = MSI_ViewExecute(view, 0);
389 MSI_ViewClose(view);
390 msiobj_release(&view->hdr);
391 return rc;
392 }
393
394 UINT msi_clone_properties(MSIPACKAGE *package)
395 {
396 static const WCHAR query_select[] = {
397 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
398 '`','P','r','o','p','e','r','t','y','`',0};
399 static const WCHAR query_insert[] = {
400 'I','N','S','E','R','T',' ','I','N','T','O',' ',
401 '`','_','P','r','o','p','e','r','t','y','`',' ',
402 '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
403 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
404 static const WCHAR query_update[] = {
405 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
406 'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
407 'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
408 MSIQUERY *view_select;
409 UINT rc;
410
411 rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
412 if (rc != ERROR_SUCCESS)
413 return rc;
414
415 rc = MSI_ViewExecute( view_select, 0 );
416 if (rc != ERROR_SUCCESS)
417 {
418 MSI_ViewClose( view_select );
419 msiobj_release( &view_select->hdr );
420 return rc;
421 }
422
423 while (1)
424 {
425 MSIQUERY *view_insert, *view_update;
426 MSIRECORD *rec_select;
427
428 rc = MSI_ViewFetch( view_select, &rec_select );
429 if (rc != ERROR_SUCCESS)
430 break;
431
432 rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
433 if (rc != ERROR_SUCCESS)
434 {
435 msiobj_release( &rec_select->hdr );
436 continue;
437 }
438
439 rc = MSI_ViewExecute( view_insert, rec_select );
440 MSI_ViewClose( view_insert );
441 msiobj_release( &view_insert->hdr );
442 if (rc != ERROR_SUCCESS)
443 {
444 MSIRECORD *rec_update;
445
446 TRACE("insert failed, trying update\n");
447
448 rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
449 if (rc != ERROR_SUCCESS)
450 {
451 WARN("open view failed %u\n", rc);
452 msiobj_release( &rec_select->hdr );
453 continue;
454 }
455
456 rec_update = MSI_CreateRecord( 2 );
457 MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
458 MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
459 rc = MSI_ViewExecute( view_update, rec_update );
460 if (rc != ERROR_SUCCESS)
461 WARN("update failed %u\n", rc);
462
463 MSI_ViewClose( view_update );
464 msiobj_release( &view_update->hdr );
465 msiobj_release( &rec_update->hdr );
466 }
467
468 msiobj_release( &rec_select->hdr );
469 }
470
471 MSI_ViewClose( view_select );
472 msiobj_release( &view_select->hdr );
473 return rc;
474 }
475
476 /*
477 * set_installed_prop
478 *
479 * Sets the "Installed" property to indicate that
480 * the product is installed for the current user.
481 */
482 static UINT set_installed_prop( MSIPACKAGE *package )
483 {
484 HKEY hkey;
485 UINT r;
486
487 if (!package->ProductCode) return ERROR_FUNCTION_FAILED;
488
489 r = MSIREG_OpenUninstallKey( package->ProductCode, package->platform, &hkey, FALSE );
490 if (r == ERROR_SUCCESS)
491 {
492 RegCloseKey( hkey );
493 msi_set_property( package->db, szInstalled, szOne, -1 );
494 }
495 return r;
496 }
497
498 static UINT set_user_sid_prop( MSIPACKAGE *package )
499 {
500 SID_NAME_USE use;
501 LPWSTR user_name;
502 LPWSTR sid_str = NULL, dom = NULL;
503 DWORD size, dom_size;
504 PSID psid = NULL;
505 UINT r = ERROR_FUNCTION_FAILED;
506
507 size = 0;
508 GetUserNameW( NULL, &size );
509
510 user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
511 if (!user_name)
512 return ERROR_OUTOFMEMORY;
513
514 if (!GetUserNameW( user_name, &size ))
515 goto done;
516
517 size = 0;
518 dom_size = 0;
519 LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
520
521 psid = msi_alloc( size );
522 dom = msi_alloc( dom_size*sizeof (WCHAR) );
523 if (!psid || !dom)
524 {
525 r = ERROR_OUTOFMEMORY;
526 goto done;
527 }
528
529 if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
530 goto done;
531
532 if (!ConvertSidToStringSidW( psid, &sid_str ))
533 goto done;
534
535 r = msi_set_property( package->db, szUserSID, sid_str, -1 );
536
537 done:
538 LocalFree( sid_str );
539 msi_free( dom );
540 msi_free( psid );
541 msi_free( user_name );
542
543 return r;
544 }
545
546 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
547 {
548 HKEY netsetup;
549 LONG res;
550 LPWSTR file = NULL;
551 DWORD index = 0, size;
552 WCHAR ver[MAX_PATH];
553 WCHAR name[MAX_PATH];
554 WCHAR windir[MAX_PATH];
555
556 static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
557 static const WCHAR sub[] = {
558 'S','o','f','t','w','a','r','e','\\',
559 'M','i','c','r','o','s','o','f','t','\\',
560 'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
561 'N','D','P',0
562 };
563 static const WCHAR subdir[] = {
564 'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
565 'F','r','a','m','e','w','o','r','k','\\',0
566 };
567
568 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
569 if (res != ERROR_SUCCESS)
570 return NULL;
571
572 GetWindowsDirectoryW(windir, MAX_PATH);
573
574 ver[0] = '\0';
575 size = MAX_PATH;
576 while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
577 {
578 index++;
579
580 /* verify existence of fusion.dll .Net 3.0 does not install a new one */
581 if (strcmpW( ver, name ) < 0)
582 {
583 LPWSTR check;
584 size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
585 check = msi_alloc(size * sizeof(WCHAR));
586
587 if (!check)
588 {
589 msi_free(file);
590 return NULL;
591 }
592
593 lstrcpyW(check, windir);
594 lstrcatW(check, szBackSlash);
595 lstrcatW(check, subdir);
596 lstrcatW(check, name);
597 lstrcatW(check, szBackSlash);
598 lstrcatW(check, fusion);
599
600 if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
601 {
602 msi_free(file);
603 file = check;
604 lstrcpyW(ver, name);
605 }
606 else
607 msi_free(check);
608 }
609 }
610
611 RegCloseKey(netsetup);
612 return file;
613 }
614
615 typedef struct tagLANGANDCODEPAGE
616 {
617 WORD wLanguage;
618 WORD wCodePage;
619 } LANGANDCODEPAGE;
620
621 static void set_msi_assembly_prop(MSIPACKAGE *package)
622 {
623 UINT val_len;
624 DWORD size, handle;
625 LPVOID version = NULL;
626 WCHAR buf[MAX_PATH];
627 LPWSTR fusion, verstr;
628 LANGANDCODEPAGE *translate;
629
630 static const WCHAR netasm[] = {
631 'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
632 };
633 static const WCHAR translation[] = {
634 '\\','V','a','r','F','i','l','e','I','n','f','o',
635 '\\','T','r','a','n','s','l','a','t','i','o','n',0
636 };
637 static const WCHAR verfmt[] = {
638 '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
639 '\\','%','0','4','x','%','0','4','x',
640 '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
641 };
642
643 fusion = get_fusion_filename(package);
644 if (!fusion)
645 return;
646
647 size = GetFileVersionInfoSizeW(fusion, &handle);
648 if (!size)
649 goto done;
650
651 version = msi_alloc(size);
652 if (!version)
653 goto done;
654
655 if (!GetFileVersionInfoW(fusion, handle, size, version))
656 goto done;
657
658 if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
659 goto done;
660
661 sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
662
663 if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
664 goto done;
665
666 if (!val_len || !verstr)
667 goto done;
668
669 msi_set_property( package->db, netasm, verstr, -1 );
670
671 done:
672 msi_free(fusion);
673 msi_free(version);
674 }
675
676 static VOID set_installer_properties(MSIPACKAGE *package)
677 {
678 WCHAR *ptr;
679 OSVERSIONINFOEXW OSVersion;
680 MEMORYSTATUSEX msex;
681 DWORD verval, len;
682 WCHAR pth[MAX_PATH], verstr[11], bufstr[22];
683 HDC dc;
684 HKEY hkey;
685 LPWSTR username, companyname;
686 SYSTEM_INFO sys_info;
687 SYSTEMTIME systemtime;
688 LANGID langid;
689
690 static const WCHAR szCommonFilesFolder[] = {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
691 static const WCHAR szProgramFilesFolder[] = {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
692 static const WCHAR szCommonAppDataFolder[] = {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
693 static const WCHAR szFavoritesFolder[] = {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
694 static const WCHAR szFontsFolder[] = {'F','o','n','t','s','F','o','l','d','e','r',0};
695 static const WCHAR szSendToFolder[] = {'S','e','n','d','T','o','F','o','l','d','e','r',0};
696 static const WCHAR szStartMenuFolder[] = {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
697 static const WCHAR szStartupFolder[] = {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
698 static const WCHAR szTemplateFolder[] = {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
699 static const WCHAR szDesktopFolder[] = {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
700 static const WCHAR szProgramMenuFolder[] = {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
701 static const WCHAR szAdminToolsFolder[] = {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
702 static const WCHAR szSystemFolder[] = {'S','y','s','t','e','m','F','o','l','d','e','r',0};
703 static const WCHAR szSystem16Folder[] = {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
704 static const WCHAR szLocalAppDataFolder[] = {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
705 static const WCHAR szMyPicturesFolder[] = {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
706 static const WCHAR szPersonalFolder[] = {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
707 static const WCHAR szWindowsVolume[] = {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
708 static const WCHAR szPrivileged[] = {'P','r','i','v','i','l','e','g','e','d',0};
709 static const WCHAR szVersion9x[] = {'V','e','r','s','i','o','n','9','X',0};
710 static const WCHAR szVersionNT[] = {'V','e','r','s','i','o','n','N','T',0};
711 static const WCHAR szMsiNTProductType[] = {'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0};
712 static const WCHAR szFormat[] = {'%','u',0};
713 static const WCHAR szFormat2[] = {'%','u','.','%','u',0};
714 static const WCHAR szWindowsBuild[] = {'W','i','n','d','o','w','s','B','u','i','l','d',0};
715 static const WCHAR szServicePackLevel[] = {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0};
716 static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
717 static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
718 static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
719 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
720 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
721 static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
722 static const WCHAR szIntFormat[] = {'%','d',0};
723 static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
724 static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
725 static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
726 static const WCHAR szCommonFiles64Folder[] = { 'C','o','m','m','o','n','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
727 static const WCHAR szProgramFiles64Folder[] = { 'P','r','o','g','r','a','m','F','i','l','e','s','6','4','F','o','l','d','e','r',0 };
728 static const WCHAR szVersionNT64[] = { 'V','e','r','s','i','o','n','N','T','6','4',0 };
729 static const WCHAR szUserInfo[] = {
730 'S','O','F','T','W','A','R','E','\\',
731 'M','i','c','r','o','s','o','f','t','\\',
732 'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
733 'U','s','e','r',' ','I','n','f','o',0
734 };
735 static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
736 static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
737 static const WCHAR szCurrentVersion[] = {
738 'S','O','F','T','W','A','R','E','\\',
739 'M','i','c','r','o','s','o','f','t','\\',
740 'W','i','n','d','o','w','s',' ','N','T','\\',
741 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
742 };
743 static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
744 static const WCHAR szRegisteredOrganization[] = {
745 'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
746 };
747 static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
748 static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
749 static const WCHAR szDate[] = {'D','a','t','e',0};
750 static const WCHAR szTime[] = {'T','i','m','e',0};
751 static const WCHAR szUserLanguageID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
752 static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
753 static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
754 static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
755 static const WCHAR szNetHoodFolder[] = {'N','e','t','H','o','o','d','F','o','l','d','e','r',0};
756 static const WCHAR szPrintHoodFolder[] = {'P','r','i','n','t','H','o','o','d','F','o','l','d','e','r',0};
757 static const WCHAR szRecentFolder[] = {'R','e','c','e','n','t','F','o','l','d','e','r',0};
758 static const WCHAR szComputerName[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
759 static const WCHAR szBrowseProperty[] = {'_','B','r','o','w','s','e','P','r','o','p','e','r','t','y',0};
760 static const WCHAR szInstallDir[] = {'I','N','S','T','A','L','L','D','I','R',0};
761
762 /*
763 * Other things that probably should be set:
764 *
765 * VirtualMemory ShellAdvSupport DefaultUIFont PackagecodeChanging
766 * CaptionHeight BorderTop BorderSide TextHeight RedirectedDllSupport
767 */
768
769 SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL, 0, pth);
770 strcatW(pth, szBackSlash);
771 msi_set_property( package->db, szCommonAppDataFolder, pth, -1 );
772
773 SHGetFolderPathW(NULL, CSIDL_FAVORITES, NULL, 0, pth);
774 strcatW(pth, szBackSlash);
775 msi_set_property( package->db, szFavoritesFolder, pth, -1 );
776
777 SHGetFolderPathW(NULL, CSIDL_FONTS, NULL, 0, pth);
778 strcatW(pth, szBackSlash);
779 msi_set_property( package->db, szFontsFolder, pth, -1 );
780
781 SHGetFolderPathW(NULL, CSIDL_SENDTO, NULL, 0, pth);
782 strcatW(pth, szBackSlash);
783 msi_set_property( package->db, szSendToFolder, pth, -1 );
784
785 SHGetFolderPathW(NULL, CSIDL_STARTMENU, NULL, 0, pth);
786 strcatW(pth, szBackSlash);
787 msi_set_property( package->db, szStartMenuFolder, pth, -1 );
788
789 SHGetFolderPathW(NULL, CSIDL_STARTUP, NULL, 0, pth);
790 strcatW(pth, szBackSlash);
791 msi_set_property( package->db, szStartupFolder, pth, -1 );
792
793 SHGetFolderPathW(NULL, CSIDL_TEMPLATES, NULL, 0, pth);
794 strcatW(pth, szBackSlash);
795 msi_set_property( package->db, szTemplateFolder, pth, -1 );
796
797 SHGetFolderPathW(NULL, CSIDL_DESKTOP, NULL, 0, pth);
798 strcatW(pth, szBackSlash);
799 msi_set_property( package->db, szDesktopFolder, pth, -1 );
800
801 /* FIXME: set to AllUsers profile path if ALLUSERS is set */
802 SHGetFolderPathW(NULL, CSIDL_PROGRAMS, NULL, 0, pth);
803 strcatW(pth, szBackSlash);
804 msi_set_property( package->db, szProgramMenuFolder, pth, -1 );
805
806 SHGetFolderPathW(NULL, CSIDL_ADMINTOOLS, NULL, 0, pth);
807 strcatW(pth, szBackSlash);
808 msi_set_property( package->db, szAdminToolsFolder, pth, -1 );
809
810 SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, pth);
811 strcatW(pth, szBackSlash);
812 msi_set_property( package->db, szAppDataFolder, pth, -1 );
813
814 SHGetFolderPathW(NULL, CSIDL_SYSTEM, NULL, 0, pth);
815 strcatW(pth, szBackSlash);
816 msi_set_property( package->db, szSystemFolder, pth, -1 );
817 msi_set_property( package->db, szSystem16Folder, pth, -1 );
818
819 SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, pth);
820 strcatW(pth, szBackSlash);
821 msi_set_property( package->db, szLocalAppDataFolder, pth, -1 );
822
823 SHGetFolderPathW(NULL, CSIDL_MYPICTURES, NULL, 0, pth);
824 strcatW(pth, szBackSlash);
825 msi_set_property( package->db, szMyPicturesFolder, pth, -1 );
826
827 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, pth);
828 strcatW(pth, szBackSlash);
829 msi_set_property( package->db, szPersonalFolder, pth, -1 );
830
831 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
832 strcatW(pth, szBackSlash);
833 msi_set_property( package->db, szWindowsFolder, pth, -1 );
834
835 SHGetFolderPathW(NULL, CSIDL_PRINTHOOD, NULL, 0, pth);
836 strcatW(pth, szBackSlash);
837 msi_set_property( package->db, szPrintHoodFolder, pth, -1 );
838
839 SHGetFolderPathW(NULL, CSIDL_NETHOOD, NULL, 0, pth);
840 strcatW(pth, szBackSlash);
841 msi_set_property( package->db, szNetHoodFolder, pth, -1 );
842
843 SHGetFolderPathW(NULL, CSIDL_RECENT, NULL, 0, pth);
844 strcatW(pth, szBackSlash);
845 msi_set_property( package->db, szRecentFolder, pth, -1 );
846
847 /* Physical Memory is specified in MB. Using total amount. */
848 msex.dwLength = sizeof(msex);
849 GlobalMemoryStatusEx( &msex );
850 len = sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys / 1024 / 1024) );
851 msi_set_property( package->db, szPhysicalMemory, bufstr, len );
852
853 SHGetFolderPathW(NULL, CSIDL_WINDOWS, NULL, 0, pth);
854 ptr = strchrW(pth,'\\');
855 if (ptr) *(ptr + 1) = 0;
856 msi_set_property( package->db, szWindowsVolume, pth, -1 );
857
858 len = GetTempPathW(MAX_PATH, pth);
859 msi_set_property( package->db, szTempFolder, pth, len );
860
861 /* in a wine environment the user is always admin and privileged */
862 msi_set_property( package->db, szAdminUser, szOne, -1 );
863 msi_set_property( package->db, szPrivileged, szOne, -1 );
864
865 /* set the os things */
866 OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
867 GetVersionExW((OSVERSIONINFOW *)&OSVersion);
868 verval = OSVersion.dwMinorVersion + OSVersion.dwMajorVersion * 100;
869 len = sprintfW( verstr, szFormat, verval );
870 switch (OSVersion.dwPlatformId)
871 {
872 case VER_PLATFORM_WIN32_WINDOWS:
873 msi_set_property( package->db, szVersion9x, verstr, len );
874 break;
875 case VER_PLATFORM_WIN32_NT:
876 msi_set_property( package->db, szVersionNT, verstr, len );
877 len = sprintfW( verstr, szFormat,OSVersion.wProductType );
878 msi_set_property( package->db, szMsiNTProductType, verstr, len );
879 break;
880 }
881 len = sprintfW( verstr, szFormat, OSVersion.dwBuildNumber );
882 msi_set_property( package->db, szWindowsBuild, verstr, len );
883 len = sprintfW( verstr, szFormat, OSVersion.wServicePackMajor );
884 msi_set_property( package->db, szServicePackLevel, verstr, len );
885
886 len = sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION );
887 msi_set_property( package->db, szVersionMsi, bufstr, len );
888 len = sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100 );
889 msi_set_property( package->db, szVersionDatabase, bufstr, len );
890
891 GetNativeSystemInfo( &sys_info );
892 len = sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
893 msi_set_property( package->db, szIntel, bufstr, len );
894 if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
895 {
896 GetSystemDirectoryW( pth, MAX_PATH );
897 PathAddBackslashW( pth );
898 msi_set_property( package->db, szSystemFolder, pth, -1 );
899
900 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
901 PathAddBackslashW( pth );
902 msi_set_property( package->db, szProgramFilesFolder, pth, -1 );
903
904 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
905 PathAddBackslashW( pth );
906 msi_set_property( package->db, szCommonFilesFolder, pth, -1 );
907 }
908 else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
909 {
910 msi_set_property( package->db, szMsiAMD64, bufstr, -1 );
911 msi_set_property( package->db, szMsix64, bufstr, -1 );
912 msi_set_property( package->db, szVersionNT64, verstr, -1 );
913
914 GetSystemDirectoryW( pth, MAX_PATH );
915 PathAddBackslashW( pth );
916 msi_set_property( package->db, szSystem64Folder, pth, -1 );
917
918 GetSystemWow64DirectoryW( pth, MAX_PATH );
919 PathAddBackslashW( pth );
920 msi_set_property( package->db, szSystemFolder, pth, -1 );
921
922 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES, NULL, 0, pth );
923 PathAddBackslashW( pth );
924 msi_set_property( package->db, szProgramFiles64Folder, pth, -1 );
925
926 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, pth );
927 PathAddBackslashW( pth );
928 msi_set_property( package->db, szProgramFilesFolder, pth, -1 );
929
930 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, pth );
931 PathAddBackslashW( pth );
932 msi_set_property( package->db, szCommonFiles64Folder, pth, -1 );
933
934 SHGetFolderPathW( NULL, CSIDL_PROGRAM_FILES_COMMONX86, NULL, 0, pth );
935 PathAddBackslashW( pth );
936 msi_set_property( package->db, szCommonFilesFolder, pth, -1 );
937 }
938
939 /* Screen properties. */
940 dc = GetDC(0);
941 len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, HORZRES) );
942 msi_set_property( package->db, szScreenX, bufstr, len );
943 len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, VERTRES) );
944 msi_set_property( package->db, szScreenY, bufstr, len );
945 len = sprintfW( bufstr, szIntFormat, GetDeviceCaps(dc, BITSPIXEL) );
946 msi_set_property( package->db, szColorBits, bufstr, len );
947 ReleaseDC(0, dc);
948
949 /* USERNAME and COMPANYNAME */
950 username = msi_dup_property( package->db, szUSERNAME );
951 companyname = msi_dup_property( package->db, szCOMPANYNAME );
952
953 if ((!username || !companyname) &&
954 RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
955 {
956 if (!username &&
957 (username = msi_reg_get_val_str( hkey, szDefName )))
958 msi_set_property( package->db, szUSERNAME, username, -1 );
959 if (!companyname &&
960 (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
961 msi_set_property( package->db, szCOMPANYNAME, companyname, -1 );
962 CloseHandle( hkey );
963 }
964 if ((!username || !companyname) &&
965 RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
966 {
967 if (!username &&
968 (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
969 msi_set_property( package->db, szUSERNAME, username, -1 );
970 if (!companyname &&
971 (companyname = msi_reg_get_val_str( hkey, szRegisteredOrganization )))
972 msi_set_property( package->db, szCOMPANYNAME, companyname, -1 );
973 CloseHandle( hkey );
974 }
975 msi_free( username );
976 msi_free( companyname );
977
978 if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
979 ERR("Failed to set the UserSID property\n");
980
981 /* Date and time properties */
982 GetSystemTime( &systemtime );
983 if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
984 NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
985 msi_set_property( package->db, szDate, bufstr, -1 );
986 else
987 ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
988
989 if (GetTimeFormatW( LOCALE_USER_DEFAULT,
990 TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
991 &systemtime, NULL, bufstr,
992 sizeof(bufstr)/sizeof(bufstr[0]) ))
993 msi_set_property( package->db, szTime, bufstr, -1 );
994 else
995 ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
996
997 set_msi_assembly_prop( package );
998
999 langid = GetUserDefaultLangID();
1000 len = sprintfW( bufstr, szIntFormat, langid );
1001 msi_set_property( package->db, szUserLanguageID, bufstr, len );
1002
1003 langid = GetSystemDefaultLangID();
1004 len = sprintfW( bufstr, szIntFormat, langid );
1005 msi_set_property( package->db, szSystemLangID, bufstr, len );
1006
1007 len = sprintfW( bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode) );
1008 msi_set_property( package->db, szProductState, bufstr, len );
1009
1010 len = 0;
1011 if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1012 {
1013 WCHAR *username;
1014 if ((username = msi_alloc( len * sizeof(WCHAR) )))
1015 {
1016 if (GetUserNameW( username, &len ))
1017 msi_set_property( package->db, szLogonUser, username, len - 1 );
1018 msi_free( username );
1019 }
1020 }
1021 len = 0;
1022 if (!GetComputerNameW( NULL, &len ) && GetLastError() == ERROR_BUFFER_OVERFLOW)
1023 {
1024 WCHAR *computername;
1025 if ((computername = msi_alloc( len * sizeof(WCHAR) )))
1026 {
1027 if (GetComputerNameW( computername, &len ))
1028 msi_set_property( package->db, szComputerName, computername, len - 1 );
1029 msi_free( computername );
1030 }
1031 }
1032 msi_set_property( package->db, szBrowseProperty, szInstallDir, -1 );
1033 }
1034
1035 static UINT msi_load_summary_properties( MSIPACKAGE *package )
1036 {
1037 UINT rc;
1038 MSIHANDLE suminfo;
1039 MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
1040 INT count;
1041 DWORD len;
1042 LPWSTR package_code;
1043 static const WCHAR szPackageCode[] = {
1044 'P','a','c','k','a','g','e','C','o','d','e',0};
1045
1046 if (!hdb) {
1047 ERR("Unable to allocate handle\n");
1048 return ERROR_OUTOFMEMORY;
1049 }
1050
1051 rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
1052 MsiCloseHandle(hdb);
1053 if (rc != ERROR_SUCCESS)
1054 {
1055 ERR("Unable to open Summary Information\n");
1056 return rc;
1057 }
1058
1059 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
1060 &count, NULL, NULL, NULL );
1061 if (rc != ERROR_SUCCESS)
1062 {
1063 WARN("Unable to query page count: %d\n", rc);
1064 goto done;
1065 }
1066
1067 /* load package code property */
1068 len = 0;
1069 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1070 NULL, NULL, NULL, &len );
1071 if (rc != ERROR_MORE_DATA)
1072 {
1073 WARN("Unable to query revision number: %d\n", rc);
1074 rc = ERROR_FUNCTION_FAILED;
1075 goto done;
1076 }
1077
1078 len++;
1079 package_code = msi_alloc( len * sizeof(WCHAR) );
1080 rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
1081 NULL, NULL, package_code, &len );
1082 if (rc != ERROR_SUCCESS)
1083 {
1084 WARN("Unable to query rev number: %d\n", rc);
1085 goto done;
1086 }
1087
1088 msi_set_property( package->db, szPackageCode, package_code, len );
1089 msi_free( package_code );
1090
1091 /* load package attributes */
1092 count = 0;
1093 MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1094 &count, NULL, NULL, NULL );
1095 package->WordCount = count;
1096
1097 done:
1098 MsiCloseHandle(suminfo);
1099 return rc;
1100 }
1101
1102 static MSIPACKAGE *msi_alloc_package( void )
1103 {
1104 MSIPACKAGE *package;
1105
1106 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1107 MSI_FreePackage );
1108 if( package )
1109 {
1110 list_init( &package->components );
1111 list_init( &package->features );
1112 list_init( &package->files );
1113 list_init( &package->filepatches );
1114 list_init( &package->tempfiles );
1115 list_init( &package->folders );
1116 list_init( &package->subscriptions );
1117 list_init( &package->appids );
1118 list_init( &package->classes );
1119 list_init( &package->mimes );
1120 list_init( &package->extensions );
1121 list_init( &package->progids );
1122 list_init( &package->RunningActions );
1123 list_init( &package->sourcelist_info );
1124 list_init( &package->sourcelist_media );
1125 list_init( &package->patches );
1126 list_init( &package->binaries );
1127 list_init( &package->cabinet_streams );
1128 }
1129
1130 return package;
1131 }
1132
1133 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1134 {
1135 BYTE *data;
1136 UINT r, sz;
1137
1138 static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1139
1140 r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1141 if (r != ERROR_SUCCESS)
1142 return r;
1143
1144 r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1145
1146 msi_free(data);
1147 return r;
1148 }
1149
1150 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1151 {
1152 /* FIXME: this should depend on the user's privileges */
1153 if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1154 {
1155 TRACE("resetting ALLUSERS property from 2 to 1\n");
1156 msi_set_property( package->db, szAllUsers, szOne, -1 );
1157 }
1158 msi_set_property( package->db, szAdminUser, szOne, -1 );
1159 }
1160
1161 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1162 {
1163 static const WCHAR fmtW[] = {'%','u',0};
1164 MSIPACKAGE *package;
1165 WCHAR uilevel[11];
1166 int len;
1167 UINT r;
1168
1169 TRACE("%p\n", db);
1170
1171 package = msi_alloc_package();
1172 if (package)
1173 {
1174 msiobj_addref( &db->hdr );
1175 package->db = db;
1176
1177 package->WordCount = 0;
1178 package->PackagePath = strdupW( db->path );
1179 package->BaseURL = strdupW( base_url );
1180
1181 create_temp_property_table( package );
1182 msi_clone_properties( package );
1183 msi_adjust_privilege_properties( package );
1184
1185 package->ProductCode = msi_dup_property( package->db, szProductCode );
1186 package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1187
1188 set_installed_prop( package );
1189 set_installer_properties( package );
1190
1191 package->ui_level = gUILevel;
1192 len = sprintfW( uilevel, fmtW, gUILevel & INSTALLUILEVEL_MASK );
1193 msi_set_property( package->db, szUILevel, uilevel, len );
1194
1195 r = msi_load_summary_properties( package );
1196 if (r != ERROR_SUCCESS)
1197 {
1198 msiobj_release( &package->hdr );
1199 return NULL;
1200 }
1201
1202 if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1203 msi_load_admin_properties( package );
1204
1205 package->log_file = INVALID_HANDLE_VALUE;
1206 }
1207 return package;
1208 }
1209
1210 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1211 {
1212 LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1213 DWORD size = 0;
1214 HRESULT hr;
1215
1216 /* call will always fail, because size is 0,
1217 * but will return ERROR_FILE_NOT_FOUND first
1218 * if the file doesn't exist
1219 */
1220 GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1221 if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1222 {
1223 cache_entry = msi_alloc( size );
1224 if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1225 {
1226 UINT error = GetLastError();
1227 msi_free( cache_entry );
1228 return error;
1229 }
1230
1231 lstrcpyW( filename, cache_entry->lpszLocalFileName );
1232 msi_free( cache_entry );
1233 return ERROR_SUCCESS;
1234 }
1235
1236 hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1237 if ( FAILED(hr) )
1238 {
1239 WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1240 return ERROR_FUNCTION_FAILED;
1241 }
1242
1243 return ERROR_SUCCESS;
1244 }
1245
1246 UINT msi_create_empty_local_file( LPWSTR path, LPCWSTR suffix )
1247 {
1248 static const WCHAR szInstaller[] = {
1249 '\\','I','n','s','t','a','l','l','e','r','\\',0};
1250 static const WCHAR fmt[] = {'%','x',0};
1251 DWORD time, len, i, offset;
1252 HANDLE handle;
1253
1254 time = GetTickCount();
1255 GetWindowsDirectoryW( path, MAX_PATH );
1256 strcatW( path, szInstaller );
1257 CreateDirectoryW( path, NULL );
1258
1259 len = strlenW(path);
1260 for (i = 0; i < 0x10000; i++)
1261 {
1262 offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1263 memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1264 handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1265 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1266 if (handle != INVALID_HANDLE_VALUE)
1267 {
1268 CloseHandle(handle);
1269 break;
1270 }
1271 if (GetLastError() != ERROR_FILE_EXISTS &&
1272 GetLastError() != ERROR_SHARING_VIOLATION)
1273 return ERROR_FUNCTION_FAILED;
1274 }
1275
1276 return ERROR_SUCCESS;
1277 }
1278
1279 static enum platform parse_platform( WCHAR *str )
1280 {
1281 if (!str[0] || !strcmpW( str, szIntel )) return PLATFORM_INTEL;
1282 else if (!strcmpW( str, szIntel64 )) return PLATFORM_INTEL64;
1283 else if (!strcmpW( str, szX64 ) || !strcmpW( str, szAMD64 )) return PLATFORM_X64;
1284 else if (!strcmpW( str, szARM )) return PLATFORM_ARM;
1285 return PLATFORM_UNKNOWN;
1286 }
1287
1288 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1289 {
1290 WCHAR *template, *p, *q, *platform;
1291 DWORD i, count;
1292
1293 package->version = msi_suminfo_get_int32( si, PID_PAGECOUNT );
1294 TRACE("version: %d\n", package->version);
1295
1296 template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1297 if (!template)
1298 return ERROR_SUCCESS; /* native accepts missing template property */
1299
1300 TRACE("template: %s\n", debugstr_w(template));
1301
1302 p = strchrW( template, ';' );
1303 if (!p)
1304 {
1305 WARN("invalid template string %s\n", debugstr_w(template));
1306 msi_free( template );
1307 return ERROR_PATCH_PACKAGE_INVALID;
1308 }
1309 *p = 0;
1310 platform = template;
1311 if ((q = strchrW( platform, ',' ))) *q = 0;
1312 package->platform = parse_platform( platform );
1313 while (package->platform == PLATFORM_UNKNOWN && q)
1314 {
1315 platform = q + 1;
1316 if ((q = strchrW( platform, ',' ))) *q = 0;
1317 package->platform = parse_platform( platform );
1318 }
1319 if (package->platform == PLATFORM_UNKNOWN)
1320 {
1321 WARN("unknown platform %s\n", debugstr_w(template));
1322 msi_free( template );
1323 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1324 }
1325 p++;
1326 if (!*p)
1327 {
1328 msi_free( template );
1329 return ERROR_SUCCESS;
1330 }
1331 count = 1;
1332 for (q = p; (q = strchrW( q, ',' )); q++) count++;
1333
1334 package->langids = msi_alloc( count * sizeof(LANGID) );
1335 if (!package->langids)
1336 {
1337 msi_free( template );
1338 return ERROR_OUTOFMEMORY;
1339 }
1340
1341 i = 0;
1342 while (*p)
1343 {
1344 q = strchrW( p, ',' );
1345 if (q) *q = 0;
1346 package->langids[i] = atoiW( p );
1347 if (!q) break;
1348 p = q + 1;
1349 i++;
1350 }
1351 package->num_langids = i + 1;
1352
1353 msi_free( template );
1354 return ERROR_SUCCESS;
1355 }
1356
1357 static UINT validate_package( MSIPACKAGE *package )
1358 {
1359 UINT i;
1360
1361 if (package->platform == PLATFORM_INTEL64)
1362 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1363 #ifndef __arm__
1364 if (package->platform == PLATFORM_ARM)
1365 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1366 #endif
1367 if (package->platform == PLATFORM_X64)
1368 {
1369 if (!is_64bit && !is_wow64)
1370 return ERROR_INSTALL_PLATFORM_UNSUPPORTED;
1371 if (package->version < 200)
1372 return ERROR_INSTALL_PACKAGE_INVALID;
1373 }
1374 if (!package->num_langids)
1375 {
1376 return ERROR_SUCCESS;
1377 }
1378 for (i = 0; i < package->num_langids; i++)
1379 {
1380 LANGID langid = package->langids[i];
1381
1382 if (PRIMARYLANGID( langid ) == LANG_NEUTRAL)
1383 {
1384 langid = MAKELANGID( PRIMARYLANGID( GetSystemDefaultLangID() ), SUBLANGID( langid ) );
1385 }
1386 if (SUBLANGID( langid ) == SUBLANG_NEUTRAL)
1387 {
1388 langid = MAKELANGID( PRIMARYLANGID( langid ), SUBLANGID( GetSystemDefaultLangID() ) );
1389 }
1390 if (IsValidLocale( langid, LCID_INSTALLED ))
1391 return ERROR_SUCCESS;
1392 }
1393 return ERROR_INSTALL_LANGUAGE_UNSUPPORTED;
1394 }
1395
1396 int msi_track_tempfile( MSIPACKAGE *package, const WCHAR *path )
1397 {
1398 MSITEMPFILE *temp;
1399
1400 TRACE("%s\n", debugstr_w(path));
1401
1402 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
1403 {
1404 if (!strcmpW( path, temp->Path )) return 0;
1405 }
1406 if (!(temp = msi_alloc_zero( sizeof (MSITEMPFILE) ))) return -1;
1407 list_add_head( &package->tempfiles, &temp->entry );
1408 temp->Path = strdupW( path );
1409 return 0;
1410 }
1411
1412 static WCHAR *get_product_code( MSIDATABASE *db )
1413 {
1414 static const WCHAR query[] = {
1415 'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1416 'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',' ',
1417 'W','H','E','R','E',' ','`','P','r','o','p','e','r','t','y','`','=',
1418 '\'','P','r','o','d','u','c','t','C','o','d','e','\'',0};
1419 MSIQUERY *view;
1420 MSIRECORD *rec;
1421 WCHAR *ret = NULL;
1422
1423 if (MSI_DatabaseOpenViewW( db, query, &view ) != ERROR_SUCCESS)
1424 {
1425 return NULL;
1426 }
1427 if (MSI_ViewExecute( view, 0 ) != ERROR_SUCCESS)
1428 {
1429 MSI_ViewClose( view );
1430 msiobj_release( &view->hdr );
1431 return NULL;
1432 }
1433 if (MSI_ViewFetch( view, &rec ) == ERROR_SUCCESS)
1434 {
1435 ret = strdupW( MSI_RecordGetString( rec, 1 ) );
1436 msiobj_release( &rec->hdr );
1437 }
1438 MSI_ViewClose( view );
1439 msiobj_release( &view->hdr );
1440 return ret;
1441 }
1442
1443 static UINT get_registered_local_package( const WCHAR *product, const WCHAR *package, WCHAR *localfile )
1444 {
1445 MSIINSTALLCONTEXT context;
1446 HKEY product_key, props_key;
1447 WCHAR *registered_package = NULL, unsquashed[GUID_SIZE];
1448 UINT r;
1449
1450 r = msi_locate_product( product, &context );
1451 if (r != ERROR_SUCCESS)
1452 return r;
1453
1454 r = MSIREG_OpenProductKey( product, NULL, context, &product_key, FALSE );
1455 if (r != ERROR_SUCCESS)
1456 return r;
1457
1458 r = MSIREG_OpenInstallProps( product, context, NULL, &props_key, FALSE );
1459 if (r != ERROR_SUCCESS)
1460 {
1461 RegCloseKey( product_key );
1462 return r;
1463 }
1464 r = ERROR_FUNCTION_FAILED;
1465 registered_package = msi_reg_get_val_str( product_key, INSTALLPROPERTY_PACKAGECODEW );
1466 if (!registered_package)
1467 goto done;
1468
1469 unsquash_guid( registered_package, unsquashed );
1470 if (!strcmpiW( package, unsquashed ))
1471 {
1472 WCHAR *filename = msi_reg_get_val_str( props_key, INSTALLPROPERTY_LOCALPACKAGEW );
1473 if (!filename)
1474 goto done;
1475
1476 strcpyW( localfile, filename );
1477 msi_free( filename );
1478 r = ERROR_SUCCESS;
1479 }
1480 done:
1481 msi_free( registered_package );
1482 RegCloseKey( props_key );
1483 RegCloseKey( product_key );
1484 return r;
1485 }
1486
1487 static WCHAR *get_package_code( MSIDATABASE *db )
1488 {
1489 WCHAR *ret;
1490 MSISUMMARYINFO *si;
1491
1492 if (!(si = MSI_GetSummaryInformationW( db->storage, 0 )))
1493 {
1494 WARN("failed to load summary info\n");
1495 return NULL;
1496 }
1497 ret = msi_suminfo_dup_string( si, PID_REVNUMBER );
1498 msiobj_release( &si->hdr );
1499 return ret;
1500 }
1501
1502 static UINT get_local_package( const WCHAR *filename, WCHAR *localfile )
1503 {
1504 WCHAR *product_code, *package_code;
1505 MSIDATABASE *db;
1506 UINT r;
1507
1508 if ((r = MSI_OpenDatabaseW( filename, MSIDBOPEN_READONLY, &db )) != ERROR_SUCCESS)
1509 {
1510 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1511 return ERROR_FILE_NOT_FOUND;
1512 return r;
1513 }
1514 if (!(product_code = get_product_code( db )))
1515 {
1516 msiobj_release( &db->hdr );
1517 return ERROR_INSTALL_PACKAGE_INVALID;
1518 }
1519 if (!(package_code = get_package_code( db )))
1520 {
1521 msi_free( product_code );
1522 msiobj_release( &db->hdr );
1523 return ERROR_INSTALL_PACKAGE_INVALID;
1524 }
1525 r = get_registered_local_package( product_code, package_code, localfile );
1526 msi_free( package_code );
1527 msi_free( product_code );
1528 msiobj_release( &db->hdr );
1529 return r;
1530 }
1531
1532 UINT msi_set_original_database_property( MSIDATABASE *db, const WCHAR *package )
1533 {
1534 UINT r;
1535
1536 if (UrlIsW( package, URLIS_URL ))
1537 r = msi_set_property( db, szOriginalDatabase, package, -1 );
1538 else if (package[0] == '#')
1539 r = msi_set_property( db, szOriginalDatabase, db->path, -1 );
1540 else
1541 {
1542 DWORD len;
1543 WCHAR *path;
1544
1545 if (!(len = GetFullPathNameW( package, 0, NULL, NULL ))) return GetLastError();
1546 if (!(path = msi_alloc( len * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
1547 len = GetFullPathNameW( package, len, path, NULL );
1548 r = msi_set_property( db, szOriginalDatabase, path, len );
1549 msi_free( path );
1550 }
1551 return r;
1552 }
1553
1554 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1555 {
1556 static const WCHAR dotmsi[] = {'.','m','s','i',0};
1557 MSIDATABASE *db;
1558 MSIPACKAGE *package;
1559 MSIHANDLE handle;
1560 LPWSTR ptr, base_url = NULL;
1561 UINT r;
1562 WCHAR localfile[MAX_PATH], cachefile[MAX_PATH];
1563 LPCWSTR file = szPackage;
1564 DWORD index = 0;
1565 MSISUMMARYINFO *si;
1566 BOOL delete_on_close = FALSE;
1567
1568 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1569
1570 localfile[0] = 0;
1571 if( szPackage[0] == '#' )
1572 {
1573 handle = atoiW(&szPackage[1]);
1574 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1575 if( !db )
1576 {
1577 IWineMsiRemoteDatabase *remote_database;
1578
1579 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1580 if ( !remote_database )
1581 return ERROR_INVALID_HANDLE;
1582
1583 IWineMsiRemoteDatabase_Release( remote_database );
1584 WARN("MsiOpenPackage not allowed during a custom action!\n");
1585
1586 return ERROR_FUNCTION_FAILED;
1587 }
1588 }
1589 else
1590 {
1591 if ( UrlIsW( szPackage, URLIS_URL ) )
1592 {
1593 r = msi_download_file( szPackage, cachefile );
1594 if (r != ERROR_SUCCESS)
1595 return r;
1596
1597 file = cachefile;
1598
1599 base_url = strdupW( szPackage );
1600 if (!base_url)
1601 return ERROR_OUTOFMEMORY;
1602
1603 ptr = strrchrW( base_url, '/' );
1604 if (ptr) *(ptr + 1) = '\0';
1605 }
1606 r = get_local_package( file, localfile );
1607 if (r != ERROR_SUCCESS || GetFileAttributesW( localfile ) == INVALID_FILE_ATTRIBUTES)
1608 {
1609 r = msi_create_empty_local_file( localfile, dotmsi );
1610 if (r != ERROR_SUCCESS)
1611 {
1612 msi_free ( base_url );
1613 return r;
1614 }
1615
1616 if (!CopyFileW( file, localfile, FALSE ))
1617 {
1618 r = GetLastError();
1619 WARN("unable to copy package %s to %s (%u)\n", debugstr_w(file), debugstr_w(localfile), r);
1620 DeleteFileW( localfile );
1621 msi_free ( base_url );
1622 return r;
1623 }
1624 delete_on_close = TRUE;
1625 }
1626 TRACE("opening package %s\n", debugstr_w( localfile ));
1627 r = MSI_OpenDatabaseW( localfile, MSIDBOPEN_TRANSACT, &db );
1628 if (r != ERROR_SUCCESS)
1629 {
1630 msi_free ( base_url );
1631 return r;
1632 }
1633 }
1634 package = MSI_CreatePackage( db, base_url );
1635 msi_free( base_url );
1636 msiobj_release( &db->hdr );
1637 if (!package) return ERROR_INSTALL_PACKAGE_INVALID;
1638 package->localfile = strdupW( localfile );
1639 package->delete_on_close = delete_on_close;
1640
1641 si = MSI_GetSummaryInformationW( db->storage, 0 );
1642 if (!si)
1643 {
1644 WARN("failed to load summary info\n");
1645 msiobj_release( &package->hdr );
1646 return ERROR_INSTALL_PACKAGE_INVALID;
1647 }
1648 r = msi_parse_summary( si, package );
1649 msiobj_release( &si->hdr );
1650 if (r != ERROR_SUCCESS)
1651 {
1652 WARN("failed to parse summary info %u\n", r);
1653 msiobj_release( &package->hdr );
1654 return r;
1655 }
1656 r = validate_package( package );
1657 if (r != ERROR_SUCCESS)
1658 {
1659 msiobj_release( &package->hdr );
1660 return r;
1661 }
1662 msi_set_property( package->db, szDatabase, db->path, -1 );
1663 msi_set_context( package );
1664
1665 while (1)
1666 {
1667 WCHAR patch_code[GUID_SIZE];
1668 r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1669 MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1670 if (r != ERROR_SUCCESS)
1671 break;
1672
1673 TRACE("found registered patch %s\n", debugstr_w(patch_code));
1674
1675 r = msi_apply_registered_patch( package, patch_code );
1676 if (r != ERROR_SUCCESS)
1677 {
1678 ERR("registered patch failed to apply %u\n", r);
1679 msiobj_release( &package->hdr );
1680 return r;
1681 }
1682 index++;
1683 }
1684 if (index)
1685 {
1686 msi_clone_properties( package );
1687 msi_adjust_privilege_properties( package );
1688 }
1689 r = msi_set_original_database_property( package->db, szPackage );
1690 if (r != ERROR_SUCCESS)
1691 {
1692 msiobj_release( &package->hdr );
1693 return r;
1694 }
1695 if (gszLogFile)
1696 package->log_file = CreateFileW( gszLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
1697 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
1698 *pPackage = package;
1699 return ERROR_SUCCESS;
1700 }
1701
1702 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1703 {
1704 MSIPACKAGE *package = NULL;
1705 UINT ret;
1706
1707 TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1708
1709 if( !szPackage || !phPackage )
1710 return ERROR_INVALID_PARAMETER;
1711
1712 if ( !*szPackage )
1713 {
1714 FIXME("Should create an empty database and package\n");
1715 return ERROR_FUNCTION_FAILED;
1716 }
1717
1718 if( dwOptions )
1719 FIXME("dwOptions %08x not supported\n", dwOptions);
1720
1721 ret = MSI_OpenPackageW( szPackage, &package );
1722 if( ret == ERROR_SUCCESS )
1723 {
1724 *phPackage = alloc_msihandle( &package->hdr );
1725 if (! *phPackage)
1726 ret = ERROR_NOT_ENOUGH_MEMORY;
1727 msiobj_release( &package->hdr );
1728 }
1729
1730 return ret;
1731 }
1732
1733 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1734 {
1735 return MsiOpenPackageExW( szPackage, 0, phPackage );
1736 }
1737
1738 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1739 {
1740 LPWSTR szwPack = NULL;
1741 UINT ret;
1742
1743 if( szPackage )
1744 {
1745 szwPack = strdupAtoW( szPackage );
1746 if( !szwPack )
1747 return ERROR_OUTOFMEMORY;
1748 }
1749
1750 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1751
1752 msi_free( szwPack );
1753
1754 return ret;
1755 }
1756
1757 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1758 {
1759 return MsiOpenPackageExA( szPackage, 0, phPackage );
1760 }
1761
1762 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1763 {
1764 MSIPACKAGE *package;
1765 MSIHANDLE handle = 0;
1766 IUnknown *remote_unk;
1767 IWineMsiRemotePackage *remote_package;
1768
1769 TRACE("(%d)\n",hInstall);
1770
1771 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1772 if( package)
1773 {
1774 handle = alloc_msihandle( &package->db->hdr );
1775 msiobj_release( &package->hdr );
1776 }
1777 else if ((remote_unk = msi_get_remote(hInstall)))
1778 {
1779 if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1780 (LPVOID *)&remote_package) == S_OK)
1781 {
1782 IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1783 IWineMsiRemotePackage_Release(remote_package);
1784 }
1785 else
1786 {
1787 WARN("remote handle %d is not a package\n", hInstall);
1788 }
1789 IUnknown_Release(remote_unk);
1790 }
1791
1792 return handle;
1793 }
1794
1795 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType, MSIRECORD *record )
1796 {
1797 static const WCHAR szActionData[] = {'A','c','t','i','o','n','D','a','t','a',0};
1798 static const WCHAR szSetProgress[] = {'S','e','t','P','r','o','g','r','e','s','s',0};
1799 static const WCHAR szActionText[] = {'A','c','t','i','o','n','T','e','x','t',0};
1800 MSIRECORD *uirow;
1801 LPWSTR deformated, message;
1802 DWORD i, len, total_len, log_type = 0;
1803 INT rc = 0;
1804 char *msg;
1805
1806 TRACE("%x\n", eMessageType);
1807
1808 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_FATALEXIT)
1809 log_type |= INSTALLLOGMODE_FATALEXIT;
1810 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1811 log_type |= INSTALLLOGMODE_ERROR;
1812 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1813 log_type |= INSTALLLOGMODE_WARNING;
1814 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1815 log_type |= INSTALLLOGMODE_USER;
1816 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1817 log_type |= INSTALLLOGMODE_INFO;
1818 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_RESOLVESOURCE)
1819 log_type |= INSTALLLOGMODE_RESOLVESOURCE;
1820 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_OUTOFDISKSPACE)
1821 log_type |= INSTALLLOGMODE_OUTOFDISKSPACE;
1822 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1823 log_type |= INSTALLLOGMODE_COMMONDATA;
1824 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1825 log_type |= INSTALLLOGMODE_ACTIONSTART;
1826 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1827 log_type |= INSTALLLOGMODE_ACTIONDATA;
1828 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1829 log_type |= INSTALLLOGMODE_PROGRESS;
1830 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INITIALIZE)
1831 log_type |= INSTALLLOGMODE_INITIALIZE;
1832 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_TERMINATE)
1833 log_type |= INSTALLLOGMODE_TERMINATE;
1834 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_SHOWDIALOG)
1835 log_type |= INSTALLLOGMODE_SHOWDIALOG;
1836
1837 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1838 {
1839 static const WCHAR template_s[]=
1840 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1841 static const WCHAR format[] =
1842 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1843 WCHAR timet[0x100];
1844 LPCWSTR action_text, action;
1845 LPWSTR deformatted = NULL;
1846
1847 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1848
1849 action = MSI_RecordGetString(record, 1);
1850 action_text = MSI_RecordGetString(record, 2);
1851
1852 if (!action || !action_text)
1853 return IDOK;
1854
1855 deformat_string(package, action_text, &deformatted);
1856
1857 len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1858 if (deformatted)
1859 len += strlenW(deformatted);
1860 message = msi_alloc(len*sizeof(WCHAR));
1861 sprintfW(message, template_s, timet, action);
1862 if (deformatted)
1863 strcatW(message, deformatted);
1864 msi_free(deformatted);
1865 }
1866 else
1867 {
1868 static const WCHAR format[] = {'%','u',':',' ',0};
1869 UINT count = MSI_RecordGetFieldCount( record );
1870 WCHAR *p;
1871
1872 total_len = 1;
1873 for (i = 1; i <= count; i++)
1874 {
1875 len = 0;
1876 MSI_RecordGetStringW( record, i, NULL, &len );
1877 total_len += len + 13;
1878 }
1879 p = message = msi_alloc( total_len * sizeof(WCHAR) );
1880 if (!p) return ERROR_OUTOFMEMORY;
1881
1882 for (i = 1; i <= count; i++)
1883 {
1884 if (count > 1)
1885 {
1886 len = sprintfW( p, format, i );
1887 total_len -= len;
1888 p += len;
1889 }
1890 len = total_len;
1891 MSI_RecordGetStringW( record, i, p, &len );
1892 total_len -= len;
1893 p += len;
1894 if (count > 1 && total_len)
1895 {
1896 *p++ = ' ';
1897 total_len--;
1898 }
1899 }
1900 p[0] = 0;
1901 }
1902
1903 TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1904 gUIFilter, log_type, debugstr_w(message));
1905
1906 /* convert it to ASCII */
1907 len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1908 msg = msi_alloc( len );
1909 WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1910
1911 if (gUIHandlerW && (gUIFilter & log_type))
1912 {
1913 rc = gUIHandlerW( gUIContext, eMessageType, message );
1914 }
1915 else if (gUIHandlerA && (gUIFilter & log_type))
1916 {
1917 rc = gUIHandlerA( gUIContext, eMessageType, msg );
1918 }
1919 else if (gUIHandlerRecord && (gUIFilter & log_type))
1920 {
1921 MSIHANDLE rec = MsiCreateRecord( 1 );
1922 MsiRecordSetStringW( rec, 0, message );
1923 rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1924 MsiCloseHandle( rec );
1925 }
1926
1927 if (!rc && package->log_file != INVALID_HANDLE_VALUE &&
1928 (eMessageType & 0xff000000) != INSTALLMESSAGE_PROGRESS)
1929 {
1930 DWORD written;
1931 WriteFile( package->log_file, msg, len - 1, &written, NULL );
1932 WriteFile( package->log_file, "\n", 1, &written, NULL );
1933 }
1934 msi_free( msg );
1935 msi_free( message );
1936
1937 switch (eMessageType & 0xff000000)
1938 {
1939 case INSTALLMESSAGE_ACTIONDATA:
1940 deformat_string(package, MSI_RecordGetString(record, 2), &deformated);
1941 uirow = MSI_CreateRecord(1);
1942 MSI_RecordSetStringW(uirow, 1, deformated);
1943 msi_free(deformated);
1944
1945 ControlEvent_FireSubscribedEvent(package, szActionData, uirow);
1946 msiobj_release(&uirow->hdr);
1947
1948 if (package->action_progress_increment)
1949 {
1950 uirow = MSI_CreateRecord(2);
1951 MSI_RecordSetInteger(uirow, 1, 2);
1952 MSI_RecordSetInteger(uirow, 2, package->action_progress_increment);
1953 ControlEvent_FireSubscribedEvent(package, szSetProgress, uirow);
1954 msiobj_release(&uirow->hdr);
1955 }
1956 break;
1957
1958 case INSTALLMESSAGE_ACTIONSTART:
1959 deformat_string(package, MSI_RecordGetString(record, 2), &deformated);
1960 uirow = MSI_CreateRecord(1);
1961 MSI_RecordSetStringW(uirow, 1, deformated);
1962 msi_free(deformated);
1963
1964 ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1965
1966 msiobj_release(&uirow->hdr);
1967 break;
1968
1969 case INSTALLMESSAGE_PROGRESS:
1970 ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1971 break;
1972 }
1973
1974 return ERROR_SUCCESS;
1975 }
1976
1977 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1978 MSIHANDLE hRecord)
1979 {
1980 UINT ret = ERROR_INVALID_HANDLE;
1981 MSIPACKAGE *package = NULL;
1982 MSIRECORD *record = NULL;
1983
1984 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1985 if( !package )
1986 {
1987 HRESULT hr;
1988 IWineMsiRemotePackage *remote_package;
1989
1990 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1991 if (!remote_package)
1992 return ERROR_INVALID_HANDLE;
1993
1994 hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1995
1996 IWineMsiRemotePackage_Release( remote_package );
1997
1998 if (FAILED(hr))
1999 {
2000 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2001 return HRESULT_CODE(hr);
2002
2003 return ERROR_FUNCTION_FAILED;
2004 }
2005
2006 return ERROR_SUCCESS;
2007 }
2008
2009 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
2010 if( !record )
2011 goto out;
2012
2013 ret = MSI_ProcessMessage( package, eMessageType, record );
2014
2015 out:
2016 msiobj_release( &package->hdr );
2017 if( record )
2018 msiobj_release( &record->hdr );
2019
2020 return ret;
2021 }
2022
2023 /* property code */
2024
2025 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
2026 {
2027 LPWSTR szwName = NULL, szwValue = NULL;
2028 UINT r = ERROR_OUTOFMEMORY;
2029
2030 szwName = strdupAtoW( szName );
2031 if( szName && !szwName )
2032 goto end;
2033
2034 szwValue = strdupAtoW( szValue );
2035 if( szValue && !szwValue )
2036 goto end;
2037
2038 r = MsiSetPropertyW( hInstall, szwName, szwValue);
2039
2040 end:
2041 msi_free( szwName );
2042 msi_free( szwValue );
2043
2044 return r;
2045 }
2046
2047 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
2048 {
2049 MSIFOLDER *folder;
2050
2051 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
2052 {
2053 if ( source )
2054 {
2055 msi_free( folder->ResolvedSource );
2056 folder->ResolvedSource = NULL;
2057 }
2058 else
2059 {
2060 msi_free( folder->ResolvedTarget );
2061 folder->ResolvedTarget = NULL;
2062 }
2063 }
2064 }
2065
2066 UINT msi_set_property( MSIDATABASE *db, const WCHAR *name, const WCHAR *value, int len )
2067 {
2068 static const WCHAR insert_query[] = {
2069 'I','N','S','E','R','T',' ','I','N','T','O',' ',
2070 '`','_','P','r','o','p','e','r','t','y','`',' ',
2071 '(','`','_','P','r','o','p','e','r','t','y','`',',','`','V','a','l','u','e','`',')',' ',
2072 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
2073 static const WCHAR update_query[] = {
2074 'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
2075 'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ','W','H','E','R','E',' ',
2076 '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2077 static const WCHAR delete_query[] = {
2078 'D','E','L','E','T','E',' ','F','R','O','M',' ',
2079 '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
2080 '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
2081 MSIQUERY *view;
2082 MSIRECORD *row = NULL;
2083 DWORD sz = 0;
2084 WCHAR query[1024];
2085 UINT rc;
2086
2087 TRACE("%p %s %s %d\n", db, debugstr_w(name), debugstr_wn(value, len), len);
2088
2089 if (!name)
2090 return ERROR_INVALID_PARAMETER;
2091
2092 /* this one is weird... */
2093 if (!name[0])
2094 return value ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
2095
2096 if (value && len < 0) len = strlenW( value );
2097
2098 rc = msi_get_property( db, name, 0, &sz );
2099 if (!value || (!*value && !len))
2100 {
2101 sprintfW( query, delete_query, name );
2102 }
2103 else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
2104 {
2105 sprintfW( query, update_query, name );
2106 row = MSI_CreateRecord(1);
2107 msi_record_set_string( row, 1, value, len );
2108 }
2109 else
2110 {
2111 strcpyW( query, insert_query );
2112 row = MSI_CreateRecord(2);
2113 msi_record_set_string( row, 1, name, -1 );
2114 msi_record_set_string( row, 2, value, len );
2115 }
2116
2117 rc = MSI_DatabaseOpenViewW(db, query, &view);
2118 if (rc == ERROR_SUCCESS)
2119 {
2120 rc = MSI_ViewExecute(view, row);
2121 MSI_ViewClose(view);
2122 msiobj_release(&view->hdr);
2123 }
2124 if (row) msiobj_release(&row->hdr);
2125 return rc;
2126 }
2127
2128 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
2129 {
2130 MSIPACKAGE *package;
2131 UINT ret;
2132
2133 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
2134 if( !package )
2135 {
2136 HRESULT hr;
2137 BSTR name = NULL, value = NULL;
2138 IWineMsiRemotePackage *remote_package;
2139
2140 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
2141 if (!remote_package)
2142 return ERROR_INVALID_HANDLE;
2143
2144 name = SysAllocString( szName );
2145 value = SysAllocString( szValue );
2146 if ((!name && szName) || (!value && szValue))
2147 {
2148 SysFreeString( name );
2149 SysFreeString( value );
2150 IWineMsiRemotePackage_Release( remote_package );
2151 return ERROR_OUTOFMEMORY;
2152 }
2153
2154 hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
2155
2156 SysFreeString( name );
2157 SysFreeString( value );
2158 IWineMsiRemotePackage_Release( remote_package );
2159
2160 if (FAILED(hr))
2161 {
2162 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2163 return HRESULT_CODE(hr);
2164
2165 return ERROR_FUNCTION_FAILED;
2166 }
2167
2168 return ERROR_SUCCESS;
2169 }
2170
2171 ret = msi_set_property( package->db, szName, szValue, -1 );
2172 if (ret == ERROR_SUCCESS && !strcmpW( szName, szSourceDir ))
2173 msi_reset_folders( package, TRUE );
2174
2175 msiobj_release( &package->hdr );
2176 return ret;
2177 }
2178
2179 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
2180 {
2181 static const WCHAR query[]= {
2182 'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
2183 'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',' ',
2184 'W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`','=','?',0};
2185 MSIRECORD *rec, *row = NULL;
2186 MSIQUERY *view;
2187 UINT r;
2188
2189 if (!name || !*name)
2190 return NULL;
2191
2192 rec = MSI_CreateRecord(1);
2193 if (!rec)
2194 return NULL;
2195
2196 MSI_RecordSetStringW(rec, 1, name);
2197
2198 r = MSI_DatabaseOpenViewW(db, query, &view);
2199 if (r == ERROR_SUCCESS)
2200 {
2201 MSI_ViewExecute(view, rec);
2202 MSI_ViewFetch(view, &row);
2203 MSI_ViewClose(view);
2204 msiobj_release(&view->hdr);
2205 }
2206 msiobj_release(&rec->hdr);
2207 return row;
2208 }
2209
2210 /* internal function, not compatible with MsiGetPropertyW */
2211 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2212 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2213 {
2214 MSIRECORD *row;
2215 UINT rc = ERROR_FUNCTION_FAILED;
2216
2217 row = msi_get_property_row( db, szName );
2218
2219 if (*pchValueBuf > 0)
2220 szValueBuf[0] = 0;
2221
2222 if (row)
2223 {
2224 rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2225 msiobj_release(&row->hdr);
2226 }
2227
2228 if (rc == ERROR_SUCCESS)
2229 TRACE("returning %s for property %s\n", debugstr_wn(szValueBuf, *pchValueBuf),
2230 debugstr_w(szName));
2231 else if (rc == ERROR_MORE_DATA)
2232 TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2233 debugstr_w(szName));
2234 else
2235 {
2236 *pchValueBuf = 0;
2237 TRACE("property %s not found\n", debugstr_w(szName));
2238 }
2239
2240 return rc;
2241 }
2242
2243 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2244 {
2245 DWORD sz = 0;
2246 LPWSTR str;
2247 UINT r;
2248
2249 r = msi_get_property(db, prop, NULL, &sz);
2250 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2251 return NULL;
2252
2253 sz++;
2254 str = msi_alloc(sz * sizeof(WCHAR));
2255 r = msi_get_property(db, prop, str, &sz);
2256 if (r != ERROR_SUCCESS)
2257 {
2258 msi_free(str);
2259 str = NULL;
2260 }
2261
2262 return str;
2263 }
2264
2265 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2266 {
2267 LPWSTR str = msi_dup_property( db, prop );
2268 int val = str ? atoiW(str) : def;
2269 msi_free(str);
2270 return val;
2271 }
2272
2273 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2274 awstring *szValueBuf, LPDWORD pchValueBuf )
2275 {
2276 MSIPACKAGE *package;
2277 MSIRECORD *row = NULL;
2278 UINT r = ERROR_FUNCTION_FAILED;
2279 LPCWSTR val = NULL;
2280 DWORD len = 0;
2281
2282 TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2283 szValueBuf->str.w, pchValueBuf );
2284
2285 if (!name)
2286 return ERROR_INVALID_PARAMETER;
2287
2288 package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2289 if (!package)
2290 {
2291 HRESULT hr;
2292 IWineMsiRemotePackage *remote_package;
2293 LPWSTR value = NULL;
2294 BSTR bname;
2295
2296 remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2297 if (!remote_package)
2298 return ERROR_INVALID_HANDLE;
2299
2300 bname = SysAllocString( name );
2301 if (!bname)
2302 {
2303 IWineMsiRemotePackage_Release( remote_package );
2304 return ERROR_OUTOFMEMORY;
2305 }
2306
2307 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2308 if (FAILED(hr))
2309 goto done;
2310
2311 len++;
2312 value = msi_alloc(len * sizeof(WCHAR));
2313 if (!value)
2314 {
2315 r = ERROR_OUTOFMEMORY;
2316 goto done;
2317 }
2318
2319 hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, value, &len );
2320 if (FAILED(hr))
2321 goto done;
2322
2323 r = msi_strcpy_to_awstring( value, len, szValueBuf, pchValueBuf );
2324
2325 /* Bug required by Adobe installers */
2326 if (!szValueBuf->unicode && !szValueBuf->str.a)
2327 *pchValueBuf *= sizeof(WCHAR);
2328
2329 done:
2330 IWineMsiRemotePackage_Release(remote_package);
2331 SysFreeString(bname);
2332 msi_free(value);
2333
2334 if (FAILED(hr))
2335 {
2336 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2337 return HRESULT_CODE(hr);
2338
2339 return ERROR_FUNCTION_FAILED;
2340 }
2341
2342 return r;
2343 }
2344
2345 row = msi_get_property_row( package->db, name );
2346 if (row)
2347 val = msi_record_get_string( row, 1, (int *)&len );
2348
2349 if (!val)
2350 val = szEmpty;
2351
2352 r = msi_strcpy_to_awstring( val, len, szValueBuf, pchValueBuf );
2353
2354 if (row)
2355 msiobj_release( &row->hdr );
2356 msiobj_release( &package->hdr );
2357
2358 return r;
2359 }
2360
2361 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2362 LPSTR szValueBuf, LPDWORD pchValueBuf )
2363 {
2364 awstring val;
2365 LPWSTR name;
2366 UINT r;
2367
2368 val.unicode = FALSE;
2369 val.str.a = szValueBuf;
2370
2371 name = strdupAtoW( szName );
2372 if (szName && !name)
2373 return ERROR_OUTOFMEMORY;
2374
2375 r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2376 msi_free( name );
2377 return r;
2378 }
2379
2380 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2381 LPWSTR szValueBuf, LPDWORD pchValueBuf )
2382 {
2383 awstring val;
2384
2385 val.unicode = TRUE;
2386 val.str.w = szValueBuf;
2387
2388 return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2389 }
2390
2391 typedef struct _msi_remote_package_impl {
2392 IWineMsiRemotePackage IWineMsiRemotePackage_iface;
2393 MSIHANDLE package;
2394 LONG refs;
2395 } msi_remote_package_impl;
2396
2397 static inline msi_remote_package_impl *impl_from_IWineMsiRemotePackage( IWineMsiRemotePackage *iface )
2398 {
2399 return CONTAINING_RECORD(iface, msi_remote_package_impl, IWineMsiRemotePackage_iface);
2400 }
2401
2402 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2403 REFIID riid,LPVOID *ppobj)
2404 {
2405 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2406 IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2407 {
2408 IWineMsiRemotePackage_AddRef( iface );
2409 *ppobj = iface;
2410 return S_OK;
2411 }
2412
2413 return E_NOINTERFACE;
2414 }
2415
2416 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2417 {
2418 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2419
2420 return InterlockedIncrement( &This->refs );
2421 }
2422
2423 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2424 {
2425 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2426 ULONG r;
2427
2428 r = InterlockedDecrement( &This->refs );
2429 if (r == 0)
2430 {
2431 MsiCloseHandle( This->package );
2432 msi_free( This );
2433 }
2434 return r;
2435 }
2436
2437 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2438 {
2439 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2440 This->package = handle;
2441 return S_OK;
2442 }
2443
2444 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2445 {
2446 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2447 IWineMsiRemoteDatabase *rdb = NULL;
2448 HRESULT hr;
2449 MSIHANDLE hdb;
2450
2451 hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2452 if (FAILED(hr) || !rdb)
2453 {
2454 ERR("Failed to create remote database\n");
2455 return hr;
2456 }
2457
2458 hdb = MsiGetActiveDatabase(This->package);
2459
2460 hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2461 if (FAILED(hr))
2462 {
2463 ERR("Failed to set the database handle\n");
2464 return hr;
2465 }
2466
2467 *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2468 return S_OK;
2469 }
2470
2471 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value, DWORD *size )
2472 {
2473 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2474 UINT r = MsiGetPropertyW(This->package, property, value, size);
2475 if (r != ERROR_SUCCESS) return HRESULT_FROM_WIN32(r);
2476 return S_OK;
2477 }
2478
2479 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2480 {
2481 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2482 UINT r = MsiSetPropertyW(This->package, property, value);
2483 return HRESULT_FROM_WIN32(r);
2484 }
2485
2486 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2487 {
2488 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2489 UINT r = MsiProcessMessage(This->package, message, record);
2490 return HRESULT_FROM_WIN32(r);
2491 }
2492
2493 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2494 {
2495 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2496 UINT r = MsiDoActionW(This->package, action);
2497 return HRESULT_FROM_WIN32(r);
2498 }
2499
2500 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2501 {
2502 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2503 UINT r = MsiSequenceW(This->package, table, sequence);
2504 return HRESULT_FROM_WIN32(r);
2505 }
2506
2507 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2508 {
2509 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2510 UINT r = MsiGetTargetPathW(This->package, folder, value, size);
2511 return HRESULT_FROM_WIN32(r);
2512 }
2513
2514 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2515 {
2516 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2517 UINT r = MsiSetTargetPathW(This->package, folder, value);
2518 return HRESULT_FROM_WIN32(r);
2519 }
2520
2521 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value, DWORD *size )
2522 {
2523 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2524 UINT r = MsiGetSourcePathW(This->package, folder, value, size);
2525 return HRESULT_FROM_WIN32(r);
2526 }
2527
2528 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2529 {
2530 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2531 *ret = MsiGetMode(This->package, mode);
2532 return S_OK;
2533 }
2534
2535 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2536 {
2537 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2538 UINT r = MsiSetMode(This->package, mode, state);
2539 return HRESULT_FROM_WIN32(r);
2540 }
2541
2542 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2543 INSTALLSTATE *installed, INSTALLSTATE *action )
2544 {
2545 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2546 UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2547 return HRESULT_FROM_WIN32(r);
2548 }
2549
2550 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2551 {
2552 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2553 UINT r = MsiSetFeatureStateW(This->package, feature, state);
2554 return HRESULT_FROM_WIN32(r);
2555 }
2556
2557 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2558 INSTALLSTATE *installed, INSTALLSTATE *action )
2559 {
2560 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2561 UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2562 return HRESULT_FROM_WIN32(r);
2563 }
2564
2565 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2566 {
2567 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2568 UINT r = MsiSetComponentStateW(This->package, component, state);
2569 return HRESULT_FROM_WIN32(r);
2570 }
2571
2572 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2573 {
2574 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2575 *language = MsiGetLanguage(This->package);
2576 return S_OK;
2577 }
2578
2579 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2580 {
2581 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2582 UINT r = MsiSetInstallLevel(This->package, level);
2583 return HRESULT_FROM_WIN32(r);
2584 }
2585
2586 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2587 BSTR *value)
2588 {
2589 DWORD size = 0;
2590 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2591 UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2592 if (r == ERROR_SUCCESS)
2593 {
2594 *value = SysAllocStringLen(NULL, size);
2595 if (!*value)
2596 return E_OUTOFMEMORY;
2597 size++;
2598 r = MsiFormatRecordW(This->package, record, *value, &size);
2599 }
2600 return HRESULT_FROM_WIN32(r);
2601 }
2602
2603 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2604 {
2605 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2606 UINT r = MsiEvaluateConditionW(This->package, condition);
2607 return HRESULT_FROM_WIN32(r);
2608 }
2609
2610 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2611 INT cost_tree, INSTALLSTATE state, INT *cost )
2612 {
2613 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2614 UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2615 return HRESULT_FROM_WIN32(r);
2616 }
2617
2618 static HRESULT WINAPI mrp_EnumComponentCosts( IWineMsiRemotePackage *iface, BSTR component,
2619 DWORD index, INSTALLSTATE state, BSTR drive,
2620 DWORD *buflen, INT *cost, INT *temp )
2621 {
2622 msi_remote_package_impl* This = impl_from_IWineMsiRemotePackage( iface );
2623 UINT r = MsiEnumComponentCostsW(This->package, component, index, state, drive, buflen, cost, temp);
2624 return HRESULT_FROM_WIN32(r);
2625 }
2626
2627 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2628 {
2629 mrp_QueryInterface,
2630 mrp_AddRef,
2631 mrp_Release,
2632 mrp_SetMsiHandle,
2633 mrp_GetActiveDatabase,
2634 mrp_GetProperty,
2635 mrp_SetProperty,
2636 mrp_ProcessMessage,
2637 mrp_DoAction,
2638 mrp_Sequence,
2639 mrp_GetTargetPath,
2640 mrp_SetTargetPath,
2641 mrp_GetSourcePath,
2642 mrp_GetMode,
2643 mrp_SetMode,
2644 mrp_GetFeatureState,
2645 mrp_SetFeatureState,
2646 mrp_GetComponentState,
2647 mrp_SetComponentState,
2648 mrp_GetLanguage,
2649 mrp_SetInstallLevel,
2650 mrp_FormatRecord,
2651 mrp_EvaluateCondition,
2652 mrp_GetFeatureCost,
2653 mrp_EnumComponentCosts
2654 };
2655
2656 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2657 {
2658 msi_remote_package_impl* This;
2659
2660 This = msi_alloc( sizeof *This );
2661 if (!This)
2662 return E_OUTOFMEMORY;
2663
2664 This->IWineMsiRemotePackage_iface.lpVtbl = &msi_remote_package_vtbl;
2665 This->package = 0;
2666 This->refs = 1;
2667
2668 *ppObj = This;
2669
2670 return S_OK;
2671 }
2672
2673 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2674 LPCWSTR property, LPWSTR value)
2675 {
2676 MSISOURCELISTINFO *info;
2677
2678 LIST_FOR_EACH_ENTRY( info, &package->sourcelist_info, MSISOURCELISTINFO, entry )
2679 {
2680 if (!strcmpW( info->value, value )) return ERROR_SUCCESS;
2681 }
2682
2683 info = msi_alloc(sizeof(MSISOURCELISTINFO));
2684 if (!info)
2685 return ERROR_OUTOFMEMORY;
2686
2687 info->context = context;
2688 info->options = options;
2689 info->property = property;
2690 info->value = strdupW(value);
2691 list_add_head(&package->sourcelist_info, &info->entry);
2692
2693 return ERROR_SUCCESS;
2694 }
2695
2696 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2697 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2698 {
2699 MSIMEDIADISK *disk;
2700
2701 LIST_FOR_EACH_ENTRY( disk, &package->sourcelist_media, MSIMEDIADISK, entry )
2702 {
2703 if (disk->disk_id == disk_id) return ERROR_SUCCESS;
2704 }
2705
2706 disk = msi_alloc(sizeof(MSIMEDIADISK));
2707 if (!disk)
2708 return ERROR_OUTOFMEMORY;
2709
2710 disk->context = context;
2711 disk->options = options;
2712 disk->disk_id = disk_id;
2713 disk->volume_label = strdupW(volume_label);
2714 disk->disk_prompt = strdupW(disk_prompt);
2715 list_add_head(&package->sourcelist_media, &disk->entry);
2716
2717 return ERROR_SUCCESS;
2718 }