public bool GetString(FormatId id, NativeStringType native, out string result)
 {
     if (GetDataPresent(id))
     {
         result = GetString(id, native);
         return(true);
     }
     result = null;
     return(false);
 }
        public string GetString(FormatId id, NativeStringType type)
        {
            var       f = DataObjectUtils.GetFormatEtc(id);
            STGMEDIUM s = default;

            try
            {
                DataObject.GetData(ref f, out s);
                return(s.GetString(type));
            }
            finally
            {
                s.Dispose();
            }
        }
Beispiel #3
0
        public void SetString(string str, NativeStringType native = NativeStringType.Unicode)
        {
            switch (native)
            {
            case NativeStringType.Unicode:
                SetData(FormatId.CF_UNICODETEXT, str);
                break;

            case NativeStringType.Ansi:
                SetData(FormatId.CF_TEXT, str);
                break;

            default:
                break;
            }
        }
 public NativeStringParamAttribute(NativeStringType type)
 {
     this.type = type;
 }
Beispiel #5
0
        private unsafe HRESULT SaveStringToHandle(ref IntPtr handle, string s, NativeStringType type)
        {
            if (handle == IntPtr.Zero)
            {
                return(HRESULT.E_INVALIDARG);
            }
            switch (type)
            {
            case NativeStringType.Unicode:
            {
                int nb = (s.Length + 1) * 2;

                // Overflow checking
                if (nb < s.Length)
                {
                    throw new ArgumentOutOfRangeException(nameof(s));
                }

                var hg = Kernel32.GlobalReAlloc(handle, nb,
                                                Kernel32.GMEM.GMEM_MOVEABLE | Kernel32.GMEM.GMEM_ZEROINIT);
                handle = hg.DangerousGetHandle();
                if (hg.IsNull)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }
                var lc = Kernel32.GlobalLock(hg);

                fixed(char *firstChar = s)
                {
                    Buffer.MemoryCopy(firstChar, lc.ToPointer(), nb, nb);
                }

                Kernel32.GlobalUnlock(hg);
                break;
            }

            case NativeStringType.Ansi:
            {
                // Ansi. See also StringToHGlobalAnsi

                long lnb = (s.Length + 1) * (long)Marshal.SystemMaxDBCSCharSize;
                int  nb  = (int)lnb;
                if (nb != lnb)
                {
                    throw new ArgumentOutOfRangeException(nameof(s));
                }

                var hg = Kernel32.GlobalReAlloc(handle, nb,
                                                Kernel32.GMEM.GMEM_MOVEABLE | Kernel32.GMEM.GMEM_ZEROINIT);
                handle = hg.DangerousGetHandle();
                if (hg.IsNull)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                var lc = Kernel32.GlobalLock(hg);
                StringToAnsiString(s, (byte *)lc.ToPointer(), nb);
                Kernel32.GlobalUnlock(hg);
                break;
            }

            case NativeStringType.Utf8:
            {
                var enc = new UTF8Encoding();
                int nb  = enc.GetByteCount(s) + 1;
                var hg  = Kernel32.GlobalReAlloc(handle, nb,
                                                 Kernel32.GMEM.GMEM_MOVEABLE | Kernel32.GMEM.GMEM_ZEROINIT);
                handle = hg.DangerousGetHandle();
                if (hg.IsNull)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                var lc = Kernel32.GlobalLock(hg);
                fixed(char *firstChar = s)
                {
                    enc.GetBytes(firstChar, s.Length, (byte *)lc.ToPointer(), nb);
                }

                Kernel32.GlobalUnlock(hg);
                break;
            }
            }
            return(HRESULT.S_OK);
        }