-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_to_clipboard.cpp
79 lines (64 loc) · 2.5 KB
/
copy_to_clipboard.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <windows.h>
#include <shlobj.h> // For CF_HDROP
#include <string>
#include <iostream>
void CopyFileToClipboard(const std::wstring& filePath) {
// Ensure the file exists
if (GetFileAttributesW(filePath.c_str()) == INVALID_FILE_ATTRIBUTES) {
std::wcerr << L"File does not exist: " << filePath << std::endl;
return;
}
// Prepare the DROPFILES structure
DROPFILES dropFiles = {0};
dropFiles.pFiles = sizeof(DROPFILES); // Offset to file list
dropFiles.fWide = TRUE; // Unicode file paths
// Calculate memory size for DROPFILES + file path (null-terminated) + extra null for double-null
size_t filePathSize = (filePath.length() + 1) * sizeof(wchar_t); // Path with null terminator
size_t globalSize = sizeof(DROPFILES) + filePathSize + sizeof(wchar_t); // DROPFILES + path + double null
// Allocate global memory for the DROPFILES structure and file path
HGLOBAL hGlobal = GlobalAlloc(GHND, globalSize);
if (!hGlobal) {
std::cerr << "GlobalAlloc failed." << std::endl;
return;
}
// Lock the memory and populate it with DROPFILES and file path
void* pGlobal = GlobalLock(hGlobal);
if (!pGlobal) {
std::cerr << "GlobalLock failed." << std::endl;
GlobalFree(hGlobal);
return;
}
// Copy DROPFILES structure
memcpy(pGlobal, &dropFiles, sizeof(DROPFILES));
// Copy the file path (null-terminated)
memcpy(static_cast<BYTE*>(pGlobal) + sizeof(DROPFILES), filePath.c_str(), filePathSize);
// Add double-null termination
memset(static_cast<BYTE*>(pGlobal) + sizeof(DROPFILES) + filePathSize, 0, sizeof(wchar_t));
// Unlock the global memory
GlobalUnlock(hGlobal);
// Open the clipboard
if (!OpenClipboard(nullptr)) {
std::cerr << "Failed to open clipboard." << std::endl;
GlobalFree(hGlobal);
return;
}
// Clear the clipboard and set CF_HDROP data
EmptyClipboard();
if (!SetClipboardData(CF_HDROP, hGlobal)) {
std::cerr << "SetClipboardData failed." << std::endl;
GlobalFree(hGlobal);
} else {
std::cout << "File copied to clipboard successfully!" << std::endl;
}
// Close the clipboard
CloseClipboard();
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <file_path>" << std::endl;
return 1;
}
std::wstring filePath = std::wstring(argv[1], argv[1] + strlen(argv[1]));
CopyFileToClipboard(filePath);
return 0;
}