netprofm: Add stubbed IConnectionPoint interface.
[wine.git] / dlls / msi / source.c
blobf4c139f51a951ca97e00add1afea818d7a2c8044
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "shlwapi.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "msipriv.h"
34 #include "wincrypt.h"
35 #include "winver.h"
36 #include "winuser.h"
37 #include "wine/unicode.h"
38 #include "sddl.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 * These apis are defined in MSI 3.0
46 typedef struct tagMediaInfo
48 struct list entry;
49 LPWSTR path;
50 WCHAR szIndex[10];
51 DWORD index;
52 } media_info;
54 static UINT OpenSourceKey(LPCWSTR szProduct, HKEY* key, DWORD dwOptions,
55 MSIINSTALLCONTEXT context, BOOL create)
57 HKEY rootkey = 0;
58 UINT rc = ERROR_FUNCTION_FAILED;
60 if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
62 if (dwOptions & MSICODE_PATCH)
63 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
64 else
65 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
66 &rootkey, create);
68 else if (context == MSIINSTALLCONTEXT_USERMANAGED)
70 if (dwOptions & MSICODE_PATCH)
71 rc = MSIREG_OpenUserPatchesKey(szProduct, &rootkey, create);
72 else
73 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
74 &rootkey, create);
76 else if (context == MSIINSTALLCONTEXT_MACHINE)
78 if (dwOptions & MSICODE_PATCH)
79 rc = MSIREG_OpenPatchesKey(szProduct, &rootkey, create);
80 else
81 rc = MSIREG_OpenProductKey(szProduct, NULL, context,
82 &rootkey, create);
85 if (rc != ERROR_SUCCESS)
87 if (dwOptions & MSICODE_PATCH)
88 return ERROR_UNKNOWN_PATCH;
89 else
90 return ERROR_UNKNOWN_PRODUCT;
93 if (create)
94 rc = RegCreateKeyW(rootkey, szSourceList, key);
95 else
97 rc = RegOpenKeyW(rootkey,szSourceList, key);
98 if (rc != ERROR_SUCCESS)
99 rc = ERROR_BAD_CONFIGURATION;
102 return rc;
105 static UINT OpenMediaSubkey(HKEY rootkey, HKEY *key, BOOL create)
107 UINT rc;
108 static const WCHAR media[] = {'M','e','d','i','a',0};
110 if (create)
111 rc = RegCreateKeyW(rootkey, media, key);
112 else
113 rc = RegOpenKeyW(rootkey,media, key);
115 return rc;
118 static UINT OpenNetworkSubkey(HKEY rootkey, HKEY *key, BOOL create)
120 UINT rc;
121 static const WCHAR net[] = {'N','e','t',0};
123 if (create)
124 rc = RegCreateKeyW(rootkey, net, key);
125 else
126 rc = RegOpenKeyW(rootkey, net, key);
128 return rc;
131 static UINT OpenURLSubkey(HKEY rootkey, HKEY *key, BOOL create)
133 UINT rc;
134 static const WCHAR URL[] = {'U','R','L',0};
136 if (create)
137 rc = RegCreateKeyW(rootkey, URL, key);
138 else
139 rc = RegOpenKeyW(rootkey, URL, key);
141 return rc;
144 /******************************************************************
145 * MsiSourceListEnumMediaDisksA (MSI.@)
147 UINT WINAPI MsiSourceListEnumMediaDisksA(LPCSTR szProductCodeOrPatchCode,
148 LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
149 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
150 LPSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
151 LPSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
153 LPWSTR product = NULL;
154 LPWSTR usersid = NULL;
155 LPWSTR volume = NULL;
156 LPWSTR prompt = NULL;
157 UINT r = ERROR_INVALID_PARAMETER;
159 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n", debugstr_a(szProductCodeOrPatchCode),
160 debugstr_a(szUserSid), dwContext, dwOptions, dwIndex, pdwDiskId,
161 szVolumeLabel, pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
163 if (szDiskPrompt && !pcchDiskPrompt)
164 return ERROR_INVALID_PARAMETER;
166 if (szProductCodeOrPatchCode) product = strdupAtoW(szProductCodeOrPatchCode);
167 if (szUserSid) usersid = strdupAtoW(szUserSid);
169 /* FIXME: add tests for an invalid format */
171 if (pcchVolumeLabel)
172 volume = msi_alloc(*pcchVolumeLabel * sizeof(WCHAR));
174 if (pcchDiskPrompt)
175 prompt = msi_alloc(*pcchDiskPrompt * sizeof(WCHAR));
177 if (volume) *volume = '\0';
178 if (prompt) *prompt = '\0';
179 r = MsiSourceListEnumMediaDisksW(product, usersid, dwContext, dwOptions,
180 dwIndex, pdwDiskId, volume, pcchVolumeLabel,
181 prompt, pcchDiskPrompt);
182 if (r != ERROR_SUCCESS)
183 goto done;
185 if (szVolumeLabel && pcchVolumeLabel)
186 WideCharToMultiByte(CP_ACP, 0, volume, -1, szVolumeLabel,
187 *pcchVolumeLabel + 1, NULL, NULL);
189 if (szDiskPrompt)
190 WideCharToMultiByte(CP_ACP, 0, prompt, -1, szDiskPrompt,
191 *pcchDiskPrompt + 1, NULL, NULL);
193 done:
194 msi_free(product);
195 msi_free(usersid);
196 msi_free(volume);
197 msi_free(prompt);
199 return r;
202 /******************************************************************
203 * MsiSourceListEnumMediaDisksW (MSI.@)
205 UINT WINAPI MsiSourceListEnumMediaDisksW(LPCWSTR szProductCodeOrPatchCode,
206 LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
207 DWORD dwOptions, DWORD dwIndex, LPDWORD pdwDiskId,
208 LPWSTR szVolumeLabel, LPDWORD pcchVolumeLabel,
209 LPWSTR szDiskPrompt, LPDWORD pcchDiskPrompt)
211 WCHAR squished_pc[GUID_SIZE];
212 WCHAR convert[11];
213 LPWSTR value = NULL;
214 LPWSTR data = NULL;
215 LPWSTR ptr, ptr2;
216 HKEY source, media;
217 DWORD valuesz, datasz = 0;
218 DWORD type;
219 DWORD numvals, size;
220 LONG res;
221 UINT r;
222 static DWORD index = 0;
224 static const WCHAR fmt[] = {'#','%','d',0};
226 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p)\n", debugstr_w(szProductCodeOrPatchCode),
227 debugstr_w(szUserSid), dwContext, dwOptions, dwIndex, szVolumeLabel,
228 pcchVolumeLabel, szDiskPrompt, pcchDiskPrompt);
230 if (!szProductCodeOrPatchCode ||
231 !squash_guid(szProductCodeOrPatchCode, squished_pc))
232 return ERROR_INVALID_PARAMETER;
234 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
235 return ERROR_INVALID_PARAMETER;
237 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
238 return ERROR_INVALID_PARAMETER;
240 if (szDiskPrompt && !pcchDiskPrompt)
241 return ERROR_INVALID_PARAMETER;
243 if (dwIndex == 0)
244 index = 0;
246 if (dwIndex != index)
247 return ERROR_INVALID_PARAMETER;
249 r = OpenSourceKey(szProductCodeOrPatchCode, &source,
250 dwOptions, dwContext, FALSE);
251 if (r != ERROR_SUCCESS)
252 return r;
254 r = OpenMediaSubkey(source, &media, FALSE);
255 if (r != ERROR_SUCCESS)
257 RegCloseKey(source);
258 return ERROR_NO_MORE_ITEMS;
261 if (!pcchVolumeLabel && !pcchDiskPrompt)
263 r = RegEnumValueW(media, dwIndex, NULL, NULL, NULL,
264 &type, NULL, NULL);
265 goto done;
268 res = RegQueryInfoKeyW(media, NULL, NULL, NULL, NULL, NULL,
269 NULL, &numvals, &valuesz, &datasz, NULL, NULL);
270 if (res != ERROR_SUCCESS)
272 r = ERROR_BAD_CONFIGURATION;
273 goto done;
276 value = msi_alloc(++valuesz * sizeof(WCHAR));
277 data = msi_alloc(++datasz * sizeof(WCHAR));
278 if (!value || !data)
280 r = ERROR_OUTOFMEMORY;
281 goto done;
284 r = RegEnumValueW(media, dwIndex, value, &valuesz,
285 NULL, &type, (LPBYTE)data, &datasz);
286 if (r != ERROR_SUCCESS)
287 goto done;
289 if (pdwDiskId)
290 *pdwDiskId = atolW(value);
292 ptr2 = data;
293 ptr = strchrW(data, ';');
294 if (!ptr)
295 ptr = data;
296 else
297 *ptr = '\0';
299 if (pcchVolumeLabel)
301 if (type == REG_DWORD)
303 sprintfW(convert, fmt, *data);
304 size = lstrlenW(convert);
305 ptr2 = convert;
307 else
308 size = lstrlenW(data);
310 if (size >= *pcchVolumeLabel)
311 r = ERROR_MORE_DATA;
312 else if (szVolumeLabel)
313 lstrcpyW(szVolumeLabel, ptr2);
315 *pcchVolumeLabel = size;
318 if (pcchDiskPrompt)
320 if (!*ptr)
321 ptr++;
323 if (type == REG_DWORD)
325 sprintfW(convert, fmt, *ptr);
326 size = lstrlenW(convert);
327 ptr = convert;
329 else
330 size = lstrlenW(ptr);
332 if (size >= *pcchDiskPrompt)
333 r = ERROR_MORE_DATA;
334 else if (szDiskPrompt)
335 lstrcpyW(szDiskPrompt, ptr);
337 *pcchDiskPrompt = size;
340 index++;
342 done:
343 msi_free(value);
344 msi_free(data);
345 RegCloseKey(source);
347 return r;
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;
361 DWORD len = 0;
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);
368 if (dwIndex == 0)
369 index = 0;
371 if (szSource && !pcchSource)
372 goto done;
374 if (dwIndex != index)
375 goto done;
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)
383 goto done;
385 source = msi_alloc(++len * sizeof(WCHAR));
386 if (!source)
388 r = ERROR_OUTOFMEMORY;
389 goto done;
392 *source = '\0';
393 r = MsiSourceListEnumSourcesW(product, usersid, dwContext, dwOptions,
394 dwIndex, source, &len);
395 if (r != ERROR_SUCCESS)
396 goto done;
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);
401 else if (szSource)
402 r = ERROR_MORE_DATA;
404 if (pcchSource)
405 *pcchSource = len - 1;
407 done:
408 msi_free(product);
409 msi_free(usersid);
410 msi_free(source);
412 if (r == ERROR_SUCCESS)
414 if (szSource || !pcchSource) index++;
416 else if (dwIndex > index)
417 index = 0;
419 return r;
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];
431 WCHAR name[32];
432 HKEY source = NULL;
433 HKEY subkey = NULL;
434 LONG res;
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);
443 if (dwIndex == 0)
444 index = 0;
446 if (!szProductCodeOrPatch || !squash_guid(szProductCodeOrPatch, squished_pc))
447 goto done;
449 if (szSource && !pcchSource)
450 goto done;
452 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
453 goto done;
455 if ((dwOptions & MSISOURCETYPE_NETWORK) && (dwOptions & MSISOURCETYPE_URL))
456 goto done;
458 if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
459 goto done;
461 if (dwIndex != index)
462 goto done;
464 r = OpenSourceKey(szProductCodeOrPatch, &source,
465 dwOptions, dwContext, FALSE);
466 if (r != ERROR_SUCCESS)
467 goto done;
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;
477 goto done;
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;
486 done:
487 RegCloseKey(subkey);
488 RegCloseKey(source);
490 if (r == ERROR_SUCCESS)
492 if (szSource || !pcchSource) index++;
494 else if (dwIndex > index)
495 index = 0;
497 return r;
500 /******************************************************************
501 * MsiSourceListGetInfoA (MSI.@)
503 UINT WINAPI MsiSourceListGetInfoA( LPCSTR szProduct, LPCSTR szUserSid,
504 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
505 LPCSTR szProperty, LPSTR szValue,
506 LPDWORD pcchValue)
508 UINT ret;
509 LPWSTR product = NULL;
510 LPWSTR usersid = NULL;
511 LPWSTR property = NULL;
512 LPWSTR value = NULL;
513 DWORD len = 0;
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)
525 goto done;
527 value = msi_alloc(++len * sizeof(WCHAR));
528 if (!value)
529 return ERROR_OUTOFMEMORY;
531 *value = '\0';
532 ret = MsiSourceListGetInfoW(product, usersid, dwContext, dwOptions,
533 property, value, &len);
534 if (ret != ERROR_SUCCESS)
535 goto done;
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);
540 else if (szValue)
541 ret = ERROR_MORE_DATA;
543 *pcchValue = len - 1;
545 done:
546 msi_free(product);
547 msi_free(usersid);
548 msi_free(property);
549 msi_free(value);
550 return ret;
553 /******************************************************************
554 * MsiSourceListGetInfoW (MSI.@)
556 UINT WINAPI MsiSourceListGetInfoW( LPCWSTR szProduct, LPCWSTR szUserSid,
557 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
558 LPCWSTR szProperty, LPWSTR szValue,
559 LPDWORD pcchValue)
561 WCHAR squished_pc[GUID_SIZE];
562 HKEY sourcekey, media;
563 LPWSTR source, ptr;
564 DWORD size;
565 UINT rc;
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;
583 if (!szProperty)
584 return ERROR_INVALID_PARAMETER;
586 if (szUserSid)
587 FIXME("Unhandled UserSid %s\n",debugstr_w(szUserSid));
589 rc = OpenSourceKey(szProduct, &sourcekey, dwOptions, dwContext, FALSE);
590 if (rc != ERROR_SUCCESS)
591 return rc;
593 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ) ||
594 !strcmpW( szProperty, INSTALLPROPERTY_DISKPROMPTW ))
596 rc = OpenMediaSubkey(sourcekey, &media, FALSE);
597 if (rc != ERROR_SUCCESS)
599 RegCloseKey(sourcekey);
600 return ERROR_SUCCESS;
603 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ))
604 szProperty = mediapack;
606 RegQueryValueExW(media, szProperty, 0, 0, (LPBYTE)szValue, pcchValue);
607 RegCloseKey(media);
609 else if (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ) ||
610 !strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDTYPEW ))
612 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
613 0, 0, NULL, &size);
614 if (rc != ERROR_SUCCESS)
616 RegCloseKey(sourcekey);
617 return ERROR_SUCCESS;
620 source = msi_alloc(size);
621 RegQueryValueExW(sourcekey, INSTALLPROPERTY_LASTUSEDSOURCEW,
622 0, 0, (LPBYTE)source, &size);
624 if (!*source)
626 msi_free(source);
627 RegCloseKey(sourcekey);
628 return ERROR_SUCCESS;
631 if (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDTYPEW ))
633 if (*source != 'n' && *source != 'u' && *source != 'm')
635 msi_free(source);
636 RegCloseKey(sourcekey);
637 return ERROR_SUCCESS;
640 ptr = source;
641 source[1] = '\0';
643 else
645 ptr = strrchrW(source, ';');
646 if (!ptr)
647 ptr = source;
648 else
649 ptr++;
652 if (szValue)
654 if (strlenW(ptr) < *pcchValue)
655 lstrcpyW(szValue, ptr);
656 else
657 rc = ERROR_MORE_DATA;
660 *pcchValue = lstrlenW(ptr);
661 msi_free(source);
663 else if (!strcmpW( szProperty, INSTALLPROPERTY_PACKAGENAMEW ))
665 *pcchValue = *pcchValue * sizeof(WCHAR);
666 rc = RegQueryValueExW(sourcekey, INSTALLPROPERTY_PACKAGENAMEW, 0, 0,
667 (LPBYTE)szValue, pcchValue);
668 if (rc != ERROR_SUCCESS && rc != ERROR_MORE_DATA)
670 *pcchValue = 0;
671 rc = ERROR_SUCCESS;
673 else
675 if (*pcchValue)
676 *pcchValue = (*pcchValue - 1) / sizeof(WCHAR);
677 if (szValue)
678 szValue[*pcchValue] = '\0';
681 else
683 FIXME("Unknown property %s\n",debugstr_w(szProperty));
684 rc = ERROR_UNKNOWN_PROPERTY;
687 RegCloseKey(sourcekey);
688 return rc;
691 /******************************************************************
692 * MsiSourceListSetInfoA (MSI.@)
694 UINT WINAPI MsiSourceListSetInfoA(LPCSTR szProduct, LPCSTR szUserSid,
695 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
696 LPCSTR szProperty, LPCSTR szValue)
698 UINT ret;
699 LPWSTR product = NULL;
700 LPWSTR usersid = NULL;
701 LPWSTR property = NULL;
702 LPWSTR value = NULL;
704 if (szProduct) product = strdupAtoW(szProduct);
705 if (szUserSid) usersid = strdupAtoW(szUserSid);
706 if (szProperty) property = strdupAtoW(szProperty);
707 if (szValue) value = strdupAtoW(szValue);
709 ret = MsiSourceListSetInfoW(product, usersid, dwContext, dwOptions,
710 property, value);
712 msi_free(product);
713 msi_free(usersid);
714 msi_free(property);
715 msi_free(value);
717 return ret;
720 UINT msi_set_last_used_source(LPCWSTR product, LPCWSTR usersid,
721 MSIINSTALLCONTEXT context, DWORD options,
722 LPCWSTR value)
724 HKEY source;
725 LPWSTR buffer;
726 WCHAR typechar;
727 DWORD size;
728 UINT r;
729 int index = 1;
731 static const WCHAR format[] = {'%','c',';','%','i',';','%','s',0};
733 if (options & MSISOURCETYPE_NETWORK)
734 typechar = 'n';
735 else if (options & MSISOURCETYPE_URL)
736 typechar = 'u';
737 else if (options & MSISOURCETYPE_MEDIA)
738 typechar = 'm';
739 else
740 return ERROR_INVALID_PARAMETER;
742 if (!(options & MSISOURCETYPE_MEDIA))
744 r = MsiSourceListAddSourceExW(product, usersid, context,
745 options, value, 0);
746 if (r != ERROR_SUCCESS)
747 return r;
749 index = 0;
750 while ((r = MsiSourceListEnumSourcesW(product, usersid, context, options,
751 index, NULL, NULL)) == ERROR_SUCCESS)
752 index++;
754 if (r != ERROR_NO_MORE_ITEMS)
755 return r;
758 size = (lstrlenW(format) + lstrlenW(value) + 7) * sizeof(WCHAR);
759 buffer = msi_alloc(size);
760 if (!buffer)
761 return ERROR_OUTOFMEMORY;
763 r = OpenSourceKey(product, &source, MSICODE_PRODUCT, context, FALSE);
764 if (r != ERROR_SUCCESS)
766 msi_free(buffer);
767 return r;
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);
775 msi_free(buffer);
777 RegCloseKey(source);
778 return r;
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;
790 LPCWSTR property;
791 UINT rc;
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;
803 if (!szProperty)
804 return ERROR_INVALID_PARAMETER;
806 if (!szValue)
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 (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ))
820 property = media_package;
822 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
823 if (rc != ERROR_SUCCESS)
824 return rc;
826 if (strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ) &&
827 dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL))
829 RegCloseKey(sourcekey);
830 return ERROR_INVALID_PARAMETER;
833 if (!strcmpW( szProperty, INSTALLPROPERTY_MEDIAPACKAGEPATHW ) ||
834 !strcmpW( szProperty, INSTALLPROPERTY_DISKPROMPTW ))
836 rc = OpenMediaSubkey(sourcekey, &media, TRUE);
837 if (rc == ERROR_SUCCESS)
839 rc = msi_reg_set_val_str(media, property, szValue);
840 RegCloseKey(media);
843 else if (!strcmpW( szProperty, INSTALLPROPERTY_PACKAGENAMEW ))
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 (!strcmpW( szProperty, INSTALLPROPERTY_LASTUSEDSOURCEW ))
853 if (!(dwOptions & (MSISOURCETYPE_NETWORK | MSISOURCETYPE_URL)))
854 rc = ERROR_INVALID_PARAMETER;
855 else
856 rc = msi_set_last_used_source(szProduct, szUserSid, dwContext,
857 dwOptions, szValue);
859 else
860 rc = ERROR_UNKNOWN_PROPERTY;
862 RegCloseKey(sourcekey);
863 return rc;
866 /******************************************************************
867 * MsiSourceListAddSourceW (MSI.@)
869 UINT WINAPI MsiSourceListAddSourceW( LPCWSTR szProduct, LPCWSTR szUserName,
870 DWORD dwReserved, LPCWSTR szSource)
872 WCHAR squished_pc[GUID_SIZE];
873 INT ret;
874 LPWSTR sidstr = NULL;
875 DWORD sidsize = 0;
876 DWORD domsize = 0;
877 DWORD context;
878 HKEY hkey = 0;
879 UINT r;
881 TRACE("%s %s %s\n", debugstr_w(szProduct), debugstr_w(szUserName), debugstr_w(szSource));
883 if (!szSource || !*szSource)
884 return ERROR_INVALID_PARAMETER;
886 if (dwReserved != 0)
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;
894 else
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);
903 msi_free(psid);
906 r = MSIREG_OpenProductKey(szProduct, NULL,
907 MSIINSTALLCONTEXT_USERMANAGED, &hkey, FALSE);
908 if (r == ERROR_SUCCESS)
909 context = MSIINSTALLCONTEXT_USERMANAGED;
910 else
912 r = MSIREG_OpenProductKey(szProduct, NULL,
913 MSIINSTALLCONTEXT_USERUNMANAGED,
914 &hkey, FALSE);
915 if (r != ERROR_SUCCESS)
916 return ERROR_UNKNOWN_PRODUCT;
918 context = MSIINSTALLCONTEXT_USERUNMANAGED;
921 RegCloseKey(hkey);
924 ret = MsiSourceListAddSourceExW(szProduct, sidstr,
925 context, MSISOURCETYPE_NETWORK, szSource, 0);
927 if (sidstr)
928 LocalFree(sidstr);
930 return ret;
933 /******************************************************************
934 * MsiSourceListAddSourceA (MSI.@)
936 UINT WINAPI MsiSourceListAddSourceA( LPCSTR szProduct, LPCSTR szUserName,
937 DWORD dwReserved, LPCSTR szSource)
939 INT ret;
940 LPWSTR szwproduct;
941 LPWSTR szwusername;
942 LPWSTR szwsource;
944 szwproduct = strdupAtoW( szProduct );
945 szwusername = strdupAtoW( szUserName );
946 szwsource = strdupAtoW( szSource );
948 ret = MsiSourceListAddSourceW(szwproduct, szwusername, dwReserved, szwsource);
950 msi_free(szwproduct);
951 msi_free(szwusername);
952 msi_free(szwsource);
954 return ret;
957 /******************************************************************
958 * MsiSourceListAddSourceExA (MSI.@)
960 UINT WINAPI MsiSourceListAddSourceExA(LPCSTR szProduct, LPCSTR szUserSid,
961 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCSTR szSource, DWORD dwIndex)
963 UINT ret;
964 LPWSTR product, usersid, source;
966 product = strdupAtoW(szProduct);
967 usersid = strdupAtoW(szUserSid);
968 source = strdupAtoW(szSource);
970 ret = MsiSourceListAddSourceExW(product, usersid, dwContext,
971 dwOptions, source, dwIndex);
973 msi_free(product);
974 msi_free(usersid);
975 msi_free(source);
977 return ret;
980 static void free_source_list(struct list *sourcelist)
982 while (!list_empty(sourcelist))
984 media_info *info = LIST_ENTRY(list_head(sourcelist), media_info, entry);
985 list_remove(&info->entry);
986 msi_free(info->path);
987 msi_free(info);
991 static void add_source_to_list(struct list *sourcelist, media_info *info,
992 DWORD *index)
994 media_info *iter;
995 BOOL found = FALSE;
996 static const WCHAR fmt[] = {'%','i',0};
998 if (index) *index = 0;
1000 if (list_empty(sourcelist))
1002 list_add_head(sourcelist, &info->entry);
1003 return;
1006 LIST_FOR_EACH_ENTRY(iter, sourcelist, media_info, entry)
1008 if (!found && info->index < iter->index)
1010 found = TRUE;
1011 list_add_before(&iter->entry, &info->entry);
1014 /* update the rest of the list */
1015 if (found)
1016 sprintfW(iter->szIndex, fmt, ++iter->index);
1017 else if (index)
1018 (*index)++;
1021 if (!found)
1022 list_add_after(&iter->entry, &info->entry);
1025 static UINT fill_source_list(struct list *sourcelist, HKEY sourcekey, DWORD *count)
1027 UINT r = ERROR_SUCCESS;
1028 DWORD index = 0;
1029 WCHAR name[10];
1030 DWORD size, val_size;
1031 media_info *entry;
1033 *count = 0;
1035 while (r == ERROR_SUCCESS)
1037 size = sizeof(name) / sizeof(name[0]);
1038 r = RegEnumValueW(sourcekey, index, name, &size, NULL, NULL, NULL, &val_size);
1039 if (r != ERROR_SUCCESS)
1040 return r;
1042 entry = msi_alloc(sizeof(media_info));
1043 if (!entry)
1044 goto error;
1046 entry->path = msi_alloc(val_size);
1047 if (!entry->path)
1049 msi_free(entry);
1050 goto error;
1053 lstrcpyW(entry->szIndex, name);
1054 entry->index = atoiW(name);
1056 size++;
1057 r = RegEnumValueW(sourcekey, index, name, &size, NULL,
1058 NULL, (LPBYTE)entry->path, &val_size);
1059 if (r != ERROR_SUCCESS)
1061 msi_free(entry->path);
1062 msi_free(entry);
1063 goto error;
1066 index = ++(*count);
1067 add_source_to_list(sourcelist, entry, NULL);
1070 error:
1071 *count = -1;
1072 free_source_list(sourcelist);
1073 return ERROR_OUTOFMEMORY;
1076 /******************************************************************
1077 * MsiSourceListAddSourceExW (MSI.@)
1079 UINT WINAPI MsiSourceListAddSourceExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1080 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, LPCWSTR szSource,
1081 DWORD dwIndex)
1083 HKEY sourcekey;
1084 HKEY typekey;
1085 UINT rc;
1086 struct list sourcelist;
1087 media_info *info;
1088 WCHAR squished_pc[GUID_SIZE];
1089 WCHAR name[10];
1090 LPWSTR source;
1091 LPCWSTR postfix;
1092 DWORD size, count;
1093 DWORD index;
1095 static const WCHAR fmt[] = {'%','i',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)
1120 return rc;
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);
1128 else
1130 ERR("unknown media type: %08x\n", dwOptions);
1131 RegCloseKey(sourcekey);
1132 return ERROR_FUNCTION_FAILED;
1134 if (rc != ERROR_SUCCESS)
1136 ERR("can't open subkey %u\n", rc);
1137 RegCloseKey(sourcekey);
1138 return rc;
1141 postfix = (dwOptions & MSISOURCETYPE_NETWORK) ? szBackSlash : szForwardSlash;
1142 if (szSource[lstrlenW(szSource) - 1] == *postfix)
1143 source = strdupW(szSource);
1144 else
1146 size = lstrlenW(szSource) + 2;
1147 source = msi_alloc(size * sizeof(WCHAR));
1148 lstrcpyW(source, szSource);
1149 lstrcatW(source, postfix);
1152 list_init(&sourcelist);
1153 rc = fill_source_list(&sourcelist, typekey, &count);
1154 if (rc != ERROR_NO_MORE_ITEMS)
1155 goto done;
1157 size = (lstrlenW(source) + 1) * sizeof(WCHAR);
1159 if (count == 0)
1161 rc = RegSetValueExW(typekey, szOne, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1162 goto done;
1164 else if (dwIndex > count || dwIndex == 0)
1166 sprintfW(name, fmt, count + 1);
1167 rc = RegSetValueExW(typekey, name, 0, REG_EXPAND_SZ, (LPBYTE)source, size);
1168 goto done;
1170 else
1172 sprintfW(name, fmt, dwIndex);
1173 info = msi_alloc(sizeof(media_info));
1174 if (!info)
1176 rc = ERROR_OUTOFMEMORY;
1177 goto done;
1180 info->path = strdupW(source);
1181 lstrcpyW(info->szIndex, name);
1182 info->index = dwIndex;
1183 add_source_to_list(&sourcelist, info, &index);
1185 LIST_FOR_EACH_ENTRY(info, &sourcelist, media_info, entry)
1187 if (info->index < index)
1188 continue;
1190 size = (lstrlenW(info->path) + 1) * sizeof(WCHAR);
1191 rc = RegSetValueExW(typekey, info->szIndex, 0,
1192 REG_EXPAND_SZ, (LPBYTE)info->path, size);
1193 if (rc != ERROR_SUCCESS)
1194 goto done;
1198 done:
1199 free_source_list(&sourcelist);
1200 msi_free(source);
1201 RegCloseKey(typekey);
1202 RegCloseKey(sourcekey);
1203 return rc;
1206 /******************************************************************
1207 * MsiSourceListAddMediaDiskA (MSI.@)
1209 UINT WINAPI MsiSourceListAddMediaDiskA(LPCSTR szProduct, LPCSTR szUserSid,
1210 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1211 LPCSTR szVolumeLabel, LPCSTR szDiskPrompt)
1213 UINT r;
1214 LPWSTR product = NULL;
1215 LPWSTR usersid = NULL;
1216 LPWSTR volume = NULL;
1217 LPWSTR prompt = NULL;
1219 if (szProduct) product = strdupAtoW(szProduct);
1220 if (szUserSid) usersid = strdupAtoW(szUserSid);
1221 if (szVolumeLabel) volume = strdupAtoW(szVolumeLabel);
1222 if (szDiskPrompt) prompt = strdupAtoW(szDiskPrompt);
1224 r = MsiSourceListAddMediaDiskW(product, usersid, dwContext, dwOptions,
1225 dwDiskId, volume, prompt);
1227 msi_free(product);
1228 msi_free(usersid);
1229 msi_free(volume);
1230 msi_free(prompt);
1232 return r;
1235 /******************************************************************
1236 * MsiSourceListAddMediaDiskW (MSI.@)
1238 UINT WINAPI MsiSourceListAddMediaDiskW(LPCWSTR szProduct, LPCWSTR szUserSid,
1239 MSIINSTALLCONTEXT dwContext, DWORD dwOptions, DWORD dwDiskId,
1240 LPCWSTR szVolumeLabel, LPCWSTR szDiskPrompt)
1242 HKEY sourcekey;
1243 HKEY mediakey;
1244 UINT rc;
1245 WCHAR szIndex[10];
1246 WCHAR squished_pc[GUID_SIZE];
1247 LPWSTR buffer;
1248 DWORD size;
1250 static const WCHAR fmt[] = {'%','i',0};
1252 TRACE("%s %s %x %x %i %s %s\n", debugstr_w(szProduct),
1253 debugstr_w(szUserSid), dwContext, dwOptions, dwDiskId,
1254 debugstr_w(szVolumeLabel), debugstr_w(szDiskPrompt));
1256 if (!szProduct || !squash_guid(szProduct, squished_pc))
1257 return ERROR_INVALID_PARAMETER;
1259 if (dwOptions != MSICODE_PRODUCT && dwOptions != MSICODE_PATCH)
1260 return ERROR_INVALID_PARAMETER;
1262 if ((szVolumeLabel && !*szVolumeLabel) || (szDiskPrompt && !*szDiskPrompt))
1263 return ERROR_INVALID_PARAMETER;
1265 if ((dwContext & MSIINSTALLCONTEXT_MACHINE) && szUserSid)
1266 return ERROR_INVALID_PARAMETER;
1268 if (dwOptions & MSICODE_PATCH)
1270 FIXME("Unhandled options MSICODE_PATCH\n");
1271 return ERROR_FUNCTION_FAILED;
1274 rc = OpenSourceKey(szProduct, &sourcekey, MSICODE_PRODUCT, dwContext, FALSE);
1275 if (rc != ERROR_SUCCESS)
1276 return rc;
1278 OpenMediaSubkey(sourcekey, &mediakey, TRUE);
1280 sprintfW(szIndex, fmt, dwDiskId);
1282 size = 2;
1283 if (szVolumeLabel) size += lstrlenW(szVolumeLabel);
1284 if (szDiskPrompt) size += lstrlenW(szDiskPrompt);
1286 size *= sizeof(WCHAR);
1287 buffer = msi_alloc(size);
1288 *buffer = '\0';
1290 if (szVolumeLabel) lstrcpyW(buffer, szVolumeLabel);
1291 lstrcatW(buffer, szSemiColon);
1292 if (szDiskPrompt) lstrcatW(buffer, szDiskPrompt);
1294 RegSetValueExW(mediakey, szIndex, 0, REG_SZ, (LPBYTE)buffer, size);
1295 msi_free(buffer);
1297 RegCloseKey(sourcekey);
1298 RegCloseKey(mediakey);
1300 return ERROR_SUCCESS;
1303 /******************************************************************
1304 * MsiSourceListClearAllA (MSI.@)
1306 UINT WINAPI MsiSourceListClearAllA( LPCSTR szProduct, LPCSTR szUserName, DWORD dwReserved )
1308 FIXME("(%s %s %d)\n", debugstr_a(szProduct), debugstr_a(szUserName), dwReserved);
1309 return ERROR_SUCCESS;
1312 /******************************************************************
1313 * MsiSourceListClearAllW (MSI.@)
1315 UINT WINAPI MsiSourceListClearAllW( LPCWSTR szProduct, LPCWSTR szUserName, DWORD dwReserved )
1317 FIXME("(%s %s %d)\n", debugstr_w(szProduct), debugstr_w(szUserName), dwReserved);
1318 return ERROR_SUCCESS;
1321 /******************************************************************
1322 * MsiSourceListClearAllExA (MSI.@)
1324 UINT WINAPI MsiSourceListClearAllExA( LPCSTR szProduct, LPCSTR szUserSid,
1325 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1327 FIXME("(%s %s %d %08x)\n", debugstr_a(szProduct), debugstr_a(szUserSid),
1328 dwContext, dwOptions);
1329 return ERROR_SUCCESS;
1332 /******************************************************************
1333 * MsiSourceListClearAllExW (MSI.@)
1335 UINT WINAPI MsiSourceListClearAllExW( LPCWSTR szProduct, LPCWSTR szUserSid,
1336 MSIINSTALLCONTEXT dwContext, DWORD dwOptions )
1338 FIXME("(%s %s %d %08x)\n", debugstr_w(szProduct), debugstr_w(szUserSid),
1339 dwContext, dwOptions);
1340 return ERROR_SUCCESS;
1343 /******************************************************************
1344 * MsiSourceListClearSourceA (MSI.@)
1346 UINT WINAPI MsiSourceListClearSourceA(LPCSTR szProductCodeOrPatchCode, LPCSTR szUserSid,
1347 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1348 LPCSTR szSource)
1350 FIXME("(%s %s %x %x %s)\n", debugstr_a(szProductCodeOrPatchCode), debugstr_a(szUserSid),
1351 dwContext, dwOptions, debugstr_a(szSource));
1352 return ERROR_SUCCESS;
1355 /******************************************************************
1356 * MsiSourceListClearSourceW (MSI.@)
1358 UINT WINAPI MsiSourceListClearSourceW(LPCWSTR szProductCodeOrPatchCode, LPCWSTR szUserSid,
1359 MSIINSTALLCONTEXT dwContext, DWORD dwOptions,
1360 LPCWSTR szSource)
1362 FIXME("(%s %s %x %x %s)\n", debugstr_w(szProductCodeOrPatchCode), debugstr_w(szUserSid),
1363 dwContext, dwOptions, debugstr_w(szSource));
1364 return ERROR_SUCCESS;