811c901dfbb30f3a43033a93b03c5061eee22ac2
[openafs.git] / src / WINNT / client_creds / shortcut.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include <objbase.h>
16 #include <initguid.h>
17 #include <windows.h>
18 #include <windowsx.h>
19 #undef INITGUID
20 #include <shlobj.h>
21 #include <shellapi.h>
22 #include <shobjidl.h>
23 #include <shlguid.h>
24 #include "afscreds.h"
25 #include "shortcut.h"
26
27
28 /*
29  * ROUTINES ___________________________________________________________________
30  *
31  */
32
33 void Shortcut_Init (void)
34 {
35    CoInitialize(0);
36 }
37
38
39 void Shortcut_Exit (void)
40 {
41    CoUninitialize();
42 }
43
44
45 BOOL Shortcut_Create (LPTSTR pszTarget, LPCTSTR pszSource, LPTSTR pszDesc, LPTSTR pszArgs)
46 {
47    IShellLink *psl;
48    HRESULT rc = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
49    if (SUCCEEDED (rc))
50       {
51       IPersistFile *ppf;
52       rc = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
53       if (SUCCEEDED (rc))
54          { 
55          rc = psl->SetPath (pszSource);
56          if (SUCCEEDED (rc))
57             {
58             rc = psl->SetDescription (pszDesc ? pszDesc : pszSource);
59             if (SUCCEEDED (rc))
60                {
61                if ( pszArgs )
62                    rc = psl->SetArguments (pszArgs);
63                    if (SUCCEEDED (rc))
64                    {
65 #ifdef UNICODE
66                    rc = ppf->Save (pszTarget, TRUE);
67 #else
68                    WORD wsz[ MAX_PATH ];
69                    MultiByteToWideChar (CP_ACP, 0, pszTarget, -1, wsz, MAX_PATH);
70                    rc = ppf->Save (wsz, TRUE);
71 #endif
72                    }
73                }
74             }
75          ppf->Release ();
76          }
77       psl->Release ();
78       }
79    return SUCCEEDED(rc) ? TRUE : FALSE;
80
81
82
83 void Shortcut_FixStartup (LPCTSTR pszLinkName, BOOL fAutoStart)
84 {
85    TCHAR szShortcut[ MAX_PATH + 10 ] = TEXT("");
86
87    HKEY hk;
88    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), &hk) == 0)
89       {
90       DWORD dwSize = sizeof(szShortcut);
91       DWORD dwType = REG_SZ;
92       RegQueryValueEx (hk, TEXT("Common Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
93       if (szShortcut[0] == TEXT('\0'))
94          {
95          dwSize = sizeof(szShortcut);
96          dwType = REG_SZ;
97          RegQueryValueEx (hk, TEXT("Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
98          }
99       RegCloseKey (hk);
100       }
101    if (szShortcut[0] == TEXT('\0'))
102       {
103       GetWindowsDirectory (szShortcut, MAX_PATH);
104       lstrcat (szShortcut, TEXT("\\Start Menu\\Programs\\Startup"));
105       }
106    lstrcat (szShortcut, TEXT("\\"));
107    lstrcat (szShortcut, pszLinkName);
108
109    TCHAR szSource[ MAX_PATH ];
110    GetModuleFileName (GetModuleHandle(NULL), szSource, MAX_PATH);
111
112    if (fAutoStart)
113       {
114       Shortcut_Create (szShortcut, szSource, "Autostart Authentication Agent", AFSCREDS_SHORTCUT_OPTIONS);
115       }
116    else // (!g.fAutoStart)
117       {
118       DeleteFile (szShortcut);
119       }
120 }
121