-- conclip.ew v0.3 -- Win32 Console Clipboard Routines -- by Greg Haberek -- -- This is a simple library for making use of the Windows -- clipboard in your Win32 console applications. -- -- Routines: -- -- SetClipboardText( sequence text ) -- text = GetClipboardText() -- -- Version History: -- -- v0.3 Added calls to GlobalAlloc, etc. as per MSDN (and jacques deschênes) -- v0.2 Added call to EmptyClipboard prior to setting data -- v0.1 Initial release include dll.e include machine.e constant kernel32 = open_dll( "kernel32.dll" ) constant user32 = open_dll( "user32.dll" ) constant xGetConsoleWindow = define_c_func( kernel32, "GetConsoleWindow", {}, C_ULONG ) constant xGlobalAlloc = define_c_func( kernel32, "GlobalAlloc", {C_UINT, C_LONG}, C_POINTER ) constant xGlobalFree = define_c_func( kernel32, "GlobalFree", {C_POINTER}, C_POINTER ) constant xGlobalSize = define_c_func( kernel32, "GlobalSize", {C_POINTER}, C_LONG ) constant xGlobalLock = define_c_func( kernel32, "GlobalLock", {C_POINTER}, C_POINTER ) constant xGlobalUnlock = define_c_func( kernel32, "GlobalUnlock", {C_POINTER}, C_POINTER ) constant xOpenClipboard = define_c_func( user32, "OpenClipboard", {C_ULONG}, C_UINT ) constant xCloseClipboard = define_c_func( user32, "CloseClipboard", {}, C_UINT ) constant xEmptyClipboard = define_c_func( user32, "EmptyClipboard", {}, C_UINT ) constant xSetClipboardData = define_c_func( user32, "SetClipboardData", {C_UINT,C_LONG}, C_LONG ) constant xGetClipboardData = define_c_func( user32, "GetClipboardData", {C_UINT}, C_LONG ) constant xIsClipboardFormatAvailable = define_c_func( user32, "IsClipboardFormatAvailable", {C_UINT}, C_UINT ) constant CF_TEXT = 1 constant GMEM_MOVEABLE = 2 constant GMEM_DDESHARE = 8192 constant GMEM_CLIPBOARD = or_bits(GMEM_MOVEABLE, GMEM_DDESHARE) object junk global procedure SetClipboardText( sequence text ) atom hCon, hGlobal, pData, hData -- get Console handle hCon = c_func( xGetConsoleWindow, {} ) -- open the clipboard if not c_func( xOpenClipboard, {hCon} ) then puts( 2, "Error opening clipboard.\n" ) return end if -- empty the clipboard if not c_func( xEmptyClipboard, {} ) then puts( 2, "Error emptying clipboard.\n" ) return end if -- allocate some memory hGlobal = c_func( xGlobalAlloc, {GMEM_CLIPBOARD, length(text)} ) if not hGlobal then puts( 2, "Error allocating memory.\n" ) junk = c_func( xCloseClipboard, {} ) return end if -- lock the memory pData = c_func( xGlobalLock, {hGlobal} ) if not pData then puts( 2, "Error locking memory.\n" ) junk = c_func( xGlobalFree, {hGlobal} ) junk = c_func( xCloseClipboard, {} ) return end if -- put the string in memory poke( pData, text ) -- unlock the memory junk = c_func( xGlobalUnlock, {hGlobal} ) -- paste the text hData = c_func( xSetClipboardData, {CF_TEXT, pData} ) if hData = 0 then puts( 2, "Error pasting to clipboard.\n" ) end if -- free the text if c_func( xGlobalFree, {hGlobal} ) then puts( 2, "Error freeing memory.\n" ) end if -- close the clipbard if not c_func( xCloseClipboard, {} ) then puts( 2, "Error closing clipboard.\n" ) end if end procedure global function GetClipboardText() atom hCon, hGlobal, pData, size, pos sequence text -- start with empty text text = "" -- get Console handle hCon = c_func( xGetConsoleWindow, {} ) -- open the clipboard if not c_func( xOpenClipboard, {hCon} ) then puts( 2, "Error opening clipboard.\n" ) return text end if -- check for data if c_func( xIsClipboardFormatAvailable, {CF_TEXT} ) then -- get the data hGlobal = c_func( xGetClipboardData, {CF_TEXT} ) if not hGlobal then puts( 2, "Error getting data.\n" ) junk = c_func( xCloseClipboard, {} ) return text end if -- lock memory pData = c_func( xGlobalLock, {hGlobal} ) if not pData then puts( 2, "Error locking memory.\n" ) junk = c_func( xCloseClipboard, {} ) return text end if -- get size size = c_func( xGlobalSize, {hGlobal} ) -- get the text text = peek({ pData, size }) -- trim the text pos = find( 0, text ) if pos then text = text[1..pos-1] end if -- unlock memory junk = c_func( xGlobalUnlock, {hGlobal} ) end if -- close the clipbard if not c_func( xCloseClipboard, {} ) then puts( 2, "Error closing clipboard.\n" ) end if return text end function