Windows-从C ++中的服务访问剪贴板 [英] windows - access clipboard from service in c++

查看:151
本文介绍了Windows-从C ++中的服务访问剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的Windows服务,该服务需要访问剪贴板并从中读取数据/将数据粘贴到剪贴板。我只考虑文本数据。

I have a Windows service written in C++ that needs to access the clipboard and read data from/paste data to it. I'm considering only textual data.

来自 MSDN 的文档,我可以使用 OpenClipboard EmptyClipboard SetClipboardData 来实现我想要的。

From MSDN's documentation, I can use OpenClipboard, EmptyClipboard and SetClipboardData to achieve what I want to.

我必须传递 NULL OpenClipboard ,因为我没有任何UI,因此也没有窗口句柄。但是,这意味着-

I would have to pass NULL to OpenClipboard since I don't have any UI and hence no window handles. However, this would mean -


如果应用程序在hwnd设置为NULL的情况下调用OpenClipboard,EmptyClipboard会将剪贴板所有者设置为NULL。这会导致SetClipboardData失败。

If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.

这意味着我无法在剪贴板上设置数据。

which would mean I can't set data on the clipboard.

解决此问题的正确方法是什么?

What would be the correct way to get around this problem? Is it possible without using any windows?

推荐答案

您肯定可以从非GUI应用程序访问剪贴板。 Windows甚至包括一个命令行应用程序(clip.exe)来执行此操作。

但是,在花太多时间进行此工作之前……
剪贴板不是在同一系统上的用户之间共享。假设您有两个用户登录。您无法从一个会话复制数据,也不能从锁定屏幕切换用户并粘贴相同的数据。
因此,大概,您的服务将像另一个用户会话一样工作,并且您的应用程序将只能看到先前在该服务的同一实例中复制的数据。

You definitely CAN access the clipboard from a non-GUI application. Windows even included a command-line app (clip.exe) to do it.
However, before you spend too much time working on this...... The clipboard isn't shared among users on the same system. Suppose you have two users logged in. You can't copy data from one session, switch users (from the lock screen) and paste that same data. So presumably, your service would act like another user session, and your app would only be able to see data that was copied earlier within that same instance of the service.

这篇关于Windows-从C ++中的服务访问剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