RE: Copy to clipboard from a Win console program? - I'm having a problem!

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

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

-- 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


-----Original Message-----
From: Robert Sch=E4chter [mailto:guest at RapidEuphoria.com]
Sent: Tuesday, December 04, 2007 6:03 AM
To: EUforum at topica.com
Subject: RE: Copy to clipboard from a Win console program? - I'm having =
a
problem!



posted by: Robert Sch=E4chter <drs at drs.??>

Greg Haberek wrote:
>
>
> A Win32 console application can still use the standard Windows API = =

> clipboard functions. I whipped up some quick code for using the
> clipboard with the console. Enjoy!
>
> -Greg
>
>
> }}}
<eucode>
> -- conclip.ew
> -- Win32 Console Clipboard Routines
> -- by Greg Haberek <ghaberek at gmail.com>
>=20
> include dll.e
> include machine.e
>=20
> constant kernel32 = open_dll( "kernel32.dll" ) constant=20
> xGetConsoleWindow = define_c_func( kernel32, = "GetConsoleWindow", =
{},=20
> C_ULONG ) constant xlstrlen = define_c_func( kernel32, "lstrlen",=20
> {C_POINTER}, = C_INT )
>=20
> constant user32 = open_dll( "user32.dll" ) constant xOpenClipboard =
==20
> define_c_func( user32, "OpenClipboard", = {C_ULONG}, C_UINT ) =
constant=20
> xCloseClipboard = define_c_func( user32, "CloseClipboard", = {},=20
> C_UINT ) constant xSetClipboardData = define_c_func( user32, ==20
> "SetClipboardData", {C_UINT,C_LONG}, C_LONG ) constant=20
> xGetClipboardData = define_c_func( user32, = "GetClipboardData",=20
> {C_UINT}, C_LONG )
>=20
> constant CF_TEXT = 1
>=20
> global procedure SetClipboardText( sequence text )
>=20
>     atom hCon, pText, hData
>    =20
>     -- get console handle
>     hCon = c_func( xGetConsoleWindow, {} )
>=20
>     -- open the clipboard
>     if not c_func( xOpenClipboard, {hCon} ) then
>         puts( 2, "Error opening clipboard.\n" )
>         return
>     end if
>    =20
>     -- allocate the text
>     pText = allocate_string( text )
>=20
>     -- 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
>=20
> end procedure
>=20
> global function GetClipboardText()
>=20
>     atom hCon, hData
>     sequence text
>    =20
>     -- get console handle
>     hCon = c_func( xGetConsoleWindow, {} )
>=20
>     -- open the clipboard
>     if not c_func( xOpenClipboard, {hCon} ) then
>         puts( 2, "Error opening clipboard.\n" )
>         return text
>     end if
>=20
>     -- start with empty text   =20
>     text = ""
>=20
>     -- copy the text
>     hData = c_func( xGetClipboardData, {CF_TEXT} )
>     if hData != 0 then
>         -- fask string peek courtesy of Al Getz
>         text = peek({ hData, c_func(xlstrlen, {hData}) })
>     end if
>=20
>     -- close the clipbard
>     if not c_func( xCloseClipboard, {} ) then
>         puts( 2, "Error closing clipboard.\n" )
>     end if
>=20
>     return text
> end function
> </eucode>
{{{

>
>
> -----Original Message-----
> From: Robert Sch=E4chter [mailto:guest at RapidEuphoria.com]
> Sent: Monday, December 03, 2007 7:26 AM
> To: EUforum at topica.com
> Subject: Copy to clipboard from a Win console program?
>
>
> posted by: Robert Sch=E4chter <drs at ?r?.at>
>
> Hi,
>
> is there an easy way to copy a plain text string to the clipboard from =

> = an Euphoria console program?
>
> I found clipbrd.e by Jacques Deschenes which would just be what I =
> needed, except it doesn't seem to work (I get a "dos_interrupt() is
> not = supported in Euphoria for WIN32" error)
>
> Thank you!
> Robert
>


Sorry, I just found out there is a problem: it works only when the =
clipboard
is empty.

When there is any content in the clipboard, SetClipboardText is executed
without error, but it doesn't overwrite the current clipboard content.

I tried to add a routine to clear the clipboard, but all I got was an =
error.

I tried to use the EmptyClipboard() routine from WINCLIP.EW, but it =
didn't
work.

So, can you help me once more, please?

Thank you,
Robert

P.S.:
my failed attempt at an "empty clipboard" routine:

constant xEmptyClipboard = define_c_func( user32, "EmptyClipboard", =
{},
C_UINT )

...

global procedure EmptyClipb() -- RS
    if not c_func( xEmptyClipboard, {} ) then
        puts( 2, "Error emptying clipboard.\n" )
        return
    end if
end procedure

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

Search



Quick Links

User menu

Not signed in.

Misc Menu