?
Solved

An easy one: CString to clipboard

Posted on 1998-09-07
7
Medium Priority
?
768 Views
Last Modified: 2008-02-01
Here's an easy 30 points for you experts:

I need to put a CString on the clipboard (without using OLE, if possible) , but I can't find the appropriate syntax.
The code's like this:

      CString str="I'm on the clipboard!!!";
      OpenClipboard();
      EmptyClipboard();
      SetClipboardData( CF_TEXT ??, hData ??? ) // This line is the problem
      CloseClipboard();

Does anybody know how to do this? (Please reply with code...)
Thanks!
Melissa
0
Comment
Question by:MelissaC
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:Andy_Keys
ID: 1172191
From the help On SetClipboard I would try somthing like
HANDLE MemHandle;
char *memptr
unsigned int StrLen;
StrLen = (str.GetLength()+1) * sizeof( TCHAR );
MemHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, StrLen);
memptr = GlobalLock( MemHandle );
ASSERT( memptr );
memset( memptr, 0, StrLen );
memcpy( memptr, (char*)(LPCTSTR)str );
GlobalUnlock( MemHandle )
SetClipboardText( CF_TEXT, MemHandle );


0
 
LVL 23

Expert Comment

by:chensu
ID: 1172192
GlobalAlloc() a memory block with the GMEM_MOVEABLE and GMEM_DDESHARE flags. GlobalLock() it and CopyMemory() the CString into the memory and GlobalUnlock() it. Then pass the handle returned by GlobalAlloc() as hData.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1172193
Oops, I didn't see Andy Keys's comment. He provided you with the code. You may reject mine and accept Andy Keys's.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:chensu
ID: 1172194
0
 
LVL 23

Expert Comment

by:chensu
ID: 1172195
Regarding Andy Keys's code, StrLen = (str.GetLength()+1) * sizeof( TCHAR );

I think * sizeof( TCHAR ) is not needed because CString::GetLength returns a count of the bytes in the string rather than a count of the characters.
0
 
LVL 2

Author Comment

by:MelissaC
ID: 1172196
Sorry, Chensu. You're real helpful but I needed some code to insert in my program. I tried Andy Keys' code and it worked (with some alterations). Andy, if you propose an answer with your code, you've gained 30 points!
Thanks both of you!
0
 
LVL 1

Accepted Solution

by:
Andy_Keys earned 90 total points
ID: 1172197
Thanks
0

Featured Post

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!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

807 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