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