Keep track of what sequence we are in and register unique
[wine/multimedia.git] / dlls / msi / helpers.c
bloba234a4c188133a76ac3d3c0759cd085ed7959d3b
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
22 * Here are helper functions formally in action.c that are used by a variaty of
23 * actions and functions.
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "action.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
40 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
42 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
43 const WCHAR szProductCode[]= {'P','r','o','d','u','c','t','C','o','d','e',0};
44 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
45 const WCHAR cszbs[]={'\\',0};
47 DWORD build_version_dword(LPCWSTR version_string)
49 SHORT major,minor;
50 WORD build;
51 DWORD rc = 0x00000000;
52 LPCWSTR ptr1;
54 ptr1 = version_string;
56 if (!ptr1)
57 return rc;
58 else
59 major = atoiW(ptr1);
62 if(ptr1)
63 ptr1 = strchrW(ptr1,'.');
64 if (ptr1)
66 ptr1++;
67 minor = atoiW(ptr1);
69 else
70 minor = 0;
72 if (ptr1)
73 ptr1 = strchrW(ptr1,'.');
75 if (ptr1)
77 ptr1++;
78 build = atoiW(ptr1);
80 else
81 build = 0;
83 rc = MAKELONG(build,MAKEWORD(minor,major));
84 TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
85 return rc;
88 UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name,
89 LPWSTR *FilePath)
91 LPWSTR ProductCode;
92 LPWSTR SystemFolder;
93 LPWSTR dest;
94 UINT rc;
96 static const WCHAR szInstaller[] =
97 {'M','i','c','r','o','s','o','f','t','\\',
98 'I','n','s','t','a','l','l','e','r','\\',0};
99 static const WCHAR szFolder[] =
100 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
102 ProductCode = load_dynamic_property(package,szProductCode,&rc);
103 if (!ProductCode)
104 return rc;
106 SystemFolder = load_dynamic_property(package,szFolder,NULL);
108 dest = build_directory_name(3, SystemFolder, szInstaller, ProductCode);
110 create_full_pathW(dest);
112 *FilePath = build_directory_name(2, dest, icon_name);
114 HeapFree(GetProcessHeap(),0,SystemFolder);
115 HeapFree(GetProcessHeap(),0,ProductCode);
116 HeapFree(GetProcessHeap(),0,dest);
117 return ERROR_SUCCESS;
120 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
122 UINT rc;
123 DWORD sz;
124 LPWSTR ret;
126 sz = 0;
127 if (MSI_RecordIsNull(row,index))
128 return NULL;
130 rc = MSI_RecordGetStringW(row,index,NULL,&sz);
132 /* having an empty string is different than NULL */
133 if (sz == 0)
135 ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
136 ret[0] = 0;
137 return ret;
140 sz ++;
141 ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
142 rc = MSI_RecordGetStringW(row,index,ret,&sz);
143 if (rc!=ERROR_SUCCESS)
145 ERR("Unable to load dynamic string\n");
146 HeapFree(GetProcessHeap(), 0, ret);
147 ret = NULL;
149 return ret;
152 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
154 DWORD sz = 0;
155 LPWSTR str;
156 UINT r;
158 r = MSI_GetPropertyW(package, prop, NULL, &sz);
159 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
161 if (rc)
162 *rc = r;
163 return NULL;
165 sz++;
166 str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
167 r = MSI_GetPropertyW(package, prop, str, &sz);
168 if (r != ERROR_SUCCESS)
170 HeapFree(GetProcessHeap(),0,str);
171 str = NULL;
173 if (rc)
174 *rc = r;
175 return str;
178 int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
180 int rc = -1;
181 DWORD i;
183 for (i = 0; i < package->loaded_components; i++)
185 if (strcmpW(Component,package->components[i].Component)==0)
187 rc = i;
188 break;
191 return rc;
194 int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
196 int rc = -1;
197 DWORD i;
199 for (i = 0; i < package->loaded_features; i++)
201 if (strcmpW(Feature,package->features[i].Feature)==0)
203 rc = i;
204 break;
207 return rc;
210 int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
212 int rc = -1;
213 DWORD i;
215 for (i = 0; i < package->loaded_files; i++)
217 if (strcmpW(file,package->files[i].File)==0)
219 rc = i;
220 break;
223 return rc;
226 int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
228 DWORD i;
229 DWORD index;
231 if (!package)
232 return -2;
234 for (i=0; i < package->loaded_files; i++)
235 if (strcmpW(package->files[i].File,name)==0)
236 return -1;
238 index = package->loaded_files;
239 package->loaded_files++;
240 if (package->loaded_files== 1)
241 package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
242 else
243 package->files = HeapReAlloc(GetProcessHeap(),0,
244 package->files , package->loaded_files * sizeof(MSIFILE));
246 memset(&package->files[index],0,sizeof(MSIFILE));
248 package->files[index].File = strdupW(name);
249 package->files[index].TargetPath = strdupW(path);
250 package->files[index].Temporary = TRUE;
252 TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));
254 return 0;
257 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
258 BOOL set_prop, MSIFOLDER **folder)
260 DWORD i;
261 LPWSTR p, path = NULL;
263 TRACE("Working to resolve %s\n",debugstr_w(name));
265 if (!name)
266 return NULL;
268 /* special resolving for Target and Source root dir */
269 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
271 if (!source)
273 LPWSTR check_path;
274 check_path = load_dynamic_property(package,cszTargetDir,NULL);
275 if (!check_path)
277 check_path = load_dynamic_property(package,cszRootDrive,NULL);
278 if (set_prop)
279 MSI_SetPropertyW(package,cszTargetDir,check_path);
282 /* correct misbuilt target dir */
283 path = build_directory_name(2, check_path, NULL);
284 if (strcmpiW(path,check_path)!=0)
285 MSI_SetPropertyW(package,cszTargetDir,path);
287 if (folder)
289 for (i = 0; i < package->loaded_folders; i++)
291 if (strcmpW(package->folders[i].Directory,name)==0)
292 break;
294 *folder = &(package->folders[i]);
296 return path;
298 else
300 path = load_dynamic_property(package,cszSourceDir,NULL);
301 if (!path)
303 path = load_dynamic_property(package,cszDatabase,NULL);
304 if (path)
306 p = strrchrW(path,'\\');
307 if (p)
308 *(p+1) = 0;
311 if (folder)
313 for (i = 0; i < package->loaded_folders; i++)
315 if (strcmpW(package->folders[i].Directory,name)==0)
316 break;
318 *folder = &(package->folders[i]);
320 return path;
324 for (i = 0; i < package->loaded_folders; i++)
326 if (strcmpW(package->folders[i].Directory,name)==0)
327 break;
330 if (i >= package->loaded_folders)
331 return NULL;
333 if (folder)
334 *folder = &(package->folders[i]);
336 if (!source && package->folders[i].ResolvedTarget)
338 path = strdupW(package->folders[i].ResolvedTarget);
339 TRACE(" already resolved to %s\n",debugstr_w(path));
340 return path;
342 else if (source && package->folders[i].ResolvedSource)
344 path = strdupW(package->folders[i].ResolvedSource);
345 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
346 return path;
348 else if (!source && package->folders[i].Property)
350 path = build_directory_name(2, package->folders[i].Property, NULL);
352 TRACE(" internally set to %s\n",debugstr_w(path));
353 if (set_prop)
354 MSI_SetPropertyW(package,name,path);
355 return path;
358 if (package->folders[i].ParentIndex >= 0)
360 LPWSTR parent = package->folders[package->folders[i].ParentIndex].Directory;
362 TRACE(" ! Parent is %s\n", debugstr_w(parent));
364 p = resolve_folder(package, parent, source, set_prop, NULL);
365 if (!source)
367 TRACE(" TargetDefault = %s\n",
368 debugstr_w(package->folders[i].TargetDefault));
370 path = build_directory_name(3, p,
371 package->folders[i].TargetDefault, NULL);
372 package->folders[i].ResolvedTarget = strdupW(path);
373 TRACE(" resolved into %s\n",debugstr_w(path));
374 if (set_prop)
375 MSI_SetPropertyW(package,name,path);
377 else
379 if (package->folders[i].SourceDefault &&
380 package->folders[i].SourceDefault[0]!='.')
381 path = build_directory_name(3, p, package->folders[i].SourceDefault, NULL);
382 else
383 path = strdupW(p);
384 TRACE(" (source)resolved into %s\n",debugstr_w(path));
385 package->folders[i].ResolvedSource = strdupW(path);
387 HeapFree(GetProcessHeap(),0,p);
389 return path;
392 /* wrapper to resist a need for a full rewrite right now */
393 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
395 if (ptr)
397 MSIRECORD *rec = MSI_CreateRecord(1);
398 DWORD size = 0;
400 MSI_RecordSetStringW(rec,0,ptr);
401 MSI_FormatRecordW(package,rec,NULL,&size);
402 if (size >= 0)
404 size++;
405 *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
406 if (size > 1)
407 MSI_FormatRecordW(package,rec,*data,&size);
408 else
409 *data[0] = 0;
410 msiobj_release( &rec->hdr );
411 return sizeof(WCHAR)*size;
413 msiobj_release( &rec->hdr );
416 *data = NULL;
417 return 0;
420 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
422 UINT count;
423 LPWSTR *newbuf = NULL;
424 if (script >= TOTAL_SCRIPTS)
426 FIXME("Unknown script requested %i\n",script);
427 return ERROR_FUNCTION_FAILED;
429 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
431 count = package->script->ActionCount[script];
432 package->script->ActionCount[script]++;
433 if (count != 0)
434 newbuf = HeapReAlloc(GetProcessHeap(),0,
435 package->script->Actions[script],
436 package->script->ActionCount[script]* sizeof(LPWSTR));
437 else
438 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
440 newbuf[count] = strdupW(action);
441 package->script->Actions[script] = newbuf;
443 return ERROR_SUCCESS;
446 static void remove_tracked_tempfiles(MSIPACKAGE* package)
448 DWORD i;
450 if (!package)
451 return;
453 for (i = 0; i < package->loaded_files; i++)
455 if (package->files[i].Temporary)
457 TRACE("Cleaning up %s\n",debugstr_w(package->files[i].TargetPath));
458 DeleteFileW(package->files[i].TargetPath);
464 /* Called when the package is being closed */
465 void ACTION_free_package_structures( MSIPACKAGE* package)
467 INT i;
469 TRACE("Freeing package action data\n");
471 remove_tracked_tempfiles(package);
473 /* No dynamic buffers in features */
474 if (package->features && package->loaded_features > 0)
475 HeapFree(GetProcessHeap(),0,package->features);
477 for (i = 0; i < package->loaded_folders; i++)
479 HeapFree(GetProcessHeap(),0,package->folders[i].Directory);
480 HeapFree(GetProcessHeap(),0,package->folders[i].TargetDefault);
481 HeapFree(GetProcessHeap(),0,package->folders[i].SourceDefault);
482 HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
483 HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedSource);
484 HeapFree(GetProcessHeap(),0,package->folders[i].Property);
486 if (package->folders && package->loaded_folders > 0)
487 HeapFree(GetProcessHeap(),0,package->folders);
489 for (i = 0; i < package->loaded_components; i++)
490 HeapFree(GetProcessHeap(),0,package->components[i].FullKeypath);
492 if (package->components && package->loaded_components > 0)
493 HeapFree(GetProcessHeap(),0,package->components);
495 for (i = 0; i < package->loaded_files; i++)
497 HeapFree(GetProcessHeap(),0,package->files[i].File);
498 HeapFree(GetProcessHeap(),0,package->files[i].FileName);
499 HeapFree(GetProcessHeap(),0,package->files[i].ShortName);
500 HeapFree(GetProcessHeap(),0,package->files[i].Version);
501 HeapFree(GetProcessHeap(),0,package->files[i].Language);
502 HeapFree(GetProcessHeap(),0,package->files[i].SourcePath);
503 HeapFree(GetProcessHeap(),0,package->files[i].TargetPath);
506 if (package->files && package->loaded_files > 0)
507 HeapFree(GetProcessHeap(),0,package->files);
509 /* clean up extension, progid, class and verb structures */
510 for (i = 0; i < package->loaded_classes; i++)
512 HeapFree(GetProcessHeap(),0,package->classes[i].Description);
513 HeapFree(GetProcessHeap(),0,package->classes[i].FileTypeMask);
514 HeapFree(GetProcessHeap(),0,package->classes[i].IconPath);
515 HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler);
516 HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler32);
517 HeapFree(GetProcessHeap(),0,package->classes[i].Argument);
518 HeapFree(GetProcessHeap(),0,package->classes[i].ProgIDText);
521 if (package->classes && package->loaded_classes > 0)
522 HeapFree(GetProcessHeap(),0,package->classes);
524 for (i = 0; i < package->loaded_extensions; i++)
526 HeapFree(GetProcessHeap(),0,package->extensions[i].ProgIDText);
529 if (package->extensions && package->loaded_extensions > 0)
530 HeapFree(GetProcessHeap(),0,package->extensions);
532 for (i = 0; i < package->loaded_progids; i++)
534 HeapFree(GetProcessHeap(),0,package->progids[i].ProgID);
535 HeapFree(GetProcessHeap(),0,package->progids[i].Description);
536 HeapFree(GetProcessHeap(),0,package->progids[i].IconPath);
539 if (package->progids && package->loaded_progids > 0)
540 HeapFree(GetProcessHeap(),0,package->progids);
542 for (i = 0; i < package->loaded_verbs; i++)
544 HeapFree(GetProcessHeap(),0,package->verbs[i].Verb);
545 HeapFree(GetProcessHeap(),0,package->verbs[i].Command);
546 HeapFree(GetProcessHeap(),0,package->verbs[i].Argument);
549 if (package->verbs && package->loaded_verbs > 0)
550 HeapFree(GetProcessHeap(),0,package->verbs);
552 for (i = 0; i < package->loaded_mimes; i++)
553 HeapFree(GetProcessHeap(),0,package->mimes[i].ContentType);
555 if (package->mimes && package->loaded_mimes > 0)
556 HeapFree(GetProcessHeap(),0,package->mimes);
558 for (i = 0; i < package->loaded_appids; i++)
560 HeapFree(GetProcessHeap(),0,package->appids[i].RemoteServerName);
561 HeapFree(GetProcessHeap(),0,package->appids[i].LocalServer);
562 HeapFree(GetProcessHeap(),0,package->appids[i].ServiceParameters);
563 HeapFree(GetProcessHeap(),0,package->appids[i].DllSurrogate);
566 if (package->appids && package->loaded_appids > 0)
567 HeapFree(GetProcessHeap(),0,package->appids);
569 if (package->script)
571 for (i = 0; i < TOTAL_SCRIPTS; i++)
573 int j;
574 for (j = 0; j < package->script->ActionCount[i]; j++)
575 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
577 HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
580 for (i = 0; i < package->script->UniqueActionsCount; i++)
581 HeapFree(GetProcessHeap(),0,package->script->UniqueActions[i]);
583 HeapFree(GetProcessHeap(),0,package->script->UniqueActions);
584 HeapFree(GetProcessHeap(),0,package->script);
587 HeapFree(GetProcessHeap(),0,package->PackagePath);
588 HeapFree(GetProcessHeap(),0,package->msiFilePath);
590 /* cleanup control event subscriptions */
591 ControlEvent_CleanupSubscriptions(package);
595 * build_directory_name()
597 * This function is to save messing round with directory names
598 * It handles adding backslashes between path segments,
599 * and can add \ at the end of the directory name if told to.
601 * It takes a variable number of arguments.
602 * It always allocates a new string for the result, so make sure
603 * to free the return value when finished with it.
605 * The first arg is the number of path segments that follow.
606 * The arguments following count are a list of path segments.
607 * A path segment may be NULL.
609 * Path segments will be added with a \ separating them.
610 * A \ will not be added after the last segment, however if the
611 * last segment is NULL, then the last character will be a \
614 LPWSTR build_directory_name(DWORD count, ...)
616 DWORD sz = 1, i;
617 LPWSTR dir;
618 va_list va;
620 va_start(va,count);
621 for(i=0; i<count; i++)
623 LPCWSTR str = va_arg(va,LPCWSTR);
624 if (str)
625 sz += strlenW(str) + 1;
627 va_end(va);
629 dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
630 dir[0]=0;
632 va_start(va,count);
633 for(i=0; i<count; i++)
635 LPCWSTR str = va_arg(va,LPCWSTR);
636 if (!str)
637 continue;
638 strcatW(dir, str);
639 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
640 strcatW(dir, cszbs);
642 return dir;
645 /***********************************************************************
646 * create_full_pathW
648 * Recursively create all directories in the path.
650 * shamelessly stolen from setupapi/queue.c
652 BOOL create_full_pathW(const WCHAR *path)
654 BOOL ret = TRUE;
655 int len;
656 WCHAR *new_path;
658 new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
659 sizeof(WCHAR));
661 strcpyW(new_path, path);
663 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
664 new_path[len - 1] = 0;
666 while(!CreateDirectoryW(new_path, NULL))
668 WCHAR *slash;
669 DWORD last_error = GetLastError();
670 if(last_error == ERROR_ALREADY_EXISTS)
671 break;
673 if(last_error != ERROR_PATH_NOT_FOUND)
675 ret = FALSE;
676 break;
679 if(!(slash = strrchrW(new_path, '\\')))
681 ret = FALSE;
682 break;
685 len = slash - new_path;
686 new_path[len] = 0;
687 if(!create_full_pathW(new_path))
689 ret = FALSE;
690 break;
692 new_path[len] = '\\';
695 HeapFree(GetProcessHeap(), 0, new_path);
696 return ret;
699 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
701 MSIRECORD * row;
703 row = MSI_CreateRecord(4);
704 MSI_RecordSetInteger(row,1,a);
705 MSI_RecordSetInteger(row,2,b);
706 MSI_RecordSetInteger(row,3,c);
707 MSI_RecordSetInteger(row,4,d);
708 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
709 msiobj_release(&row->hdr);
711 msi_dialog_check_messages(NULL);
714 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
716 static const WCHAR Query_t[] =
717 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
718 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
719 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
720 ' ','\'','%','s','\'',0};
721 WCHAR message[1024];
722 MSIRECORD * row = 0;
723 DWORD size;
724 static const WCHAR szActionData[] =
725 {'A','c','t','i','o','n','D','a','t','a',0};
727 if (!package->LastAction || strcmpW(package->LastAction,action))
729 row = MSI_QueryGetRecord(package->db, Query_t, action);
730 if (!row)
731 return;
733 if (MSI_RecordIsNull(row,3))
735 msiobj_release(&row->hdr);
736 return;
739 /* update the cached actionformat */
740 HeapFree(GetProcessHeap(),0,package->ActionFormat);
741 package->ActionFormat = load_dynamic_stringW(row,3);
743 HeapFree(GetProcessHeap(),0,package->LastAction);
744 package->LastAction = strdupW(action);
746 msiobj_release(&row->hdr);
749 MSI_RecordSetStringW(record,0,package->ActionFormat);
750 size = 1024;
751 MSI_FormatRecordW(package,record,message,&size);
753 row = MSI_CreateRecord(1);
754 MSI_RecordSetStringW(row,1,message);
756 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
758 ControlEvent_FireSubscribedEvent(package,szActionData, row);
760 msiobj_release(&row->hdr);
763 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, INT index,
764 INSTALLSTATE check )
766 if (package->components[index].Installed == check)
767 return FALSE;
769 if (package->components[index].ActionRequest == check)
770 return TRUE;
771 else
772 return FALSE;
775 BOOL ACTION_VerifyFeatureForAction(MSIPACKAGE* package, INT index,
776 INSTALLSTATE check )
778 if (package->features[index].Installed == check)
779 return FALSE;
781 if (package->features[index].ActionRequest == check)
782 return TRUE;
783 else
784 return FALSE;
787 void reduce_to_longfilename(WCHAR* filename)
789 LPWSTR p = strchrW(filename,'|');
790 if (p)
791 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
794 void reduce_to_shortfilename(WCHAR* filename)
796 LPWSTR p = strchrW(filename,'|');
797 if (p)
798 *p = 0;
801 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
802 MSICOMPONENT* component, LPCWSTR feature)
804 LPWSTR productid=NULL;
805 GUID clsid;
806 WCHAR productid_85[21];
807 WCHAR component_85[21];
809 * I have a fair bit of confusion as to when a < is used and when a > is
810 * used. I do not think i have it right...
812 * Ok it appears that the > is used if there is a guid for the compoenent
813 * and the < is used if not.
815 static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
816 static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
817 LPWSTR output = NULL;
818 DWORD sz = 0;
820 memset(productid_85,0,sizeof(productid_85));
821 memset(component_85,0,sizeof(component_85));
823 productid = load_dynamic_property(package,szProductCode,NULL);
824 CLSIDFromString(productid, &clsid);
826 encode_base85_guid(&clsid,productid_85);
828 CLSIDFromString(component->ComponentId, &clsid);
829 encode_base85_guid(&clsid,component_85);
831 TRACE("Doing something with this... %s %s %s\n",
832 debugstr_w(productid_85), debugstr_w(feature),
833 debugstr_w(component_85));
835 sz = lstrlenW(productid_85) + lstrlenW(feature);
836 if (component)
837 sz += lstrlenW(component_85);
839 sz+=3;
840 sz *= sizeof(WCHAR);
842 output = HeapAlloc(GetProcessHeap(),0,sz);
843 memset(output,0,sz);
845 if (component)
846 sprintfW(output,fmt2,productid_85,feature,component_85);
847 else
848 sprintfW(output,fmt1,productid_85,feature);
850 HeapFree(GetProcessHeap(),0,productid);
852 return output;
855 /* update compoennt state based on a feature change */
856 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
858 int i;
859 INSTALLSTATE newstate;
860 MSIFEATURE *feature;
862 i = get_loaded_feature(package,szFeature);
863 if (i < 0)
864 return;
866 feature = &package->features[i];
867 newstate = feature->ActionRequest;
869 for( i = 0; i < feature->ComponentCount; i++)
871 MSICOMPONENT* component = &package->components[feature->Components[i]];
873 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
874 newstate, debugstr_w(component->Component), component->Installed,
875 component->Action, component->ActionRequest);
877 if (!component->Enabled)
878 continue;
879 else
881 if (newstate == INSTALLSTATE_LOCAL)
883 component->ActionRequest = INSTALLSTATE_LOCAL;
884 component->Action = INSTALLSTATE_LOCAL;
886 else
888 int j,k;
890 component->ActionRequest = newstate;
891 component->Action = newstate;
893 /*if any other feature wants is local we need to set it local*/
894 for (j = 0;
895 j < package->loaded_features &&
896 component->ActionRequest != INSTALLSTATE_LOCAL;
897 j++)
899 for (k = 0; k < package->features[j].ComponentCount; k++)
900 if ( package->features[j].Components[k] ==
901 feature->Components[i] )
903 if (package->features[j].ActionRequest ==
904 INSTALLSTATE_LOCAL)
906 TRACE("Saved by %s\n", debugstr_w(package->features[j].Feature));
907 component->ActionRequest = INSTALLSTATE_LOCAL;
908 component->Action = INSTALLSTATE_LOCAL;
910 break;
915 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
916 newstate, debugstr_w(component->Component), component->Installed,
917 component->Action, component->ActionRequest);
921 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
923 UINT count;
924 LPWSTR *newbuf = NULL;
926 if (!package || !package->script)
927 return FALSE;
929 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
931 count = package->script->UniqueActionsCount;
932 package->script->UniqueActionsCount++;
933 if (count != 0)
934 newbuf = HeapReAlloc(GetProcessHeap(),0,
935 package->script->UniqueActions,
936 package->script->UniqueActionsCount* sizeof(LPWSTR));
937 else
938 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
940 newbuf[count] = strdupW(action);
941 package->script->UniqueActions = newbuf;
943 return ERROR_SUCCESS;
946 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
948 INT i;
950 if (!package || !package->script)
951 return FALSE;
953 for (i = 0; i < package->script->UniqueActionsCount; i++)
954 if (!strcmpW(package->script->UniqueActions[i],action))
955 return TRUE;
957 return FALSE;
960 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
962 static const WCHAR query[] = {'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ','F','R','O','M',' ','`','E','r','r','o','r','`',' ','W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ','%','i',0};
964 MSIRECORD *rec;
965 MSIRECORD *row;
966 DWORD size = 0;
967 DWORD i;
968 va_list va;
969 LPCWSTR str;
970 LPWSTR data;
972 row = MSI_QueryGetRecord(package->db, query, error);
973 if (!row)
974 return 0;
976 rec = MSI_CreateRecord(count+2);
978 str = MSI_RecordGetString(row,1);
979 MSI_RecordSetStringW(rec,0,str);
980 msiobj_release( &row->hdr );
981 MSI_RecordSetInteger(rec,1,error);
983 va_start(va,count);
984 for (i = 0; i < count; i++)
986 str = va_arg(va,LPCWSTR);
987 MSI_RecordSetStringW(rec,(i+2),str);
989 va_end(va);
991 MSI_FormatRecordW(package,rec,NULL,&size);
992 if (size >= 0)
994 size++;
995 data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
996 if (size > 1)
997 MSI_FormatRecordW(package,rec,data,&size);
998 else
999 data[0] = 0;
1000 msiobj_release( &rec->hdr );
1001 return data;
1004 msiobj_release( &rec->hdr );
1005 data = NULL;
1006 return data;