C++和C#实现剪切板数据交互

c#端由于system.windows.form自带的剪切板功能太少,所以写了一个Helper类把接口转了出来。这样就可以用不同的uint的id了。

并且自带的剪切板必须执行在[STAThread]模式下,很麻烦

而c++端拷贝字符串由于编码问题,需要使用宽字符。否则会乱码

 

 

c#


 

ClipboardHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Hont.Win32
{
    public static class ClipboardHelper
    {
        #region Win32 Interface
        [DllImport("user32.dll")]
        public static extern bool EmptyClipboard();
        [DllImport("user32.dll", SetLastError = true)]
        private extern static IntPtr SetClipboardData(uint format, IntPtr handle);
        [DllImport("user32.dll")]
        static extern IntPtr GetClipboardData(uint uFormat);
        [DllImport("user32.dll")]
        static extern bool IsClipboardFormatAvailable(uint format);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool OpenClipboard(IntPtr hWndNewOwner);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool CloseClipboard();
        [DllImport("kernel32.dll")]
        static extern IntPtr GlobalLock(IntPtr hMem);
        [DllImport("kernel32.dll")]
        static extern bool GlobalUnlock(IntPtr hMem);

        public const uint CF_UNICODETEXT = 13;
        #endregion

        public static bool CopyToClipboard(uint id, string content)
        {
            if (OpenClipboard(IntPtr.Zero))
            {
                EmptyClipboard();
                IntPtr hmem = Marshal.StringToHGlobalUni(content);
                var ptr = GlobalLock(hmem);
                GlobalUnlock(ptr);
                SetClipboardData(id, ptr);
                CloseClipboard();
                return true;
            }
            return false;
        }

        public static string GetFromClipboard(uint id)
        {
            if (!IsClipboardFormatAvailable(id)) return null;
            if (!OpenClipboard(IntPtr.Zero)) return null;

            string data = null;
            var hGlobal = GetClipboardData(id);
            if (hGlobal != IntPtr.Zero)
            {
                var lpwcstr = GlobalLock(hGlobal);
                if (lpwcstr != IntPtr.Zero)
                {
                    data = Marshal.PtrToStringAuto(lpwcstr);
                    GlobalUnlock(lpwcstr);
                }
            }
            CloseClipboard();
            
            return data;
        }
    }
}
View Code

 

Main

using System;

namespace ClipboardTest
{
    class Program
    {
        //[STAThread]
        static void Main(string[] args)
        {
            //ClipboardHelper.CopyToClipboard(90, "qweqweqwe");
            var str = ClipboardHelper.GetFromClipboard(90);
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }
}
View Code

 

 

 

c++


 

#include<Windows.h>
#include <iostream>
#include <windows.h>
#include <ctime>
#include <atlstr.h>
#include "stdafx.h"


BOOL CopyStringToClipBoard(HWND hOwner, CString strSource)
{
    if (::OpenClipboard(hOwner))
    {
        int buff_size = strSource.GetLength();
        CStringW strWide = CStringW(strSource);
        int nLen = strWide.GetLength();
        HANDLE clipbuffer;
        char* buffer;
        ::EmptyClipboard();
        clipbuffer = ::GlobalAlloc(GMEM_DDESHARE, (nLen + 1) * 2);
        buffer = (char*)::GlobalLock(clipbuffer);
        memset(buffer, 0, (nLen + 1) * 2);
        memcpy_s(buffer, nLen * 2, strWide.GetBuffer(0), nLen * 2);
        strWide.ReleaseBuffer();
        ::GlobalUnlock(clipbuffer);
        ::SetClipboardData(90, clipbuffer);
        ::CloseClipboard();
    }
    return FALSE;
}

BOOL GetTextFromClipboard()
{
    if (::OpenClipboard(NULL))
    {
        HGLOBAL hMem = GetClipboardData(90);
        if (NULL != hMem)
        {
            char* lpStr = (char*)::GlobalLock(hMem);
            if (NULL != lpStr)
            {
                printf(lpStr);
                ::GlobalUnlock(hMem);
            }
        }
        ::CloseClipboard();
        return TRUE;
    }
    return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
    CopyStringToClipBoard(NULL, "asdsad");
    GetTextFromClipboard();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/hont/p/4189620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值