#include once "window9.bi"

Function GetClipBoardText() As USTRING Export
	OpenClipboard(0)
	dim as any ptr hMem
	dim wsRet as USTRING
	#ifdef UNICODE
        dim as HANDLE hClip = GetClipboardData(CF_UNICODETEXT)
        if hClip = 0 then
        	CloseClipboard()
        	return wsRet
        EndIf
        dim as long iSize = GlobalSize(hClip)
		Dim wBuf As WSTRING ptr = Callocate((iSize+2)*sizeof(wstring))
		if wBuf then
			hMem = GlobalLock(hClip)
			if hMem then
				CopyMemory(wBuf, hMem, iSize)
			endif
			wsRet = *wBuf
			deallocate wBuf
		endif
	#else	
		dim as HANDLE hClip = GetClipboardData(CF_TEXT)
        if hClip = 0 then
        	CloseClipboard()
        	return wsRet
        EndIf
        dim as long iSize = GlobalSize(hClip)
		Dim sBuf As USTRING = Space(iSize)
		hMem = GlobalLock(hClip)
		if hMem then
			CopyMemory(Strptr(sBuf), hMem, iSize)
		EndIf
		wsRet = sbuf
	#EndIf
	if hMem then GlobalUnlock(hMem)
	CloseClipboard()
	return wsRet
End Function
