Reputation: 45
I have a two office add-in which communicates using clipboard, for example: copy and paste table or picture from excel to word. While copy\paste operation running, user can corrupt data in clipboard if he copy something in clipboard. I tried lock clipboard after copy in first application and unlock clipboard before paste in second application.
I can lock Clipboard in one application, but can't unlock it in another application
This is class with wrappers for Clipboard
public class MyClipboard
{
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool OpenClipboard(IntPtr hwnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseClipboard();
public void Close()
{
CloseClipboard();
}
public void Open()
{
OpenClipboard(IntPtr.Zero);
}
}
First application :
MyClipboard clipboard = new MyClipboard();
clipboard.Open();
...
Second application :
MyClipboard clipboard = new MyClipboard();
clipboard.Close();
...
What should I do to Open clipboard from one application, and close clipboard from another application?
Thanks!
Upvotes: 1
Views: 2577
Reputation: 419
Rather than using the clipboard to share data between your apps (as you say the user could modify the clipboard) I'd use something like a
Here is someothers ideas.. What is the simplest method of inter-process communication between 2 C# processes?
If you really want to use the clipboard you could put in some extra custom attributes to the format to make sure that user has not used the clipboard themselves. But it seems like bad practice to use the clipboard in this way, but that's my opion.
HTH. David
Upvotes: 1