Home > Mobile >  How can I copy something to the clipboard in C ?
How can I copy something to the clipboard in C ?

Time:10-01

How is it possible to copy something to the clipboard in c ? I have searched for a solution but I can't find any examples.

Would be great if you have a code example aswell. thank you

CodePudding user response:

An example to copy a text into it.

#include <windows.h>

int main(void)
{
    const char* str = "Text copied to clipboard.";
    const size_t len = strlen(str)   1;

    HGLOBAL hgl =  GlobalAlloc(GMEM_MOVEABLE, len);
    memcpy(GlobalLock(hgl), str, len);
    GlobalUnlock(hgl);

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

    return 0;
}

CodePudding user response:

You can, but it is OS specified task. Or just use QApplication::clipboard on Qt.

  •  Tags:  
  • c
  • Related