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

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>
> 
> 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 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
>     -- 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
>         -- fask string peek courtesy of Al Getz
>         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>
{{{

> 
> 
> -----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     » topic index » view message » categorize

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

-- 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 message » categorize

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

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

-- 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 message » categorize

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

Sorry to tell you that Greg but the your  conclip.ew is not designed as it
should be according to MSDN.
pointers used for clipboard operation must be allocated using GlobalAlloc().
Although  conclip.ew seem to works fine, you can't certified it will in all
cases.

In November 1997 I written the following code which does essentially the same
except it use global memory.
ref: http://www.rapideuphoria.com/api_wrap.zip

-- NAME: winclip.ew
-- PURPOSE: wrapper for win32 clipboard functions.
-- DATE: November 21th, 1997
-- BY: Jacques Deschenes,  e-mail: desja at globetrotter.qc.ca
-- revision history:
--   July 29th, 1998
--    added 
--       CopyTextToClipboard()  and GetTextFromClipboard()
--       alloc_text_clip()   and peek_text_clip()

include machine.e
include dll.e
include user32.ew

-- those constant have been moved to user32.ew
--global constant
--    WM_COPY = 769,
--    WM_CUT = 768,
--    WM_PASTE  = 770, 
--    WM_ASKCBFORMATNAME = 780,
--    WM_CHANGECBCHAIN = 781,
--    WM_DESTROYCLIPBOARD = 775,
--    WM_DRAWCLIPBOARD = 776,
--    WM_HSCROLLCLIPBOARD = 782,
--    WM_PAINTCLIPBOARD = 777,
--    WM_RENDERALLFORMATS = 774,
--    WM_RENDERFORMAT = 773,
--    WM_SIZECLIPBOARD = 779,
--    WM_VSCROLLCLIPBOARD  = 778
    
global constant 
    CF_BITMAP     =  2,
    CF_DIB =  8,
    CF_PALETTE    =  9,
    CF_ENHMETAFILE =  14,
    CF_METAFILEPICT = 3,
    CF_OEMTEXT      = 7,
    CF_TEXT = 1,
    CF_UNICODETEXT  = 13,
    CF_DIF  = 5,
    CF_DSPBITMAP    = 130,
    CF_DSPENHMETAFILE       = 142,
    CF_DSPMETAFILEPICT      = 131,
    CF_DSPTEXT      = 129,
    CF_GDIOBJFIRST  = 768,
    CF_GDIOBJLAST   = 1023,
    CF_HDROP        = 15,
    CF_LOCALE       = 16,
    CF_OWNERDISPLAY = 128,
    CF_PENDATA      = 10,
    CF_PRIVATEFIRST = 512,
    CF_PRIVATELAST  = 767,
    CF_RIFF = 11,
    CF_SYLK = 4,
    CF_WAVE = 12,
    CF_TIFF = 6
 
atom 
    iChangeClipboardChain,
    iCloseClipboard,
    iCountClipboardFormats,
    iEmptyClipboard,
    iEnumClipboardFormats,
    iGetClipboardData,
    iGetClipboardFormatName,
    iGetClipboardOwner,
    iGetClipboardViewer,
    iGetOpenClipboardWindow,
    iGetPriorityClipboardFormat,
    iIsClipboardFormatAvailable,
    iOpenClipboard,
    iRegisterClipboardFormat,
    iSetClipboardData,
    iSetClipboardViewer,
    iGlobalAlloc,
    iGlobalFree,
    iDeleteMetaFile,
    iGlobalLock,
    iGlobalUnlock,
    iGlobalSize

global procedure ChangeClipboardChain(atom hwnd, atom hNextWnd)
     c_proc(iChangeClipboardChain,{hwnd, hNextWnd})
end procedure 

global procedure CloseClipboard()
    c_proc(iCloseClipboard,{})
end  procedure

global function CountClipboardFormats()
    return c_func(iCountClipboardFormats,{})
end function

global function EmptyClipboard()
    return c_func(iEmptyClipboard,{})
end function

global function EnumClipboardFormats(atom format)
    return c_func(iEnumClipboardFormats,{format})
end function

global function GetClipboardData(atom format)
    return c_func(iGetClipboardData,{format})
end function

global function GetClipboardFormatName(atom format)
atom pStr, count
sequence name

    pStr = allocate(33)
    count = c_func(iGetClipboardFormatName,{format, pStr,32})
    if count then
        name = peek({pStr,count})
        free(pStr)
        return name
    else
        free(pStr)
        return 0
    end if
end function

global function GetClipboardOwner()
    return c_func(iGetClipboardOwner,{})
end function

global function GetClipboardViewer()
    return c_func(iGetClipboardViewer,{})
end function

global function GetOpenClipboardWindow()
    return c_func(iGetOpenClipboardWindow,{})
end  function

global function GetPriorityClipboardFormat(sequence list)
atom pList, result
    pList = allocate(4*length(list))
    poke4(pList,list)
    result = c_func(iGetPriorityClipboardFormat,{pList,length(list)})
    free(pList)
    return result
end function

global function IsClipboardFormatAvailable(atom format)
    return c_func(iIsClipboardFormatAvailable,{format})
end function

global function OpenClipboard(atom hwnd)
    return c_func(iOpenClipboard,{hwnd})
end function

global function RegisterClipboardFormat(sequence formatName)
atom pStr, ok
    pStr = allocate_string(formatName)
    ok = c_func(iRegisterClipboardFormat,{pStr})
    free(pStr)
    return ok
end function 

global function SetClipboardData(atom format, atom hData)
    return c_func(iSetClipboardData,{format,hData})
end function

global function SetClipboardViewer(atom hwnd)
    return c_func(iSetClipboardViewer,{hwnd})
end function

-- global memory allocation stuff 
-- global memory is shared by all processes  (programs)
global constant -- allocation flags constants

    GMEM_FIXED      = 0,
    GMEM_MOVEABLE   = 2,
    GMEM_MODIFY = 128,
    GPTR    = 64,
    GHND    = 66,
    GMEM_DDESHARE   = 8192,
    GMEM_DISCARDABLE        = 256,
    GMEM_LOWER      = 4096,
    GMEM_NOCOMPACT  = 16,
    GMEM_NODISCARD  = 32,
    GMEM_NOT_BANKED = 4096,
    GMEM_NOTIFY     = 16384,
    GMEM_SHARE      = 8192,
    GMEM_ZEROINIT   = 64,
    GMEM_DISCARDED  = 16384,
    GMEM_INVALID_HANDLE     = 32768,
    GMEM_LOCKCOUNT  = 255,
    
    GMEM_CLIPBOARD = or_all({GMEM_MOVEABLE,GMEM_DDESHARE})

global function GlobalAlloc(atom flags, integer nBytes)
    return c_func(iGlobalAlloc,{flags,nBytes})
end function

global procedure GlobalFree(atom hmem)
atom ok
    ok = c_func(iGlobalFree,{hmem})
end procedure

global function GlobalLock(atom hGlobalMem)
    return c_func(iGlobalLock,{hGlobalMem})
end function

global procedure GlobalUnlock(atom GlobalPtr)
   c_proc(iGlobalUnlock,{GlobalPtr})
end procedure

global function GlobalSize(atom hGlobal)
  return c_func(iGlobalSize,{hGlobal})
end function

global procedure DeleteMetaFile(atom hmeta)
atom ok
    ok = c_func(iDeleteMetaFile,{hmeta})
end procedure

--------------  helper functions and procedures

global function alloc_text_clip(sequence string) -- allocate string  in global
memory
atom hGlobal, pData
  string &= 0
  hGlobal = GlobalAlloc(GMEM_CLIPBOARD,length(string))
  if not hGlobal then
    return NULL
  end if
  pData = GlobalLock(hGlobal)
  if not pData then
    GlobalFree(hGlobal)
    return NULL
  end if
  poke(pData,string)
  GlobalUnlock(hGlobal)
  return hGlobal
end function

global function peek_text_clip(atom hCF_DATA) -- peek a string  from clipboard
object
atom pData
sequence clip
integer size, zero
        
  pData = GlobalLock(hCF_DATA)
  if not pData then
    return {}
  end if
  size = GlobalSize(hCF_DATA)
  clip = peek({pData,size})
  zero = find(0,clip)
  GlobalUnlock(hCF_DATA)
  return clip[1..zero-1]
end function


global function GetTextFromClipboard(atom hOwner) -- get CF_TEXT data from
clipboard
-- return -1 if coudn't get clipboard ownership
atom ok, hClip
sequence clip
   if not OpenClipboard(hOwner) then
      return -1
   end if
   clip = {}
   if IsClipboardFormatAvailable(CF_TEXT) then
     hClip = GetClipboardData(CF_TEXT)
     clip = peek_text_clip(hClip)
   end if
   CloseClipboard()
   return clip
end function

global function CopyTextToClipboard(atom hOwner, sequence text) -- copy CF_TEXT
data to clipboard
-- return 1 if succeed else return 0
atom ok, hClip
   if not OpenClipboard(hOwner) then
      return  0
   end if
   if not EmptyClipboard() then return 0 end if
   hClip = alloc_text_clip(text)
   if not hClip then return 0 end if
   ok = SetClipboardData(CF_TEXT,hClip)
   CloseClipboard()
   return ok
end function

-----------------------------------

procedure InitWinClip()
atom user32, kernel32, gdi32

    user32 = open_dll("user32.dll")
    kernel32 = open_dll("kernel32.dll")
    gdi32 = open_dll("gdi32.dll")
    if find(0,{user32,kernel32,gdi32}) then
        puts(1,"Failed to open dll library.\n")
        abort(1)
    end if
    iChangeClipboardChain = define_c_proc(user32,"ChangeClipboardChain",
                                {C_UINT,C_UINT})
    iCloseClipboard = define_c_proc(user32,"CloseClipboard",{})
    iCountClipboardFormats = define_c_func(user32,"CountClipboardFormats",
                                        {},C_INT)
    iEmptyClipboard = define_c_func(user32,"EmptyClipboard",{},C_INT)
    iEnumClipboardFormats = define_c_func(user32,"EnumClipboardFormats",
                                    {C_UINT},C_UINT)
    iGetClipboardData = define_c_func(user32,"GetClipboardData",{C_UINT},C_UINT)
    iGetClipboardFormatName = define_c_func(user32,"GetClipboardFormatNameA",
                                {C_UINT,C_UINT,C_UINT},C_UINT)
    iGetClipboardOwner = define_c_func(user32,"GetClipboardOwner",{},C_UINT)
    iGetClipboardViewer = define_c_func(user32,"GetClipboardViewer",{},C_UINT)
    iGetOpenClipboardWindow = define_c_func(user32,"GetOpenClipboardWindow",
                                    {},C_UINT)
    iGetPriorityClipboardFormat = define_c_func(user32,
                "GetPriorityClipboardFormat",{C_UINT,C_UINT},C_UINT)
    iIsClipboardFormatAvailable = define_c_func(user32,
            "IsClipboardFormatAvailable",{C_UINT},C_INT)
    iOpenClipboard = define_c_func(user32,"OpenClipboard",{C_UINT},C_INT)
    iRegisterClipboardFormat = define_c_func(user32,
                "RegisterClipboardFormatA",{C_UINT},C_UINT)
    iSetClipboardData = define_c_func(user32,"SetClipboardData",
                                    {C_UINT,C_UINT},C_UINT)
    iSetClipboardViewer  = define_c_func(user32,"SetClipboardViewer",
                                        {C_UINT},C_UINT)
    iGlobalAlloc = define_c_func(kernel32,"GlobalAlloc",{C_INT,C_INT},C_INT)
    iGlobalFree = define_c_func(kernel32,"GlobalFree",{C_INT},C_INT)
    iDeleteMetaFile = define_c_func(gdi32,"DeleteMetaFile",{C_INT},C_INT)
    iGlobalLock = define_c_func(kernel32,"GlobalLock",{C_INT},C_INT)
    iGlobalUnlock = define_c_proc(kernel32,"GlobalUnlock",{C_INT})
    iGlobalSize = define_c_func(kernel32,"GlobalSize",{C_INT},C_INT)
    if find(-1,{
      iChangeClipboardChain,
      iCloseClipboard,
      iCountClipboardFormats,
      iEmptyClipboard,
      iEnumClipboardFormats,
      iGetClipboardData,
      iGetClipboardFormatName,
      iGetClipboardOwner,
      iGetClipboardViewer,
      iGetOpenClipboardWindow,
      iGetPriorityClipboardFormat,
      iIsClipboardFormatAvailable,
      iOpenClipboard,
      iRegisterClipboardFormat,
      iSetClipboardData,
      iSetClipboardViewer,
      iGlobalAlloc,
      iGlobalFree,
      iDeleteMetaFile,
      iGlobalLock,
      iGlobalUnlock,
      iGlobalSize
    }) then
        puts(1,"Winclip.ew initialisation failed.\n" &
                "One of the dll function was not found.\n")
    end if
end procedure

InitWinClip()


regards,
Jacqques Deschênes

new topic     » goto parent     » topic index » view message » categorize

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

jacques deschênes wrote:
> 
> 
> Sorry to tell you that Greg but the your  conclip.ew is not designed as it
> should
> be according to MSDN.
> pointers used for clipboard operation must be allocated using GlobalAlloc().
> Although  conclip.ew seem to works fine, you can't certified it will in all
> cases.
> 
> In November 1997 I written the following code which does essentially the same
> except it use global memory.
> ref: <a
> href="http://www.rapideuphoria.com/api_wrap.zip">http://www.rapideuphoria.com/api_wrap.zip</a>
> 
> }}}
<eucode>
> -- NAME: winclip.ew
> -- PURPOSE: wrapper for win32 clipboard functions.
> -- DATE: November 21th, 1997
> -- BY: Jacques Deschenes,  e-mail: desja at globetrotter.qc.ca
> -- revision history:
> --   July 29th, 1998
> --    added 
> --       CopyTextToClipboard()  and GetTextFromClipboard()
> --       alloc_text_clip()   and peek_text_clip()
> 
> include machine.e
> include dll.e
> include user32.ew
> 
> -- those constant have been moved to user32.ew
> --global constant
> --    WM_COPY = 769,
> --    WM_CUT = 768,
> --    WM_PASTE  = 770, 
> --    WM_ASKCBFORMATNAME = 780,
> --    WM_CHANGECBCHAIN = 781,
> --    WM_DESTROYCLIPBOARD = 775,
> --    WM_DRAWCLIPBOARD = 776,
> --    WM_HSCROLLCLIPBOARD = 782,
> --    WM_PAINTCLIPBOARD = 777,
> --    WM_RENDERALLFORMATS = 774,
> --    WM_RENDERFORMAT = 773,
> --    WM_SIZECLIPBOARD = 779,
> --    WM_VSCROLLCLIPBOARD  = 778
>     
> global constant 
>     CF_BITMAP     =  2,
>     CF_DIB =  8,
>     CF_PALETTE    =  9,
>     CF_ENHMETAFILE =  14,
>     CF_METAFILEPICT = 3,
>     CF_OEMTEXT      = 7,
>     CF_TEXT = 1,
>     CF_UNICODETEXT  = 13,
>     CF_DIF  = 5,
>     CF_DSPBITMAP    = 130,
>     CF_DSPENHMETAFILE       = 142,
>     CF_DSPMETAFILEPICT      = 131,
>     CF_DSPTEXT      = 129,
>     CF_GDIOBJFIRST  = 768,
>     CF_GDIOBJLAST   = 1023,
>     CF_HDROP        = 15,
>     CF_LOCALE       = 16,
>     CF_OWNERDISPLAY = 128,
>     CF_PENDATA      = 10,
>     CF_PRIVATEFIRST = 512,
>     CF_PRIVATELAST  = 767,
>     CF_RIFF = 11,
>     CF_SYLK = 4,
>     CF_WAVE = 12,
>     CF_TIFF = 6
>  
> atom 
>     iChangeClipboardChain,
>     iCloseClipboard,
>     iCountClipboardFormats,
>     iEmptyClipboard,
>     iEnumClipboardFormats,
>     iGetClipboardData,
>     iGetClipboardFormatName,
>     iGetClipboardOwner,
>     iGetClipboardViewer,
>     iGetOpenClipboardWindow,
>     iGetPriorityClipboardFormat,
>     iIsClipboardFormatAvailable,
>     iOpenClipboard,
>     iRegisterClipboardFormat,
>     iSetClipboardData,
>     iSetClipboardViewer,
>     iGlobalAlloc,
>     iGlobalFree,
>     iDeleteMetaFile,
>     iGlobalLock,
>     iGlobalUnlock,
>     iGlobalSize
> 
> global procedure ChangeClipboardChain(atom hwnd, atom hNextWnd)
>      c_proc(iChangeClipboardChain,{hwnd, hNextWnd})
> end procedure 
> 
> global procedure CloseClipboard()
>     c_proc(iCloseClipboard,{})
> end  procedure
> 
> global function CountClipboardFormats()
>     return c_func(iCountClipboardFormats,{})
> end function
> 
> global function EmptyClipboard()
>     return c_func(iEmptyClipboard,{})
> end function
> 
> global function EnumClipboardFormats(atom format)
>     return c_func(iEnumClipboardFormats,{format})
> end function
> 
> global function GetClipboardData(atom format)
>     return c_func(iGetClipboardData,{format})
> end function
> 
> global function GetClipboardFormatName(atom format)
> atom pStr, count
> sequence name
> 
>     pStr = allocate(33)
>     count = c_func(iGetClipboardFormatName,{format, pStr,32})
>     if count then
>         name = peek({pStr,count})
>         free(pStr)
>         return name
>     else
>         free(pStr)
>         return 0
>     end if
> end function
> 
> global function GetClipboardOwner()
>     return c_func(iGetClipboardOwner,{})
> end function
> 
> global function GetClipboardViewer()
>     return c_func(iGetClipboardViewer,{})
> end function
> 
> global function GetOpenClipboardWindow()
>     return c_func(iGetOpenClipboardWindow,{})
> end  function
> 
> global function GetPriorityClipboardFormat(sequence list)
> atom pList, result
>     pList = allocate(4*length(list))
>     poke4(pList,list)
>     result = c_func(iGetPriorityClipboardFormat,{pList,length(list)})
>     free(pList)
>     return result
> end function
> 
> global function IsClipboardFormatAvailable(atom format)
>     return c_func(iIsClipboardFormatAvailable,{format})
> end function
> 
> global function OpenClipboard(atom hwnd)
>     return c_func(iOpenClipboard,{hwnd})
> end function
> 
> global function RegisterClipboardFormat(sequence formatName)
> atom pStr, ok
>     pStr = allocate_string(formatName)
>     ok = c_func(iRegisterClipboardFormat,{pStr})
>     free(pStr)
>     return ok
> end function 
> 
> global function SetClipboardData(atom format, atom hData)
>     return c_func(iSetClipboardData,{format,hData})
> end function
> 
> global function SetClipboardViewer(atom hwnd)
>     return c_func(iSetClipboardViewer,{hwnd})
> end function
> 
> -- global memory allocation stuff 
> -- global memory is shared by all processes  (programs)
> global constant -- allocation flags constants
> 
>     GMEM_FIXED      = 0,
>     GMEM_MOVEABLE   = 2,
>     GMEM_MODIFY = 128,
>     GPTR    = 64,
>     GHND    = 66,
>     GMEM_DDESHARE   = 8192,
>     GMEM_DISCARDABLE        = 256,
>     GMEM_LOWER      = 4096,
>     GMEM_NOCOMPACT  = 16,
>     GMEM_NODISCARD  = 32,
>     GMEM_NOT_BANKED = 4096,
>     GMEM_NOTIFY     = 16384,
>     GMEM_SHARE      = 8192,
>     GMEM_ZEROINIT   = 64,
>     GMEM_DISCARDED  = 16384,
<snip>

