-
-
Save Lyoko-Jeremie/c7913fe825188092669d21ddce54f8e9 to your computer and use it in GitHub Desktop.
xClip (Reverse Clip) for Windows - Simulate reverse process of the clip command on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* xClip (Reverse Clip) for Windows | |
* Author: Abhishek Bhattacharya <[email protected]> | |
* | |
* 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: <[email protected]>" | |
"\nLicensed 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