Prevent Paste outside my app

G

Guest

Hello,

I am looking for a clever way to prevent someone from pasting copied text
outside of my app.

I know how to monitor the clipboard and get an WM_DRAWCLIPBOARD message when
text is stored in the clipboard... If only I could detect some kind of
outgoing message that the clipboard is about to paste, then I could check to
see if my app is currently in focus and allow/deny as necessary.

I would just erase the clipboard upon the app loosing focus, but I can see
cases where that would be a pain to the user if they just had to click
somewhere else real quick.

Anyone have any ideas?

Thank you all for reading this.

Rob K
 
G

Guest

It's kind of a pain but you could implement your own clipboard just for your
application.
 
G

Guest

Hey Jeffrey,

Thank you for replying. Kinda funny you mentioned that, because that is
what I started doing before I realized that the 2 different activeX Word
controls (from the browser control) would loose their formating. I couldn't
program something sophisticated enough to preserve the tables, graphs, etc,
through my own clipboard.

I also thought about capturing all CTRL+V's that happen outside of my app
(through a hook), but I cannot prevent the user from right-clicking somewhere
and clicking paste. (I tried catching the WM_PASTE message system-wide, but
that message doesn't seem to be what is being sent when copying from a Word
doc to Notepad :-{ ).

Any other thoughts/ideas?

Thanks again for replying.

Rob K
 
S

Stoitcho Goutsev \(100\)

Rob,

You can add the text under some private clipboard format.

class Program
{

[STAThread]
static void Main(string[] args)
{

Clipboard.SetData("MyFormat", "Hello World");
if (Clipboard.ContainsData("MyFormat"))
{
Console.WriteLine("Paste - {0}",
(string)Clipboard.GetData("MyFormat"));
}
Console.ReadLine();


}
}



As long as the format is private no other application will ever attempt to
get this data from the clipboard, but two instances of your application can
use it. To prevent that you can add one more piece of information to the
clipboard that will be some application ID, e.g. GUID that you create upon
application start.
Before pasting from the clipboard the application first read and check this
ID against application's own ID and if they don't match then don't paste.
 
G

Guest

Yes, this does help. Thank you Stoitcho. But one thing remains a mystery to
me. When you said:
Before pasting from the clipboard the application first read and check this
ID against application's own ID and if they don't match then don't paste.

How do I know when this event is happening?... how I know that the user is
pasting something? Once I know this, I think everything should work!

I tried your code, and it works... Notepad will not paste what the program
puts in the clipboard.

Almost there! Thanks again for helping.

Rob K
Stoitcho Goutsev (100) said:
Rob,

You can add the text under some private clipboard format.

class Program
{

[STAThread]
static void Main(string[] args)
{

Clipboard.SetData("MyFormat", "Hello World");
if (Clipboard.ContainsData("MyFormat"))
{
Console.WriteLine("Paste - {0}",
(string)Clipboard.GetData("MyFormat"));
}
Console.ReadLine();


}
}



As long as the format is private no other application will ever attempt to
get this data from the clipboard, but two instances of your application can
use it. To prevent that you can add one more piece of information to the
clipboard that will be some application ID, e.g. GUID that you create upon
application start.
Before pasting from the clipboard the application first read and check this
ID against application's own ID and if they don't match then don't paste.
 
D

Dan Manges

Rob,

You could prevent users from selecting the text. If they can't select
the text, they could still take a screenshot, but they wouldn't be able
to actually copy and paste text. May be an easier solution than working
with the clipboard.

Dan
 
S

Stoitcho Goutsev \(100\)

Rob,

Other application won't paste you private clipboard format.

What I mean is when two instances of your applications wants to paste from
the clipboard and you want to prevent one isntance to paste the text that
the other one has put there.

In this case in your application, in the code that is supposed to read data
from the clipboard you need to check first.


--

Stoitcho Goutsev (100)

RobKinney1 said:
Yes, this does help. Thank you Stoitcho. But one thing remains a mystery
to
me. When you said:
Before pasting from the clipboard the application first read and check
this
ID against application's own ID and if they don't match then don't paste.

How do I know when this event is happening?... how I know that the user is
pasting something? Once I know this, I think everything should work!

I tried your code, and it works... Notepad will not paste what the program
puts in the clipboard.

Almost there! Thanks again for helping.

Rob K
Stoitcho Goutsev (100) said:
Rob,

You can add the text under some private clipboard format.

class Program
{

[STAThread]
static void Main(string[] args)
{

Clipboard.SetData("MyFormat", "Hello World");
if (Clipboard.ContainsData("MyFormat"))
{
Console.WriteLine("Paste - {0}",
(string)Clipboard.GetData("MyFormat"));
}
Console.ReadLine();


}
}



As long as the format is private no other application will ever attempt
to
get this data from the clipboard, but two instances of your application
can
use it. To prevent that you can add one more piece of information to the
clipboard that will be some application ID, e.g. GUID that you create
upon
application start.
Before pasting from the clipboard the application first read and check
this
ID against application's own ID and if they don't match then don't paste.
 
G

Guest

Hello Dan,

Thank you for replying. I have been working part of the day on this and I
don't quite have it yet...

I have 2 instances of Word running in my app... I would like them to be able
to paste from one instance to the other at will, but not outside my app.

Looks like we are going to have to clear the clipboard when my App looses
focus since I can't figure out how to detect if they are pasting outside my
app.

Your right, they can always do a screenshot, but at least we can make it
very difficult and inconvenient for them ;~}
 
G

Guest

Stoitcho, I have been playing with this part of the day today, but it seems
when I do this, I loose Microsoft Word's proprietary clipboard format and I
can't seem to restore it. Do you know what the format for Word would be?

On message WM_DRAWCLIPBOARD:
object clipData = Clipboard.GetData("object");
Clipboard.SetData("myFormat", clipData);

.... but when it comes time to do a SetData to turn it back to its original
state, I don't know what to put in here:

// restore back to Microsoft Words format:
Clipboard.SetData("What_Goes_Here?", clipData);

Any ideas?

Thanks again for the help!

Rob K
 
S

Stoitcho Goutsev \(100\)

Rob,

I read you answer to Dan. I thought that you have an application that you
wrote and have full control over. Now I understand that you want to prevent
copying from Word.
I don't think my idea will work in this case. If you add your own stuff to
the clipboard that will definitely clear the old content. If you somehow
preserve the old content it is going to be available for paste in another
applications.
 
I

Ian Semmel

Yes, but they can still scan the bitmap to get and decode the text.

If you allow people to read your text on the screen, they will find a way to
copy it.
 
J

james.curran

Looks like we are going to have to clear the clipboard when my App looses focus since I can't figure out how to detect if they are pasting outside my app. <<<

That has it's own problem. For example, I have AppA, AppB and your app
all running. I copy something from AppA intending to copy it to AppB.
I Alt-Tab --- ooop, I go too far and activate your app. No problem, I
just ALt-Tab again to get to AppB -- and Poof -- My clipboard is empty.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top