6

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

laooglee
  • 137
  • 13
  • 1
    This may be a case of an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you provide more information on what you are trying to do? – Kredns Jul 18 '19 at 13:23
  • 1
    @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). – laooglee Jul 18 '19 at 13:24
  • 1
    The clipboard is there for the USER to decide what gets copied to it, not a background process. – Neil Jul 18 '19 at 13:55
  • Since this would be a big security problem, why do you expect it to work? – nilsK Jul 18 '19 at 13:57
  • 1
    @nilsK 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 logic – laooglee Jul 18 '19 at 14:00
  • 1
    @laooglee 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. – Nyerguds Aug 01 '19 at 09:09
  • @Nyerguds 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 it – laooglee Aug 13 '19 at 12:48
  • There were some security issues around lock + clipboard: https://www.mcafee.com/blogs/other-blogs/mcafee-labs/unintended-clipboard-paste-function-in-windows-10-leads-to-information-leak-in-rs1/ and https://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 higher – Simon Mourier May 22 '20 at 09:51
  • I still confuse about your problem. It would be nice if you provide an example to simulate the problem. – Carson May 29 '20 at 02:35
  • 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? – Dmytro Zakharov May 29 '20 at 07:08
  • @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. – Colm Bhandal May 29 '20 at 07:25
  • 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://stackoverflow.com/a/48501919/5134722 – Colm Bhandal May 29 '20 at 07:39

2 Answers2

0

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

knkumar93
  • 128
  • 1
  • 10
  • 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#? – Colm Bhandal May 25 '20 at 08:10
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
Jag
  • 1
  • 1