Jacqques:

I already pointed the user to your code in an early post.

quote:

Download this file it has a wrapper for using clipboard in windows.


http://www.rapideuphoria.com/api_wrap.zip

end quote:

Bernie

My files in archive:
WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API 

Can be downloaded here:
http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan

new topic     » goto parent     » topic index » view message » categorize

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

Thanks for the tip! I've had this code laying around for a while and =
never
knew that! I have updated my code accordingly and posted the update on =
the
User Contributions page with thanks to you. :)

-Greg


-----Original Message-----
From: jacques desch=EAnes [mailto:guest at RapidEuphoria.com]
Sent: Tuesday, December 04, 2007 5:31 PM
To: EUforum at topica.com
Subject: RE: Copy to clipboard from a Win console program? - I'm having =
a
problem!



posted by: jacques desch=EAnes <desja at globetrot??r.net>




Sorry to tell you that Greg but the your  conclip.ew is not designed as =
it
should be according to MSDN.
pointers used for clipboard operation must be allocated using =
GlobalAlloc().
Although  conclip.ew seem to works fine, you can't certified it will in =
all
cases.

In November 1997 I written the following code which does essentially the
same except it use global memory.
ref: http://www.rapideuphoria.com/api_wrap.zip

-- NAME: winclip.ew
-- PURPOSE: wrapper for win32 clipboard functions.
-- DATE: November 21th, 1997
-- BY: Jacques Deschenes,  e-mail: desja at globetrotter.qc.ca
-- revision history:
--   July 29th, 1998
--    added=20
--       CopyTextToClipboard()  and GetTextFromClipboard()
--       alloc_text_clip()   and peek_text_clip()

