1 /*
2  * PROJECT:     ReactOS Applications
3  * LICENSE:     LGPL - See COPYING in the top level directory
4  * FILE:        base/applications/msconfig/startuppage.c
5  * PURPOSE:     Startup page message handler
6  * COPYRIGHT:   Copyright 2005-2006 Christoph von Wittich <Christoph@ApiViewer.de>
7  *
8  */
9 
10 #include "precomp.h"
11 
12 HWND hStartupPage;
13 HWND hStartupListCtrl;
14 HWND hStartupDialog;
15 
16 void GetAutostartEntriesFromRegistry ( HKEY hRootKey, TCHAR* KeyName );
17 void GetDisabledAutostartEntriesFromRegistry (TCHAR * szBasePath);
18 
19 INT_PTR CALLBACK
20 StartupPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
21 {
22     LV_COLUMN   column;
23     TCHAR       szTemp[256];
24     DWORD dwStyle;
25 
26     UNREFERENCED_PARAMETER(lParam);
27     UNREFERENCED_PARAMETER(wParam);
28 
29     switch (message) {
30     case WM_INITDIALOG:
31 
32         hStartupListCtrl = GetDlgItem(hDlg, IDC_STARTUP_LIST);
33         hStartupDialog = hDlg;
34 
35         dwStyle = (DWORD) SendMessage(hStartupListCtrl, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
36         dwStyle = dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES;
37         SendMessage(hStartupListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, dwStyle);
38 
39         SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
40 
41         // Initialize the application page's controls
42         column.mask = LVCF_TEXT | LVCF_WIDTH;
43 
44         LoadString(hInst, IDS_STARTUP_COLUMN_ELEMENT, szTemp, 256);
45         column.pszText = szTemp;
46         column.cx = 150;
47         (void)ListView_InsertColumn(hStartupListCtrl, 0, &column);
48 
49         column.mask = LVCF_TEXT | LVCF_WIDTH;
50         LoadString(hInst, IDS_STARTUP_COLUMN_CMD, szTemp, 256);
51         column.pszText = szTemp;
52         column.cx = 150;
53         (void)ListView_InsertColumn(hStartupListCtrl, 1, &column);
54 
55         column.mask = LVCF_TEXT | LVCF_WIDTH;
56         LoadString(hInst, IDS_STARTUP_COLUMN_PATH, szTemp, 256);
57         column.pszText = szTemp;
58         column.cx = 250;
59         (void)ListView_InsertColumn(hStartupListCtrl, 2, &column);
60 
61         GetAutostartEntriesFromRegistry(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"));
62         GetAutostartEntriesFromRegistry(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"));
63         GetDisabledAutostartEntriesFromRegistry (_T("SOFTWARE\\Microsoft\\Shared Tools\\MSConfig\\startupreg"));
64         GetDisabledAutostartEntriesFromRegistry (_T("SOFTWARE\\Microsoft\\Shared Tools\\MSConfig\\startupfolder"));
65 
66         //FIXME: What about HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Userinit
67         //FIXME: Common Startup (startmenu)
68 
69         return TRUE;
70     }
71 
72     return 0;
73 }
74 
75 
76  void
77 GetDisabledAutostartEntriesFromRegistry (TCHAR * szBasePath)
78 {
79     HKEY hKey, hSubKey;
80     DWORD Index, SubIndex, dwValues, dwSubValues;
81     DWORD dwValueLength, dwDataLength = MAX_VALUE_NAME;
82     LV_ITEM item;
83     TCHAR* Data;
84     TCHAR szValueName[MAX_KEY_LENGTH];
85     TCHAR szSubValueName[MAX_KEY_LENGTH];
86     TCHAR szSubPath[MAX_KEY_LENGTH];
87 
88     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szBasePath, 0, KEY_READ | KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS)
89     {
90         if (RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwValues, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
91         {
92             for (Index = 0; Index < dwValues; Index++)
93             {
94                 dwValueLength = MAX_KEY_LENGTH;
95                 dwDataLength = MAX_VALUE_NAME;
96                 Data = HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR));
97                 if (Data == NULL)
98                     break;
99 
100                 if(RegEnumKeyEx(hKey, Index, szValueName, &dwValueLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
101                 {
102                     HeapFree(GetProcessHeap(), 0, Data);
103                     continue;
104                 }
105                 _stprintf(szSubPath, _T("%s\\%s"), szBasePath, szValueName);
106                 memset(&item, 0, sizeof(LV_ITEM));
107                 item.mask = LVIF_TEXT;
108                 item.iImage = 0;
109                 item.pszText = szValueName;
110                 item.iItem = ListView_GetItemCount(hStartupListCtrl);
111                 item.lParam = 0;
112                 (void)ListView_InsertItem(hStartupListCtrl, &item);
113                 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubPath, 0, KEY_READ | KEY_ENUMERATE_SUB_KEYS, &hSubKey) == ERROR_SUCCESS)
114                 {
115                     if (RegQueryInfoKey(hSubKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwSubValues, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
116                     {
117                         for(SubIndex = 0; SubIndex < dwSubValues; SubIndex++)
118                         {
119                             dwValueLength = MAX_KEY_LENGTH;
120                             dwDataLength = MAX_VALUE_NAME;
121                             if(RegEnumValue(hSubKey, SubIndex, szSubValueName, &dwValueLength, NULL, NULL, (LPBYTE)Data, &dwDataLength) == ERROR_SUCCESS)
122                             {
123                                 item.iSubItem = -1;
124                                 if (!_tcscmp(szSubValueName, _T("command")))
125                                     item.iSubItem = 1;
126                                 else if (!_tcscmp(szSubValueName, _T("key")) || !_tcscmp(szSubValueName, _T("location")))
127                                     item.iSubItem = 2;
128                                 else if (!_tcscmp(szSubValueName, _T("item")))
129                                     item.iSubItem = 0;
130                                 if (item.iSubItem != -1)
131                                 {
132                                     GetLongPathName(Data, Data, (DWORD) _tcsclen(Data));
133                                     item.pszText = Data;
134                                     SendMessage(hStartupListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
135                                 }
136                             }
137                         }
138                     }
139                 }
140                 RegCloseKey(hSubKey);
141                 HeapFree(GetProcessHeap(), 0, Data);
142             }
143         }
144         RegCloseKey(hKey);
145     }
146 }
147 
148 void
149 GetAutostartEntriesFromRegistry ( HKEY hRootKey, TCHAR* KeyName )
150 {
151     HKEY hKey;
152     DWORD Index, dwValues, retVal, dwType;
153     DWORD dwValueLength, dwDataLength = MAX_VALUE_NAME;
154     TCHAR* Data;
155     TCHAR lpValueName[MAX_KEY_LENGTH];
156     TCHAR Path[MAX_KEY_LENGTH + 5];
157     LV_ITEM item;
158 
159     if (RegOpenKeyEx(hRootKey, KeyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
160     {
161         if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValues, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
162         {
163             for (Index = 0, retVal = ERROR_SUCCESS; Index < dwValues; Index++)
164             {
165                 dwValueLength = MAX_KEY_LENGTH;
166                 dwDataLength = MAX_VALUE_NAME;
167                 Data = HeapAlloc(GetProcessHeap(), 0, MAX_VALUE_NAME * sizeof(TCHAR));
168                 if (Data == NULL)
169                     break;
170                 retVal = RegEnumValue(hKey, Index, lpValueName, &dwValueLength, NULL, &dwType, (LPBYTE)Data, &dwDataLength);
171                 if (retVal == ERROR_SUCCESS)
172                 {
173                     memset(&item, 0, sizeof(LV_ITEM));
174                     item.mask = LVIF_TEXT;
175                     item.iImage = 0;
176                     item.pszText = lpValueName;
177                     item.iItem = ListView_GetItemCount(hStartupListCtrl);
178                     item.lParam = 0;
179                     (void)ListView_InsertItem(hStartupListCtrl, &item);
180                     ListView_SetCheckState(hStartupListCtrl, item.iItem, TRUE);
181 
182                     if ((dwType == REG_SZ) || (dwType == REG_EXPAND_SZ))
183                     {
184                         GetLongPathName(Data, Data, (DWORD) _tcsclen(Data));
185                         item.pszText = Data;
186                         item.iSubItem = 1;
187                         SendMessage(hStartupListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
188                     }
189 
190                     switch ((ULONG_PTR)hRootKey)
191                     {
192                     case (ULONG_PTR)HKEY_LOCAL_MACHINE:
193                         _tcscpy(Path, _T("HKLM\\\0"));
194                         break;
195                     case (ULONG_PTR)HKEY_CURRENT_USER:
196                         _tcscpy(Path, _T("HKCU\\\0"));
197                         break;
198                     default:
199                         _tcscpy(Path, _T("\0"));
200                     }
201 
202                     _tcscat(Path, KeyName);
203                     item.pszText = Path;
204                     item.iSubItem = 2;
205                     SendMessage(hStartupListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
206                 }
207                 HeapFree(GetProcessHeap(), 0, Data);
208             }
209         }
210         RegCloseKey(hKey);
211     }
212 
213 }
214