Copy to clipboard

Sep 22, 2013 at 11:28pm
Hello,
So I'm trying to use copy to clipboard function in order to copy a string to the clipboard. The problem is to create another string after the first string has been copied using cin.get();

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void clipBoard()
{
	const char* output = "Hello!";
	const size_t len = strlen(output) + 1;
	HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
	memcpy(GlobalLock(hMem), output, len);
	GlobalUnlock(hMem);
	OpenClipboard(0);
	EmptyClipboard();
	SetClipboardData(CF_TEXT, hMem);
	CloseClipboard();
	cout << "'" << output << "'" << " has been successfully copied\n";
	cin.get()
        //Right here I want another string to be copied to clipboard after a button has been pressed due to 'cin.get();'
}


I appreciate everyone's support.

- IBCFQ
Last edited on Sep 23, 2013 at 12:22pm
Sep 23, 2013 at 12:20pm
Bump.
Sep 23, 2013 at 2:10pm
Do you want to append the second string to your current content or do you want to overwrite the data previously saved to the clipboard?
Sep 23, 2013 at 5:22pm
Thanks for your response, I want to practically make another string (const char*) to be copied on clipboard once you press a key.

For example:
After Hello being copied, Hello 2 would be copied after pressing a key.
Sep 23, 2013 at 5:54pm
I'm still not sure what the interpretation of that is. Is the existing contents of the clipboard to be replaced by the new string, or appended to it?

"Hello" - first string
" 2" - second string
append second string to first, result = "Hello 2"
Or - is the second string "Hello 2" and the result to be "HelloHello 2".

Lets say
"Hello" - first string
"There" - second string
What is the required contents of the clipboard after copying the second string, please.
Sep 23, 2013 at 6:05pm
No, that's what I'm not trying to do.

The program runs >> Copies to clipboard 'Hello'. Once you press a key because of cin.get(); Hello has to be cleared from clipboard than another string will be copied for example: Bye

So >> Copy first first string which is 'Hello'
After you press a button clear hello from clipboard
Copy 'Bye' to clipboard.

Understood : >?
Last edited on Sep 23, 2013 at 6:06pm
Sep 23, 2013 at 6:09pm
Thank you, that's clear. I see now that the answer to the question put by Computergeeuk01 is: overwrite.
Sep 23, 2013 at 6:12pm
No need to thank me, I should thank you :)! I'l try to overwrite the variable output to another char text, If I can't figure it out can you help me? Thanks a lot for your help.
Sep 23, 2013 at 6:50pm
Still can't figure out a way..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void clipBoard(const char* output = "Hello")
{	
	while(true)
	{
	const size_t len = strlen(output) + 1;
	HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
	memcpy(GlobalLock(hMem), output, len);
	GlobalUnlock(hMem);
	OpenClipboard(0);
	EmptyClipboard();
	SetClipboardData(CF_TEXT, hMem);
	CloseClipboard();	
	cout << "'" << output << "'" << " has been successfully copied\n";
	cin.get();
	output = "Bye";
	}
}
Sep 23, 2013 at 7:05pm
I would prefer to keep the function clipBoard() as simple as possible, so that it does just one thing - it copies the supplied string to the clipboard.

Then put any other logic in the code which calls that function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void clipBoard(const char* output);

int main()
{
    string output = "Hello";
    
    clipBoard(output.c_str());
    cout << "'" << output << "'" << " has been successfully copied\n";
    cin.get();
    output = "Bye";
    clipBoard(output.c_str());
    cout << "'" << output << "'" << " has been successfully copied\n";
    cin.get();
	
    return 0;
}

void clipBoard(const char* output )
{	
    const size_t len = strlen(output) + 1;
    HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
    memcpy(GlobalLock(hMem), output, len);
    GlobalUnlock(hMem);
    OpenClipboard(0);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, hMem);
    CloseClipboard();	
}
Sep 23, 2013 at 7:14pm
Thanks! Never thought of that solution! I guess I am a spaghetti coder haha.
If I may ask, what's c_str?
Sep 23, 2013 at 7:28pm
what's c_str?

That's a way to get the c-string equivalent of a std::string.
http://www.cplusplus.com/reference/string/string/c_str/

A simpler approach might be to change the function definition to use a std::string as its parameter then the function could just be called like this: clipBoard(output);

Though that requires a couple of minor changes to the function:
1
2
3
4
5
6
7
8
9
10
11
void clipBoard(const std::string & output)
{	
    const size_t len = output.size() + 1;
    HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
    memcpy(GlobalLock(hMem), output.c_str(), len);
    GlobalUnlock(hMem);
    OpenClipboard(0);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, hMem);
    CloseClipboard();	
}
Topic archived. No new replies allowed.