#include once "window9.bi"

#ifdef UNICODE
	Function SetClipboardFile(byref sFile As USTRING) As Integer export
		Dim iLen As Integer
		iLen = Len(sFile)
		if iLen = 0 then return FALSE
		dim as UByte ptr pPatternDest = callocate((iLen+2)*2)
		if pPatternDest = 0 then return FALSE
		dim iIndex as Long	
		dim as UByte ptr pPatternString = cast(Byte ptr , *sFile)	
		for i as Long = 0 to iLen*2		
			if pPatternString[i] = &h7C then			
				pPatternString[i] = 0			
			EndIf		
			pPatternDest[iIndex] = pPatternString[i]		
			iIndex +=1		
		Next
		If OpenClipboard(0) Then
			EmptyClipboard()
			Dim df As DROPFILES Ptr
			Dim hGlobal As HGLOBAL
			hGlobal = GlobalAlloc(GMEM_SHARE+GMEM_MOVEABLE+GMEM_ZEROINIT, SizeOf(DROPFILES) + (iLen+1)*2 + 2)
			If hGlobal<>0 Then
				df = GlobalLock(hGlobal)
				df->pFiles = SizeOf(DROPFILES)
				df->fWide = 1
				MoveMemory(Cast(Byte Ptr,df) + SizeOf(DROPFILES), pPatternDest, (iLen)*2)
				GlobalUnlock(hGlobal)
				If SetClipboardData(CF_HDROP, hGlobal) Then
					GlobalFree(hGlobal)
					Function = TRUE
				EndIf
			EndIf
			CloseClipboard()
		EndIf
		if pPatternDest then deallocate(pPatternDest)
	End Function
#else
	Function SetClipboardFile(byref sFile As String) As Integer export
		Dim iLen As Integer
		iLen = Len(sFile)
		if iLen = 0 then return FALSE
		If OpenClipboard(0) Then
			EmptyClipboard()
			Dim df As DROPFILES Ptr
			Dim hGlobal As HGLOBAL
			hGlobal = GlobalAlloc(GMEM_SHARE+GMEM_MOVEABLE+GMEM_ZEROINIT, SizeOf(DROPFILES) + iLen + 2)
			If hGlobal<>0 Then
				df = GlobalLock(hGlobal)
				df->pFiles = SizeOf(DROPFILES)
				MoveMemory(Cast(Byte Ptr,df) + SizeOf(DROPFILES), SAdd(sFile), iLen )
				GlobalUnlock(hGlobal)
				If SetClipboardData(CF_HDROP, hGlobal) Then
					GlobalFree(hGlobal)
					Function = TRUE
				EndIf
			EndIf
			CloseClipboard()
		EndIf
	End Function
#EndIf
