コード例 #1
0
        /// <summary>
        /// Gets a formatted string representation of the Record, optionally using a Session to format properties.
        /// </summary>
        /// <param name="provider">an optional Session instance that will be used to lookup any
        /// properties in the Record's format string</param>
        /// <returns>A formatted string representation of the Record.</returns>
        /// <remarks><p>
        /// If field 0 of the Record is set to a nonempty string, it is used to format the data in the Record.
        /// </p><p>
        /// Win32 MSI API:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msiformatrecord.asp">MsiFormatRecord</a>
        /// </p></remarks>
        /// <seealso cref="FormatString"/>
        /// <seealso cref="Session.FormatRecord(Record)"/>
        public string ToString(IFormatProvider provider)
        {
            if (this.IsFormatStringInvalid) // Format string is invalid
            {
                // TODO: return all values by default?
                return(String.Empty);
            }

            InstallerHandle session       = provider as InstallerHandle;
            int             sessionHandle = session != null ? (int)session.Handle : 0;
            StringBuilder   buf           = new StringBuilder(String.Empty);
            uint            bufSize       = 1;
            uint            ret           = RemotableNativeMethods.MsiFormatRecord(sessionHandle, (int)this.Handle, buf, ref bufSize);

            if (ret == (uint)NativeMethods.Error.MORE_DATA)
            {
                bufSize++;
                buf = new StringBuilder((int)bufSize);
                ret = RemotableNativeMethods.MsiFormatRecord(sessionHandle, (int)this.Handle, buf, ref bufSize);
            }
            if (ret != 0)
            {
                throw InstallerException.ExceptionFromReturnCode(ret);
            }
            return(buf.ToString());
        }