コード例 #1
0
        private HRESULT SaveHtmlToHandle(IntPtr handle, string str)
        {
            if (handle == IntPtr.Zero)
            {
                return(HRESULT.E_INVALIDARG);
            }
            IntPtr newHandle = IntPtr.Zero;

            UTF8Encoding encoding = new UTF8Encoding();

            byte[] bytes = encoding.GetBytes(str);
            newHandle = Kernel32.GlobalReAlloc(
                handle,
                (uint)bytes.Length + 1,
                Kernel32.GMEM.MOVEABLE | Kernel32.GMEM.DDESHARE | Kernel32.GMEM.ZEROINIT);
            if (newHandle == IntPtr.Zero)
            {
                return(HRESULT.E_OUTOFMEMORY);
            }

            IntPtr ptr = Kernel32.GlobalLock(newHandle);

            if (ptr == IntPtr.Zero)
            {
                return(HRESULT.E_OUTOFMEMORY);
            }

            try
            {
                UnsafeNativeMethods.CopyMemory(ptr, bytes, bytes.Length);
                Marshal.Copy(new byte[] { 0 }, 0, (IntPtr)((long)ptr + bytes.Length), 1);
            }
            finally
            {
                Kernel32.GlobalUnlock(newHandle);
            }

            return(HRESULT.S_OK);
        }
コード例 #2
0
        private HRESULT SaveStringToHandle(IntPtr handle, string str, bool unicode)
        {
            if (handle == IntPtr.Zero)
            {
                return(HRESULT.E_INVALIDARG);
            }
            IntPtr newHandle = IntPtr.Zero;

            if (unicode)
            {
                uint byteSize = (uint)str.Length * 2 + 2;
                newHandle = Kernel32.GlobalReAlloc(
                    handle,
                    byteSize,
                    Kernel32.GMEM.MOVEABLE | Kernel32.GMEM.DDESHARE | Kernel32.GMEM.ZEROINIT);
                if (newHandle == IntPtr.Zero)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                IntPtr ptr = Kernel32.GlobalLock(newHandle);
                if (ptr == IntPtr.Zero)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                char[] chars = str.ToCharArray(0, str.Length);
                UnsafeNativeMethods.CopyMemoryW(ptr, chars, chars.Length * 2);
            }
            else
            {
                int pinvokeSize = UnsafeNativeMethods.WideCharToMultiByte(0 /*CP_ACP*/, 0, str, str.Length, null, 0, IntPtr.Zero, IntPtr.Zero);

                byte[] strBytes = new byte[pinvokeSize];
                UnsafeNativeMethods.WideCharToMultiByte(0 /*CP_ACP*/, 0, str, str.Length, strBytes, strBytes.Length, IntPtr.Zero, IntPtr.Zero);

                newHandle = Kernel32.GlobalReAlloc(
                    handle,
                    (uint)pinvokeSize + 1,

                    Kernel32.GMEM.MOVEABLE | Kernel32.GMEM.DDESHARE | Kernel32.GMEM.ZEROINIT);
                if (newHandle == IntPtr.Zero)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                IntPtr ptr = Kernel32.GlobalLock(newHandle);
                if (ptr == IntPtr.Zero)
                {
                    return(HRESULT.E_OUTOFMEMORY);
                }

                UnsafeNativeMethods.CopyMemory(ptr, strBytes, pinvokeSize);
                Marshal.Copy(new byte[] { 0 }, 0, (IntPtr)((long)ptr + pinvokeSize), 1);
            }

            if (newHandle != IntPtr.Zero)
            {
                Kernel32.GlobalUnlock(newHandle);
            }
            return(HRESULT.S_OK);
        }