Exemple #1
0
        /// <summary>
        /// Create from a package full name.
        /// </summary>
        /// <param name="package_full_name">The package full name.</param>
        /// <param name="full_information">Query for full information (needs to be installed for the current user).</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The package identity.</returns>
        public static NtResult <PackageIdentity> CreateFromFullName(string package_full_name, bool full_information, bool throw_on_error)
        {
            PackageFlags flags  = full_information ? PackageFlags.Full : PackageFlags.Basic;
            int          length = 0;

            var result = Win32NativeMethods.GetStagedPackagePathByFullName(package_full_name, ref length, null);

            if (result != Win32Error.ERROR_INSUFFICIENT_BUFFER)
            {
                return(result.CreateResultFromDosError <PackageIdentity>(throw_on_error));
            }

            var builder = new StringBuilder(length);

            result = Win32NativeMethods.GetStagedPackagePathByFullName(package_full_name, ref length, builder);
            if (result != Win32Error.SUCCESS)
            {
                return(result.CreateResultFromDosError <PackageIdentity>(throw_on_error));
            }

            result = _get_staged_package_origin(package_full_name, out PackageOrigin origin);
            if (result != Win32Error.SUCCESS)
            {
                return(result.CreateResultFromDosError <PackageIdentity>(throw_on_error));
            }

            length = 0;
            result = Win32NativeMethods.PackageIdFromFullName(package_full_name, flags, ref length, SafeHGlobalBuffer.Null);
            if (result != Win32Error.ERROR_INSUFFICIENT_BUFFER)
            {
                return(result.CreateResultFromDosError <PackageIdentity>(throw_on_error));
            }

            using (var buffer = new SafeStructureInOutBuffer <PACKAGE_ID>(length, false))
            {
                result = Win32NativeMethods.PackageIdFromFullName(package_full_name, flags, ref length, buffer);
                if (result != Win32Error.SUCCESS)
                {
                    return(result.CreateResultFromDosError <PackageIdentity>(throw_on_error));
                }

                return(new PackageIdentity(package_full_name, buffer.Result, origin, builder.ToString()).CreateResult());
            }
        }