简体   繁体   中英

Lock / unlock clipboard from different applications

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM