Solved

An easy one: CString to clipboard

Posted on 1998-09-07
7
759 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 30 total points
ID: 1172197
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now