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

2

7

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

Boley answered 18/7, 2019 at 13:17 Comment(12)
This may be a case of an XY problem. Can you provide more information on what you are trying to do?Boisterous
@mjwills my real problem is writing a script for application, which support only input from clipboard on certain stage to work properly. There's a workaround, but much more cumbersome, than just read data from clipboard (it's not "paste" it's some internal application command, that consume clipboard content directly).Boley
The clipboard is there for the USER to decide what gets copied to it, not a background process.Novercal
Since this would be a big security problem, why do you expect it to work?Kinney
@Kinney it properly worked on Windows 7, and I did not found any details on clipboard behavior for Win 10 changes on this specific scenario. So I assumed the process can be migrated to Win10 with the same logicBoley
@Boley That's the mindset that makes loads of old VPN login software use Java browser applets. Security marches on, and sometimes the only way to keep old stuff working is by using virtual machines... or by replacing the actual core of the problem by something new and more secure.Gyrostatics
@Gyrostatics true - that's not the best option. But unfortunately the only available for now. And we will try to to go for VM to handle itBoley
There were some security issues around lock + clipboard: mcafee.com/blogs/other-blogs/mcafee-labs/… and msitpros.com/?p=3764 I can't find any Microsoft reference though. Maybe related to the fact you can now have history and a cloud clipboard features with Windows 10. The security risk is higherLoisloise
I still confuse about your problem. It would be nice if you provide an example to simulate the problem.Nonmetallic
Correct me if I am wrong, but in general the flow seems to be like this: 1. User starts some long running process (Main App) and locks computer 2. Main App calls Sub App and passes over some data 3. Sub App processes data, but can only send the result back via Clipboard, as the only mean of data transmission So Sub App cannot wait for the user to login to continue, as it would defeat the purpose of running after user walks away and waits. Am I missing something?Triclinium
@DmytroZakharov I do not presume to speak on behalf of the OP, but for my own use case it's as follows. User launches a long running Main app from an Excel VSTO AddIn. The Main app needs to read a tonne of data from a really huge Excel sheet (2 million cells), including cell background colours. We've implemented a solution which copies to the clipboard and parses the clipboard data from memory, because the Interop interface has no efficient way of reading background colours. But if the Main app is left running and the lock screen comes down, the clipboard operation fails.Telex
I suppose a workaround for my use case is close workbook, read in data using a different library e.g. ClosedXML, then reopen the workbook. Solution is here: https://mcmap.net/q/1629408/-how-to-mix-excel-interop-with-closedxml-stylesTelex
C
0

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

Chaim answered 24/5, 2020 at 7:46 Comment(1)
What you're suggesting is too restrictive in the general case; certainly for my case. Our C# program only launches with Excel, and we don't want to launch Excel every time a user logs in. As well as that, it's not clear from your answer how to actually achieve in code what you suggest. Could you give a minimal working example, e.g. in C#?Telex
L
0

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
Leboeuf answered 11/7, 2022 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.