Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Clipboard Issue

Clipboard Issue

Scheduled Pinned Locked Moved Clever Code
helpdebuggingquestion
7 Posts 3 Posters 22 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #1

    Not so subtle. This code is an extremely simple form of the code which caused the bug. The code works without any issues in most circumstances.

    void CopyToClipboard(CString str)
    {
    HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE,
    (str.Length() + 1) * sizeof(TCHAR));
    if (hglb == NULL)
    return;

    LPTSTR lptstr = GlobalLock(hglb);
    memcpy(lptstr, static_cast<LPCTSTR>(str),
    (str.Length() + 1) * sizeof(TCHAR));

    GlobalUnlock(hglb);

    // Place the handle on the clipboard.
    SetClipboardData(CF_TEXT, hglb);
    }

    What broke the code?


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

    R 1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      Not so subtle. This code is an extremely simple form of the code which caused the bug. The code works without any issues in most circumstances.

      void CopyToClipboard(CString str)
      {
      HGLOBAL hglb = GlobalAlloc(GMEM_MOVEABLE,
      (str.Length() + 1) * sizeof(TCHAR));
      if (hglb == NULL)
      return;

      LPTSTR lptstr = GlobalLock(hglb);
      memcpy(lptstr, static_cast<LPCTSTR>(str),
      (str.Length() + 1) * sizeof(TCHAR));

      GlobalUnlock(hglb);

      // Place the handle on the clipboard.
      SetClipboardData(CF_TEXT, hglb);
      }

      What broke the code?


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      Rama Krishna Vavilala wrote:

      What broke the code?

      A Unicode build? CF_TEXT is ANSI text only.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      R PJ ArendsP 2 Replies Last reply
      0
      • R Ryan Binns

        Rama Krishna Vavilala wrote:

        What broke the code?

        A Unicode build? CF_TEXT is ANSI text only.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        Exactly. It was embarrassing when I found that in a beta demo.


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

        R 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          Exactly. It was embarrassing when I found that in a beta demo.


          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          Rama Krishna Vavilala wrote:

          It was embarrassing when I found that in a beta demo.

          Don't be too embarrassed. I'm sure many people have done that before :)

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          1 Reply Last reply
          0
          • R Ryan Binns

            Rama Krishna Vavilala wrote:

            What broke the code?

            A Unicode build? CF_TEXT is ANSI text only.

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            I looked at that too, but I was under the impression that UNICODE apps would automagically translate CF_TEXT to CF_UNICODETEXT. Maybe that is only when reading the clipboard:doh:

            Within you lies the power for good; Use it!

            R R 2 Replies Last reply
            0
            • PJ ArendsP PJ Arends

              I looked at that too, but I was under the impression that UNICODE apps would automagically translate CF_TEXT to CF_UNICODETEXT. Maybe that is only when reading the clipboard:doh:

              R Offline
              R Offline
              Rama Krishna Vavilala
              wrote on last edited by
              #6

              If that happened automatically there will be lot of problems. For example cutting and pasting between an Unicode app and a ANSI app. So you have to either implement CF_TEXT only or both CF_TEXT and CF_UNICODETEXT


              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

              1 Reply Last reply
              0
              • PJ ArendsP PJ Arends

                I looked at that too, but I was under the impression that UNICODE apps would automagically translate CF_TEXT to CF_UNICODETEXT. Maybe that is only when reading the clipboard:doh:

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #7

                PJ Arends wrote:

                I looked at that too, but I was under the impression that UNICODE apps would automagically translate CF_TEXT to CF_UNICODETEXT.

                Nope. I have set up a macro that #defines another constant to be one or the other depending on whether I'm compiling for Unicode, but it's not done automatically. I can't remember about reading, but I think you still have to specify it. Remember that you can copy both to the clipboard, so both are available at any time.

                Ryan

                "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups