0

This is a great forum, I have learned many things here. I have never posted in all these year, or asked a single question. I learned PHP from forums and became very good at it. I am now learning C++ and I'm quite surprised how much more code it takes to accomplish the same simple goals that a 20 character piece of PHP code can do. But it's been fun to learn and I'm really enjoying it. I have searched this forum and Google, and can't find anything on this issue I'm having. It's a mystery that makes no sense to me, but I'm a beginner with C++ and desktop application development. Perhaps I'm searching for the wrong phrases, but I can't find anything on this.

I have created a program that copies clipboard contents into a text file when the user presses ctrl+c . I spent several nights learning about this and got that to work. But it does something strange that I can't figure out.

I am creating a Windows GUI I am using Code::Blocks with standard complier. I have windows 7

My function compiles and works totally fine. But.... if I use ctrl+c on a web page in a browser, it prints the last sting, not the current content being copied. Yet this same program while running, will write the correct current highlighted content in notepad or any other application. Only in web browsers it does this where it writes the last copied data. In notepad the same keys, same function, same program, while it's still running even... will write the correct data to the text file.

How is it possible that it works 100% and shows no warnings or errors when compiled, but in web page browsers it writes old data ? If I use ctrl+c twice in a row on a web page, it then will write 2 times to the text file, first being the old content from last time I copied, and then the correct highlighted content on the web page. But it works perfect in notpad or wordpad or any of those applications.

What is going on here ? I'm so new to C++ I am not getting it. But, I can't find any questions here that seem to be same issue on windows. any help is greatly appreciated. I have been coming here for years, and never ask any questions, just read read read.... but this one's got me stumped and I'm too new to C++ to have a clue what's going on here.

// copy clipboard when user types left ctrl and c at same time.
            if (key == 0x43){

      if( GetAsyncKeyState( 0xA2 ) )
            {

  if (!OpenClipboard(NULL))
HANDLE h = GetClipboardData(CF_TEXT);

// variables f and k as clipboard dump beginning and ending
LPCSTR f = "[COPY TO CLIPBOARD]";
LPCSTR k = "[COPY TO CLIPBOARD ENDS]";

printf("%s\n", (char *)h); // prints clipboard into console

// prints clipboard content to the text output file sandwiched by f and k
fprintf(output,"%s", (char *)f);
fprintf(output,"%s",(char *)h);
fprintf(output,"%s",(char *)k);



  CloseClipboard();


            }
}

Any clue why I get last copied data on web sites but not in notpad ?

thanks in advance.

Owen Delahoy
  • 806
  • 6
  • 22
CodeSponge
  • 27
  • 6
  • 1
    You are never locking to get the actual text pointer. Please use the pattern found in this SO question: http://stackoverflow.com/questions/14762456/getclipboarddatacf-text – Tim Biegeleisen Dec 17 '16 at 11:23
  • Thanks Tim, I'm reading it now. But why does it work fine in notepad or wordpad when I use ctrl+c ? it writes the correct data. only web pages ? – CodeSponge Dec 17 '16 at 11:48
  • Undefined behavior means anything can happen. The reason why it works when you use it wrong is uninteresting, probably complicated, and very implementation-dependent. – Cody Gray - on strike Dec 17 '16 at 12:15
  • ok, I added globallock but still doesn't get the right content when on web pages. – CodeSponge Dec 17 '16 at 12:43
  • ` HANDLE h = GetClipboardData(CF_TEXT); // Lock the handle to get the actual text pointer static_cast( GlobalLock(h)); // delaring variables f and k as clipboard dump beginning and ending LPCSTR f = "[COPY TO CLIPBOARD]"; LPCSTR k = "[COPY TO CLIPBOARD ENDS]"; // printf("%s\n", (char *)h); // dumps clipboard into console // prints character to the text output file sandwiched by f and k fprintf(output,"%s", (char *)f); fprintf(output,"%s",(char *)h); fprintf(output,"%s",(char *)k); // Release the lock GlobalUnlock( h ); CloseClipboard(); } }` – CodeSponge Dec 17 '16 at 12:48
  • doesn't seem to let me post code in replies or edit a post with code in it. :-\ – CodeSponge Dec 17 '16 at 13:04
  • There's no error checking at all. And the life story is not helpful. There are countless examples on the web. We don't need more. – David Heffernan Dec 17 '16 at 13:28
  • as I said, I'm a begginer. and.... didn't give you my life story, just some background so jerks like you won't think I don't read stuff on here first before asking a qestion.... which is WHAT THIS FORUM IS FOR ! and NO I have not found any help on this, only post from arrogant jerks like you telling other people to "look it up" I have. for days. and the answer I got here was only a "direction" to look in. not a real helpful answer. so if you think there's tons of posts on this exact problem, please post one. – CodeSponge Dec 18 '16 at 08:29
  • and.... I was saying thanks to this forum for the all the helpful post I've read over the years.... if you don't like my post.... don't read it, and don't comment. – CodeSponge Dec 18 '16 at 08:31
  • I have read the other post, and I don't get it. I've tried implimenting that into my simple program, but again, not getting it... and no help from guys like you. – CodeSponge Dec 18 '16 at 08:45
  • I find it annoying how arrogant C++ programmers are, especially here. You know the answer, and mock me for not undersanding or sharing some background here on the forum instead of providing any help at all. – CodeSponge Dec 18 '16 at 08:47

0 Answers0