?
Solved

An easy one: CString to clipboard

Posted on 1998-09-07
7
Medium Priority
?
766 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 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