Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / WINNT / client_creds / shortcut.cpp
1 extern "C" {
2 #include <afs/param.h>
3 #include <afs/stds.h>
4 }
5
6 #include <objbase.h>
7 #include <initguid.h>
8 #include <windows.h>
9 #include <windowsx.h>
10 #include <shlobj.h>
11 #include <shellapi.h>
12 #include "shortcut.h"
13
14
15 /*
16  * ROUTINES ___________________________________________________________________
17  *
18  */
19
20 void Shortcut_Init (void)
21 {
22    CoInitialize(0);
23 }
24
25
26 void Shortcut_Exit (void)
27 {
28    CoUninitialize();
29 }
30
31
32 BOOL Shortcut_Create (LPTSTR pszTarget, LPCTSTR pszSource, LPTSTR pszDesc)
33 {
34    IShellLink *psl;
35    HRESULT rc = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
36    if (SUCCEEDED (rc))
37       {
38       IPersistFile *ppf;
39       rc = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
40       if (SUCCEEDED (rc))
41          { 
42          rc = psl->SetPath (pszSource);
43          if (SUCCEEDED (rc))
44             {
45             rc = psl->SetDescription (pszDesc ? pszDesc : pszSource);
46             if (SUCCEEDED (rc))
47                {
48 #ifdef UNICODE
49                rc = ppf->Save (pszTarget, TRUE);
50 #else
51                WORD wsz[ MAX_PATH ];
52                MultiByteToWideChar (CP_ACP, 0, pszTarget, -1, wsz, MAX_PATH);
53                rc = ppf->Save (wsz, TRUE);
54 #endif
55                }
56             }
57          ppf->Release ();
58          }
59       psl->Release ();
60       }
61    return SUCCEEDED(rc) ? TRUE : FALSE;
62
63
64
65 void Shortcut_FixStartup (LPCTSTR pszLinkName, BOOL fAutoStart)
66 {
67    TCHAR szShortcut[ MAX_PATH ] = TEXT("");
68
69    HKEY hk;
70    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), &hk) == 0)
71       {
72       DWORD dwSize = sizeof(szShortcut);
73       DWORD dwType = REG_SZ;
74       RegQueryValueEx (hk, TEXT("Common Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
75       if (szShortcut[0] == TEXT('\0'))
76          {
77          dwSize = sizeof(szShortcut);
78          dwType = REG_SZ;
79          RegQueryValueEx (hk, TEXT("Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
80          }
81       RegCloseKey (hk);
82       }
83    if (szShortcut[0] == TEXT('\0'))
84       {
85       GetWindowsDirectory (szShortcut, MAX_PATH);
86       lstrcat (szShortcut, TEXT("\\Start Menu\\Programs\\Startup"));
87       }
88    lstrcat (szShortcut, TEXT("\\"));
89    lstrcat (szShortcut, pszLinkName);
90
91    TCHAR szSource[ MAX_PATH ];
92    GetModuleFileName (GetModuleHandle(NULL), szSource, MAX_PATH);
93
94    if (fAutoStart)
95       {
96       Shortcut_Create (szShortcut, szSource);
97       }
98    else // (!g.fAutoStart)
99       {
100       DeleteFile (szShortcut);
101       }
102 }
103