-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsymlink-paste.cpp
More file actions
126 lines (108 loc) · 2.62 KB
/
symlink-paste.cpp
File metadata and controls
126 lines (108 loc) · 2.62 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "Shlwapi.lib")
#include <windows.h>
#include <Ole2.h>
#include <ShellAPI.h>
#include <tchar.h>
#include <strsafe.h>
#include <Shlwapi.h>
enum PasteErrors { None = 0, InvalidClipboard, TooManyFiles, FileAlreadyExists, UnknownError };
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
PasteErrors error = None;
LPDATAOBJECT pData = 0;
wchar_t sourceName[MAX_PATH];
if (S_OK == OleGetClipboard(&pData))
{
FORMATETC format = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
FORMATETC textformat = { CF_UNICODETEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM medium;
if (S_OK == pData->GetData(&format, &medium))
{
HDROP hDrop = static_cast<HDROP>(GlobalLock(medium.hGlobal));
if (hDrop != NULL)
{
UINT nFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
if (nFiles == 1)
{
DragQueryFile(hDrop, 0, sourceName, MAX_PATH);
}
else
{
error = TooManyFiles;
}
GlobalUnlock(medium.hGlobal);
}
else
{
error = InvalidClipboard;
}
ReleaseStgMedium(&medium);
}
else if (S_OK == pData->GetData(&textformat, &medium))
{
LPCTSTR ptxt = (LPCTSTR)GlobalLock(medium.hGlobal);
if (ptxt)
{
StringCchCopy(sourceName, MAX_PATH, ptxt);
GlobalUnlock(medium.hGlobal);
}
else
{
error = InvalidClipboard;
}
ReleaseStgMedium(&medium);
}
else
{
error = InvalidClipboard;
}
pData->Release();
}
else
{
error = InvalidClipboard;
}
if (error == None)
{
sourceName[MAX_PATH - 1] = 0;
if (!PathIsRelative(sourceName) && PathFileExists(sourceName))
{
wchar_t targetName[MAX_PATH];
StringCchCopy(targetName, MAX_PATH, sourceName);
PathStripPath(targetName);
if (!PathFileExists(targetName))
{
if (!CreateSymbolicLink(targetName, sourceName, PathIsDirectory(sourceName) ? SYMBOLIC_LINK_FLAG_DIRECTORY : NULL))
{
error = UnknownError;
}
}
else
{
error = FileAlreadyExists;
}
}
else
{
error = InvalidClipboard;
}
}
if (error == InvalidClipboard)
{
MessageBox(NULL, L"You have to copy a file or folder first.", L"Paste Symlink", MB_ICONERROR);
}
else if (error == TooManyFiles)
{
MessageBox(NULL, L"Only one file or folder at a time, please.", L"Paste Symlink", MB_ICONERROR);
}
else if (error == FileAlreadyExists)
{
MessageBox(NULL, L"A file with the same name already exists.", L"Paste Symlink", MB_ICONERROR);
}
else if (error == UnknownError)
{
MessageBox(NULL, L"Couldn't create the symbolic link.", L"Paste Symlink", MB_ICONERROR);
}
return 0;
}