Clipboard

Blitz3D Forums/Blitz3D Beginners Area/Clipboard

REDi(Posted 2004) [#1]
hi all

is it possible to access the windows clipboard?


Hansie(Posted 2004) [#2]
@Cliff

try and have a look here : http://www.blitzbasic.com/codearcs/codearcs.php?code=699


REDi(Posted 2004) [#3]
Thanx mate


Darkuni(Posted 2004) [#4]
I'm going to post this to each message that discusses the clipboard and references this code snippet because I've spent HOURS banging my head on why it was causing crashes, and since I have it figured out, I figured I better share it.

The clipboard requires proper opening and closing, just like any other 'stream' in the Windows world. The OS does NOT control the 'locking' or 'unlocking' of this stream, so its ENTIRELY possible to have COLLISIONS in requests for the clipboard.

The OPENCLIPBOARD command used in the sample isn't checked to be SURE that the clipboard was opened correctly. Because of that, it allows EXAMINECLIPBOARD to execute EVEN THOUGH YOU DON'T HAVE A HANDLE TO IT!

This will blow up your Blitz program - and the debugger won't help you at all.

I know what you're thinking .. what are the CHANCES of clipboard collision? BETTER THAN YOU THINK. If you're polling it every loop, you WILL eventually collide with another program using the clipboard.

What sucks is, you don't have to USE the clipboard for a collision to occur. Seems many programs (like IE) does SOMETHING with the clipboard when you close it. So, your program is happily polling away ... and you close IE ... and BOOM, your program blows up.

My first solution was to poll LESS often, say every half a second. My crash rate went down DRAMATICALLY, but didn't end .. just made it harder to pin down.

Finally I did some research on the clipboard API and figured out that OPENCLIPBOARD will return a true/false to let you know what the status is on the open command. From there, of course, you can control whether or not you actually perform another command that will crash the program or not.

Here is some slightly revised code that compensates for that.

Once this code was in place, my crashes stopped. :) GOD I hope I save someone the hours of headache I spent on this.

Function ReadClipboardText$()
Local cb_TEXT=1
Local txt$=""
If OpenClipboard(0) Then
If ExamineClipboard(cb_TEXT)
txt$=GetClipboardData$(cb_TEXT)
EndIf
CloseClipboard
End If
Return txt$
End Function


Kanati(Posted 2004) [#5]
I'm probably going to add clipboard functionality to my printer userlib too so that will be another option. :)


REDi(Posted 2004) [#6]
Thanx Darkuni it works a treat now :)


soja(Posted 2004) [#7]
Thanks for figuring that out, Shane. I was wondering if that could be the case, but I thought to myself, "no, it's not a problem; I'm not using the clipboard anywhere else!" It's good to know not to take this for granted.


TeraBit(Posted 2004) [#8]
Thanks Shane. I was just starting to get a headache with this one!


EOF(Posted 2004) [#9]
Hi Shane,
Thankyou for finding that potential collision problem. I have updated the code snippet.
Sorry for causing you all that 'head banging'. I'll send you some headache pills :-/

Clipboard - Text Copy & Paste *Updated*


Darkuni(Posted 2004) [#10]
Glad I could give back! No asprin required. You got me started ... I'm just glad I could do it - it GREATLY enhanced my app ... oh, which is public now, BTW ...

http://www.hostsfileeditor.com

Thanks to everyone.


Wiebo(Posted 2004) [#11]
heh. I use notepad for that.