Re: Copy Clipboard in file

Discussion in 'C Programming' started by jacob navia, Oct 4, 2008.

  1. jacob navia

    jacob navia Guest

    Over wrote:
    > Hi
    >
    > it's possible with language C, (Mingw or Djgpp)
    > copying the clipboard of WinXp into file.txt?
    >
    > which function must be use.
    >
    > Thanks
    >
    >
    >


    The clipboard supports several formats. You can retrieve images, text,
    or many other types of data. Let’s see how to retrieve the most simple
    one: text.
    // This function will retrieve the text from the clipboard
    // if available. Returns the retrieved text or NULL if an
    // error occurs.
    // The user should free the text with the free() function when it is
    // done with it.
    char *ImportClipboard(HWND hwnd)
    {
    HANDLE hClipData;
    char *str = NULL;
    char * result = NULL;
    result = 0;
    if (IsClipboardFormatAvailable(CF_TEXT)) {
    if (OpenClipboard(hwnd)) {
    hClipData = GetClipboardData(CF_TEXT);
    if (hClipData) {
    str = GlobalLock(hClipData);
    result = strdup(str);
    GlobalUnlock(hClipData);
    }
    }
    CloseClipboard();
    }
    return (result);
    }

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Oct 4, 2008
    #1
    1. Advertising

  2. On Sat, 04 Oct 2008 11:17:56 +0200, jacob navia wrote:

    > The clipboard supports several formats. You can retrieve images, text,
    > or many other types of data. Let¢s see how to retrieve the most simple
    > one: text.
    > // This function will retrieve the text from the clipboard
    > // if available. Returns the retrieved text or NULL if an
    > // error occurs.
    > // The user should free the text with the free() function when it is
    > // done with it.
    > char *ImportClipboard(HWND hwnd)
    > {
    > HANDLE hClipData;
    > char *str = NULL;
    > char * result = NULL;
    > result = 0;
    > if (IsClipboardFormatAvailable(CF_TEXT)) {
    > if (OpenClipboard(hwnd)) {
    > hClipData = GetClipboardData(CF_TEXT);
    > if (hClipData) {
    > str = GlobalLock(hClipData);
    > result = strdup(str);
    > GlobalUnlock(hClipData);
    > }
    > }
    > CloseClipboard();
    > }
    > return (result);
    > }


    Jacob--

    I've never seen a treatment of the clipboard that didn't have the full
    trappings of MFC. I had a couple questions, and I'll try to keep them as
    topical as they can be.

    1) If things like CF_TEXT were given meaning somewhere earlier, along with
    functions like GetClipboardData(), could this be standard C? These would
    normally find defintions in windows.h (?), but instaed of including a
    non-standard header, couldn't you write the above as C99?

    2) I can't get my head around char *ImportClipboard(HWND hwnd) . How do
    you call this using C instead of the OS distributing a message.
    --
    Richard Milhous Nixon

    All humor is derrived from pain, ergo nothing in Heaven is funny.
    ~~ Mark Twain
    Richard Nixon, Oct 6, 2008
    #2
    1. Advertising

  3. jacob navia

    jacob navia Guest

    Richard Nixon wrote:
    > On Sat, 04 Oct 2008 11:17:56 +0200, jacob navia wrote:
    >
    >> The clipboard supports several formats. You can retrieve images, text,
    >> or many other types of data. Let¢s see how to retrieve the most simple
    >> one: text.
    >> // This function will retrieve the text from the clipboard
    >> // if available. Returns the retrieved text or NULL if an
    >> // error occurs.
    >> // The user should free the text with the free() function when it is
    >> // done with it.
    >> char *ImportClipboard(HWND hwnd)
    >> {
    >> HANDLE hClipData;
    >> char *str = NULL;
    >> char * result = NULL;
    >> result = 0;
    >> if (IsClipboardFormatAvailable(CF_TEXT)) {
    >> if (OpenClipboard(hwnd)) {
    >> hClipData = GetClipboardData(CF_TEXT);
    >> if (hClipData) {
    >> str = GlobalLock(hClipData);
    >> result = strdup(str);
    >> GlobalUnlock(hClipData);
    >> }
    >> }
    >> CloseClipboard();
    >> }
    >> return (result);
    >> }

    >
    > Jacob--
    >
    > I've never seen a treatment of the clipboard that didn't have the full
    > trappings of MFC.


    You can use the windows API directly, without going through MFC that
    will eventually call the windows API anyway.


    > I had a couple questions, and I'll try to keep them as
    > topical as they can be.
    >
    > 1) If things like CF_TEXT were given meaning somewhere earlier, along with
    > functions like GetClipboardData(), could this be standard C?


    You can
    #define CF_TEXT 1

    but rewriting GetClipboardData() would mean to reimplement
    the clipboard, i.e. to write ALL the windows API for the
    Clipboard.

    This is (of course) doable, since Microsoft did it in C. But it
    would mean that you write a NEW clipboard, that would not be
    recognized by other applications that use the windows clipboard.

    One of the BIG problems under linux is precisely that each
    program implements its own clipboard and programs do not
    exchange clipboard data unless they happen to use the same
    clipboard!


    > These would
    > normally find defintions in windows.h (?), but instaed of including a
    > non-standard header, couldn't you write the above as C99?
    >


    It *is* C99. Note that a clipboard is associated to a window that
    is the owner of the clipboard. That is why the hwnd parameter is
    necessary.

    > 2) I can't get my head around char *ImportClipboard(HWND hwnd) . How do
    > you call this using C instead of the OS distributing a message.


    There is no "message" here. The window is needed to determine
    which application owns the clipboard.

    A work around is to just use the GetCurrentWindow() API but then,
    depending on which window has the focus, you would get a window
    that maybe doesn't even belong to your application.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Oct 6, 2008
    #3
  4. jacob navia <> writes:
    > Richard Nixon wrote:

    [...]
    >> These would
    >> normally find defintions in windows.h (?), but instaed of including a
    >> non-standard header, couldn't you write the above as C99?

    >
    > It *is* C99. Note that a clipboard is associated to a window that
    > is the owner of the clipboard. That is why the hwnd parameter is
    > necessary.


    He asked about using C99 *instead of* including a non-standard header.

    C99 itself has no clipboard support. To use the clipboard under
    Windows, you need to use Windows-specific extensions. The experts on
    the topic hang out in comp.os.ms-windows.programmer.win32. jacob,
    shouldn't you have mentined that yourself?

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 6, 2008
    #4
  5. On 6 Oct 2008 at 5:36, jacob navia wrote:
    > One of the BIG problems under linux is precisely that each program
    > implements its own clipboard and programs do not exchange clipboard
    > data unless they happen to use the same clipboard!


    That's not really true in practice.

    *Most* programs choose to support the X convention of using the PRIMARY
    atom for currently-selected text, and the CLIPBOARD atom for
    Windows-style cut/copy/paste menus. Some programs and GUI toolkits also
    have their own clipboard arrangements.
    Antoninus Twink, Oct 6, 2008
    #5
  6. jacob navia

    Bart Guest

    On Oct 6, 12:48 am, Richard Nixon <> wrote:
    > On Sat, 04 Oct 2008 11:17:56 +0200, jacob navia wrote:


    > > char *ImportClipboard(HWND hwnd)


    > I've never seen a treatment of the clipboard that didn't have the full
    > trappings of MFC.  I had a couple questions, and I'll try to keep them as
    > topical as they can be.
    >
    > 1) If things like CF_TEXT were given meaning somewhere earlier, along with
    > functions like GetClipboardData(), could this be standard C?  These would
    > normally find defintions in windows.h (?), but instaed of including a
    > non-standard header, couldn't you write the above as C99?
    >
    > 2)  I can't get my head around char *ImportClipboard(HWND hwnd) .  How do
    > you call this using C instead of the OS distributing a message.


    These functions simply exist in a DLL file (although this is Windows-
    specific library), I think USER32.DLL. Names like CF_TEXT are just
    constants.

    Given suitable Docs, you can declare your own versions of
    ImportClipboard, CF_TEXT etc, without using windows.h. But you might
    find you need to use non-standard features to declare such functions,
    due to different call conventions, etc.

    --
    Bartc
    Bart, Oct 6, 2008
    #6
  7. jacob navia

    asit Guest

    All these codes work perfectly,but if I want to detect whether there
    is a change in the clip board content , how can I know ???
    asit, Oct 7, 2008
    #7
  8. On Sun, 05 Oct 2008 23:36:59 -0700, Keith Thompson wrote:

    > jacob navia <> writes:
    >> Richard Nixon wrote:

    > [...]
    >>> These would
    >>> normally find defintions in windows.h (?), but instaed of including a
    >>> non-standard header, couldn't you write the above as C99?

    >>
    >> It *is* C99. Note that a clipboard is associated to a window that
    >> is the owner of the clipboard. That is why the hwnd parameter is
    >> necessary.

    >
    > He asked about using C99 *instead of* including a non-standard header.
    >
    > C99 itself has no clipboard support. To use the clipboard under
    > Windows, you need to use Windows-specific extensions. The experts on
    > the topic hang out in comp.os.ms-windows.programmer.win32. jacob,
    > shouldn't you have mentined that yourself?
    >
    > [...]


    I hadn't had good luck with ng's that looked like the above (there are
    hundreds), but this seems like a viable group.

    Indeed, the fella got his clipboard question answered there.

    An interesting article on usenet:
    http://www.computerworld.com/action/article.do?command=viewArticleBasic&articleId=9116064
    --
    Richard Milhous Nixon

    It is my belief that nearly any invented quotation, played with confidence,
    stands a good chance to deceive.
    ~~ Mark Twain
    Richard Nixon, Oct 7, 2008
    #8
  9. asit <> writes:
    > All these codes work perfectly,but if I want to detect whether there
    > is a change in the clip board content , how can I know ???


    All *what* codes work perfectly?

    When you post a followup, please quote enough context so that your
    article makes sense to someone who hasn't seen the article to which
    you're replying. (Some newsreaders let you see the parent article,
    but (a) it can be inconvenient, and (b) I just tried it, and my
    newsreader was unable to fetch the parent article.)

    As I think you've already been told, standard C does not include any
    support for clipboards; any such support is specific to your C
    implementation or to your operating system. If I recall correctly,
    you're using MS Windows, so your best bet is probably to post to
    comp.os.ms-windows.programmer.win32.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 7, 2008
    #9
  10. jacob navia

    Bartc Guest

    "asit" <> wrote in message
    news:...
    > All these codes work perfectly,but if I want to detect whether there
    > is a change in the clip board content , how can I know ???


    Copy the current text contents of the clipboard, as a string.

    Next time, compare that string with the current contents (bearing in mind
    that the clipboard data can also be non-text).

    This is not very demanding C.

    If you need a /notification/ of a change, you might want to look at
    www.msdn.com or ask in topical group (.win32?).

    --
    Bartc
    Bartc, Oct 7, 2008
    #10
  11. jacob navia

    CBFalconer Guest

    asit wrote:
    >
    > All these codes work perfectly,but if I want to detect whether
    > there is a change in the clip board content, how can I know ???


    If you want to post a followup via groups.google.com, ensure
    you quote enough for the article to make sense. Google is only
    an interface to Usenet; it's not Usenet itself. Don't assume
    your readers can, or ever will, see any previous articles.

    More details at: <http://cfaj.freeshell.org/google/>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Oct 8, 2008
    #11
  12. CBFalconer <> writes:
    > asit wrote:
    >>
    >> All these codes work perfectly,but if I want to detect whether
    >> there is a change in the clip board content, how can I know ???

    >
    > If you want to post a followup via groups.google.com, ensure
    > you quote enough for the article to make sense. Google is only
    > an interface to Usenet; it's not Usenet itself. Don't assume
    > your readers can, or ever will, see any previous articles.
    >
    > More details at: <http://cfaj.freeshell.org/google/>


    I told him essentially the same thing (though without the URL) more
    than 8 hours before your followup.

    I encourage you to find a way to read Usenet that allows you to check
    for other followups before posting. The details are, of course,
    off-topic.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 8, 2008
    #12
  13. On Sun, 5 Oct 2008 17:48:26 -0600, Richard Nixon
    <> wrote:

    > On Sat, 04 Oct 2008 11:17:56 +0200, jacob navia wrote:

    <snip: accessing Windows clipboard>

    Windows itself is offtopic here, but the general concepts of accessing
    'system' (OS, layered, etc.) code I consider to be sufficiently on.

    > I've never seen a treatment of the clipboard that didn't have the full
    > trappings of MFC. I had a couple questions, and I'll try to keep them as
    > topical as they can be.
    >
    > 1) If things like CF_TEXT were given meaning somewhere earlier, along with
    > functions like GetClipboardData(), could this be standard C? These would
    > normally find defintions in windows.h (?), but instaed of including a
    > non-standard header, couldn't you write the above as C99?
    >

    Constants like CF_TEXT, and some simple macros, would be defined in
    windows.h (or some variant or piece of it), but routines would only be
    _declared_ there, and _defined_ (implemented) as object code in a
    library -- for WinAPI routines specifically, a DLL such as gdi32.dll .

    You could write the first three of these in standard C (or at least
    standard C with minor extensions like __stdcall) yourself, if you
    wanted. But they would be of no use without the callable routine(s),
    and you won't be able to write those in standard C, at least not
    entirely, because they depend on accessing OS features. Moreover the
    parts you can write yourself are of no value -- even negative value --
    if they aren't consistent with the callable routines, so it is a
    Really Good Idea to use the .h file(s) supplied (at least originally)
    by the same people who supplied the routines -- in this case, MS.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Oct 13, 2008
    #13
  14. In article <>,
    David Thompson <> wrote:
    ....
    >You could write the first three of these in standard C (or at least
    >standard C with minor extensions like __stdcall) yourself, if you
    >wanted. But they would be of no use without the callable routine(s),
    >and you won't be able to write those in standard C, at least not
    >entirely, because they depend on accessing OS features. Moreover the
    >parts you can write yourself are of no value -- even negative value --
    >if they aren't consistent with the callable routines, so it is a
    >Really Good Idea to use the .h file(s) supplied (at least originally)
    >by the same people who supplied the routines -- in this case, MS.


    No, no, no. You've got it all wrong.

    It is established CLC dogma that it is a Good Thing, that adds value to
    the world, to write your own versions of OS functions, so that they be
    now done in conforming, religiously, pedantically, dogmatically,
    CLC-correct form.

    For example, see RH's approved version of sleep().
    The only version of sleep() that he has ever used (and, of course, the
    only version you should ever use - if you want to be a member of the
    He-Man CLC society).
    Kenny McCormack, Oct 13, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    766
  2. Dan Bishop
    Replies:
    0
    Views:
    233
    Dan Bishop
    Aug 26, 2008
  3. Replies:
    4
    Views:
    266
    Randy Webb
    Dec 14, 2004
  4. Mahsha

    Clipboard - Copy Image To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    1
    Views:
    580
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
  5. Mahsha

    Clipboard - Copy Text To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    3
    Views:
    648
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
Loading...

Share This Page