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