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_OpenProductKey(szProduct, context, &rootkey, create);
69 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
71 if (dwOptions & MSICODE_PATCH)
72 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
74 rc = MSIREG_OpenProductKey(szProduct, context, &rootkey, create);
76 else if (context == MSIINSTALLCONTEXT_MACHINE)
78 if (dwOptions & MSICODE_PATCH)
79 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
81 rc = MSIREG_OpenProductKey(szProduct, context, &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 DWORD 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 DWORD 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 DWORD 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 (strlenW(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_OpenProductKey(szProduct, MSIINSTALLCONTEXT_USERMANAGED,
908 if (r == ERROR_SUCCESS)
909 context = MSIINSTALLCONTEXT_USERMANAGED;
912 r = MSIREG_OpenProductKey(szProduct, MSIINSTALLCONTEXT_USERUNMANAGED,
914 if (r != ERROR_SUCCESS)
915 return ERROR_UNKNOWN_PRODUCT;
917 context = MSIINSTALLCONTEXT_USERUNMANAGED;
923 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
924 context, MSISOURCETYPE_NETWORK, szSource, 0);
932 /******************************************************************
933 * MsiSourceListAddSourceA (MSI.@)
935 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
936 DWORD dwReserved, LPCSTR szSource)
943 szwproduct = strdupAtoW( szProduct );
944 szwusername = strdupAtoW( szUserName );
945 szwsource = strdupAtoW( szSource );
947 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
949 msi_free(szwproduct);
950 msi_free(szwusername);
956 /******************************************************************
957 * MsiSourceListAddSourceExA (MSI.@)
959 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
960 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
963 LPWSTR product, usersid, source;
965 product = strdupAtoW(szProduct);
966 usersid = strdupAtoW(szUserSid);
967 source = strdupAtoW(szSource);
969 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
970 dwOptions, source, dwIndex);
979 static void free_source_list(struct list *sourcelist)
981 while (!list_empty(sourcelist))
983 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
984 list_remove(&info->entry);
985 msi_free(info->path);
990 static void add_source_to_list(struct list *sourcelist, media_info *info,
995 static const WCHAR fmt[] = {'%','i',0};
997 if (index) *index = 0;
999 if (list_empty(sourcelist))
1001 list_add_head(sourcelist, &info->entry);
1005 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1007 if (!found && info->index < iter->index)
1010 list_add_before(&iter->entry, &info->entry);
1013 /* update the rest of the list */
1015 sprintfW(iter->szIndex, fmt, ++iter->index);
1021 list_add_after(&iter->entry, &info->entry);
1024 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1026 UINT r = ERROR_SUCCESS;
1029 DWORD size, val_size;
1034 while (r == ERROR_SUCCESS)
1036 size = sizeof(name) / sizeof(name[0]);
1037 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1038 if (r != ERROR_SUCCESS)
1041 entry = msi_alloc(sizeof(media_info));
1045 entry->path = msi_alloc(val_size);
1052 lstrcpyW(entry->szIndex, name);
1053 entry->index = atoiW(name);
1056 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1057 NULL, (LPBYTE)entry->path, &val_size);
1058 if (r != ERROR_SUCCESS)
1060 msi_free(entry->path);
1066 add_source_to_list(sourcelist, entry, NULL);
1071 free_source_list(sourcelist);
1072 return ERROR_OUTOFMEMORY;
1075 /******************************************************************
1076 * MsiSourceListAddSourceExW (MSI.@)
1078 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1079 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1085 struct list sourcelist;
1087 WCHAR squished_pc[GUID_SIZE];
1094 static const WCHAR fmt[] = {'%','i',0};
1095 static const WCHAR one[] = {'1',0};
1096 static const WCHAR backslash[] = {'\\',0};
1097 static const WCHAR forwardslash[] = {'/',0};
1099 TRACE("%s %s %x %x %s %i\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1100 dwContext, dwOptions, debugstr_w(szSource), dwIndex);
1102 if (!szProduct || !squash_guid(szProduct, squished_pc))
1103 return ERROR_INVALID_PARAMETER;
1105 if (!szSource || !*szSource)
1106 return ERROR_INVALID_PARAMETER;
1108 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
1109 return ERROR_INVALID_PARAMETER;
1111 if (dwOptions & MSICODE_PATCH)
1113 FIXME("Unhandled options MSICODE_PATCH\n");
1114 return ERROR_FUNCTION_FAILED;
1117 if (szUserSid && (dwContext & MSIINSTALLCONTEXT_MACHINE))
1118 return ERROR_INVALID_PARAMETER;
1120 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1121 if (rc != ERROR_SUCCESS)
1124 if (dwOptions & MSISOURCETYPE_NETWORK)
1125 rc = OpenNetworkSubkey(sourcekey, &typekey, TRUE);
1126 else if (dwOptions & MSISOURCETYPE_URL)
1127 rc = OpenURLSubkey(sourcekey, &typekey, TRUE);
1128 else if (dwOptions & MSISOURCETYPE_MEDIA)
1129 rc = OpenMediaSubkey(sourcekey, &typekey, TRUE);
1132 ERR("unknown media type: %08x\n", dwOptions);
1133 RegCloseKey(sourcekey);
1134 return ERROR_FUNCTION_FAILED;
1137 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? backslash : forwardslash;
1138 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1139 source = strdupW(szSource);
1142 size = lstrlenW(szSource) + 2;
1143 source = msi_alloc(size * sizeof(WCHAR));
1144 lstrcpyW(source, szSource);
1145 lstrcatW(source, postfix);
1148 list_init(&sourcelist);
1149 rc = fill_source_list(&sourcelist, typekey, &count);
1150 if (rc != ERROR_NO_MORE_ITEMS)
1153 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1157 rc = RegSetValueExW(typekey, one, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1160 else if (dwIndex > count || dwIndex == 0)
1162 sprintfW(name, fmt, count + 1);
1163 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1168 sprintfW(name, fmt, dwIndex);
1169 info = msi_alloc(sizeof(media_info));
1172 rc = ERROR_OUTOFMEMORY;
1176 info->path = strdupW(source);
1177 lstrcpyW(info->szIndex, name);
1178 info->index = dwIndex;
1179 add_source_to_list(&sourcelist, info, &index);
1181 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1183 if (info->index < index)
1186 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1187 rc = RegSetValueExW(typekey, info->szIndex, 0,
1188 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1189 if (rc != ERROR_SUCCESS)
1195 free_source_list(&sourcelist);
1197 RegCloseKey(typekey);
1198 RegCloseKey(sourcekey);
1202 /******************************************************************
1203 * MsiSourceListAddMediaDiskA (MSI.@)
1205 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1206 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1207 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1210 LPWSTR product = NULL;
1211 LPWSTR usersid = NULL;
1212 LPWSTR volume = NULL;
1213 LPWSTR prompt = NULL;
1215 if (szProduct) product = strdupAtoW(szProduct);
1216 if (szUserSid) usersid = strdupAtoW(szUserSid);
1217 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1218 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1220 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1221 dwDiskId, volume, prompt);
1231 /******************************************************************
1232 * MsiSourceListAddMediaDiskW (MSI.@)
1234 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1235 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1236 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1242 WCHAR squished_pc[GUID_SIZE];
1246 static const WCHAR fmt[] = {'%','i',0};
1247 static const WCHAR semicolon[] = {';',0};
1249 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1250 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1251 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1253 if (!szProduct || !squash_guid(szProduct, squished_pc))
1254 return ERROR_INVALID_PARAMETER;
1256 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1257 return ERROR_INVALID_PARAMETER;
1259 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1260 return ERROR_INVALID_PARAMETER;
1262 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1263 return ERROR_INVALID_PARAMETER;
1265 if (dwOptions & MSICODE_PATCH)
1267 FIXME("Unhandled options MSICODE_PATCH\n");
1268 return ERROR_FUNCTION_FAILED;
1271 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1272 if (rc != ERROR_SUCCESS)
1275 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1277 sprintfW(szIndex, fmt, dwDiskId);
1280 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1281 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1283 size *= sizeof(WCHAR);
1284 buffer = msi_alloc(size);
1287 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1288 lstrcatW(buffer, semicolon);
1289 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1291 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1294 RegCloseKey(sourcekey);
1295 RegCloseKey(mediakey);
1297 return ERROR_SUCCESS;
1300 /******************************************************************
1301 * MsiSourceListClearAllA (MSI.@)
1303 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1305 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1306 return ERROR_SUCCESS;
1309 /******************************************************************
1310 * MsiSourceListClearAllW (MSI.@)
1312 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1314 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1315 return ERROR_SUCCESS;