-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shortcut.cpp
173 lines (140 loc) · 3.68 KB
/
Shortcut.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//////////////////////////////////////////////////////////////////////////
//
// Query the shortcut .LNK for the target path
//
// Used by stat()
//
// Copyright (c) 2004, Algin Technology LLC
// Written by Alan Klietz
// Distributed under GNU General Public License version 2.
//
// $Id: Shortcut.cpp,v 1.4 2008/08/28 22:44:32 cvsalan Exp $
//
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ole2.h>
#include <shlobj.h> // IShellLink
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#ifndef errno
extern int errno;
#endif
#include <string.h>
#include <wchar.h>
#include <tchar.h>
//#include <mbstring.h>
extern "C" {
#include "dirent.h"
#include "xmbrtowc.h" // for get_codepage()
#include "more.h"
#include "ls.h"
}
//
// Query the shortcut .LNK for the target path
//
extern "C" char *
_GetShortcutTarget(struct cache_entry *ce, char *szPath)
{
HRESULT hr;
IShellLink *psl=NULL;
IPersistFile* ppf=NULL;
wchar_t wszPath[FILENAME_MAX];
DWORD dwFlags;
LPITEMIDLIST/*PIDLIST_ABSOLUTE*/ pidl = NULL;
szPath[0] = '\0';
//
// Initialize COM
//
if (!gbComInitialized) {
gbComInitialized = TRUE;
if (FAILED(CoInitialize(NULL))) {
more_fputs("Unable to initialize COM\n", stdmore_err);
exit(1);
}
}
//
// Convert the path name to Unicode
//
if (MultiByteToWideChar(get_codepage(), 0, ce->ce_abspath, -1,
wszPath, FILENAME_MAX) == 0) {
goto fail;
}
//
// Create an IShellLink object
//
hr = CoCreateInstance(CLSID_ShellLink/*ref*/, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink/*ref*/, (LPVOID*)&psl);
if (FAILED(hr)) goto fail;
//
// Query IShellLink for the IPersistFile interface
//
hr = psl->QueryInterface(IID_IPersistFile/*ref*/, (LPVOID*)&ppf);
if (FAILED(hr)) goto fail;
#ifdef _DEBUG
//more_printf("Shortcut Load(\"%ws\")\n", wszPath);
#endif
//
// Load the .LNK file
//
hr = ppf->Load(wszPath, STGM_READ);
if (FAILED(hr)) goto fail;
//
// Resolve the shortcut. Disable GUI (SLR_NO_UI) if not found,
// Do not try exhaustive file search (SLR_NOSEARCH).
// Do not update the shortcut (SLR_NOUPDATE).
//
// Query the Distributed Link Tracking Service iff --slow
//
dwFlags = SLR_NO_UI|SLR_NOSEARCH|SLR_NOUPDATE;
if (run_fast) {
dwFlags |= SLR_NOTRACK; // no Distributed Link Tracking
}
//dwFlags |= SLR_ANY_MATCH; // UNDOCUMENTED (not needed for MSI?)
dwFlags |= SLR_INVOKE_MSI; // Required for MSI 'Darwin' link resolution
hr = psl->Resolve(NULL, dwFlags);
//if (FAILED(hr)) goto fail; // ignore failure to get orphan link
//
// Get the target path. This should always succeed even for
// orphan links.
//
//
// We must use PIDLs to resolve MSI 'Darwin' style advertised
// shortcuts.
//
// Get the PIDL list
hr = psl->GetIDList(&pidl);
//
// First try to resolve an MSI 'Darwin' encoded/advertised shortcut
//
if (FAILED(hr) || !::SHGetPathFromIDList(pidl, szPath)) {
//
// Fall back to using IShellLink::GetPath
//
// Note: A shortcut to a non-file resource will return
// a zero-length string and S_FALSE (and succeed).
//
hr = psl->GetPath(szPath, FILENAME_MAX, NULL, /*SLGP_RAWPATH*/0);
}
if (szPath[0] == '\0' || hr == S_FALSE || FAILED(hr)) { // unable to query path
//
// We have to return something, so punt
//
lstrcpyn(szPath, "[Non-file link]", FILENAME_MAX);
}
ppf->Release();
psl->Release();
return szPath;
fail:
if (ppf) ppf->Release();
if (psl) psl->Release();
return NULL;
}
/*
vim:tabstop=4:shiftwidth=4:noexpandtab
*/