Welcome to mirror list, hosted at ThFree Co, Russian Federation.

winjump.c « windows - github.com/mRemoteNG/PuTTYNG.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 358504fdafffd617759f82e89806e55bb1c335ea (plain)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
 * winjump.c: support for Windows 7 jump lists.
 *
 * The Windows 7 jumplist is a customizable list defined by the
 * application. It is persistent across application restarts: the OS
 * maintains the list when the app is not running. The list is shown
 * when the user right-clicks on the taskbar button of a running app
 * or a pinned non-running application. We use the jumplist to
 * maintain a list of recently started saved sessions, started either
 * by doubleclicking on a saved session, or with the command line
 * "-load" parameter.
 *
 * Since the jumplist is write-only: it can only be replaced and the
 * current list cannot be read, we must maintain the contents of the
 * list persistantly in the registry. The file winstore.h contains
 * functions to directly manipulate these registry entries. This file
 * contains higher level functions to manipulate the jumplist.
 */

#include <assert.h>

#include "putty.h"
#include "storage.h"

#define MAX_JUMPLIST_ITEMS 30 /* PuTTY will never show more items in
                               * the jumplist than this, regardless of
                               * user preferences. */

/*
 * COM structures and functions.
 */
#ifndef PROPERTYKEY_DEFINED
#define PROPERTYKEY_DEFINED
typedef struct _tagpropertykey {
    GUID fmtid;
    DWORD pid;
} PROPERTYKEY;
#endif
#ifndef _REFPROPVARIANT_DEFINED
#define _REFPROPVARIANT_DEFINED
typedef PROPVARIANT *REFPROPVARIANT;
#endif
/* MinGW doesn't define this yet: */
#ifndef _PROPVARIANTINIT_DEFINED_
#define _PROPVARIANTINIT_DEFINED_
#define PropVariantInit(pvar) memset((pvar),0,sizeof(PROPVARIANT))
#endif

#define IID_IShellLink IID_IShellLinkA

typedef struct ICustomDestinationListVtbl {
    HRESULT ( __stdcall *QueryInterface ) (
        /* [in] ICustomDestinationList*/ void *This,
        /* [in] */  const GUID * const riid,
        /* [out] */ void **ppvObject);

    ULONG ( __stdcall *AddRef )(
        /* [in] ICustomDestinationList*/ void *This);

    ULONG ( __stdcall *Release )(
        /* [in] ICustomDestinationList*/ void *This);

    HRESULT ( __stdcall *SetAppID )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [string][in] */ LPCWSTR pszAppID);

    HRESULT ( __stdcall *BeginList )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [out] */ UINT *pcMinSlots,
        /* [in] */  const GUID * const riid,
        /* [out] */ void **ppv);

    HRESULT ( __stdcall *AppendCategory )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [string][in] */ LPCWSTR pszCategory,
        /* [in] IObjectArray*/ void *poa);

    HRESULT ( __stdcall *AppendKnownCategory )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [in] KNOWNDESTCATEGORY*/ int category);

    HRESULT ( __stdcall *AddUserTasks )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [in] IObjectArray*/ void *poa);

    HRESULT ( __stdcall *CommitList )(
        /* [in] ICustomDestinationList*/ void *This);

    HRESULT ( __stdcall *GetRemovedDestinations )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [in] */ const IID * const riid,
        /* [out] */ void **ppv);

    HRESULT ( __stdcall *DeleteList )(
        /* [in] ICustomDestinationList*/ void *This,
        /* [string][unique][in] */ LPCWSTR pszAppID);

    HRESULT ( __stdcall *AbortList )(
        /* [in] ICustomDestinationList*/ void *This);

} ICustomDestinationListVtbl;

typedef struct ICustomDestinationList
{
    ICustomDestinationListVtbl *lpVtbl;
} ICustomDestinationList;

typedef struct IObjectArrayVtbl
{
    HRESULT ( __stdcall *QueryInterface )(
        /* [in] IObjectArray*/ void *This,
        /* [in] */ const GUID * const riid,
        /* [out] */ void **ppvObject);

    ULONG ( __stdcall *AddRef )(
        /* [in] IObjectArray*/ void *This);

    ULONG ( __stdcall *Release )(
        /* [in] IObjectArray*/ void *This);

    HRESULT ( __stdcall *GetCount )(
        /* [in] IObjectArray*/ void *This,
        /* [out] */ UINT *pcObjects);

    HRESULT ( __stdcall *GetAt )(
        /* [in] IObjectArray*/ void *This,
        /* [in] */ UINT uiIndex,
        /* [in] */ const GUID * const riid,
        /* [out] */ void **ppv);

} IObjectArrayVtbl;

