#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <windows.h>
size_t textToClipboard(const char *text) {
size_t length = std::strlen(text);
size_t charsCopied = 0;
if (OpenClipboard(NULL) != 0) {
if (EmptyClipboard() != 0) {
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, length+1);
if (hMem != NULL) {
void *p = GlobalLock(hMem);
if (p != NULL) {
std::memcpy(p, text, length+1);
GlobalUnlock(hMem);
if (SetClipboardData(CF_TEXT, hMem) != 0) {
charsCopied = length;
}
}
}
}
CloseClipboard();
}
return charsCopied;
}
int main() {
std::vector<char> input;
bool done = false;
while (done == false) {
int ch = std::fgetc(stdin);
if (ch != EOF) {
input.push_back((char)ch);
}
else {
done = true;
}
}
input.push_back(0);
size_t charsCopied = textToClipboard(&input[0]);
std::printf("%u char(s) read from stdin.\n", input.size()-1);
std::printf("%u char(s) copied to the clipboard\n", charsCopied);
return 0;
}
Friday, July 18, 2008
clip.cpp - A win32 clipboard utility
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment