0

My copy code:

if OpenClipboard(mainwnd.Handle) then
  MemHandle := GlobalAlloc(GHND or GMEM_SHARE, Succ(StrLen(pLclCopies)));
  if MemHandle <> 0 Then
   Begin
    try 
     StrCopy(GlobalLock(MemHandle), pLclCopies);
     GlobalUnlock(MemHandle);
     SetClipboardData(cf_LocalVar,MemHandle);
    Finally
     CloseClipboard;
     GlobalFree(MemHandle);
    end;
   end;

and my paste code:

if clipboard.HasFormat(cf_LocalVar) then
  begin
   ClipBoard.Open;
   try
    MyHandle := Clipboard.GetAsHandle(cf_LocalVar);
    LocalsTextPtr := GlobalLock(MyHandle);
    CheckForCopiedLocals(LocalsTextPtr, TextPtr); //What I do with the pasted data.
    GlobalUnLock(MyHandle);
   finally
    Clipboard.Close;
   end;
  end;

My goal is to copy not only text from a special editor in my program, but also to copy some underlying variable data related to that editor. Most everything seems to be working fine clipboard wise - I'm seeing my copied text, and the 'cf_LocalVar' format appear in the ClipBook viewer on windows.

It's when I get to the paste side and the line LocalsTextPtr := GlobalLock(MyHandle); doesn't get the copied data from the first bit of code. I see that it makes it into pLclCopies but then can't be sure that it's stored in the clipboard.

NB I have left out emptyclipboard from my code as this would get rid of the cf_text that I need along with the cf_LocalVar.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Chucky
  • 1,701
  • 7
  • 28
  • 62
  • Call `Clipboard.Open()` before calling `Clipboard.HasFormat()`, not after. Also, add error handling. `GlobalLock()`, `SetClipboardData()`, and `Clipboard.GetAsHandle()` can all potentially fail. – Remy Lebeau Jan 20 '14 at 16:32
  • I've moved `code`Clipboard.Open() but still not getting any data from LocalsTextPtr. I'll add some error handling like you say to try and narrow the problem down. – Chucky Jan 20 '14 at 16:53
  • Is `GetAsHandle()` or `GlobalLock()` returning zero? What is `pLclCopies` and `LocalsTextPtr` declared as, and what does the content of `pLclCopies` look like? – Remy Lebeau Jan 20 '14 at 17:28
  • 1
    The complete lack of error checking is the first problem. Why did you decide not to include it? Unless I am mistaken, you are not Chuck Norris. – David Heffernan Jan 20 '14 at 19:37
  • On the copying side, why use `OpenClipboard` and `CloseClipboard` rather than the `TClipboard` methods? And you need to try blocks. Don't skimp on try blocks? You may as well not bother with them at all if you won't do them properly. If `MemHandle = 0` then you never close the clipboard. – David Heffernan Jan 20 '14 at 19:41
  • For the call to `GlobalAlloc`, you only need `GMEM_MOVEABLE`. That's it. – David Heffernan Jan 20 '14 at 19:46
  • You'll have to forgive me for overlooking some of these standards. I've been using this guide which omits them: http://delphi.about.com/od/windowsshellapi/a/clipboard_spy_3.htm – Chucky Jan 23 '14 at 09:57
  • I'm also not understanding the difference between the TClipboard procedures and the Open and Close ones the guide advises. Which to use and why? – Chucky Jan 23 '14 at 09:58

0 Answers0