typedef struct IObjectArray
{
    IObjectArrayVtbl *lpVtbl;
} IObjectArray;

typedef struct IShellLinkVtbl
{
    HRESULT ( __stdcall *QueryInterface )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ const GUID * const riid,
        /* [out] */ void **ppvObject);

    ULONG ( __stdcall *AddRef )(
        /* [in] IShellLink*/ void *This);

    ULONG ( __stdcall *Release )(
        /* [in] IShellLink*/ void *This);

    HRESULT ( __stdcall *GetPath )(
        /* [in] IShellLink*/ void *This,
        /* [string][out] */ LPSTR pszFile,
        /* [in] */ int cch,
        /* [unique][out][in] */ WIN32_FIND_DATAA *pfd,
        /* [in] */ DWORD fFlags);

    HRESULT ( __stdcall *GetIDList )(
        /* [in] IShellLink*/ void *This,
        /* [out] LPITEMIDLIST*/ void **ppidl);

    HRESULT ( __stdcall *SetIDList )(
        /* [in] IShellLink*/ void *This,
        /* [in] LPITEMIDLIST*/ void *pidl);

    HRESULT ( __stdcall *GetDescription )(
        /* [in] IShellLink*/ void *This,
        /* [string][out] */ LPSTR pszName,
        /* [in] */ int cch);

    HRESULT ( __stdcall *SetDescription )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszName);

    HRESULT ( __stdcall *GetWorkingDirectory )(
        /* [in] IShellLink*/ void *This,
        /* [string][out] */ LPSTR pszDir,
        /* [in] */ int cch);

    HRESULT ( __stdcall *SetWorkingDirectory )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszDir);

    HRESULT ( __stdcall *GetArguments )(
        /* [in] IShellLink*/ void *This,
        /* [string][out] */ LPSTR pszArgs,
        /* [in] */ int cch);

    HRESULT ( __stdcall *SetArguments )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszArgs);

    HRESULT ( __stdcall *GetHotkey )(
        /* [in] IShellLink*/ void *This,
        /* [out] */ WORD *pwHotkey);

    HRESULT ( __stdcall *SetHotkey )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ WORD wHotkey);

    HRESULT ( __stdcall *GetShowCmd )(
        /* [in] IShellLink*/ void *This,
        /* [out] */ int *piShowCmd);

    HRESULT ( __stdcall *SetShowCmd )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ int iShowCmd);

    HRESULT ( __stdcall *GetIconLocation )(
        /* [in] IShellLink*/ void *This,
        /* [string][out] */ LPSTR pszIconPath,
        /* [in] */ int cch,
        /* [out] */ int *piIcon);

    HRESULT ( __stdcall *SetIconLocation )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszIconPath,
        /* [in] */ int iIcon);

    HRESULT ( __stdcall *SetRelativePath )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszPathRel,
        /* [in] */ DWORD dwReserved);

    HRESULT ( __stdcall *Resolve )(
        /* [in] IShellLink*/ void *This,
        /* [unique][in] */ HWND hwnd,
        /* [in] */ DWORD fFlags);

    HRESULT ( __stdcall *SetPath )(
        /* [in] IShellLink*/ void *This,
        /* [string][in] */ LPCSTR pszFile);

} IShellLinkVtbl;

typedef struct IShellLink
{
    IShellLinkVtbl *lpVtbl;
} IShellLink;

typedef struct IObjectCollectionVtbl
{
    HRESULT ( __stdcall *QueryInterface )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ const GUID * const riid,
        /* [out] */ void **ppvObject);

    ULONG ( __stdcall *AddRef )(
        /* [in] IShellLink*/ void *This);

    ULONG ( __stdcall *Release )(
        /* [in] IShellLink*/ void *This);

    HRESULT ( __stdcall *GetCount )(
        /* [in] IShellLink*/ void *This,
        /* [out] */ UINT *pcObjects);

    HRESULT ( __stdcall *GetAt )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ UINT uiIndex,
        /* [in] */ const GUID * const riid,
        /* [iid_is][out] */ void **ppv);

    HRESULT ( __stdcall *AddObject )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ void *punk);

    HRESULT ( __stdcall *AddFromArray )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ IObjectArray *poaSource);

    HRESULT ( __stdcall *RemoveObjectAt )(
        /* [in] IShellLink*/ void *This,
        /* [in] */ UINT uiIndex);

    HRESULT ( __stdcall *Clear )(
        /* [in] IShellLink*/ void *This);

} IObjectCollectionVtbl;

