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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
31 #include "wine/debug.h"
38 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 * These apis are defined in MSI 3.0
47 typedef struct tagMediaInfo
55 static UINT OpenSourceKey(LPCWSTR szProduct, HKEY* key, DWORD dwOptions,
56 MSIINSTALLCONTEXT context, BOOL create)
59 UINT rc = ERROR_FUNCTION_FAILED;
60 static const WCHAR szSourceList[] = {'S','o','u','r','c','e','L','i','s','t',0};
62 if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
64 if (dwOptions & MSICODE_PATCH)
65 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
67 rc = MSIREG_OpenUserProductsKey(szProduct, &rootkey, create);
69 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
71 if (dwOptions & MSICODE_PATCH)
72 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
74 rc = MSIREG_OpenLocalManagedProductKey(szProduct, &rootkey, create);
76 else if (context == MSIINSTALLCONTEXT_MACHINE)
78 if (dwOptions & MSICODE_PATCH)
79 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
81 rc = MSIREG_OpenLocalClassesProductKey(szProduct, &rootkey, create);
84 if (rc != ERROR_SUCCESS)
86 if (dwOptions & MSICODE_PATCH)
87 return ERROR_UNKNOWN_PATCH;
89 return ERROR_UNKNOWN_PRODUCT;
93 rc = RegCreateKeyW(rootkey, szSourceList, key);
96 rc = RegOpenKeyW(rootkey,szSourceList, key);
97 if (rc != ERROR_SUCCESS)
98 rc = ERROR_BAD_CONFIGURATION;
104 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
107 static const WCHAR media[] = {'M','e','d','i','a',0};
110 rc = RegCreateKeyW(rootkey, media, key);
112 rc = RegOpenKeyW(rootkey,media, key);
117 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
120 static const WCHAR net[] = {'N','e','t',0};
123 rc = RegCreateKeyW(rootkey, net, key);
125 rc = RegOpenKeyW(rootkey, net, key);
130 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
133 static const WCHAR URL[] = {'U','R','L',0};
136 rc = RegCreateKeyW(rootkey, URL, key);
138 rc = RegOpenKeyW(rootkey, URL, key);
143 /******************************************************************
144 * MsiSourceListEnumMediaDisksA (MSI.@)
146 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
147 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
148 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
149 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
150 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
152 LPWSTR product = NULL;
153 LPWSTR usersid = NULL;
154 LPWSTR volume = NULL;
155 LPWSTR prompt = NULL;
156 UINT r = ERROR_INVALID_PARAMETER;
158 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
159 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
160 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
162 if (szDiskPrompt && !pcchDiskPrompt)
163 return ERROR_INVALID_PARAMETER;
165 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
166 if (szUserSid) usersid = strdupAtoW(szUserSid);
168 /* FIXME: add tests for an invalid format */
171 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
174 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
176 if (volume) *volume = '\0';
177 if (prompt) *prompt = '\0';
178 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
179 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
180 prompt, pcchDiskPrompt);
181 if (r != ERROR_SUCCESS)
184 if (szVolumeLabel && pcchVolumeLabel)
185 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
186 *pcchVolumeLabel + 1, NULL, NULL);
189 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
190 *pcchDiskPrompt + 1, NULL, NULL);
201 /******************************************************************
202 * MsiSourceListEnumMediaDisksW (MSI.@)
204 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
205 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
206 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
207 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
208 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
210 WCHAR squished_pc[GUID_SIZE];
216 DWORD valuesz, datasz = 0;
221 static int index = 0;
223 static const WCHAR fmt[] = {'#','%','d',0};
225 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
226 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
227 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
229 if (!szProductCodeOrPatchCode ||
230 !squash_guid(szProductCodeOrPatchCode, squished_pc))
231 return ERROR_INVALID_PARAMETER;
233 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
234 return ERROR_INVALID_PARAMETER;
236 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
237 return ERROR_INVALID_PARAMETER;
239 if (szDiskPrompt && !pcchDiskPrompt)
240 return ERROR_INVALID_PARAMETER;
245 if (dwIndex != index)
246 return ERROR_INVALID_PARAMETER;
248 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
249 dwOptions, dwContext, FALSE);
250 if (r != ERROR_SUCCESS)
253 r = OpenMediaSubkey(source, &media, FALSE);
254 if (r != ERROR_SUCCESS)
257 return ERROR_NO_MORE_ITEMS;
260 if (!pcchVolumeLabel && !pcchDiskPrompt)
262 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
267 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
268 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
269 if (res != ERROR_SUCCESS)
271 r = ERROR_BAD_CONFIGURATION;
275 value = msi_alloc(++valuesz * sizeof(WCHAR));
276 data = msi_alloc(++datasz * sizeof(WCHAR));
279 r = ERROR_OUTOFMEMORY;
283 r = RegEnumValueW(media, dwIndex, value, &valuesz,
284 NULL, &type, (LPBYTE)data, &datasz);
285 if (r != ERROR_SUCCESS)
289 *pdwDiskId = atolW(value);
292 ptr = strchrW(data, ';');
300 if (type == REG_DWORD)
302 sprintfW(convert, fmt, *data);
303 size = lstrlenW(convert);
307 size = lstrlenW(data);
309 if (size >= *pcchVolumeLabel)
311 else if (szVolumeLabel)
312 lstrcpyW(szVolumeLabel, ptr2);
314 *pcchVolumeLabel = size;
322 if (type == REG_DWORD)
324 sprintfW(convert, fmt, *ptr);
325 size = lstrlenW(convert);
329 size = lstrlenW(ptr);
331 size = lstrlenW(ptr);
332 if (size >= *pcchDiskPrompt)
334 else if (szDiskPrompt)
335 lstrcpyW(szDiskPrompt, ptr);
337 *pcchDiskPrompt = size;
350 /******************************************************************
351 * MsiSourceListEnumSourcesA (MSI.@)
353 UINT WINAPI MsiSourceListEnumSourcesA(LPCSTR szProductCodeOrPatch, LPCSTR szUserSid,
354 MSIINSTALLCONTEXT dwContext,
355 DWORD dwOptions, DWORD dwIndex,
356 LPSTR szSource, LPDWORD pcchSource)
358 LPWSTR product = NULL;
359 LPWSTR usersid = NULL;
360 LPWSTR source = NULL;
362 UINT r = ERROR_INVALID_PARAMETER;
363 static int index = 0;
365 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_a(szProductCodeOrPatch),
366 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
371 if (szSource && !pcchSource)
374 if (dwIndex != index)
377 if (szProductCodeOrPatch) product = strdupAtoW(szProductCodeOrPatch);
378 if (szUserSid) usersid = strdupAtoW(szUserSid);
380 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
381 dwIndex, NULL, &len);
382 if (r != ERROR_SUCCESS)
385 source = msi_alloc(++len * sizeof(WCHAR));
388 r = ERROR_OUTOFMEMORY;
393 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
394 dwIndex, source, &len);
395 if (r != ERROR_SUCCESS)
398 len = WideCharToMultiByte(CP_ACP, 0, source, -1, NULL, 0, NULL, NULL);
399 if (pcchSource && *pcchSource >= len)
400 WideCharToMultiByte(CP_ACP, 0, source, -1, szSource, len, NULL, NULL);
405 *pcchSource = len - 1;
412 if (r == ERROR_SUCCESS)
414 if (szSource || !pcchSource) index++;
416 else if (dwIndex > index)
422 /******************************************************************
423 * MsiSourceListEnumSourcesW (MSI.@)
425 UINT WINAPI MsiSourceListEnumSourcesW(LPCWSTR szProductCodeOrPatch, LPCWSTR szUserSid,
426 MSIINSTALLCONTEXT dwContext,
427 DWORD dwOptions, DWORD dwIndex,
428 LPWSTR szSource, LPDWORD pcchSource)
430 WCHAR squished_pc[GUID_SIZE];
435 UINT r = ERROR_INVALID_PARAMETER;
436 static int index = 0;
438 static const WCHAR format[] = {'%','d',0};
440 TRACE("(%s, %s, %d, %d, %d, %p, %p)\n", debugstr_w(szProductCodeOrPatch),
441 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szSource, pcchSource);
446 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
449 if (szSource && !pcchSource)
452 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
455 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
458 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
461 if (dwIndex != index)
464 r = OpenSourceKey(szProductCodeOrPatch, &source,
465 dwOptions, dwContext, FALSE);
466 if (r != ERROR_SUCCESS)
469 if (dwOptions & MSISOURCETYPE_NETWORK)
470 r = OpenNetworkSubkey(source, &subkey, FALSE);
471 else if (dwOptions & MSISOURCETYPE_URL)
472 r = OpenURLSubkey(source, &subkey, FALSE);
474 if (r != ERROR_SUCCESS)
476 r = ERROR_NO_MORE_ITEMS;
480 sprintfW(name, format, dwIndex + 1);
482 res = RegQueryValueExW(subkey, name, 0, 0, (LPBYTE)szSource, pcchSource);
483 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
484 r = ERROR_NO_MORE_ITEMS;
490 if (r == ERROR_SUCCESS)
492 if (szSource || !pcchSource) index++;
494 else if (dwIndex > index)
500 /******************************************************************
501 * MsiSourceListGetInfoA (MSI.@)
503 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
504 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
505 LPCSTR szProperty, LPSTR szValue,
509 LPWSTR product = NULL;
510 LPWSTR usersid = NULL;
511 LPWSTR property = NULL;
515 if (szValue && !pcchValue)
516 return ERROR_INVALID_PARAMETER;
518 if (szProduct) product = strdupAtoW(szProduct);
519 if (szUserSid) usersid = strdupAtoW(szUserSid);
520 if (szProperty) property = strdupAtoW(szProperty);
522 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
523 property, NULL, &len);
524 if (ret != ERROR_SUCCESS)
527 value = msi_alloc(++len * sizeof(WCHAR));
529 return ERROR_OUTOFMEMORY;
532 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
533 property, value, &len);
534 if (ret != ERROR_SUCCESS)
537 len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
538 if (*pcchValue >= len)
539 WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
541 ret = ERROR_MORE_DATA;
543 *pcchValue = len - 1;
553 /******************************************************************
554 * MsiSourceListGetInfoW (MSI.@)
556 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
557 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
558 LPCWSTR szProperty, LPWSTR szValue,
561 WCHAR squished_pc[GUID_SIZE];
562 HKEY sourcekey, media;
567 static const WCHAR mediapack[] = {
568 'M','e','d','i','a','P','a','c','k','a','g','e',0};
570 TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szProperty));
572 if (!szProduct || !squash_guid(szProduct, squished_pc))
573 return ERROR_INVALID_PARAMETER;
575 if (szValue && !pcchValue)
576 return ERROR_INVALID_PARAMETER;
578 if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
579 dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
580 dwContext != MSIINSTALLCONTEXT_MACHINE)
581 return ERROR_INVALID_PARAMETER;
584 return ERROR_INVALID_PARAMETER;
587 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
589 if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED)
590 FIXME("Unhandled context %d\n", dwContext);
592 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
593 if (rc != ERROR_SUCCESS)
596 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
597 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
599 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
600 if (rc != ERROR_SUCCESS)
602 RegCloseKey(sourcekey);
603 return ERROR_SUCCESS;
606 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
607 szProperty = mediapack;
609 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
612 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) ||
613 !lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
615 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
617 if (rc != ERROR_SUCCESS)
619 RegCloseKey(sourcekey);
620 return ERROR_SUCCESS;
623 source = msi_alloc(size);
624 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
625 0, 0, (LPBYTE)source, &size);
630 RegCloseKey(sourcekey);
631 return ERROR_SUCCESS;
634 if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDTYPEW))
636 if (*source != 'n' && *source != 'u' && *source != 'm')
639 RegCloseKey(sourcekey);
640 return ERROR_SUCCESS;
648 ptr = strrchrW(source, ';');
657 if (lstrlenW(ptr) < *pcchValue)
658 lstrcpyW(szValue, ptr);
660 rc = ERROR_MORE_DATA;
663 *pcchValue = lstrlenW(ptr);
666 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
668 *pcchValue = *pcchValue * sizeof(WCHAR);
669 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
670 (LPBYTE)szValue, pcchValue);
671 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
679 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
681 szValue[*pcchValue] = '\0';
686 FIXME("Unknown property %s\n",debugstr_w(szProperty));
687 rc = ERROR_UNKNOWN_PROPERTY;
690 RegCloseKey(sourcekey);
694 /******************************************************************
695 * MsiSourceListSetInfoA (MSI.@)
697 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
698 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
699 LPCSTR szProperty, LPCSTR szValue)
702 LPWSTR product = NULL;
703 LPWSTR usersid = NULL;
704 LPWSTR property = NULL;
707 if (szProduct) product = strdupAtoW(szProduct);
708 if (szUserSid) usersid = strdupAtoW(szUserSid);
709 if (szProperty) property = strdupAtoW(szProperty);
710 if (szValue) value = strdupAtoW(szValue);
712 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
723 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
724 MSIINSTALLCONTEXT context, DWORD options,
734 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
736 if (options & MSISOURCETYPE_NETWORK)
738 else if (options & MSISOURCETYPE_URL)
740 else if (options & MSISOURCETYPE_MEDIA)
743 return ERROR_INVALID_PARAMETER;
745 if (!(options & MSISOURCETYPE_MEDIA))
747 r = MsiSourceListAddSourceExW(product, usersid, context,
749 if (r != ERROR_SUCCESS)
753 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
754 index, NULL, NULL)) == ERROR_SUCCESS)
757 if (r != ERROR_NO_MORE_ITEMS)
761 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
762 buffer = msi_alloc(size);
764 return ERROR_OUTOFMEMORY;
766 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
767 if (r != ERROR_SUCCESS)
770 sprintfW(buffer, format, typechar, index, value);
772 size = (lstrlenW(buffer) + 1) * sizeof(WCHAR);
773 r = RegSetValueExW(source, INSTALLPROPERTY_LASTUSEDSOURCEW, 0,
774 REG_SZ, (LPBYTE)buffer, size);
781 /******************************************************************
782 * MsiSourceListSetInfoW (MSI.@)
784 UINT WINAPI MsiSourceListSetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
785 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
786 LPCWSTR szProperty, LPCWSTR szValue)
788 WCHAR squished_pc[GUID_SIZE];
789 HKEY sourcekey, media;
793 static const WCHAR media_package[] = {
794 'M','e','d','i','a','P','a','c','k','a','g','e',0
797 TRACE("%s %s %x %x %s %s\n", debugstr_w(szProduct), debugstr_w(szUserSid),
798 dwContext, dwOptions, debugstr_w(szProperty), debugstr_w(szValue));
800 if (!szProduct || !squash_guid(szProduct, squished_pc))
801 return ERROR_INVALID_PARAMETER;
804 return ERROR_INVALID_PARAMETER;
807 return ERROR_UNKNOWN_PROPERTY;
809 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
810 return ERROR_INVALID_PARAMETER;
812 if (dwOptions & MSICODE_PATCH)
814 FIXME("Unhandled options MSICODE_PATCH\n");
815 return ERROR_UNKNOWN_PATCH;
818 property = szProperty;
819 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW))
820 property = media_package;
822 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
823 if (rc != ERROR_SUCCESS)
826 if (lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW) &&
827 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
829 RegCloseKey(sourcekey);
830 return ERROR_INVALID_PARAMETER;
833 if (!lstrcmpW(szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW) ||
834 !lstrcmpW(szProperty, INSTALLPROPERTY_DISKPROMPTW))
836 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
837 if (rc == ERROR_SUCCESS)
839 rc = msi_reg_set_val_str(media, property, szValue);
843 else if (strcmpW(INSTALLPROPERTY_PACKAGENAMEW, szProperty)==0)
845 DWORD size = (lstrlenW(szValue) + 1) * sizeof(WCHAR);
846 rc = RegSetValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0,
847 REG_SZ, (const BYTE *)szValue, size);
848 if (rc != ERROR_SUCCESS)
849 rc = ERROR_UNKNOWN_PROPERTY;
851 else if (!lstrcmpW(szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW))
853 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
854 rc = ERROR_INVALID_PARAMETER;
856 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
860 rc = ERROR_UNKNOWN_PROPERTY;
862 RegCloseKey(sourcekey);
866 /******************************************************************
867 * MsiSourceListAddSourceW (MSI.@)
869 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
870 DWORD dwReserved, LPCWSTR szSource)
872 WCHAR squished_pc[GUID_SIZE];
874 LPWSTR sidstr = NULL;
881 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
883 if (!szSource || !*szSource)
884 return ERROR_INVALID_PARAMETER;
887 return ERROR_INVALID_PARAMETER;
889 if (!szProduct || !squash_guid(szProduct, squished_pc))
890 return ERROR_INVALID_PARAMETER;
892 if (!szUserName || !*szUserName)
893 context = MSIINSTALLCONTEXT_MACHINE;
896 if (LookupAccountNameW(NULL, szUserName, NULL, &sidsize, NULL, &domsize, NULL))
898 PSID psid = msi_alloc(sidsize);
900 if (LookupAccountNameW(NULL, szUserName, psid, &sidsize, NULL, &domsize, NULL))
901 ConvertSidToStringSidW(psid, &sidstr);
906 r = MSIREG_OpenLocalManagedProductKey(szProduct, &hkey, FALSE);
907 if (r == ERROR_SUCCESS)
908 context = MSIINSTALLCONTEXT_USERMANAGED;
911 r = MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE);
912 if (r != ERROR_SUCCESS)
913 return ERROR_UNKNOWN_PRODUCT;
915 context = MSIINSTALLCONTEXT_USERUNMANAGED;
921 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
922 context, MSISOURCETYPE_NETWORK, szSource, 0);
930 /******************************************************************
931 * MsiSourceListAddSourceA (MSI.@)
933 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
934 DWORD dwReserved, LPCSTR szSource)
941 szwproduct = strdupAtoW( szProduct );
942 szwusername = strdupAtoW( szUserName );
943 szwsource = strdupAtoW( szSource );
945 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
947 msi_free(szwproduct);
948 msi_free(szwusername);
954 /******************************************************************
955 * MsiSourceListAddSourceExA (MSI.@)
957 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
958 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
961 LPWSTR product, usersid, source;
963 product = strdupAtoW(szProduct);
964 usersid = strdupAtoW(szUserSid);
965 source = strdupAtoW(szSource);
967 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
968 dwOptions, source, dwIndex);
977 static void free_source_list(struct list *sourcelist)
979 while (!list_empty(sourcelist))
981 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
982 list_remove(&info->entry);
983 msi_free(info->path);
988 static void add_source_to_list(struct list *sourcelist, media_info *info,
993 static const WCHAR fmt[] = {'%','i',0};
995 if (index) *index = 0;
997 if (list_empty(sourcelist))
999 list_add_head(sourcelist, &info->entry);
1003 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1005 if (!found && info->index < iter->index)
1008 list_add_before(&iter->entry, &info->entry);
1011 /* update the rest of the list */
1013 sprintfW(iter->szIndex, fmt, ++iter->index);
1019 list_add_after(&iter->entry, &info->entry);
1022 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1024 UINT r = ERROR_SUCCESS;
1027 DWORD size, val_size;
1032 while (r == ERROR_SUCCESS)
1034 size = sizeof(name) / sizeof(name[0]);
1035 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1036 if (r != ERROR_SUCCESS)
1039 entry = msi_alloc(sizeof(media_info));
1043 entry->path = msi_alloc(val_size);
1050 lstrcpyW(entry->szIndex, name);
1051 entry->index = atoiW(name);
1054 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1055 NULL, (LPBYTE)entry->path, &val_size);
1056 if (r != ERROR_SUCCESS)
1058 msi_free(entry->path);
1064 add_source_to_list(sourcelist, entry, NULL);
1069 free_source_list(sourcelist);
1070 return ERROR_OUTOFMEMORY;
1073 /******************************************************************
1074 * MsiSourceListAddSourceExW (MSI.@)
1076 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1077 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1083 struct list sourcelist;
1085 WCHAR squished_pc[GUID_SIZE];
1092 static const WCHAR fmt[] = {'%','i',0};
1093 static const WCHAR one[] = {'1',0};
1094 static const WCHAR backslash[] = {'\\',0};
1095 static const WCHAR forwardslash[] = {'/',0};
1097 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1098 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1100 if (!szProduct || !squash_guid(szProduct, squished_pc))
1101 return ERROR_INVALID_PARAMETER;
1103 if (!szSource || !*szSource)
1104 return ERROR_INVALID_PARAMETER;
1106 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1107 return ERROR_INVALID_PARAMETER;
1109 if (dwOptions & MSICODE_PATCH)
1111 FIXME("Unhandled options MSICODE_PATCH\n");
1112 return ERROR_FUNCTION_FAILED;
1115 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1116 return ERROR_INVALID_PARAMETER;
1118 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1119 if (rc != ERROR_SUCCESS)
1122 if (dwOptions & MSISOURCETYPE_NETWORK)
1123 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1124 else if (dwOptions & MSISOURCETYPE_URL)
1125 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1126 else if (dwOptions & MSISOURCETYPE_MEDIA)
1127 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1130 ERR("unknown media type: %08x\n", dwOptions);
1131 RegCloseKey(sourcekey);
1132 return ERROR_FUNCTION_FAILED;
1135 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1136 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1137 source = strdupW(szSource);
1140 size = lstrlenW(szSource) + 2;
1141 source = msi_alloc(size * sizeof(WCHAR));
1142 lstrcpyW(source, szSource);
1143 lstrcatW(source, postfix);
1146 list_init(&sourcelist);
1147 rc = fill_source_list(&sourcelist, typekey, &count);
1148 if (rc != ERROR_NO_MORE_ITEMS)
1151 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1155 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1158 else if (dwIndex > count || dwIndex == 0)
1160 sprintfW(name, fmt, count + 1);
1161 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1166 sprintfW(name, fmt, dwIndex);
1167 info = msi_alloc(sizeof(media_info));
1170 rc = ERROR_OUTOFMEMORY;
1174 info->path = strdupW(source);
1175 lstrcpyW(info->szIndex, name);
1176 info->index = dwIndex;
1177 add_source_to_list(&sourcelist, info, &index);
1179 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1181 if (info->index < index)
1184 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1185 rc = RegSetValueExW(typekey, info->szIndex, 0,
1186 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1187 if (rc != ERROR_SUCCESS)
1193 free_source_list(&sourcelist);
1195 RegCloseKey(typekey);
1196 RegCloseKey(sourcekey);
1200 /******************************************************************
1201 * MsiSourceListAddMediaDiskA (MSI.@)
1203 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1204 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1205 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1208 LPWSTR product = NULL;
1209 LPWSTR usersid = NULL;
1210 LPWSTR volume = NULL;
1211 LPWSTR prompt = NULL;
1213 if (szProduct) product = strdupAtoW(szProduct);
1214 if (szUserSid) usersid = strdupAtoW(szUserSid);
1215 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1216 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1218 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1219 dwDiskId, volume, prompt);
1229 /******************************************************************
1230 * MsiSourceListAddMediaDiskW (MSI.@)
1232 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1233 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1234 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1240 WCHAR squished_pc[GUID_SIZE];
1244 static const WCHAR fmt[] = {'%','i',0};
1245 static const WCHAR semicolon[] = {';',0};
1247 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1248 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1249 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1251 if (!szProduct || !squash_guid(szProduct, squished_pc))
1252 return ERROR_INVALID_PARAMETER;
1254 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1255 return ERROR_INVALID_PARAMETER;
1257 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1258 return ERROR_INVALID_PARAMETER;
1260 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1261 return ERROR_INVALID_PARAMETER;
1263 if (dwOptions & MSICODE_PATCH)
1265 FIXME("Unhandled options MSICODE_PATCH\n");
1266 return ERROR_FUNCTION_FAILED;
1269 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1270 if (rc != ERROR_SUCCESS)
1273 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1275 sprintfW(szIndex, fmt, dwDiskId);
1278 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1279 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1281 size *= sizeof(WCHAR);
1282 buffer = msi_alloc(size);
1285 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1286 lstrcatW(buffer, semicolon);
1287 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1289 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1292 RegCloseKey(sourcekey);
1293 RegCloseKey(mediakey);
1295 return ERROR_SUCCESS;
1298 /******************************************************************
1299 * MsiSourceListClearAllA (MSI.@)
1301 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1303 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1304 return ERROR_SUCCESS;
1307 /******************************************************************
1308 * MsiSourceListClearAllW (MSI.@)
1310 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1312 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1313 return ERROR_SUCCESS;