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

这一生的挚爱 提交于 2020-06-08 05:33:28

问题


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


回答1:


In order to do this, your background process should be running in user account and should get launched during the user login.



来源:https://stackoverflow.com/questions/57095433/put-text-to-clipboard-in-the-background-process-on-locked-windows-10-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!