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