从GetClipboardData原生方法获取byte[]

本文关键字:byte 获取 方法 GetClipboardData 原生 | 更新日期: 2023-09-27 18:16:28

我正在尝试使用c#/. net的本机方法获取剪贴板数据。问题是我弄乱了数据。下面是我的代码:

IntPtr pointer = GetClipboardData(dataformat);
int size = Marshal.SizeOf(pointer);
byte[] buff = new byte[size];
Marshal.Copy(data, buff, 0, size);

这是我使用的GetClipboardData方法的pinvoke:

[DllImport("user32.dll")]
private static extern IntPtr GetClipboardData(uint uFormat);
谁能告诉我我哪里错了?

从GetClipboardData原生方法获取byte[]

如果您想从剪贴板中获取字节数组,例如表示unicode文本,代码将是这样的:

using System;
using System.Runtime.InteropServices;
using System.Text;
public class ClipboardHelper
{
    #region Win32
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsClipboardFormatAvailable(uint format);
    [DllImport("User32.dll", SetLastError = true)]
    private static extern IntPtr GetClipboardData(uint uFormat);
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool OpenClipboard(IntPtr hWndNewOwner);
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseClipboard();
    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern IntPtr GlobalLock(IntPtr hMem);
    [DllImport("Kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GlobalUnlock(IntPtr hMem);
    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern int GlobalSize(IntPtr hMem);
    private const uint CF_UNICODETEXT = 13U;
    #endregion
    public static string GetText()
    {
        if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
            return null;
        try
        {
            if (!OpenClipboard(IntPtr.Zero))
                return null;
            IntPtr handle = GetClipboardData(CF_UNICODETEXT);
            if (handle == IntPtr.Zero)
                return null;
            IntPtr pointer = IntPtr.Zero;
            try
            {
                pointer = GlobalLock(handle);
                if (pointer == IntPtr.Zero)
                    return null;
                int size = GlobalSize(handle);
                byte[] buff = new byte[size];
                Marshal.Copy(pointer, buff, 0, size);
                return Encoding.Unicode.GetString(buff).TrimEnd(''0');
            }
            finally
            {
                if (pointer != IntPtr.Zero)
                    GlobalUnlock(handle);
            }
        }
        finally
        {
            CloseClipboard();
        }
    }
}