typedef struct IObjectCollection
{
    IObjectCollectionVtbl *lpVtbl;
} IObjectCollection;

typedef struct IPropertyStoreVtbl
{
    HRESULT ( __stdcall *QueryInterface )(
        /* [in] IPropertyStore*/ void *This,
        /* [in] */ const GUID * const riid,
        /* [iid_is][out] */ void **ppvObject);

    ULONG ( __stdcall *AddRef )(
        /* [in] IPropertyStore*/ void *This);

    ULONG ( __stdcall *Release )(
        /* [in] IPropertyStore*/ void *This);

    HRESULT ( __stdcall *GetCount )(
        /* [in] IPropertyStore*/ void *This,
        /* [out] */ DWORD *cProps);

    HRESULT ( __stdcall *GetAt )(
        /* [in] IPropertyStore*/ void *This,
        /* [in] */ DWORD iProp,
        /* [out] */ PROPERTYKEY *pkey);

    HRESULT ( __stdcall *GetValue )(
        /* [in] IPropertyStore*/ void *This,
        /* [in] */ const PROPERTYKEY * const key,
        /* [out] */ PROPVARIANT *pv);

    HRESULT ( __stdcall *SetValue )(
        /* [in] IPropertyStore*/ void *This,
        /* [in] */ const PROPERTYKEY * const key,
        /* [in] */ REFPROPVARIANT propvar);

    HRESULT ( __stdcall *Commit )(
        /* [in] IPropertyStore*/ void *This);
} IPropertyStoreVtbl;

typedef struct IPropertyStore
{
    IPropertyStoreVtbl *lpVtbl;
} IPropertyStore;

static const CLSID CLSID_DestinationList = {
    0x77f10cf0, 0x3db5, 0x4966, {0xb5,0x20,0xb7,0xc5,0x4f,0xd3,0x5e,0xd6}
};
static const CLSID CLSID_ShellLink = {
    0x00021401, 0x0000, 0x0000, {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}
};
static const CLSID CLSID_EnumerableObjectCollection = {
    0x2d3468c1, 0x36a7, 0x43b6, {0xac,0x24,0xd3,0xf0,0x2f,0xd9,0x60,0x7a}
};
static const IID IID_IObjectCollection = {
    0x5632b1a4, 0xe38a, 0x400a, {0x92,0x8a,0xd4,0xcd,0x63,0x23,0x02,0x95}
};
static const IID IID_IShellLink = {
    0x000214ee, 0x0000, 0x0000, {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}
};
static const IID IID_ICustomDestinationList = {
    0x6332debf, 0x87b5, 0x4670, {0x90,0xc0,0x5e,0x57,0xb4,0x08,0xa4,0x9e}
};
static const IID IID_IObjectArray = {
    0x92ca9dcd, 0x5622, 0x4bba, {0xa8,0x05,0x5e,0x9f,0x54,0x1b,0xd8,0xc9}
};
static const IID IID_IPropertyStore = {
    0x886d8eeb, 0x8cf2, 0x4446, {0x8d,0x02,0xcd,0xba,0x1d,0xbd,0xcf,0x99}
};
static const PROPERTYKEY PKEY_Title = {
    {0xf29f85e0, 0x4ff9, 0x1068, {0xab,0x91,0x08,0x00,0x2b,0x27,0xb3,0xd9}},
    0x00000002
};

/* Type-checking macro to provide arguments for CoCreateInstance()
 * etc, ensuring that 'obj' really is a 'type **'. */
#define typecheck(checkexpr, result) \
    (sizeof(checkexpr) ? (result) : (result))
#define COMPTR(type, obj) &IID_##type, \
    typecheck((obj)-(type **)(obj), (void **)(void *)(obj))

static char putty_path[2048];

