thread-and-timer-corrections-to-afscreds-20031121
[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 "shortcut.h"
25
26
27 /*
28  * ROUTINES ___________________________________________________________________
29  *
30  */
31
32 void Shortcut_Init (void)
33 {
34    CoInitialize(0);
35 }
36
37
38 void Shortcut_Exit (void)
39 {
40    CoUninitialize();
41 }
42
43
44 BOOL Shortcut_Create (LPTSTR pszTarget, LPCTSTR pszSource, LPTSTR pszDesc, LPTSTR pszArgs)
45 {
46    IShellLink *psl;
47    HRESULT rc = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
48    if (SUCCEEDED (rc))
49       {
50       IPersistFile *ppf;
51       rc = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
52       if (SUCCEEDED (rc))
53          { 
54          rc = psl->SetPath (pszSource);
55          if (SUCCEEDED (rc))
56             {
57             rc = psl->SetDescription (pszDesc ? pszDesc : pszSource);
58             if (SUCCEEDED (rc))
59                {
60                if ( pszArgs )
61                    rc = psl->SetArguments (pszArgs);
62                    if (SUCCEEDED (rc))
63                    {
64 #ifdef UNICODE
65                    rc = ppf->Save (pszTarget, TRUE);
66 #else
67                    WORD wsz[ MAX_PATH ];
68                    MultiByteToWideChar (CP_ACP, 0, pszTarget, -1, wsz, MAX_PATH);
69                    rc = ppf->Save (wsz, TRUE);
70 #endif
71                    }
72                }
73             }
74          ppf->Release ();
75          }
76       psl->Release ();
77       }
78    return SUCCEEDED(rc) ? TRUE : FALSE;
79
80
81
82 void Shortcut_FixStartup (LPCTSTR pszLinkName, BOOL fAutoStart)
83 {
84    TCHAR szShortcut[ MAX_PATH + 10 ] = TEXT("");
85
86    HKEY hk;
87    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), &hk) == 0)
88       {
89       DWORD dwSize = sizeof(szShortcut);
90       DWORD dwType = REG_SZ;
91       RegQueryValueEx (hk, TEXT("Common Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
92       if (szShortcut[0] == TEXT('\0'))
93          {
94          dwSize = sizeof(szShortcut);
95          dwType = REG_SZ;
96          RegQueryValueEx (hk, TEXT("Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
97          }
98       RegCloseKey (hk);
99       }
100    if (szShortcut[0] == TEXT('\0'))
101       {
102       GetWindowsDirectory (szShortcut, MAX_PATH);
103       lstrcat (szShortcut, TEXT("\\Start Menu\\Programs\\Startup"));
104       }
105    lstrcat (szShortcut, TEXT("\\"));
106    lstrcat (szShortcut, pszLinkName);
107
108    TCHAR szSource[ MAX_PATH ];
109    GetModuleFileName (GetModuleHandle(NULL), szSource, MAX_PATH);
110
111    if (fAutoStart)
112       {
113       Shortcut_Create (szShortcut, szSource, "Autostart Authentication Agent");
114       }
115    else // (!g.fAutoStart)
116       {
117       DeleteFile (szShortcut);
118       }
119 }
120