include machine.e
include dll.e
include user32.ew

-- those constant have been moved to user32.ew --global constant
--    WM_COPY = 769,
--    WM_CUT = 768,
--    WM_PASTE  = 770,=20
--    WM_ASKCBFORMATNAME = 780,
--    WM_CHANGECBCHAIN = 781,
--    WM_DESTROYCLIPBOARD = 775,
--    WM_DRAWCLIPBOARD = 776,
--    WM_HSCROLLCLIPBOARD = 782,
--    WM_PAINTCLIPBOARD = 777,
--    WM_RENDERALLFORMATS = 774,
--    WM_RENDERFORMAT = 773,
--    WM_SIZECLIPBOARD = 779,
--    WM_VSCROLLCLIPBOARD  = 778
   =20
global constant=20
    CF_BITMAP     =  2,
    CF_DIB =  8,
    CF_PALETTE    =  9,
    CF_ENHMETAFILE =  14,
    CF_METAFILEPICT = 3,
    CF_OEMTEXT      = 7,
    CF_TEXT = 1,
    CF_UNICODETEXT  = 13,
    CF_DIF  = 5,
    CF_DSPBITMAP    = 130,
    CF_DSPENHMETAFILE       = 142,
    CF_DSPMETAFILEPICT      = 131,
    CF_DSPTEXT      = 129,
    CF_GDIOBJFIRST  = 768,
    CF_GDIOBJLAST   = 1023,
    CF_HDROP        = 15,
    CF_LOCALE       = 16,
    CF_OWNERDISPLAY = 128,
    CF_PENDATA      = 10,
    CF_PRIVATEFIRST = 512,
    CF_PRIVATELAST  = 767,
    CF_RIFF = 11,
    CF_SYLK = 4,
    CF_WAVE = 12,
    CF_TIFF = 6
