Strings and API calls
BlitzMax Forums/BlitzMax Programming/Strings and API calls
| ||
I need to pass a string to the Win32 API function 'SetClipboardData' but I'm not having much luck ... What I'm attempting to do is place a string of characters into the clipboard. I've tried various methods but the string does not arrive there. SetClipboardData wants an INT which is a handle to the string of text. In the example here I'm converting the text to a sequence of bytes and passing the results as an INT. No luck though. Any ideas how you do this? ' clipboard paste text example Extern "Win32" Function OpenClipboard%(hwnd%) Function CloseClipboard%() Function EmptyClipboard%() Function SetClipboardData%(format%,hMem%) End Extern ' ----------------------------------------------- Global txt$="Hello from BlitzMax!" Const CF_TEXT=1 If OpenClipboard(0) EmptyClipboard SetClipboardData CF_TEXT, Int(txt.ToCString()) CloseClipboard FlushMem Else RuntimeError "Failed to open Clipboard" EndIf End |
| ||
After a bit of reading it seems the memory for the clipboard must be globally allocated. Im no expert on this but the below works fine here. Hope it helpsStrict ' clipboard paste text example Extern "Win32" Const GMEM_FIXED = 0 Const CF_TEXT=1 Function OpenClipboard(hwnd%) Function CloseClipboard() Function EmptyClipboard() Function SetClipboardData(format%, hMem:Byte ptr) Function GlobalAlloc:byte ptr( uflags, bytes ) Function GlobalFree( buf:Byte Ptr ) End Extern ' ----------------------------------------------- Global txt:String="Hello from BlitzMax!" local hbuf:Byte Ptr If OpenClipboard(0) EmptyClipboard hbuf = GlobalAllocString( txt ) SetClipboardData CF_TEXT, hbuf CloseClipboard Else RuntimeError "Failed to open Clipboard" EndIf if hbuf GlobalFree( hbuf ) End Function GlobalAllocString:Byte Ptr( txt$ ) local p:byte ptr= GlobalAlloc( GMEM_FIXED , len(txt)+1 ) for local i=0 until len(txt) p[i]=txt[i] next p[len(txt)+1]=0 return p EndFunction |
| ||
Works here too. Complicated stuff hey. Thankyou. |
| ||
Awesome, thats what i tried to figure out last two days. Men, you rock both. GetClipboardData(CF_Text) returns an int? How can i use it to show clipboard data as string? |
| ||
GetClipboardData should really return a pointer to the object in the memory so then you can use String.FromCString( pointer ) to obtain the text.Strict ' clipboard paste text example Extern "Win32" Const GMEM_FIXED = 0 Const CF_TEXT=1 Function OpenClipboard(hwnd%) Function CloseClipboard() Function EmptyClipboard() Function SetClipboardData(format%, hMem:Byte ptr) Function GetClipboardData:Byte Ptr( format ) Function GlobalAlloc:byte ptr( uflags, bytes ) Function GlobalFree( buf:Byte Ptr ) End Extern ' ----------------------------------------------- Global txt:String="Hello from BlitzMax!" const write_to = 1 const read_from = 2 local status = write_to'read_from if status = write_to 'write to the clipboard local hbuf:Byte Ptr If OpenClipboard(0) EmptyClipboard hbuf = GlobalAllocString( txt ) SetClipboardData CF_TEXT, hbuf CloseClipboard Else RuntimeError "Failed to open Clipboard" EndIf if hbuf GlobalFree( hbuf ) else 'read contents of the clipboard If OpenClipboard(0) local clipdat$ = String.FromCString( GetClipboardData( CF_TEXT ) ) print clipdat endif endif End Function GlobalAllocString:Byte Ptr( txt$ ) local p:byte ptr= GlobalAlloc( GMEM_FIXED , len(txt)+1 ) for local i=0 until len(txt) p[i]=txt[i] next p[len(txt)+1]=0 return p EndFunction |
| ||
Uh? FromCString was needed.. Didnt know that command and wasted several hours shifiting pointers around. Great Job, a usefull example. |
| ||
.... |
| ||
NOTE: Most Windows API Functions that use strings expect an ASCIIZ string which has a Null terminator on the end. Enjoy |