Wrapping DLLs in Euphoria (was: [OT] Swap RGB)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Pete Lomax wrote:

> Ricardo Forno wrote:

<snip>

>> One thing that recently I wondered how is it done is how some programs
>> intercept the Ctrl-C call to the clipboard, and then manage to store
>> the clipboard contents in a file. Can this be done with some system dll?
>
> JL says he can do, I wouldn't mind seeing this as well.

- Run the following program.
- Switch to any other programs, and copy _text_ to the clipboard
  by using CTRL+C one or more times.
- Press ESC to terminate this program.

BTW: There should be a built-in fast peek_string() function in Euphoria.
include dll.e
include machine.e
include misc.e

--===========================[ Utilities ]==============================
-- Windows API wrappers

constant CF_TEXT = 1

atom User32
integer OpenClipboard_, GetClipboardData_, CloseClipboard_, GetAsyncKeyState_

if platform() = WIN32 then
   User32 = open_dll("user32.dll")
   OpenClipboard_    = define_c_func(User32, "OpenClipboard", {C_LONG}, C_LONG)
GetClipboardData_ = define_c_func(User32, "GetClipboardData", {C_LONG},
   C_LONG)
   CloseClipboard_   = define_c_func(User32, "CloseClipboard", {}, C_LONG)
GetAsyncKeyState_ = define_c_func(User32, "GetAsyncKeyState", {C_LONG},
   C_SHORT)
end if

------------------------------------------------------------------------
-- Other utilities

constant SL_CODE = allocate(26)

poke(SL_CODE, {
     -- After the routine has pushed 4 bytes on the stack, first int
     -- argument is at stack offset +8.
     #57,                        -- push   edi
     #8B, #7C, #24, #08,         -- mov    edi, [esp+8]
     #B9, #FF, #FF, #FF, #FF,    -- mov    ecx, #FFFFFFFF
     #B0, #00,                   -- mov    al, 0
     #FC,                        -- cld
     #F2, #AE,                   -- repne  scasb
     #B8, #FE, #FF, #FF, #FF,    -- mov    eax, #FFFFFFFE
     #29, #C8,                   -- sub    eax, ecx
     #5F,                        -- pop    edi
     #C2, #04, #00               -- ret    4    -- pop 4 bytes off the stack
   })

constant STRLEN = define_c_func("", SL_CODE, {C_POINTER}, C_ULONG)

-- Usage:
-- len = c_func(STRLEN, {addr})
--
-- 'addr' is a pointer to the first character of a null-terminated
-- string in memory;
-- returns the length of the string in bytes (not including the
-- terminating null character); 
-- works like the Windows API function LstrLen(), but is cross-platform


function peek_string (atom addr)
   -- read a null-terminated string, starting at address 'addr' in memory
   return peek({addr, c_func(STRLEN,{addr})})   
end function


--=======================[ Desired functions ]==========================

global function clipboard_text()
   sequence ret
   atom lpszText, void

   ret = ""
   if c_func(OpenClipboard_, {NULL}) then
      lpszText = c_func(GetClipboardData_, {CF_TEXT})
      if lpszText then
         ret = peek_string(lpszText)
      end if
      void = c_func(CloseClipboard_, {})
   end if
   return ret
end function

global function is_key_down (atom key)
   return and_bits(c_func(GetAsyncKeyState_, {key}), #8000) != 0
end function


--==============================[ Demo ]================================

without warning

constant
   VK_ESCAPE  = #1B,
   VK_CONTROL = #11,
   VK_C       = #43

sequence filename
integer fn

filename = "collection.txt"

fn = open(filename, "w")
if fn = -1 then
   printf(1, "Can't open '%s'.", {filename})
   if getc(0) then end if
   abort(1)
end if

while not is_key_down(VK_ESCAPE) do          -- ESC terminates the program
   if is_key_down(VK_CONTROL)
   and is_key_down(VK_C) then                -- Ctrl+C
      sleep(1)
      puts(fn, clipboard_text() & "\n")
   end if
end while
close(fn)

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu