Strings and API calls

BlitzMax Forums/BlitzMax Programming/Strings and API calls

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 helps

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 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