Solved

An easy one: CString to clipboard

Posted on 1998-09-07
7
758 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 23

Expert Comment

by:chensu
Comment Utility
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

10 Experts available now in Live!

Get 1:1 Help Now