Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Printing to clipboard

Name: Anonymous 2012-03-20 12:40

Greetings,

I'm looking to print/export text to the clipboard in a C++ program and this way works just fine for 1 or more lines or text:

        HGLOBAL hText;
        char *pText;
        hText = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 100);
        pText = (char*)GlobalLock(hText);
        strcpy(pText, "Example text line 1\r\nExample text line 2);
        GlobalUnlock(hText);

        OpenClipboard(NULL);
        EmptyClipboard();
        SetClipboardData(CF_TEXT, hText);
        CloseClipboard();

The problem is I've got some variables, and despite a lot of effort I cannot figure out how to print the text script + the variables inside it.

TL;DR: How do I into text + variables to clipboard in C++?

Name: Anonymous 2012-03-20 13:09

C++ doesn't have the concept of a clipboard so your question doesn't really have an answer until you specify an environment with a clipboard.

Name: Anonymous 2012-03-20 13:12

Sorry, that'd be Windows' clipboard. Forgot to mention that.

Name: Anonymous 2012-03-20 13:16

>>3
Then I don't know, I don't program for substandard platforms.

Name: Anonymous 2012-03-20 14:00

Would sprintf work if I crammed the variables into a string, then using the name of that string in strcpy(pText, stringName);?

Name: Anonymous 2012-03-20 14:59

>>5
Why wouldn't it? You print the variables into a string and then use it exactly the same way as you would any other string.

Name: Anonymous 2012-03-20 15:00

>>1
strcpy ``considered harmful'' by Microsoft. Use StringCchCopyA or strcpy_s to prevent buffer overflows.

Name: Anonymous 2012-03-20 15:05

>>6
>>7

My thanks to you both! I was testing sprintf, but due to a space in one of the variables (a string) or maybe because it was a string the application just crashes when it hits that point.

A problem with strcpy I had was that it was throwing off errors if I gave it the string made by sprintf... I have so much to learn, but your input has been very helpful!

Name: Anonymous 2012-03-20 15:27

SetClipboardDataEx(CF_TEXT, hText, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

Name: Anonymous 2012-03-20 18:59

>Microshaft Windows
>Hungarian notation

sage

Name: Dubs Guy 2012-03-22 13:50

DUBS, DUBS EVERYWHERE!

Don't change these.
Name: Email:
Entire Thread Thread List