| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.BootstrapperApplicationApi |
| 4 | { |
| 5 | using System; |
| 6 | using System.Runtime.InteropServices; |
| 7 | using System.Security; |
| 8 | using System.Text; |
| 9 | |
| 10 | internal static class StrUtil |
| 11 | { |
| 12 | [DllImport("mbanative.dll", ExactSpelling = true)] |
| 13 | internal static extern void StrFree( |
| 14 | IntPtr scz |
| 15 | ); |
| 16 | |
| 17 | internal sealed class StrHandle : SafeHandleZeroIsDefaultAndInvalid |
| 18 | { |
| 19 | protected override bool ReleaseHandle() |
| 20 | { |
| 21 | StrFree(this.handle); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | public string ToUniString() |
| 26 | { |
| 27 | return Marshal.PtrToStringUni(this.handle); |
| 28 | } |
| 29 | |
| 30 | public SecureString ToSecureString() |
| 31 | { |
| 32 | if (this.handle == IntPtr.Zero) |
| 33 | { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | SecureString value = new SecureString(); |
| 38 | char c; |
| 39 | for (int charIndex = 0; ; charIndex++) |
| 40 | { |
| 41 | c = (char)Marshal.ReadInt16(this.handle, charIndex * UnicodeEncoding.CharSize); |
| 42 | if (c == '\0') |
| 43 | { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | value.AppendChar(c); |
| 48 | } |
| 49 | |
| 50 | return value; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |