-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
45 lines (32 loc) · 1.02 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
static const int kCmdBufSize = 1024;
HWND GetConsoleHwnd();
void CheckError(BOOL error_condition, const char* msg) {
if (error_condition) {
fprintf(stderr, "%s\n", msg);
system("pause");
exit(1);
}
}
int main(int argc, char* argv[]) {
CheckError(argc != 2, "Pass batchfile that reads clipboard.");
HWND hwnd = GetConsoleHwnd();
CheckError(hwnd == NULL, "Pass batchfile that reads clipboard.");
BOOL is_opened = OpenClipboard(hwnd);
CheckError(is_opened == FALSE, "Can not open clipboard.\n");
HGLOBAL hmem = GetClipboardData(CF_TEXT);
CheckError(hmem == NULL, "Keep text in clipboard.");
TCHAR *cliptext = (TCHAR*)GlobalLock(hmem);
CheckError(cliptext == NULL, "Can not lock clipboard.");
// Make console command;
char cmd[kCmdBufSize];
int ret = sprintf(cmd, "cmd /c \"%s\" %s", argv[1], cliptext);
CheckError(ret < 0, "Text is Too long");
// Run!!!
system(cmd);
GlobalUnlock(hmem);
CloseClipboard();
return 0;
}