?
Solved

Copy RTF (Rich Text Format) UNICODE string to clipboard

Posted on 2005-05-04
14
Medium Priority
?
1,409 Views
Last Modified: 2013-11-20
I have an UNICODE String in RTF format like this:

TCHAR szRTF[100];
_tcscpy(szRTF, _T("{\\rtf1{\\fonttbl{\\f0\\fnil Arial;}}\r\n\\f0\\fs24 ABC}"));

The above string means a string (ABC) in Word or Wordpad with Font Face "Arial" and Font Size 12 (fs24/2).

In the real case, the string is not ABC, but an UNICODE string, e.g ASCII 256, ASCII 197...

I want to copy these string to clipboard using the following method:

int l=_tcslen(szRTF) * sizeof(TCHAR);
HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, l);
OpenClipboard(NULL);
EmptyClipboard();
LPTSTR lptstrCopy = GlobalLock(hglbCopy); //I got error here
memcpy(lptstrCopy, szRTF, l);
GlobalUnlock(hglbCopy);
SetClipboardData(::RegisterClipboardFormat(CF_RTF), hglbCopy);
CloseClipboard();

However, I got error in line LPTSTR lptstrCopy = GlobalLock(hglbCopy); because GlobalLock return lpvoid. I tried to change this line into:
LPVOID lptstrCopy = GlobalLock(hglbCopy);  and paste clipboard data to WordPad. I can only get the first letter "{".

I also tried to convert the UNICODE string to ANSI using T2A(szRTF) but I am afraid that this will destory my UNICODE string. Need you help.



0
Comment
Question by:iProgram
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13925533
HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, l);

does this return a valid result?

from help
Return Values
If the function succeeds, the return value is a handle to the newly allocated memory object.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
If the heap does not contain sufficient free space to satisfy the request, GlobalAlloc returns NULL. Because NULL is used to indicate an error, virtual address zero is never allocated. It is, therefore, easy to detect the use of a NULL pointer.

0
 
LVL 1

Author Comment

by:iProgram
ID: 13926779
Of cause.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13926985
You should try to

HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, l);
OpenClipboard(NULL);
EmptyClipboard();
LPTSTR lptstrCopy = (LPTSTR) GlobalLock(hglbCopy);
memcpy(lptstrCopy, szRTF, l);
SetClipboardDataW(::RegisterClipboardFormat(CF_RTF), hglbCopy); // use UNICODE call explicitly
CloseClipboard();
GlobalUnlock(hglbCopy);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:iProgram
ID: 13927552
jkr, do you mean the key is SetClipboardDataW? Can't SetClipboardData switch to SetClipboardDataW automatically for me?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13928637
Actually, it should. But note that I moved the 'GlobalUnlock()' call from *before* 'SetClipboardDate()' to after 'CloseClipboard()'.
0
 
LVL 1

Author Comment

by:iProgram
ID: 13932315
Why you move 'GlobalUnlock()' line? I copied above code from msdn, btw.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13932343
Since IMHO you were doint that too early.
0
 
LVL 1

Author Comment

by:iProgram
ID: 13933192
error C2065: 'SetClipboardDataW' : undeclared identifier
0
 
LVL 1

Author Comment

by:iProgram
ID: 13933217
I just checked winuser.h and there is no SetClipboardData, SetClipboardDataA & SetClipboardDataW defined in it. VC6.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 13933359
Erm - your original question said it fails BEFORE the alteration jkr suggests.  Where does it actually fail?
0
 
LVL 1

Author Comment

by:iProgram
ID: 13934365
Ok I know the answer. CF_RTF does not support UNICODE at all. so there is no SetClipboardDataW. For UNICODE char, we have to "Encode" them first. For example:
TCHAR c=161; This char should be encoded to \'a1. And this is the correct method to convert UBICODE to ANSI without losing data.
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16368756
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question