1. RE: Copy to clipboard from a Win console program? - problem solved!

Greg Haberek wrote:
> 
> 
> I have submitted this to the User Contributions page.
> 
> -Greg=20
> 
> -----Original Message-----
> From: Greg Haberek [mailto:ghaberek at gmail.com]=20
> Sent: Tuesday, December 04, 2007 8:35 AM
> To: EUforum at topica.com
> Subject: RE: Copy to clipboard from a Win console program? - I'm having =
> a
> problem!
> 
> 
> IIRC, you have to open the clipboard before doing anything with it,
> including emptying it. Here's the modified code. Not the call to
> EmptyClipboard *after* opening it and before setting data. I tested it
> several times and it seems to work.
> 
> -Greg
> 
> }}}
<eucode>
> -- conclip.ew v0.2
> -- Win32 Console Clipboard Routines
> -- by Greg Haberek <ghaberek at gmail.com>
> 
> include dll.e
> include machine.e
> 
> constant kernel32 = open_dll( "kernel32.dll" ) constant =
> xGetConsoleWindow =
> define_c_func( kernel32, "GetConsoleWindow", {}, C_ULONG ) constant =
> xlstrlen
> = define_c_func( kernel32, "lstrlen", {C_POINTER}, C_INT )
> 
> constant user32 = open_dll( "user32.dll" ) 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 CF_TEXT = 1
> 
> global procedure SetClipboardText( sequence text )
> 
>     atom hCon, pText, hData
>    =20
>     -- 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
>    =20
>     -- empty the clipboard
>     if not c_func( xEmptyClipboard, {} ) then
>         puts( 2, "Error emptying clipboard.\n" )
>         return
>     end if
>    =20
>     -- allocate the text
>     pText = allocate_string( text )
> 
>     -- paste the text
>     hData = c_func( xSetClipboardData, {CF_TEXT, pText} )
>     if hData = 0 then
>         puts( 2, "Error pasting to clipboard.\n" )
>     end if
>    =20
>     -- free the text
>     free( pText )
>    =20
>     -- close the clipbard
>     if not c_func( xCloseClipboard, {} ) then
>         puts( 2, "Error closing clipboard.\n" )
>     end if
> 
> end procedure
> 
> global function GetClipboardText()
> 
>     atom hCon, hData
>     sequence text
>    =20
>     -- 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
> 
>     -- start with empty text   =20
>     text = ""
> 
>     -- copy the text
>     hData = c_func( xGetClipboardData, {CF_TEXT} )
>     if hData != 0 then
>         text = peek({ hData, c_func(xlstrlen, {hData}) })
>     end if
> 
>     -- close the clipbard
>     if not c_func( xCloseClipboard, {} ) then
>         puts( 2, "Error closing clipboard.\n" )
>     end if
> 
>     return text
> end function
> </eucode>
{{{


(snip)


YES!

Thank you, again!

Robert

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu