【问题标题】:Getting byte[] from GetClipboardData native method从 GetClipboardData 本机方法获取 byte[]
【发布时间】:2015-10-07 23:10:59
【问题描述】:

我正在尝试使用带有 C#/.NET 的 this native method 获取剪贴板数据。问题是我正在处理数据。这是我的代码:

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);

谁能告诉我哪里出错了?

【问题讨论】:

  • 你看过这里提供的例子吗:msdn.microsoft.com/en-us/library/windows/desktop/…
  • @Roy 是内置函数,看OP提供的链接
  • @Roy 我会为你提供 pinvoke。见编辑。
  • data 实际上是一个 句柄 而不是直接指向内存的指针。您需要使用GlobalLock() 锁定它 以获取内存块。 See this answer here。它是 c++,但你明白了。您不只是使用 .NET Clipboard 类的任何原因?
  • 还有第二个示例进一步显示从剪贴板检索信息。

标签: c# .net clipboard unmanaged intptr


【解决方案1】:

如果您想从剪贴板中获取字节数组,例如表示 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();
        }
    }
}

【讨论】:

  • 感谢您拼写出来,虽然我已经从上面的 cmets 中弄清楚了,但我非常感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
相关资源
最近更新 更多