=20
atom=20
    iChangeClipboardChain,
    iCloseClipboard,
    iCountClipboardFormats,
    iEmptyClipboard,
    iEnumClipboardFormats,
    iGetClipboardData,
    iGetClipboardFormatName,
    iGetClipboardOwner,
    iGetClipboardViewer,
    iGetOpenClipboardWindow,
    iGetPriorityClipboardFormat,
    iIsClipboardFormatAvailable,
    iOpenClipboard,
    iRegisterClipboardFormat,
    iSetClipboardData,
    iSetClipboardViewer,
    iGlobalAlloc,
    iGlobalFree,
    iDeleteMetaFile,
    iGlobalLock,
    iGlobalUnlock,
    iGlobalSize

global procedure ChangeClipboardChain(atom hwnd, atom hNextWnd)
     c_proc(iChangeClipboardChain,{hwnd, hNextWnd}) end procedure=20

global procedure CloseClipboard()
    c_proc(iCloseClipboard,{})
end  procedure

global function CountClipboardFormats()
    return c_func(iCountClipboardFormats,{}) end function

global function EmptyClipboard()
    return c_func(iEmptyClipboard,{})
end function

global function EnumClipboardFormats(atom format)
    return c_func(iEnumClipboardFormats,{format})
end function

global function GetClipboardData(atom format)
    return c_func(iGetClipboardData,{format})