/*
 * Function to make an IShellLink describing a particular PuTTY
 * command. If 'appname' is null, the command run will be the one
 * returned by GetModuleFileName, i.e. our own executable; if it's
 * non-null then it will be assumed to be a filename in the same
 * directory as our own executable, and the return value will be NULL
 * if that file doesn't exist.
 *
 * If 'sessionname' is null then no command line will be passed to the
 * program. If it's non-null, the command line will be that text
 * prefixed with an @ (to load a PuTTY saved session).
 *
 * Hence, you can launch a saved session using make_shell_link(NULL,
 * sessionname), and launch another app using e.g.
 * make_shell_link("puttygen.exe", NULL).
 */
static IShellLink *make_shell_link(const char *appname,
                                   const char *sessionname)
{
    IShellLink *ret;
    char *app_path, *param_string, *desc_string;
    IPropertyStore *pPS;
    PROPVARIANT pv;

    /* Retrieve path to executable. */
    if (!putty_path[0])
        GetModuleFileName(NULL, putty_path, sizeof(putty_path) - 1);
    if (appname) {
        char *p, *q = putty_path;
        FILE *fp;

        if ((p = strrchr(q, '\\')) != NULL) q = p+1;
        if ((p = strrchr(q, ':')) != NULL) q = p+1;
        app_path = dupprintf("%.*s%s", (int)(q - putty_path), putty_path,
                             appname);
        if ((fp = fopen(app_path, "r")) == NULL) {
            sfree(app_path);
            return NULL;
        }
        fclose(fp);
    } else {
        app_path = dupstr(putty_path);
    }

    /* Check if this is a valid session, otherwise don't add. */
    if (sessionname) {
        settings_r *psettings_tmp = open_settings_r(sessionname);
        if (!psettings_tmp) {
            sfree(app_path);
            return NULL;
        }
        close_settings_r(psettings_tmp);
    }

    /* Create the new item. */
    if (!SUCCEEDED(CoCreateInstance(&CLSID_ShellLink, NULL,
                                    CLSCTX_INPROC_SERVER,
                                    COMPTR(IShellLink, &ret)))) {
        sfree(app_path);
        return NULL;
    }

    /* Set path, parameters, icon and description. */
    ret->lpVtbl->SetPath(ret, app_path);

    if (sessionname) {
        /* The leading space is reported to work around a Windows 10
         * behaviour change in which an argument string starting with
         * '@' causes the SetArguments method to silently do the wrong
         * thing. */
        param_string = dupcat(" @", sessionname);
    } else {
        param_string = dupstr("");
    }
    ret->lpVtbl->SetArguments(ret, param_string);
    sfree(param_string);

    if (sessionname) {
        desc_string = dupcat("Connect to PuTTY session '", sessionname, "'");
    } else {
        assert(appname);
        desc_string = dupprintf("Run %.*s",
                                (int)strcspn(appname, "."), appname);
    }
    ret->lpVtbl->SetDescription(ret, desc_string);
    sfree(desc_string);

    ret->lpVtbl->SetIconLocation(ret, app_path, 0);

    /* To set the link title, we require the property store of the link. */
    if (SUCCEEDED(ret->lpVtbl->QueryInterface(ret,
                                              COMPTR(IPropertyStore, &pPS)))) {
        PropVariantInit(&pv);
        pv.vt = VT_LPSTR;
        if (sessionname) {
            pv.pszVal = dupstr(sessionname);
        } else {
            assert(appname);
            pv.pszVal = dupprintf("Run %.*s",
                                  (int)strcspn(appname, "."), appname);
        }
        pPS->lpVtbl->SetValue(pPS, &PKEY_Title, &pv);
        sfree(pv.pszVal);
        pPS->lpVtbl->Commit(pPS);
        pPS->lpVtbl->Release(pPS);
    }

    sfree(app_path);

    return ret;
}

