Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Copy Clipboard in file

Reply
Thread Tools

Re: Copy Clipboard in file

 
 
jacob navia
Guest
Posts: n/a
 
      10-04-2008
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
 
Reply With Quote
 
 
 
 
Richard Nixon
Guest
Posts: n/a
 
      10-05-2008
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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-06-2008
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-06-2008
jacob navia <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      10-06-2008
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.

 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      10-06-2008
On Oct 6, 12:48*am, Richard Nixon <(E-Mail Removed)> 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
 
Reply With Quote
 
asit
Guest
Posts: n/a
 
      10-07-2008
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 ???
 
Reply With Quote
 
Richard Nixon
Guest
Posts: n/a
 
      10-07-2008
On Sun, 05 Oct 2008 23:36:59 -0700, Keith Thompson wrote:

> jacob navia <(E-Mail Removed)> 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/...icleId=9116064
--
Richard Milhous Nixon

It is my belief that nearly any invented quotation, played with confidence,
stands a good chance to deceive.
~~ Mark Twain
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-07-2008
asit <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      10-07-2008

"asit" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clipboard - Copy Text To Clipboard Mahsha Javascript 3 06-01-2009 08:19 PM
Clipboard - Copy Image To Clipboard Mahsha Javascript 1 06-01-2009 05:06 PM
$clipboard = Gtk::Clipboard.get('PRIMARY')... under windows Dan Bishop Ruby 0 08-26-2008 01:47 PM
clipboard.setContents destroys clipboard.getContents? spunibard@gmail.com Java 2 03-02-2007 07:52 PM
Using VB.Net or C#, utilizing the clipboard object, how to copy an MS Excel graphic from the clipboard to an image control and obtain its source reference TC ASP .Net 5 09-01-2004 04:49 AM



Advertisments