end function

global function GetClipboardFormatName(atom format) atom pStr, count
sequence name

    pStr = allocate(33)
    count = c_func(iGetClipboardFormatName,{format, pStr,32})
    if count then
        name = peek({pStr,count})
        free(pStr)
        return name
    else
        free(pStr)
        return 0
    end if
end function

global function GetClipboardOwner()
    return c_func(iGetClipboardOwner,{}) end function

global function GetClipboardViewer()
    return c_func(iGetClipboardViewer,{}) end function

global function GetOpenClipboardWindow()
    return c_func(iGetOpenClipboardWindow,{})
end  function

global function GetPriorityClipboardFormat(sequence list) atom pList, =
result
    pList = allocate(4*length(list))
    poke4(pList,list)
    result = c_func(iGetPriorityClipboardFormat,{pList,length(list)})
    free(pList)
    return result
end function

global function IsClipboardFormatAvailable(atom format)
    return c_func(iIsClipboardFormatAvailable,{format})
end function

global function OpenClipboard(atom hwnd)
    return c_func(iOpenClipboard,{hwnd}) end function

global function RegisterClipboardFormat(sequence formatName) atom pStr, =
ok
    pStr = allocate_string(formatName)
    ok = c_func(iRegisterClipboardFormat,{pStr})
    free(pStr)
    return ok