/* Updates jumplist from registry. */
static void update_jumplist_from_registry(void)
{
    const char *piterator;
    UINT num_items;
    int jumplist_counter;
    UINT nremoved;

    /* Variables used by the cleanup code must be initialised to NULL,
     * so that we don't try to free or release them if they were never
     * set up. */
    ICustomDestinationList *pCDL = NULL;
    char *pjumplist_reg_entries = NULL;
    IObjectCollection *collection = NULL;
    IObjectArray *array = NULL;
    IShellLink *link = NULL;
    IObjectArray *pRemoved = NULL;
    bool need_abort = false;

    /*
     * Create an ICustomDestinationList: the top-level object which
     * deals with jump list management.
     */
    if (!SUCCEEDED(CoCreateInstance(&CLSID_DestinationList, NULL,
                                    CLSCTX_INPROC_SERVER,
                                    COMPTR(ICustomDestinationList, &pCDL))))
        goto cleanup;

    /*
     * Call its BeginList method to start compiling a list. This gives
     * us back 'num_items' (a hint derived from systemwide
     * configuration about how many things to put on the list) and
     * 'pRemoved' (user configuration about things to leave off the
     * list).
     */
    if (!SUCCEEDED(pCDL->lpVtbl->BeginList(pCDL, &num_items,
                                           COMPTR(IObjectArray, &pRemoved))))
        goto cleanup;
    need_abort = true;
    if (!SUCCEEDED(pRemoved->lpVtbl->GetCount(pRemoved, &nremoved)))
        nremoved = 0;

    /*
     * Create an object collection to form the 'Recent Sessions'
     * category on the jump list.
     */
    if (!SUCCEEDED(CoCreateInstance(&CLSID_EnumerableObjectCollection,
                                    NULL, CLSCTX_INPROC_SERVER,
                                    COMPTR(IObjectCollection, &collection))))
        goto cleanup;

    /*
     * Go through the jump list entries from the registry and add each
     * one to the collection.
     */
    pjumplist_reg_entries = get_jumplist_registry_entries();
    piterator = pjumplist_reg_entries;
    jumplist_counter = 0;
    while (*piterator != '\0' &&
           (jumplist_counter < min(MAX_JUMPLIST_ITEMS, (int) num_items))) {
        link = make_shell_link(NULL, piterator);
        if (link) {
            UINT i;
            bool found;

            /*
             * Check that the link isn't in the user-removed list.
             */
            for (i = 0, found = false; i < nremoved && !found; i++) {
                IShellLink *rlink;
                if (SUCCEEDED(pRemoved->lpVtbl->GetAt
                              (pRemoved, i, COMPTR(IShellLink, &rlink)))) {
                    char desc1[2048], desc2[2048];
                    if (SUCCEEDED(link->lpVtbl->GetDescription
                                  (link, desc1, sizeof(desc1)-1)) &&
                        SUCCEEDED(rlink->lpVtbl->GetDescription
                                  (rlink, desc2, sizeof(desc2)-1)) &&
                        !strcmp(desc1, desc2)) {
                        found = true;
                    }
                    rlink->lpVtbl->Release(rlink);
                }
            }

            if (!found) {
                collection->lpVtbl->AddObject(collection, link);
                jumplist_counter++;
            }

            link->lpVtbl->Release(link);
            link = NULL;
        }
        piterator += strlen(piterator) + 1;
    }
    sfree(pjumplist_reg_entries);
    pjumplist_reg_entries = NULL;

    /*
     * Get the array form of the collection we've just constructed,
     * and put it in the jump list.
     */
    if (!SUCCEEDED(collection->lpVtbl->QueryInterface
                   (collection, COMPTR(IObjectArray, &array))))
        goto cleanup;

    pCDL->lpVtbl->AppendCategory(pCDL, L"Recent Sessions", array);

    /*
     * Create an object collection to form the 'Tasks' category on the
     * jump list.
     */
    if (!SUCCEEDED(CoCreateInstance(&CLSID_EnumerableObjectCollection,
                                    NULL, CLSCTX_INPROC_SERVER,
                                    COMPTR(IObjectCollection, &collection))))
        goto cleanup;

    /*
     * Add task entries for PuTTYgen and Pageant.
     */
    piterator = "Pageant.exe\0PuTTYgen.exe\0\0";
    while (*piterator != '\0') {
        link = make_shell_link(piterator, NULL);
        if (link) {
            collection->lpVtbl->AddObject(collection, link);
            link->lpVtbl->Release(link);
            link = NULL;
        }
        piterator += strlen(piterator) + 1;
    }

    /*
     * Get the array form of the collection we've just constructed,
     * and put it in the jump list.
     */
    if (!SUCCEEDED(collection->lpVtbl->QueryInterface
                   (collection, COMPTR(IObjectArray, &array))))
        goto cleanup;

    pCDL->lpVtbl->AddUserTasks(pCDL, array);

    /*
     * Now we can clean up the array and collection variables, so as
     * to be able to reuse them.
     */
    array->lpVtbl->Release(array);
    array = NULL;
    collection->lpVtbl->Release(collection);
    collection = NULL;

    /*
     * Create another object collection to form the user tasks
     * category.
     */
    if (!SUCCEEDED(CoCreateInstance(&CLSID_EnumerableObjectCollection,
                                    NULL, CLSCTX_INPROC_SERVER,
                                    COMPTR(IObjectCollection, &collection))))
        goto cleanup;

    /*
     * Get the array form of the collection we've just constructed,
     * and put it in the jump list.
     */
    if (!SUCCEEDED(collection->lpVtbl->QueryInterface
                   (collection, COMPTR(IObjectArray, &array))))
        goto cleanup;

    pCDL->lpVtbl->AddUserTasks(pCDL, array);

    /*
     * Now we can clean up the array and collection variables, so as
     * to be able to reuse them.
     */
    array->lpVtbl->Release(array);
    array = NULL;
    collection->lpVtbl->Release(collection);
    collection = NULL;

    /*
     * Commit the jump list.
     */
    pCDL->lpVtbl->CommitList(pCDL);
    need_abort = false;

    /*
     * Clean up.
     */
  cleanup:
    if (pRemoved) pRemoved->lpVtbl->Release(pRemoved);
    if (pCDL && need_abort) pCDL->lpVtbl->AbortList(pCDL);
    if (pCDL) pCDL->lpVtbl->Release(pCDL);
    if (collection) collection->lpVtbl->Release(collection);
    if (array) array->lpVtbl->Release(array);
    if (link) link->lpVtbl->Release(link);
    sfree(pjumplist_reg_entries);
}

