locked
SetClipboardData and CF_UNICODETEXT causes heap corruption error RRS feed

  • Question

  • An MFC application with Unicode strings running under Windows 7 and compiled with VC10.  I reuse working code to copy text to the clipboard from prior applications that used multi-byte character strings.  Text buffer is created and filled correctly.  It has a size equal to two times (the number of characters in the string + 1).  However SetClipboardData causes a heap corruption error.  The only change made from prior code was to change CF_TEXT to CF_UNICODETEXT.  Memory allocation code uses sizeof (TCHAR).  Do any other changes have to be made?  Thanks.

    Monday, January 17, 2011 5:52 PM

Answers

  • When using Unicode text data format, each string is terminated with a CR/LF pair...did you add to their length for that? (BTW, that is two additional characters, plus a terminating NULL character.)
    • Edited by jinzai Monday, January 17, 2011 6:43 PM typo
    • Marked as answer by lucy-liu Tuesday, January 25, 2011 6:11 AM
    Monday, January 17, 2011 6:43 PM
  • when you use unicode chars remember that each char has 2 bytes so when you allocate buffer string length+1 ii can be not enough becouse

    when you allocate buffer in bytes you allocate not enough space  you should allocate  your string length *2 plus if your string  is ended with CR/LF is 4 more bytes plus one byte for NULL character added at the end of each string

    • Marked as answer by lucy-liu Tuesday, January 25, 2011 6:11 AM
    Monday, January 17, 2011 8:01 PM

All replies

  • When using Unicode text data format, each string is terminated with a CR/LF pair...did you add to their length for that? (BTW, that is two additional characters, plus a terminating NULL character.)
    • Edited by jinzai Monday, January 17, 2011 6:43 PM typo
    • Marked as answer by lucy-liu Tuesday, January 25, 2011 6:11 AM
    Monday, January 17, 2011 6:43 PM
  • when you use unicode chars remember that each char has 2 bytes so when you allocate buffer string length+1 ii can be not enough becouse

    when you allocate buffer in bytes you allocate not enough space  you should allocate  your string length *2 plus if your string  is ended with CR/LF is 4 more bytes plus one byte for NULL character added at the end of each string

    • Marked as answer by lucy-liu Tuesday, January 25, 2011 6:11 AM
    Monday, January 17, 2011 8:01 PM