end function=20

global function SetClipboardData(atom format, atom hData)
    return c_func(iSetClipboardData,{format,hData})
end function

global function SetClipboardViewer(atom hwnd)
    return c_func(iSetClipboardViewer,{hwnd})
end function

-- global memory allocation stuff
-- global memory is shared by all processes  (programs) global constant =
--
allocation flags constants

    GMEM_FIXED      = 0,
    GMEM_MOVEABLE   = 2,
    GMEM_MODIFY = 128,
    GPTR    = 64,
    GHND    = 66,
    GMEM_DDESHARE   = 8192,
    GMEM_DISCARDABLE        = 256,
    GMEM_LOWER      = 4096,
    GMEM_NOCOMPACT  = 16,
    GMEM_NODISCARD  = 32,
    GMEM_NOT_BANKED = 4096,
    GMEM_NOTIFY     = 16384,
    GMEM_SHARE      = 8192,
    GMEM_ZEROINIT   = 64,
    GMEM_DISCARDED  = 16384,
    GMEM_INVALID_HANDLE     = 32768,
    GMEM_LOCKCOUNT  = 255,
   =20
    GMEM_CLIPBOARD = or_all({GMEM_MOVEABLE,GMEM_DDESHARE})

global function GlobalAlloc(atom flags, integer nBytes)
    return c_func(iGlobalAlloc,{flags,nBytes})
