Page 2 of 2

Re: Monitor the clipboard

Posted: Sun Jan 17, 2016 11:18 pm
by Lunasole
The monitor code based on such clipboard chain works poorly in Win7 (I have one my old program ported from XP, it cannot work stable for a lot of days as it worked on XP, for some reason it is sometimes dropped from viewers chain by that new rewritten stupid Windows subsystem, and it is so hard to track this). There is another way from new windows APIs, but complicated enough and didn't tried yet, anyway it should be used on Vista and higher instead of this

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 8:06 am
by BarryG
I was just trying Rashad's code here -> https://www.purebasic.fr/english/viewto ... 69#p480169

But it doesn't work anymore. It fails with the PeekS() line, as shown below. Yes, I've copied some rich text to the clipboard before running it.

Rashad, are you able to update it, pretty please? Hehe.

Code: Select all

CF_RTF=RegisterClipboardFormat_("Rich Text Format")

If OpenClipboard_(0)
    cbHnd = GetClipboardData_(CF_RTF)
    txtLen = lstrlen_(cbHnd)*SizeOf(character)
    cliprtf$ = Space(txtLen)   
    CopyMemory_( cliprtf$, cbHnd, txtLen)
    cliprtf$ = PeekS(@cliprtf$,txtLen,#PB_UTF8) ; <- FAILS :(
    Debug cliprt$ ; Returns an empty string due to PeekS() failing.
    cliprtf$=ReplaceString(cliprtf$,"PureBasic","PUREBASIC")
    mem = GlobalAlloc_(#GHND,lstrlen_(cliprtf$)*SizeOf(character)+1)
    lpstr = GlobalLock_(mem)
    PokeS(lpstr,cliprtf$,-1,#PB_UTF8)
    GlobalUnlock_(mem)
    EmptyClipboard_()
    SetClipboardData_(CF_RTF,mem)
    CloseClipboard_()   
    GlobalFree_(mem)
EndIf

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 10:18 am
by RASHAD
Hi BarryG
Works as expected just it is typo mistake

Code: Select all

;Debug cliprt$ ; Returns an empty string due to PeekS() failing.
Debug cliprtf$ 

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 10:22 am
by Axolotl
The code works for me, but only if a text is stored in the clipboard at the start of the program.
Otherwise the handle of GetClipboardData_(CF_RTF) is already NULL.

BTW: With PB v600 the EditorGadget() can also directly read the RTF text correctly with Ctrl+V.

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 10:27 am
by Axolotl
Works as expected just it is typo mistake

EnableExplicit
is your best friend, even in the smallest examples. :oops:

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 12:05 pm
by BarryG
Hmm, okay that incorrect variable name was just a one-off typo when I posted the question. My bad. But that's not the problem.

Debugging with the correct variable name still shows that the clipboard text wasn't copied to it with PeekS() - it returns an empty string. Look at the debug output below after I copied the rich text from WordPad and ran the code. You can clearly see the variable name is correct (it's highlighted when selected in the IDE), and that I've copied the rich text (it's showing in the Windows Clipboard history).

So, ignore my typo: the code below still isn't working.

Image

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 12:19 pm
by RASHAD
Tested with PB 5.73 x86 - x64 Windows 11 x64
Works like a charm
What is your configuration ?

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 12:45 pm
by RASHAD
Try the next snippet it's less headache

Code: Select all


CF_RTF=RegisterClipboardFormat_("Rich Text Format")

If OpenClipboard_(0)
  hMem = GetClipboardData_(CF_RTF)
  If hMem
    txtLen = GlobalSize_(hMem)
    src = GlobalLock_(hMem)
    If src
      dest = AllocateMemory(txtLen )
      If dest
          CopyMemory(src,dest,txtLen)
          Debug PeekS(dest,txtLen,#PB_UTF8)
      EndIf
      GlobalUnlock_(hMem)
    EndIf
  EndIf
  CloseClipboard_()
EndIf

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 12:47 pm
by BarryG
RASHAD wrote: Fri Jul 22, 2022 12:19 pmWhat is your configuration ?
Win 10 Pro (64-bit) with PureBasic 6.00 LTS (x64). Here's a video -> https://i.imgur.com/AdJUSdW.mp4

Your second example doesn't show anything in the Debug Output, either. Probably a PureBasic bug?

Re: Monitor the clipboard

Posted: Fri Jul 22, 2022 1:18 pm
by Axolotl
I tried this code and with text copied from wordpad returns this in the output:

Code: Select all

; EnableExplicit 

CF_RTF = RegisterClipboardFormat_("Rich Text Format")
Debug CF_RTF 
If OpenClipboard_(0) 
  hMem = GetClipboardData_(CF_RTF)
  If hMem 
Debug hMem 
    txtLen = GlobalSize_(hMem)
    src = GlobalLock_(hMem)
    If src
Debug src 
      dest = AllocateMemory(txtLen )
      If dest
Debug dest 
          CopyMemory(src,dest,txtLen)
          Debug ">> " + PeekS(dest,txtLen,#PB_UTF8)
      EndIf
      GlobalUnlock_(hMem)
    EndIf
  EndIf
  CloseClipboard_()
EndIf
; my output with clipboard rich text 
; 49292
; 4416848
; 4416848
; 77269088
; >> {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
; {\*\generator Riched20 10.0.19041}\viewkind4\uc1 
; \pard\sa200\sl276\slmult1\f0\fs22\lang7 the word \b purebasic\b0  is bold \par
; }
And no difference between ASM and C backend (v600 on Win10 Home)

Re: Monitor the clipboard

Posted: Sat Jul 23, 2022 1:00 am
by BarryG
Thanks to Axolotl's code, I worked out the problem. Seems you need to have something added to the string BEFORE what PeekS() creates for that string.

These first two examples work and show the expected output:

Code: Select all

Debug ">> " + PeekS(dest,txtLen,#PB_UTF8) ; Shows expected output

Code: Select all

text$ = ">> " + PeekS(dest,txtLen,#PB_UTF8)
Debug text$ ; Shows expected output
But these two don't work and show nothing in the Debug Output window:

Code: Select all

Debug PeekS(dest,txtLen,#PB_UTF8) ; Shows nothing

Code: Select all

text$ = PeekS(dest,txtLen,#PB_UTF8)
Debug text$ ; Shows nothing
Is this a PureBasic bug, maybe?

Re: Monitor the clipboard

Posted: Sat Jul 23, 2022 11:01 am
by Axolotl
That's weird.
I confirm your observation with #PB_Compiler_Backend = #PB_Backend_C only once. (I probably made a mistake in the test steps.)
I can change my code to

Code: Select all

Debug PeekS(dest,txtLen,#PB_UTF8) ; Shows expected output -- worked on both backends at my computer ????  
BTW: I use the automatically convertion from integer to string if there is a string at the beginning. that's why I like to use a "" at the beginning in debug. For many outputs debug "VarName = " + VarName is also quite helpful.