[994dbc]: Shortcut.cpp Maximize Restore History

Download this file

Shortcut.cpp    515 lines (425 with data), 15.9 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//*******************************************************************
//
// FILE: Shortcut.cpp
//
// AUTHOR: Thomas Latuske <CobUser@GMX.de>
//
// COMPONENT: CShortcut
//
// DATE: 04.05.2004
//
// COMMENTS: Update 11.05.2004:
// - added ResolveLink
// - added GetSpecialFolder (split off)
// Update 09.11.2006: (ronys)
// - Get to compile cleanly under MSVS2005
// - const correctness, char -> TCHAR, minor cleanups
//*******************************************************************
// Includes
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Shortcut.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CShortcut::CShortcut()
{
m_sCmdArg.Empty();
}
CShortcut::~CShortcut()
{
}
/*!
***************************************************************
\param LnkTarget - The File/Folder the link belongs to
\param LnkName - The name of the ShortCut
\param SpecialFolder - where to put the shortcut (See #defines below)
\param LnkDescription - an application can use it to store\n
any text information and can retrieve\n
it with "IShellLink::GetDescription"\n
\param IconLocation - path to the file where the icon is located\n
that should be used. Can be an empty string\n
\param IconIndex - the # of the icon in the file\n
\return BOOL - ShortCut created or not\n
Defines for Special Folders:
SendTo Menu/Folder: CSIDL_SENDTO
Desktop for current User CSIDL_DESKTOP
Desktop: CSIDL_COMMON_DESKTOPDIRECTORY
Autostart for current User: CSIDL_STARTUP
Autostart: CSIDL_COMMON_STARTUP
Start-menu for current User: CSIDL_STARTMENU
Start-menu: CSIDL_STARTMENU
Programms-menu for current User:CSIDL_COMMON_STARTMENU
and some more.....
*****************************************************************/
BOOL CShortcut::CreateShortCut(const CString &LnkTarget,
const CString &LnkName, UINT SpecialFolder,
const CString &LnkDescription,
const CString &IconLocation, UINT IconIndex)
{
HRESULT hr;
CFile cfFull;
CString sExePath, sExe, sSpecialFolder;
TCHAR *chTmp = sExePath.GetBuffer(MAX_PATH);
GetModuleFileName(NULL, chTmp, MAX_PATH);
sExePath.ReleaseBuffer();
// Find the Special Folder:
if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
return FALSE;
sSpecialFolder += LnkName + _T(".") + _T("lnk");
if(LnkTarget == _T("_this")) {
cfFull.SetFilePath(sExePath);
sExe = cfFull.GetFileName();
sExe.Delete(sExe.Find(_T(".")) + 1, 3);
} else {
sExePath = LnkTarget;
}
// Create the ShortCut:
CoInitialize(NULL);
BOOL bRet = FALSE;
IShellLink* psl;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*) &psl))) {
IPersistFile* ppf;
//
psl->SetPath(sExePath);
psl->SetDescription(LnkDescription);
if(!m_sCmdArg.IsEmpty())
psl->SetArguments(m_sCmdArg);
if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf))) {
#ifndef UNICODE
WCHAR wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,
sSpecialFolder,
-1,
wsz,
MAX_PATH);
#endif
/* Call IShellLink::SetIconLocation with the file containing
the icon and the index of the icon */
if(!IconLocation.IsEmpty()) {
hr = psl->SetIconLocation(IconLocation, IconIndex);
#ifdef _DEBUG
if(FAILED(hr))
TRACE(_T("IconLocation not changed!\n"));
#endif
}
#ifndef UNICODE
if(SUCCEEDED(ppf->Save(wsz, TRUE)))
#else
if(SUCCEEDED(ppf->Save(sSpecialFolder, TRUE)))
#endif
{
bRet = TRUE;
}
ppf->Release();
}
psl->Release();
}
TRACE(bRet ? _T("Lnk Written!\n") :
_T("Lnk NOT Written! CreateShortCut(...) failed!\n"));
return bRet;
}
/*!
***************************************************************
\param LnkName - The name of the ShortCut
\param SpecialFolder - Location of the shortcut (See #defines below)
\return void
Defines for Special Folders:
SendTo Menu/Folder: CSIDL_SENDTO
Desktop for current User CSIDL_DESKTOP
Desktop: CSIDL_COMMON_DESKTOPDIRECTORY
Autostart for current User: CSIDL_STARTUP
Autostart: CSIDL_COMMON_STARTUP
Start-menu for current User: CSIDL_STARTMENU
Start-menu: CSIDL_STARTMENU
Programms-menu for current User:CSIDL_COMMON_STARTMENU
and some more.....
*****************************************************************/
BOOL CShortcut::DeleteShortCut(const CString &LnkName, UINT SpecialFolder)
{
CFile cfFull;
CString sExePath, sExe, sSpecialFolder;
TCHAR *chTmp = sExePath.GetBuffer(MAX_PATH);
GetModuleFileName(NULL, chTmp, MAX_PATH);
sExePath.ReleaseBuffer();
if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
return FALSE;
// Work with the special folder's path (contained in szPath)
cfFull.SetFilePath(sExePath);
sExe = cfFull.GetFileName();
sExe.Delete(sExe.Find(_T(".")) + 1, 3);
sSpecialFolder += LnkName + _T(".") + _T("lnk");
// DELETE THE LINK:
SHFILEOPSTRUCT FIO;
memset(&FIO, 0, sizeof SHFILEOPSTRUCT);
// FIO.pTo=NULL; // MUST be NULL
FIO.wFunc=FO_DELETE;
FIO.fFlags=FOF_NOERRORUI|FOF_NOCONFIRMATION;
if(sSpecialFolder.Find(TCHAR('\0'))!=sSpecialFolder.GetLength()) {
FIO.fFlags|=FOF_MULTIDESTFILES;
}
if(sSpecialFolder.Right(1)) {
sSpecialFolder+=TCHAR('\0');
}
FIO.pFrom=&*sSpecialFolder;
int bD = SHFileOperation(&FIO);
if(!bD) {
TRACE(_T("Lnk Deleted!\n"));
return TRUE;
} else {
TRACE(_T("Lnk NOT Deleted! DeleteShortCut(...) FAILED!\n"));
return FALSE;
}
}
/*********************************************
This routine tests if a link already exists:
*********************************************/
BOOL CShortcut::isLinkExist(const CString &LnkName,
UINT SpecialFolder) const
{
CFileStatus cfStatus;
CString sSpecialFolder;
// Find the Special Folder:
if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
return FALSE;
// Work with the special folder's path (contained in szPath)
sSpecialFolder += _T("\\");
sSpecialFolder += LnkName + _T(".") + _T("lnk");
if(CFile::GetStatus(sSpecialFolder, cfStatus)) {
#ifdef _DEBUG
afxDump << _T("Full file name = ") << cfStatus.m_szFullName << _T("\n");
#endif
return TRUE;
} else {
#ifdef _DEBUG
afxDump << _T("File NOT available = ") << cfStatus.m_szFullName << _T("\n");
#endif
return FALSE;
}
}
/*********************************************
This routine resolves the lnk destination:
\param LnkName - The name of the ShortCut\n
\param SpecialFolder - where to put the shortcut (See #defines above (MSDN))\n
\param hwnd - handle of the parent window for MessageBoxes\n
the shell may need to display\n
\param LnkPath - Reference to a CString that receives the linkpath if\n
routine is successful else the string will be empty\n
\param LnkDescription - Reference to a CString that receives the Description\n
of the link else the string will be empty\n
\returns a HRESULT \n
*********************************************/
HRESULT CShortcut::ResolveLink(const CString &LnkName, UINT SpecialFolder,
HWND hwnd, CString &LnkPath,
CString &LnkDescription)
{
HRESULT hres;
IShellLink* psl;
TCHAR *szGotPath = LnkPath.GetBuffer(MAX_PATH);
TCHAR *szDescription = LnkDescription.GetBuffer(MAX_PATH);
CString sLnkFile, sSpecialFolder;
CString sLong;
WIN32_FIND_DATA wfd;
// get the path to the special folder:
if(!GetSpecialFolder(SpecialFolder, sSpecialFolder))
return 1; // return ERROR
// build a linkfile:
sLnkFile = sSpecialFolder + LnkName + _T(".lnk");
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*) &psl);
if(SUCCEEDED(hres)) {
IPersistFile* ppf;
// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
if (SUCCEEDED(hres)) {
#ifndef UNICODE
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP,
0,
sLnkFile,//lpszLinkFile,
-1,
wsz,
MAX_PATH); // Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);
#else
hres = ppf->Load(sLnkFile, STGM_READ);
#endif
if (SUCCEEDED(hres)) { // Resolve the link.
hres = psl->Resolve(hwnd, SLR_ANY_MATCH);
if (SUCCEEDED(hres)) {
// Get the path to the link target.
hres = psl->GetPath(szGotPath,
MAX_PATH,
(WIN32_FIND_DATA *)&wfd,
SLGP_SHORTPATH );
LnkPath.ReleaseBuffer();
if (!SUCCEEDED(hres)) {
LnkDescription.ReleaseBuffer();
return hres; // application-defined function
}
// Get the description of the target:
hres = psl->GetDescription(szDescription, MAX_PATH);
LnkDescription.ReleaseBuffer();
if (!SUCCEEDED(hres))
return hres;
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
// whether OS is <= NT4 or not... use this helper:
if(ShortToLongPathName(LnkPath, sLong) > LnkPath.GetLength())
LnkPath = sLong;
return hres;
}
/*********************************************
This routine is a helper that finds\n
the path to the special folder:\n
\param SpecialFolder - an UINT-define (See #defines above or (MSDN))\n
\param SpecialFolderString - Reference to a CString that receives the\n
path to the special folder
\returns a BOOL - Found or not \n
*********************************************/
BOOL CShortcut::GetSpecialFolder(UINT SpecialFolder,
CString &SpecialFolderString) const
{
HRESULT hr;
// Find the Special Folder:
// Allocate a pointer to an Item ID list
LPITEMIDLIST pidl;
// Get a pointer to an item ID list that
// represents the path of a special folder
hr = SHGetSpecialFolderLocation(NULL, SpecialFolder, &pidl);
if(SUCCEEDED(hr)) { // Convert the item ID list's binary
// representation into a file system path
TCHAR szPath[_MAX_PATH];
if(SHGetPathFromIDList(pidl, szPath)) {
// Allocate a pointer to an IMalloc interface
LPMALLOC pMalloc;
// Get the address of our task allocator's IMalloc interface
hr = SHGetMalloc(&pMalloc);
// Free the item ID list allocated by SHGetSpecialFolderLocation
pMalloc->Free(pidl);
// Free our task allocator
pMalloc->Release();
// Work with the special folder's path (contained in szPath)
SpecialFolderString = szPath;
SpecialFolderString += _T("\\");
return TRUE;
}
}
return FALSE;
}
/*********************************************
This routine is a helper that builds a long\n
path of a short (8+3) path:\n
\param sShortPath - short path to convert\n
\param sLongPath - string that receives the long path\n
\returns a BOOL - Found or not \n
*********************************************/
// if the extension contains '~', replace it with '*'!!!!!!!!!!!!!!!
int CShortcut::ShortToLongPathName(const CString &sShortPath, CString &sLongPath)
{
// Catch null pointers.
if (sShortPath.IsEmpty()) return 0;
// Check whether the input path is valid.
if (0xffffffff == GetFileAttributes(sShortPath)) return 0;
// Special characters.
CString sep = _T("\\");
CString colon = _T(":");
CString sDrive, sCutPath, sTmpShort;
// Copy the short path into the work buffer and convert forward
// slashes to backslashes.
CString path = sShortPath;
path.Replace(_T("/"), sep);
// We need a marker for stepping through the path.
int right = 0;
// Parse the first bit of the path.
if (path.GetLength() >= 2 && isalpha(path[0]) && colon == path[1]) {// Drive letter?
if (2 == path.GetLength()) { // �bare� drive letter
right = -1; // skip main block
} else if (sep == path[2]) { // drive letter + backslash
// FindFirstFile doesn�t like "X:\"
if (3 == path.GetLength()) {
right = -1; // skip main block
}
}
else return 0; // parsing failure
} else if (path.GetLength() >= 1 && sep == path[0]) {
if (1 == path.GetLength()) { // �bare� backslash
right = -1; // skip main block
} else {
if (sep == path[1]) { // is it UNC?
// Find end of machine name
right = path.Find(sep);
if (right == -1)
return 0;
// Find end of share name
right = path.Find(sep);
if (right == 1)
return 0;
}
++right;
}
}
// else FindFirstFile will handle relative paths
// The data block for FindFirstFile.
WIN32_FIND_DATA fd;
// extract the Drive-name:
sDrive = path.Left(3);
path.Delete(0, 3);
sTmpShort = sDrive;
sLongPath = sDrive;
// Main parse block - step through path.
while (right != -1) {
// Find next separator.
right = path.Find(sep);
if(right == 0) {
path.Delete(0, 1);
sTmpShort += sep;
right = path.Find(sep);
} else if(right == -1 && path.GetLength() <= 0) {
return sLongPath.GetLength();
}
// Temporarily replace the separator with a null character so that
// the path so far can be passed to FindFirstFile.
if(right == -1)
sCutPath = path.Left(path.GetLength());
else
sCutPath = path.Left(right);
path.Delete(0, right);
sTmpShort += sCutPath;
// See what FindFirstFile makes of the path so far.
HANDLE hf = FindFirstFile(sTmpShort, &fd);
if (INVALID_HANDLE_VALUE == hf)
return 0;
FindClose(hf);
// The file was found add it to the output path:
if(right != -1)
sLongPath += fd.cFileName + sep;
else
sLongPath += fd.cFileName;
}
// Check whether the output path is valid:
if (0xffffffff == GetFileAttributes(sLongPath)) return 0;
// return the number of characters in the long path:
return sLongPath.GetLength();
}