end function

global procedure GlobalFree(atom hmem)
atom ok
    ok = c_func(iGlobalFree,{hmem})
end procedure

global function GlobalLock(atom hGlobalMem)
    return c_func(iGlobalLock,{hGlobalMem}) end function

global procedure GlobalUnlock(atom GlobalPtr)
   c_proc(iGlobalUnlock,{GlobalPtr})
end procedure

global function GlobalSize(atom hGlobal)
  return c_func(iGlobalSize,{hGlobal})
end function

global procedure DeleteMetaFile(atom hmeta) atom ok
    ok = c_func(iDeleteMetaFile,{hmeta}) end procedure

--------------  helper functions and procedures

global function alloc_text_clip(sequence string) -- allocate string  in
global memory atom hGlobal, pData
  string &= 0
  hGlobal = GlobalAlloc(GMEM_CLIPBOARD,length(string))
  if not hGlobal then
    return NULL
  end if
  pData = GlobalLock(hGlobal)
  if not pData then
    GlobalFree(hGlobal)
    return NULL
  end if
  poke(pData,string)
  GlobalUnlock(hGlobal)
  return hGlobal
end function

global function peek_text_clip(atom hCF_DATA) -- peek a string  from
clipboard object atom pData sequence clip integer size, zero
       =20
  pData = GlobalLock(hCF_DATA)
  if not pData then
    return {}
  end if
  size = GlobalSize(hCF_DATA)
  clip = peek({pData,size})
  zero = find(0,clip)
  GlobalUnlock(hCF_DATA)
  return clip[1..zero-1]
end function


global function GetTextFromClipboard(atom hOwner) -- get CF_TEXT data =
from
clipboard
-- return -1 if coudn't get clipboard ownership atom ok, hClip sequence =
clip
   if not OpenClipboard(hOwner) then
      return -1
   end if
   clip = {}
   if IsClipboardFormatAvailable(CF_TEXT) then
     hClip = GetClipboardData(CF_TEXT)
     clip = peek_text_clip(hClip)
   end if
   CloseClipboard()
   return clip
end function

global function CopyTextToClipboard(atom hOwner, sequence text) -- copy
CF_TEXT data to clipboard
-- return 1 if succeed else return 0
atom ok, hClip
   if not OpenClipboard(hOwner) then
      return  0
   end if
   if not EmptyClipboard() then return 0 end if
   hClip = alloc_text_clip(text)
   if not hClip then return 0 end if
   ok = SetClipboardData(CF_TEXT,hClip)
   CloseClipboard()
   return ok
end function

-----------------------------------

procedure InitWinClip()
atom user32, kernel32, gdi32

    user32 = open_dll("user32.dll")
    kernel32 = open_dll("kernel32.dll")
    gdi32 = open_dll("gdi32.dll")
    if find(0,{user32,kernel32,gdi32}) then
        puts(1,"Failed to open dll library.\n")
        abort(1)
    end if
    iChangeClipboardChain = =
define_c_proc(user32,"ChangeClipboardChain",
                                {C_UINT,C_UINT})
    iCloseClipboard = define_c_proc(user32,"CloseClipboard",{})
    iCountClipboardFormats = =
define_c_func(user32,"CountClipboardFormats",
                                        {},C_INT)
    iEmptyClipboard = define_c_func(user32,"EmptyClipboard",{},C_INT)
    iEnumClipboardFormats = =
