Example #1
0
        private int MakeString(IntPtr src, UIntPtr byteCount, ref IntPtr newZString, ZStringFormat format)
        {
            if (src != IntPtr.Zero)
            {
                ulong stringLength = byteCount.ToUInt64();

                if (stringLength == 0)
                {
                    newZString = Empty;
                }
                else
                {
                    // The framework cannot create a string longer than Int32.MaxValue.
                    if (stringLength > int.MaxValue)
                    {
                        return(PSError.kASOutOfMemory);
                    }

                    try
                    {
                        ZString zstring = new ZString(src, (int)stringLength, format);
                        newZString = GenerateDictionaryKey();
                        strings.Add(newZString, zstring);
                    }
                    catch (OutOfMemoryException)
                    {
                        return(PSError.kASOutOfMemory);
                    }
                }

                return(PSError.kASNoError);
            }

            return(PSError.kASBadParameter);
        }
Example #2
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ZString"/> class.
            /// </summary>
            /// <param name="ptr">The pointer to the native string.</param>
            /// <param name="length">The length of the native string.</param>
            /// <param name="format">The format of the native string.</param>
            /// <exception cref="ArgumentNullException"><paramref name="ptr"/> is null.</exception>
            /// <exception cref="InvalidEnumArgumentException"><paramref name="format"/> does not specify a valid <see cref="ZStringFormat"/> value.</exception>
            /// <exception cref="OutOfMemoryException">Insufficient memory to create the ZString.</exception>
            public ZString(IntPtr ptr, int length, ZStringFormat format)
            {
                if (ptr == IntPtr.Zero)
                {
                    throw new ArgumentNullException(nameof(ptr));
                }

                refCount = 1;

                switch (format)
                {
                case ZStringFormat.Ansi:
                    data = Marshal.PtrToStringAnsi(ptr, length).TrimEnd('\0');
                    break;

                case ZStringFormat.Unicode:
                    data = Marshal.PtrToStringUni(ptr, length).TrimEnd('\0');
                    break;

                case ZStringFormat.Pascal:
                    data = PtrToStringPascal(ptr, length);
                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ZStringFormat));
                }
            }