reshuffling of dlls
[reactos.git] / reactos / dll / win32 / msi / package.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2004 Aric Stewart for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #define NONAMELESSUNION
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "shlwapi.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "objidl.h"
35 #include "wincrypt.h"
36 #include "winuser.h"
37 #include "shlobj.h"
38 #include "wine/unicode.h"
39 #include "objbase.h"
40
41 #include "msipriv.h"
42 #include "action.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45
46 static void MSI_FreePackage( MSIOBJECTHDR *arg)
47 {
48 MSIPACKAGE *package= (MSIPACKAGE*) arg;
49
50 if( package->dialog )
51 msi_dialog_destroy( package->dialog );
52 ACTION_free_package_structures(package);
53
54 msiobj_release( &package->db->hdr );
55 }
56
57 static UINT clone_properties(MSIDATABASE *db)
58 {
59 MSIQUERY * view = NULL;
60 UINT rc;
61 static const WCHAR CreateSql[] = {
62 'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','_','P','r','o',
63 'p','e','r','t','y','`',' ','(',' ','`','_','P','r','o','p','e','r','t',
64 'y','`',' ','C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U',
65 'L','L',',',' ','`','V','a','l','u','e','`',' ','C','H','A','R','(','9',
66 '8',')',' ','N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R',
67 'Y',' ','K','E','Y',' ','`','_','P','r','o','p','e','r','t','y','`',')',0};
68 static const WCHAR Query[] = {
69 'S','E','L','E','C','T',' ','*',' ',
70 'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
71 static const WCHAR Insert[] = {
72 'I','N','S','E','R','T',' ','i','n','t','o',' ',
73 '`','_','P','r','o','p','e','r','t','y','`',' ',
74 '(','`','_','P','r','o','p','e','r','t','y','`',',',
75 '`','V','a','l','u','e','`',')',' ',
76 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
77
78 /* create the temporary properties table */
79 rc = MSI_DatabaseOpenViewW(db, CreateSql, &view);
80 if (rc != ERROR_SUCCESS)
81 return rc;
82 rc = MSI_ViewExecute(view,0);
83 MSI_ViewClose(view);
84 msiobj_release(&view->hdr);
85 if (rc != ERROR_SUCCESS)
86 return rc;
87
88 /* clone the existing properties */
89 rc = MSI_DatabaseOpenViewW(db, Query, &view);
90 if (rc != ERROR_SUCCESS)
91 return rc;
92
93 rc = MSI_ViewExecute(view, 0);
94 if (rc != ERROR_SUCCESS)
95 {
96 MSI_ViewClose(view);
97 msiobj_release(&view->hdr);
98 return rc;
99 }
100 while (1)
101 {
102 MSIRECORD * row;
103 MSIQUERY * view2;
104
105 rc = MSI_ViewFetch(view,&row);
106 if (rc != ERROR_SUCCESS)
107 break;
108
109 rc = MSI_DatabaseOpenViewW(db,Insert,&view2);
110 if (rc!= ERROR_SUCCESS)
111 continue;
112 rc = MSI_ViewExecute(view2,row);
113 MSI_ViewClose(view2);
114 msiobj_release(&view2->hdr);
115
116 if (rc == ERROR_SUCCESS)
117 msiobj_release(&row->hdr);
118 }
119 MSI_ViewClose(view);
120 msiobj_release(&view->hdr);
121
122 return rc;
123 }
124
125 /*
126 * set_installed_prop
127 *
128 * Sets the "Installed" property to indicate that
129 * the product is installed for the current user.
130 */
131 static UINT set_installed_prop( MSIPACKAGE *package )
132 {
133 static const WCHAR szInstalled[] = {
134 'I','n','s','t','a','l','l','e','d',0 };
135 WCHAR val[2] = { '1', 0 };
136 HKEY hkey = 0;
137 UINT r;
138
139 r = MSIREG_OpenUninstallKey( package->ProductCode, &hkey, FALSE );
140 if (r == ERROR_SUCCESS)
141 {
142 RegCloseKey( hkey );
143 MSI_SetPropertyW( package, szInstalled, val );
144 }
145
146 return r;
147 }
148
149 /*
150 * There are a whole slew of these we need to set
151 *
152 *
153 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/properties.asp
154 */
155 static VOID set_installer_properties(MSIPACKAGE *package)
156 {
157 WCHAR pth[MAX_PATH];
158 WCHAR *ptr;
159 OSVERSIONINFOA OSVersion;
160 MEMORYSTATUSEX msex;
161 DWORD verval;
162 WCHAR verstr[10], bufstr[20];
163 HDC dc;
164
165 static const WCHAR cszbs[]={'\\',0};
166 static const WCHAR CFF[] =
167 {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
168 static const WCHAR PFF[] =
169 {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
170 static const WCHAR CADF[] =
171 {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
172 static const WCHAR FaF[] =
173 {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
174 static const WCHAR FoF[] =
175 {'F','o','n','t','s','F','o','l','d','e','r',0};
176 static const WCHAR SendTF[] =
177 {'S','e','n','d','T','o','F','o','l','d','e','r',0};
178 static const WCHAR SMF[] =
179 {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
180 static const WCHAR StF[] =
181 {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
182 static const WCHAR TemplF[] =
183 {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
184 static const WCHAR DF[] =
185 {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
186 static const WCHAR PMF[] =
187 {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
188 static const WCHAR ATF[] =
189 {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
190 static const WCHAR ADF[] =
191 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
192 static const WCHAR SF[] =
193 {'S','y','s','t','e','m','F','o','l','d','e','r',0};
194 static const WCHAR SF16[] =
195 {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
196 static const WCHAR LADF[] =
197 {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
198 static const WCHAR MPF[] =
199 {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
200 static const WCHAR PF[] =
201 {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
202 static const WCHAR WF[] =
203 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
204 static const WCHAR WV[] =
205 {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
206 static const WCHAR TF[]=
207 {'T','e','m','p','F','o','l','d','e','r',0};
208 static const WCHAR szAdminUser[] =
209 {'A','d','m','i','n','U','s','e','r',0};
210 static const WCHAR szPriv[] =
211 {'P','r','i','v','i','l','e','g','e','d',0};
212 static const WCHAR szOne[] =
213 {'1',0};
214 static const WCHAR v9x[] = { 'V','e','r','s','i','o','n','9','X',0 };
215 static const WCHAR vNT[] = { 'V','e','r','s','i','o','n','N','T',0 };
216 static const WCHAR szFormat[] = {'%','l','i',0};
217 static const WCHAR szWinBuild[] =
218 {'W','i','n','d','o','w','s','B','u','i','l','d', 0 };
219 static const WCHAR szSPL[] =
220 {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0 };
221 static const WCHAR szSix[] = {'6',0 };
222
223 static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
224 static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
225 static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
226 /* Screen properties */
227 static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
228 static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
229 static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
230 static const WCHAR szScreenFormat[] = {'%','d',0};
231 static const WCHAR szIntel[] = { 'I','n','t','e','l',0 };
232 SYSTEM_INFO sys_info;
233
234 /*
235 * Other things that probably should be set:
236 *
237 * SystemLanguageID ComputerName UserLanguageID LogonUser VirtualMemory
238 * Intel ShellAdvSupport DefaultUIFont VersionDatabase PackagecodeChanging
239 * ProductState CaptionHeight BorderTop BorderSide TextHeight
240 * RedirectedDllSupport Time Date Privileged
241 */
242
243 SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES_COMMON,NULL,0,pth);
244 strcatW(pth,cszbs);
245 MSI_SetPropertyW(package, CFF, pth);
246
247 SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES,NULL,0,pth);
248 strcatW(pth,cszbs);
249 MSI_SetPropertyW(package, PFF, pth);
250
251 SHGetFolderPathW(NULL,CSIDL_COMMON_APPDATA,NULL,0,pth);
252 strcatW(pth,cszbs);
253 MSI_SetPropertyW(package, CADF, pth);
254
255 SHGetFolderPathW(NULL,CSIDL_FAVORITES,NULL,0,pth);
256 strcatW(pth,cszbs);
257 MSI_SetPropertyW(package, FaF, pth);
258
259 SHGetFolderPathW(NULL,CSIDL_FONTS,NULL,0,pth);
260 strcatW(pth,cszbs);
261 MSI_SetPropertyW(package, FoF, pth);
262
263 SHGetFolderPathW(NULL,CSIDL_SENDTO,NULL,0,pth);
264 strcatW(pth,cszbs);
265 MSI_SetPropertyW(package, SendTF, pth);
266
267 SHGetFolderPathW(NULL,CSIDL_STARTMENU,NULL,0,pth);
268 strcatW(pth,cszbs);
269 MSI_SetPropertyW(package, SMF, pth);
270
271 SHGetFolderPathW(NULL,CSIDL_STARTUP,NULL,0,pth);
272 strcatW(pth,cszbs);
273 MSI_SetPropertyW(package, StF, pth);
274
275 SHGetFolderPathW(NULL,CSIDL_TEMPLATES,NULL,0,pth);
276 strcatW(pth,cszbs);
277 MSI_SetPropertyW(package, TemplF, pth);
278
279 SHGetFolderPathW(NULL,CSIDL_DESKTOP,NULL,0,pth);
280 strcatW(pth,cszbs);
281 MSI_SetPropertyW(package, DF, pth);
282
283 SHGetFolderPathW(NULL,CSIDL_PROGRAMS,NULL,0,pth);
284 strcatW(pth,cszbs);
285 MSI_SetPropertyW(package, PMF, pth);
286
287 SHGetFolderPathW(NULL,CSIDL_ADMINTOOLS,NULL,0,pth);
288 strcatW(pth,cszbs);
289 MSI_SetPropertyW(package, ATF, pth);
290
291 SHGetFolderPathW(NULL,CSIDL_APPDATA,NULL,0,pth);
292 strcatW(pth,cszbs);
293 MSI_SetPropertyW(package, ADF, pth);
294
295 SHGetFolderPathW(NULL,CSIDL_SYSTEM,NULL,0,pth);
296 strcatW(pth,cszbs);
297 MSI_SetPropertyW(package, SF, pth);
298 MSI_SetPropertyW(package, SF16, pth);
299
300 SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,pth);
301 strcatW(pth,cszbs);
302 MSI_SetPropertyW(package, LADF, pth);
303
304 SHGetFolderPathW(NULL,CSIDL_MYPICTURES,NULL,0,pth);
305 strcatW(pth,cszbs);
306 MSI_SetPropertyW(package, MPF, pth);
307
308 SHGetFolderPathW(NULL,CSIDL_PERSONAL,NULL,0,pth);
309 strcatW(pth,cszbs);
310 MSI_SetPropertyW(package, PF, pth);
311
312 SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
313 strcatW(pth,cszbs);
314 MSI_SetPropertyW(package, WF, pth);
315
316 /* Physical Memory is specified in MB. Using total amount. */
317 msex.dwLength = sizeof(msex);
318 GlobalMemoryStatusEx( &msex );
319 sprintfW( bufstr, szScreenFormat, (int)(msex.ullTotalPhys/1024/1024));
320 MSI_SetPropertyW(package, szPhysicalMemory, bufstr);
321
322 SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
323 ptr = strchrW(pth,'\\');
324 if (ptr)
325 *(ptr+1) = 0;
326 MSI_SetPropertyW(package, WV, pth);
327
328 GetTempPathW(MAX_PATH,pth);
329 MSI_SetPropertyW(package, TF, pth);
330
331
332 /* in a wine environment the user is always admin and privileged */
333 MSI_SetPropertyW(package,szAdminUser,szOne);
334 MSI_SetPropertyW(package,szPriv,szOne);
335
336 /* set the os things */
337 OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
338 GetVersionExA(&OSVersion);
339 verval = OSVersion.dwMinorVersion+OSVersion.dwMajorVersion*100;
340 sprintfW(verstr,szFormat,verval);
341 switch (OSVersion.dwPlatformId)
342 {
343 case VER_PLATFORM_WIN32_WINDOWS:
344 MSI_SetPropertyW(package,v9x,verstr);
345 break;
346 case VER_PLATFORM_WIN32_NT:
347 MSI_SetPropertyW(package,vNT,verstr);
348 break;
349 }
350 sprintfW(verstr,szFormat,OSVersion.dwBuildNumber);
351 MSI_SetPropertyW(package,szWinBuild,verstr);
352 /* just fudge this */
353 MSI_SetPropertyW(package,szSPL,szSix);
354
355 sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
356 MSI_SetPropertyW( package, szVersionMsi, bufstr );
357
358 GetSystemInfo( &sys_info );
359 if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
360 {
361 sprintfW( bufstr, szScreenFormat, sys_info.wProcessorLevel );
362 MSI_SetPropertyW( package, szIntel, bufstr );
363 }
364
365 /* Screen properties. */
366 dc = GetDC(0);
367 sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, HORZRES ) );
368 MSI_SetPropertyW( package, szScreenX, bufstr );
369 sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, VERTRES ));
370 MSI_SetPropertyW( package, szScreenY, bufstr );
371 sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, BITSPIXEL ));
372 MSI_SetPropertyW( package, szColorBits, bufstr );
373 ReleaseDC(0, dc);
374 }
375
376 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db )
377 {
378 static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
379 static const WCHAR szpi[] = {'%','i',0};
380 static const WCHAR szProductCode[] = {
381 'P','r','o','d','u','c','t','C','o','d','e',0};
382 MSIPACKAGE *package = NULL;
383 WCHAR uilevel[10];
384
385 TRACE("%p\n", db);
386
387 package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
388 MSI_FreePackage );
389 if( package )
390 {
391 msiobj_addref( &db->hdr );
392
393 package->db = db;
394 list_init( &package->components );
395 list_init( &package->features );
396 list_init( &package->files );
397 list_init( &package->tempfiles );
398 list_init( &package->folders );
399 package->ActionFormat = NULL;
400 package->LastAction = NULL;
401 package->dialog = NULL;
402 package->next_dialog = NULL;
403 list_init( &package->subscriptions );
404 list_init( &package->appids );
405 list_init( &package->classes );
406 list_init( &package->mimes );
407 list_init( &package->extensions );
408 list_init( &package->progids );
409 list_init( &package->RunningActions );
410
411 /* OK, here is where we do a slew of things to the database to
412 * prep for all that is to come as a package */
413
414 clone_properties(db);
415 set_installer_properties(package);
416 sprintfW(uilevel,szpi,gUILevel);
417 MSI_SetPropertyW(package, szLevel, uilevel);
418
419 package->ProductCode = msi_dup_property( package, szProductCode );
420 set_installed_prop( package );
421 }
422
423 return package;
424 }
425
426 /*
427 * copy_package_to_temp [internal]
428 *
429 * copy the msi file to a temp file to prevent locking a CD
430 * with a multi disc install
431 *
432 * FIXME: I think this is wrong, and instead of copying the package,
433 * we should read all the tables to memory, then open the
434 * database to read binary streams on demand.
435 */
436 static LPCWSTR copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
437 {
438 WCHAR path[MAX_PATH];
439 static const WCHAR szMSI[] = {'M','S','I',0};
440
441 GetTempPathW( MAX_PATH, path );
442 GetTempFileNameW( path, szMSI, 0, filename );
443
444 if( !CopyFileW( szPackage, filename, FALSE ) )
445 {
446 ERR("failed to copy package to temp path %s\n", debugstr_w(filename) );
447 return szPackage;
448 }
449
450 TRACE("Opening relocated package %s\n", debugstr_w( filename ));
451 return filename;
452 }
453
454 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
455 {
456 MSIDATABASE *db = NULL;
457 MSIPACKAGE *package;
458 MSIHANDLE handle;
459 UINT r;
460
461 TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
462
463 if( szPackage[0] == '#' )
464 {
465 handle = atoiW(&szPackage[1]);
466 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
467 if( !db )
468 return ERROR_INVALID_HANDLE;
469 }
470 else
471 {
472 WCHAR temppath[MAX_PATH];
473 LPCWSTR file = copy_package_to_temp( szPackage, temppath );
474
475 r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY, &db );
476
477 if (file != szPackage)
478 DeleteFileW( file );
479
480 if( r != ERROR_SUCCESS )
481 return r;
482 }
483
484 package = MSI_CreatePackage( db );
485 msiobj_release( &db->hdr );
486 if( !package )
487 return ERROR_FUNCTION_FAILED;
488
489 /*
490 * FIXME: I don't think this is right. Maybe we should be storing the
491 * name of the database in the MSIDATABASE structure and fetching this
492 * info from there, or maybe this is only relevant to cached databases.
493 */
494 if( szPackage[0] != '#' )
495 {
496 static const WCHAR OriginalDatabase[] =
497 {'O','r','i','g','i','n','a','l','D','a','t','a','b','a','s','e',0};
498 static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
499
500 MSI_SetPropertyW( package, OriginalDatabase, szPackage );
501 MSI_SetPropertyW( package, Database, szPackage );
502 }
503
504 *pPackage = package;
505
506 return ERROR_SUCCESS;
507 }
508
509 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
510 {
511 MSIPACKAGE *package = NULL;
512 UINT ret;
513
514 TRACE("%s %08lx %p\n", debugstr_w(szPackage), dwOptions, phPackage );
515
516 if( dwOptions )
517 FIXME("dwOptions %08lx not supported\n", dwOptions);
518
519 ret = MSI_OpenPackageW( szPackage, &package );
520 if( ret == ERROR_SUCCESS )
521 {
522 *phPackage = alloc_msihandle( &package->hdr );
523 msiobj_release( &package->hdr );
524 }
525
526 return ret;
527 }
528
529 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
530 {
531 return MsiOpenPackageExW( szPackage, 0, phPackage );
532 }
533
534 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
535 {
536 LPWSTR szwPack = NULL;
537 UINT ret;
538
539 if( szPackage )
540 {
541 szwPack = strdupAtoW( szPackage );
542 if( !szwPack )
543 return ERROR_OUTOFMEMORY;
544 }
545
546 ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
547
548 msi_free( szwPack );
549
550 return ret;
551 }
552
553 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
554 {
555 return MsiOpenPackageExA( szPackage, 0, phPackage );
556 }
557
558 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
559 {
560 MSIPACKAGE *package;
561 MSIHANDLE handle = 0;
562
563 TRACE("(%ld)\n",hInstall);
564
565 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
566 if( package)
567 {
568 handle = alloc_msihandle( &package->db->hdr );
569 msiobj_release( &package->hdr );
570 }
571
572 return handle;
573 }
574
575 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
576 MSIRECORD *record)
577 {
578 DWORD log_type = 0;
579 LPWSTR message;
580 DWORD sz;
581 DWORD total_size = 0;
582 INT msg_field=1;
583 INT i;
584 INT rc;
585 char *msg;
586 int len;
587
588 TRACE("%x\n", eMessageType);
589 rc = 0;
590
591 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
592 log_type |= INSTALLLOGMODE_ERROR;
593 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
594 log_type |= INSTALLLOGMODE_WARNING;
595 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
596 log_type |= INSTALLLOGMODE_USER;
597 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
598 log_type |= INSTALLLOGMODE_INFO;
599 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
600 log_type |= INSTALLLOGMODE_COMMONDATA;
601 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
602 log_type |= INSTALLLOGMODE_ACTIONSTART;
603 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
604 log_type |= INSTALLLOGMODE_ACTIONDATA;
605 /* just a guess */
606 if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
607 log_type |= 0x800;
608
609 message = msi_alloc(1*sizeof (WCHAR));
610 message[0]=0;
611 msg_field = MSI_RecordGetFieldCount(record);
612 for (i = 1; i <= msg_field; i++)
613 {
614 LPWSTR tmp;
615 WCHAR number[3];
616 static const WCHAR format[] = { '%','i',':',' ',0};
617 static const WCHAR space[] = { ' ',0};
618 sz = 0;
619 MSI_RecordGetStringW(record,i,NULL,&sz);
620 sz+=4;
621 total_size+=sz*sizeof(WCHAR);
622 tmp = msi_alloc(sz*sizeof(WCHAR));
623 message = msi_realloc(message,total_size*sizeof (WCHAR));
624
625 MSI_RecordGetStringW(record,i,tmp,&sz);
626
627 if (msg_field > 1)
628 {
629 sprintfW(number,format,i);
630 strcatW(message,number);
631 }
632 strcatW(message,tmp);
633 if (msg_field > 1)
634 strcatW(message,space);
635
636 msi_free(tmp);
637 }
638
639 TRACE("(%p %lx %lx %s)\n",gUIHandlerA, gUIFilter, log_type,
640 debugstr_w(message));
641
642 /* convert it to ASCII */
643 len = WideCharToMultiByte( CP_ACP, 0, message, -1,
644 NULL, 0, NULL, NULL );
645 msg = msi_alloc( len );
646 WideCharToMultiByte( CP_ACP, 0, message, -1,
647 msg, len, NULL, NULL );
648
649 if (gUIHandlerA && (gUIFilter & log_type))
650 {
651 rc = gUIHandlerA(gUIContext,eMessageType,msg);
652 }
653
654 if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
655 INSTALLMESSAGE_PROGRESS))
656 {
657 DWORD write;
658 HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
659 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
660
661 if (log_file != INVALID_HANDLE_VALUE)
662 {
663 SetFilePointer(log_file,0, NULL, FILE_END);
664 WriteFile(log_file,msg,strlen(msg),&write,NULL);
665 WriteFile(log_file,"\n",1,&write,NULL);
666 CloseHandle(log_file);
667 }
668 }
669 msi_free( msg );
670
671 msi_free( message);
672 return ERROR_SUCCESS;
673 }
674
675 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
676 MSIHANDLE hRecord)
677 {
678 UINT ret = ERROR_INVALID_HANDLE;
679 MSIPACKAGE *package = NULL;
680 MSIRECORD *record = NULL;
681
682 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
683 if( !package )
684 return ERROR_INVALID_HANDLE;
685
686 record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
687 if( !record )
688 goto out;
689
690 ret = MSI_ProcessMessage( package, eMessageType, record );
691
692 out:
693 msiobj_release( &package->hdr );
694 if( record )
695 msiobj_release( &record->hdr );
696
697 return ret;
698 }
699
700 /* property code */
701 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
702 {
703 LPWSTR szwName = NULL, szwValue = NULL;
704 UINT r = ERROR_OUTOFMEMORY;
705
706 szwName = strdupAtoW( szName );
707 if( szName && !szwName )
708 goto end;
709
710 szwValue = strdupAtoW( szValue );
711 if( szValue && !szwValue )
712 goto end;
713
714 r = MsiSetPropertyW( hInstall, szwName, szwValue);
715
716 end:
717 msi_free( szwName );
718 msi_free( szwValue );
719
720 return r;
721 }
722
723 UINT MSI_SetPropertyW( MSIPACKAGE *package, LPCWSTR szName, LPCWSTR szValue)
724 {
725 MSIQUERY *view;
726 MSIRECORD *row;
727 UINT rc;
728 DWORD sz = 0;
729 static const WCHAR Insert[]=
730 {'I','N','S','E','R','T',' ','i','n','t','o',' ','`','_','P','r','o','p'
731 ,'e','r','t','y','`',' ','(','`','_','P','r','o','p','e','r','t','y','`'
732 ,',','`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
733 ,' ','(','?',',','?',')',0};
734 static const WCHAR Update[]=
735 {'U','P','D','A','T','E',' ','_','P','r','o','p','e'
736 ,'r','t','y',' ','s','e','t',' ','`','V','a','l','u','e','`',' ','='
737 ,' ','?',' ','w','h','e','r','e',' ','`','_','P','r','o','p'
738 ,'e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
739 WCHAR Query[1024];
740
741 TRACE("%p %s %s\n", package, debugstr_w(szName), debugstr_w(szValue));
742
743 if (!szName)
744 return ERROR_INVALID_PARAMETER;
745
746 /* this one is weird... */
747 if (!szName[0])
748 return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
749
750 rc = MSI_GetPropertyW(package,szName,0,&sz);
751 if (rc==ERROR_MORE_DATA || rc == ERROR_SUCCESS)
752 {
753 sprintfW(Query,Update,szName);
754
755 row = MSI_CreateRecord(1);
756 MSI_RecordSetStringW(row,1,szValue);
757
758 }
759 else
760 {
761 strcpyW(Query,Insert);
762
763 row = MSI_CreateRecord(2);
764 MSI_RecordSetStringW(row,1,szName);
765 MSI_RecordSetStringW(row,2,szValue);
766 }
767
768 rc = MSI_DatabaseOpenViewW(package->db,Query,&view);
769 if (rc == ERROR_SUCCESS)
770 {
771 rc = MSI_ViewExecute(view,row);
772
773 MSI_ViewClose(view);
774 msiobj_release(&view->hdr);
775 }
776 msiobj_release(&row->hdr);
777
778 return rc;
779 }
780
781 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
782 {
783 MSIPACKAGE *package;
784 UINT ret;
785
786 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
787 if( !package )
788 return ERROR_INVALID_HANDLE;
789 ret = MSI_SetPropertyW( package, szName, szValue);
790 msiobj_release( &package->hdr );
791 return ret;
792 }
793
794 static MSIRECORD *MSI_GetPropertyRow( MSIPACKAGE *package, LPCWSTR name )
795 {
796 static const WCHAR query[]=
797 {'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
798 'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
799 ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
800 '=','\'','%','s','\'',0};
801
802 if (!name || !name[0])
803 return NULL;
804
805 return MSI_QueryGetRecord( package->db, query, name );
806 }
807
808 /* internal function, not compatible with MsiGetPropertyW */
809 UINT MSI_GetPropertyW( MSIPACKAGE *package, LPCWSTR szName,
810 LPWSTR szValueBuf, DWORD* pchValueBuf )
811 {
812 MSIRECORD *row;
813 UINT rc = ERROR_FUNCTION_FAILED;
814
815 row = MSI_GetPropertyRow( package, szName );
816
817 if (*pchValueBuf > 0)
818 szValueBuf[0] = 0;
819
820 if (row)
821 {
822 rc = MSI_RecordGetStringW(row,1,szValueBuf,pchValueBuf);
823 msiobj_release(&row->hdr);
824 }
825
826 if (rc == ERROR_SUCCESS)
827 TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
828 debugstr_w(szName));
829 else if (rc == ERROR_MORE_DATA)
830 TRACE("need %li sized buffer for %s\n", *pchValueBuf,
831 debugstr_w(szName));
832 else
833 {
834 *pchValueBuf = 0;
835 TRACE("property %s not found\n", debugstr_w(szName));
836 }
837
838 return rc;
839 }
840
841 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
842 awstring *szValueBuf, DWORD* pchValueBuf )
843 {
844 static const WCHAR empty[] = {0};
845 MSIPACKAGE *package;
846 MSIRECORD *row = NULL;
847 UINT r;
848 LPCWSTR val = NULL;
849
850 TRACE("%lu %s %p %p\n", handle, debugstr_w(name),
851 szValueBuf->str.w, pchValueBuf );
852
853 if (!name)
854 return ERROR_INVALID_PARAMETER;
855
856 package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
857 if (!package)
858 return ERROR_INVALID_HANDLE;
859
860 row = MSI_GetPropertyRow( package, name );
861 if (row)
862 val = MSI_RecordGetString( row, 1 );
863
864 if (!val)
865 val = empty;
866
867 r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
868
869 if (row)
870 msiobj_release( &row->hdr );
871 msiobj_release( &package->hdr );
872
873 return r;
874 }
875
876 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
877 LPSTR szValueBuf, DWORD* pchValueBuf )
878 {
879 awstring val;
880 LPWSTR name;
881 UINT r;
882
883 val.unicode = FALSE;
884 val.str.a = szValueBuf;
885
886 name = strdupAtoW( szName );
887 if (szName && !name)
888 return ERROR_OUTOFMEMORY;
889
890 r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
891 msi_free( name );
892 return r;
893 }
894
895 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
896 LPWSTR szValueBuf, DWORD* pchValueBuf )
897 {
898 awstring val;
899
900 val.unicode = TRUE;
901 val.str.w = szValueBuf;
902
903 return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
904 }