/* Clears the entire jumplist. */
void clear_jumplist(void)
{
    ICustomDestinationList *pCDL;

    if (CoCreateInstance(&CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER,
                         COMPTR(ICustomDestinationList, &pCDL)) == S_OK) {
        pCDL->lpVtbl->DeleteList(pCDL, NULL);
        pCDL->lpVtbl->Release(pCDL);
    }

}

/* Adds a saved session to the Windows 7 jumplist. */
void add_session_to_jumplist(const char * const sessionname)
{
    if ((osMajorVersion < 6) || (osMajorVersion == 6 && osMinorVersion < 1))
        return;                        /* do nothing on pre-Win7 systems */

    if (add_to_jumplist_registry(sessionname) == JUMPLISTREG_OK) {
        update_jumplist_from_registry();
    } else {
        /* Make sure we don't leave the jumplist dangling. */
        clear_jumplist();
    }
}

/* Removes a saved session from the Windows jumplist. */
void remove_session_from_jumplist(const char * const sessionname)
{
    if ((osMajorVersion < 6) || (osMajorVersion == 6 && osMinorVersion < 1))
        return;                        /* do nothing on pre-Win7 systems */

    if (remove_from_jumplist_registry(sessionname) == JUMPLISTREG_OK) {
        update_jumplist_from_registry();
    } else {
        /* Make sure we don't leave the jumplist dangling. */
        clear_jumplist();
    }
}

/* Set Explicit App User Model Id to fix removable media error with
   jump lists */

bool set_explicit_app_user_model_id(void)
{
  DECL_WINDOWS_FUNCTION(static, HRESULT, SetCurrentProcessExplicitAppUserModelID,
                        (PCWSTR));

  static HMODULE shell32_module = 0;

    if (!shell32_module)
    {
        shell32_module = load_system32_dll("Shell32.dll");
        /*
         * We can't typecheck this function here, because it's defined
         * in <shobjidl.h>, which we're not including due to clashes
         * with all the manual-COM machinery above.
         */
        GET_WINDOWS_FUNCTION_NO_TYPECHECK(
            shell32_module, SetCurrentProcessExplicitAppUserModelID);
    }

    if (p_SetCurrentProcessExplicitAppUserModelID)
    {
        if (p_SetCurrentProcessExplicitAppUserModelID(L"SimonTatham.PuTTY") == S_OK)
        {
          return true;
        }
        return false;
    }
    /* Function doesn't exist, which is ok for Pre-7 systems */

    return true;

}