define_c_func(user32,"EnumClipboardFormats",
                                    {C_UINT},C_UINT)
    iGetClipboardData =
define_c_func(user32,"GetClipboardData",{C_UINT},C_UINT)
    iGetClipboardFormatName =
define_c_func(user32,"GetClipboardFormatNameA",
                                {C_UINT,C_UINT,C_UINT},C_UINT)
    iGetClipboardOwner = =
define_c_func(user32,"GetClipboardOwner",{},C_UINT)
    iGetClipboardViewer =
define_c_func(user32,"GetClipboardViewer",{},C_UINT)
    iGetOpenClipboardWindow = =
define_c_func(user32,"GetOpenClipboardWindow",
                                    {},C_UINT)
    iGetPriorityClipboardFormat = define_c_func(user32,
                "GetPriorityClipboardFormat",{C_UINT,C_UINT},C_UINT)
    iIsClipboardFormatAvailable = define_c_func(user32,
            "IsClipboardFormatAvailable",{C_UINT},C_INT)
    iOpenClipboard = =
define_c_func(user32,"OpenClipboard",{C_UINT},C_INT)
    iRegisterClipboardFormat = define_c_func(user32,
                "RegisterClipboardFormatA",{C_UINT},C_UINT)
    iSetClipboardData = define_c_func(user32,"SetClipboardData",
                                    {C_UINT,C_UINT},C_UINT)
    iSetClipboardViewer  = define_c_func(user32,"SetClipboardViewer",
                                        {C_UINT},C_UINT)
    iGlobalAlloc = =
define_c_func(kernel32,"GlobalAlloc",{C_INT,C_INT},C_INT)
    iGlobalFree = define_c_func(kernel32,"GlobalFree",{C_INT},C_INT)
    iDeleteMetaFile = =
define_c_func(gdi32,"DeleteMetaFile",{C_INT},C_INT)
    iGlobalLock = define_c_func(kernel32,"GlobalLock",{C_INT},C_INT)
    iGlobalUnlock = define_c_proc(kernel32,"GlobalUnlock",{C_INT})
    iGlobalSize = define_c_func(kernel32,"GlobalSize",{C_INT},C_INT)
    if find(-1,{
      iChangeClipboardChain,
      iCloseClipboard,
      iCountClipboardFormats,
      iEmptyClipboard,
      iEnumClipboardFormats,
      iGetClipboardData,
      iGetClipboardFormatName,
      iGetClipboardOwner,
      iGetClipboardViewer,
      iGetOpenClipboardWindow,
      iGetPriorityClipboardFormat,
      iIsClipboardFormatAvailable,
      iOpenClipboard,
      iRegisterClipboardFormat,
      iSetClipboardData,
      iSetClipboardViewer,
      iGlobalAlloc,
      iGlobalFree,
      iDeleteMetaFile,
      iGlobalLock,
      iGlobalUnlock,
      iGlobalSize
    }) then
        puts(1,"Winclip.ew initialisation failed.\n" &
                "One of the dll function was not found.\n")
    end if
end procedure

InitWinClip()


regards,
Jacqques Desch=EAnes

new topic     » goto parent     » topic index » view message » categorize

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

Greg,

I see one problem with your conclip.ew (trying it on XP) it lost the last
character of the text string so I changed..
    hGlobal = c_func( xGlobalAlloc, {GMEM_CLIPBOARD, length(text)} )
to
    hGlobal = c_func( xGlobalAlloc, {GMEM_CLIPBOARD, length(text)+1} )

then all was fine...

PeteS

new topic     » goto parent     » topic index » view message » categorize

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

Ah! You're right! I forgot about the null terminator. :p

I'll update it in a little bit...

-Greg
 

-----Original Message-----
From: Pete Stoner [mailto:guest at RapidEuphoria.com] 
Sent: Friday, December 07, 2007 12:48 PM
To: EUforum at topica.com
Subject: RE: Copy to clipboard from a Win console program? - I'm having a
problem!



posted by: Pete Stoner <stoner.pete at g?ai?.com>

Greg,

I see one problem with your conclip.ew (trying it on XP) it lost the last
character of the text string so I changed..
    hGlobal = c_func( xGlobalAlloc, {GMEM_CLIPBOARD, length(text)} ) to
    hGlobal = c_func( xGlobalAlloc, {GMEM_CLIPBOARD, length(text)+1} )

then all was fine...

PeteS

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu