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