Skip to content

Instantly share code, notes, and snippets.

@babhishek21
Created March 21, 2018 20:57
Show Gist options
  • Save babhishek21/b0f78f57f287ca0b2611895a72aca400 to your computer and use it in GitHub Desktop.
Save babhishek21/b0f78f57f287ca0b2611895a72aca400 to your computer and use it in GitHub Desktop.
xClip (Reverse Clip) for Windows - Simulate reverse process of the clip command on Windows
/**
* xClip (Reverse Clip) for Windows
* Author: Abhishek Bhattacharya <babhishek21@yahoo.co.in>
*
* Build Environment:
* Windows 10 Version 1607 (Windows 7 or above should work fine)
* MinGW g++ (GCC) 5.3.0 or above (with flag --std=c++11)
* UPX x3.91 or above
* Runtime Environment:
* Windows 7 or above
* Build Commands:
* g++ -std=c++11 -Os -s xclip.cpp -o xclip.exe
* upx xclip.exe
*
* Usage: xclip [OPTION]
* Redirects text contents of Windows Clipboard to the standard output.
* This is essentially a "paste" replacement to the Windows CLIP command.
*
* -h, --help Displays this help message.
*
* Examples:
* xclip | type View the text contents from Windows clipboard.
* xclip > readme.txt Appends a copy of the text from Windows clipboard to the file readme.txt.
*/
//////////////////////////////////////
// xClip (Reverse Clip) for Windows //
//////////////////////////////////////
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
string getClipTextData();
inline bool isArgHelp(const char *arg);
void displayHelpContents();
int main(int argc, const char *argv[]) {
if(argc > 1 && isArgHelp(argv[1])) {
displayHelpContents();
return 0;
}
cout << getClipTextData() << endl;
return 0;
}
string getClipTextData() {
if (!IsClipboardFormatAvailable(CF_TEXT))
return string(); // error
if(!OpenClipboard(nullptr))
return string(); // error
HANDLE hData = GetClipboardData(CF_TEXT);
if(hData == nullptr)
return string(); // error
const char *ptrTxt = static_cast<const char *>(GlobalLock(hData));
string retVal(ptrTxt);
GlobalUnlock(hData);
CloseClipboard();
return retVal;
}
///////////
// Utils //
///////////
inline bool isArgHelp(const char *arg) {
return (string(arg) == "-h" || string(arg) == "--help");
}
void displayHelpContents() {
cout << "Usage: xclip [OPTION]"
"\nRedirects text contents of Windows Clipboard to the standard output."
"\nThis is essentially a \"paste\" replacement to the Windows CLIP command."
"\n\n -h, --help Displays this help message."
"\n\n Examples:"
"\n xclip | type View the text contents from Windows clipboard."
"\n xclip > readme.txt Appends a copy of the text from"
"\n Windows clipboard to the file readme.txt."
"\n\nCreated by Abhishek Bhattacharya. Report issues to: <babhishek21@yahoo.co.in>"
"\nLicensed under The MIT License (MIT). More information at: <https://opensource.org/licenses/MIT>";
}
@babhishek21
Copy link
Author

Build Instructions

Build Environment:

  • Windows 10 Version 1607 (Windows 7 or above should work fine)
  • MinGW g++ (GCC) 5.3.0 or above (with flag --std=c++11)
  • UPX x3.91 or above

Runtime Environment:

  • Windows 7 or above

Build Commands:

$ g++ -std=c++11 -Os -s xclip.cpp -o xclip.exe
$ upx xclip.exe

@babhishek21
Copy link
Author

Usage Instructions & License Information

Usage: xclip [OPTION]
Redirects text contents of Windows Clipboard to the standard output.
This is essentially a "paste" replacement to the Windows CLIP command.

  -h, --help          Displays this help message.

Examples:
xclip | type        View the text contents from Windows clipboard.
xclip > readme.txt  Appends a copy of the text from Windows clipboard to the file 'readme.txt'.


Created by Abhishek Bhattacharya. Report issues to: <babhishek21@yahoo.co.in>
Licensed under The MIT License (MIT). More information at: <https://opensource.org/licenses/MIT>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment