简体   繁体   中英

Put text to clipboard in the background process on locked Windows 10 machine

I'm building a process (so far I've tried VBA, Python and C# on .Net Framework 4.7.2) which requires to put some string to clipboard on Windows 10 machine behind the lock screen. For testing I've reduced it to only two commands (pseudo code, since 3 languages used. Details in the end of the question):

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

Clipboard is responsive on unlocked session, but becomes unavailable and returns "clipboard locked" error (language specific), when machine is locked.

I've tested several clipboard related techniques found in google/stackoverflow for mentioned languages (about 6 in total) and none works so far.

Machine is running on Windows 10 Enterprise (tested on 3 different machines with the same version).

Code examples:

C# opt 1:

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

}

C# opt 2 (for check what is locking clipboard):

    [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 opt.1:

Dim clipboard As MSForms.DataObject    
Set clipboard = New MSForms.DataObject   
clipboard.SetText "text for input"
clipboard.PutInClipboard

VBA opt.2: Text To Clipboard in VBA Windows 10 Issue

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

Use tkinter in python, more here https://www.daniweb.com/programming/software-development/code/487653/access-the-clipboard-via-tkinter but the below works for me for getting data from the clipboard whilst PC is locked. In my case the paste function is automated in another software but you would use cb.clipboard_append('text') to get the text in.

from tkinter import Tk
try:

    cb = Tk()
    cb.selection_get(selection='CLIPBOARD')
except:
    selection=None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM