我正在构建一个进程(到目前为止,我已经在.Net Framework 4.7.2上尝试了VBA,Python和C#),该过程需要在锁定屏幕后面的Windows 10计算机上的剪贴板中放置一些字符串。
为了进行测试,我将其简化为仅两个命令(伪代码,因为使用了3种语言。问题末尾的详细信息):

SleepForFiveSec(); //to provide time for locking screen
// now locking machine
SetClipboardContent();

剪贴板对未锁定的 session 作出响应,但在计算机锁定时,剪贴板不可用,并返回“剪贴板锁定”错误(特定于语言)。

我已经测试了在google/stackoverflow中发现的几种与剪贴板相关的技术,适用于上述提到的语言(总共约6种),到目前为止还没有一种工作。

计算机在Windows 10企业版上运行(在3台具有相同版本的不同计算机上进行了测试)。

代码示例:

C#选项1:
using System.Windows.Forms;
.....
[STAThread]
static void Main()
{
    System.Threading.Thread.Sleep(5000);
    Clipboard.SetText("test copy clip");

}

C#opt 2(用于检查锁定剪贴板的内容):
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern IntPtr GetOpenClipboardWindow();

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int GetWindowText(int hwnd, StringBuilder text, int count);

    private static string getOpenClipboardWindowText()
    {
        IntPtr hwnd = GetOpenClipboardWindow();
        StringBuilder sb = new StringBuilder(501);
        GetWindowText(hwnd.ToInt32(), sb, 500);
        return sb.ToString();
    }

python opt.1:
import pyperclip
import time

time.sleep(5)
pyperclip.copy('text')

python opt.2:
import win32clipboard
import time

time.sleep(5)
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('text')
win32clipboard.CloseClipboard()

VBA选项1:
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "text for input"
clipboard.PutInClipboard

VBA选项2:
Text To Clipboard in VBA Windows 10 Issue

最佳答案

为此,您的后台进程应以用户帐户运行,并应在用户登录期间启动。

关于c# - 在锁定的Windows 10计算机上的后台进程中将文本放入剪贴板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57095433/

10-13 09:15