Ejemplo n.º 1
0
        /// <summary>
        /// Gets the set of installed components for products in the indicated context.
        /// </summary>
        /// <exception cref="InstallerException">The installer configuration data is corrupt</exception>
        /// <remarks><p>
        /// Win32 MSI API:
        /// <a href="http://msdn.microsoft.com/library/dd407947.aspx">MsiEnumComponentsEx</a>
        /// </p></remarks>
        public static IEnumerable <ComponentInstallation> Components(string szUserSid, UserContexts dwContext)
        {
            uint          pcchSid = 32;
            StringBuilder szSid   = new StringBuilder((int)pcchSid);
            StringBuilder buf     = new StringBuilder(40);
            UserContexts  installedContext;

            for (uint i = 0; true; i++)
            {
                uint ret = NativeMethods.MsiEnumComponentsEx(szUserSid, dwContext, i, buf, out installedContext, szSid, ref pcchSid);
                if (ret == (uint)NativeMethods.Error.MORE_DATA)
                {
                    szSid.EnsureCapacity((int)++pcchSid);
                    ret = NativeMethods.MsiEnumComponentsEx(szUserSid, dwContext, i, buf, out installedContext, szSid, ref pcchSid);
                }
                if (ret == (uint)NativeMethods.Error.NO_MORE_ITEMS)
                {
                    break;
                }
                if (ret != 0)
                {
                    throw InstallerException.ExceptionFromReturnCode(ret);
                }
                yield return(new ComponentInstallation(buf.ToString(), szSid.ToString(), installedContext));
            }
        }