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