[NTDLL]
[reactos.git] / reactos / base / applications / control / control.c
1 /*
2 * PROJECT: ReactOS System Control Panel
3 * FILE: base/applications/control/control.c
4 * PURPOSE: ReactOS System Control Panel
5 * PROGRAMMERS: Gero Kuehn (reactos.filter@gkware.com)
6 * Colin Finck (mail@colinfinck.de)
7 */
8
9 #include "control.h"
10
11 static const TCHAR szWindowClass[] = _T("DummyControlClass");
12
13 HANDLE hProcessHeap;
14 HINSTANCE hInst;
15
16 static
17 INT
18 OpenShellFolder(LPTSTR lpFolderCLSID)
19 {
20 TCHAR szParameters[MAX_PATH];
21
22 /* Open a shell folder using "explorer.exe".
23 The passed CLSID's are all subfolders of the "Control Panel" shell folder. */
24 _tcscpy(szParameters, _T("/n,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}"));
25 _tcscat(szParameters, lpFolderCLSID);
26
27 return (INT_PTR)ShellExecute(NULL,
28 _T("open"),
29 _T("explorer.exe"),
30 szParameters,
31 NULL,
32 SW_SHOWDEFAULT) > 32;
33 }
34
35 static
36 INT
37 RunControlPanel(LPTSTR lpCmd)
38 {
39 TCHAR szParameters[MAX_PATH];
40
41 _tcscpy(szParameters, _T("shell32.dll,Control_RunDLL "));
42 _tcscat(szParameters, lpCmd);
43
44 return RUNDLL(szParameters);
45 }
46
47 int
48 WINAPI
49 _tWinMain(HINSTANCE hInstance,
50 HINSTANCE hPrevInstance,
51 LPTSTR lpCmdLine,
52 int nCmdShow)
53 {
54 HKEY hKey;
55
56 hInst = hInstance;
57 hProcessHeap = GetProcessHeap();
58
59 /* Show the control panel window if no argument or "panel" was passed */
60 if(lpCmdLine[0] == 0 || !_tcsicmp(lpCmdLine, _T("panel")))
61 return OpenShellFolder(_T(""));
62
63 /* Check one of the built-in control panel handlers */
64 if (!_tcsicmp(lpCmdLine, _T("admintools"))) return OpenShellFolder(_T("\\::{D20EA4E1-3957-11d2-A40B-0C5020524153}"));
65 else if (!_tcsicmp(lpCmdLine, _T("color"))) return RunControlPanel(_T("desk.cpl")); /* TODO: Switch to the "Apperance" tab */
66 else if (!_tcsicmp(lpCmdLine, _T("date/time"))) return RunControlPanel(_T("timedate.cpl"));
67 else if (!_tcsicmp(lpCmdLine, _T("desktop"))) return RunControlPanel(_T("desk.cpl"));
68 else if (!_tcsicmp(lpCmdLine, _T("folders"))) return RUNDLL(_T("shell32.dll,Options_RunDLL"));
69 else if (!_tcsicmp(lpCmdLine, _T("fonts"))) return OpenShellFolder(_T("\\::{D20EA4E1-3957-11d2-A40B-0C5020524152}"));
70 else if (!_tcsicmp(lpCmdLine, _T("infrared"))) return RunControlPanel(_T("irprops.cpl"));
71 else if (!_tcsicmp(lpCmdLine, _T("international"))) return RunControlPanel(_T("intl.cpl"));
72 else if (!_tcsicmp(lpCmdLine, _T("keyboard"))) return RunControlPanel(_T("main.cpl @1"));
73 else if (!_tcsicmp(lpCmdLine, _T("mouse"))) return RunControlPanel(_T("main.cpl @0"));
74 else if (!_tcsicmp(lpCmdLine, _T("netconnections"))) return OpenShellFolder(_T("\\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}"));
75 else if (!_tcsicmp(lpCmdLine, _T("netware"))) return RunControlPanel(_T("nwc.cpl"));
76 else if (!_tcsicmp(lpCmdLine, _T("ports"))) return RunControlPanel(_T("sysdm.cpl")); /* TODO: Switch to the "Computer Name" tab */
77 else if (!_tcsicmp(lpCmdLine, _T("printers"))) return OpenShellFolder(_T("\\::{2227A280-3AEA-1069-A2DE-08002B30309D}"));
78 else if (!_tcsicmp(lpCmdLine, _T("scannercamera"))) return OpenShellFolder(_T("\\::{E211B736-43FD-11D1-9EFB-0000F8757FCD}"));
79 else if (!_tcsicmp(lpCmdLine, _T("schedtasks"))) return OpenShellFolder(_T("\\::{D6277990-4C6A-11CF-8D87-00AA0060F5BF}"));
80 else if (!_tcsicmp(lpCmdLine, _T("telephony"))) return RunControlPanel(_T("telephon.cpl"));
81 else if (!_tcsicmp(lpCmdLine, _T("userpasswords"))) return RunControlPanel(_T("nusrmgr.cpl")); /* Graphical User Account Manager */
82 else if (!_tcsicmp(lpCmdLine, _T("userpasswords2"))) return RUNDLL(_T("netplwiz.dll,UsersRunDll")); /* Dialog based advanced User Account Manager */
83
84 /* It is none of them, so look for a handler in the registry */
85 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
86 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls"),
87 0,
88 KEY_QUERY_VALUE,
89 &hKey) == ERROR_SUCCESS)
90 {
91 DWORD dwIndex;
92
93 for(dwIndex = 0; ; ++dwIndex)
94 {
95 DWORD dwDataSize;
96 DWORD dwValueSize = MAX_VALUE_NAME;
97 TCHAR szValueName[MAX_VALUE_NAME];
98
99 /* Get the value name and data size */
100 if(RegEnumValue(hKey,
101 dwIndex,
102 szValueName,
103 &dwValueSize,
104 0,
105 NULL,
106 NULL,
107 &dwDataSize) != ERROR_SUCCESS)
108 break;
109
110 /* Check if the parameter is the value name */
111 if(!_tcsicmp(lpCmdLine, szValueName))
112 {
113 LPTSTR pszData;
114
115 /* Allocate memory for the data plus two more characters, so we can quote the file name if required */
116 pszData = (LPTSTR) HeapAlloc(hProcessHeap,
117 0,
118 dwDataSize + 2 * sizeof(TCHAR));
119 ++pszData;
120
121 /* This value is the one we are looking for, so get the data. It is the path to a .cpl file */
122 if(RegQueryValueEx(hKey,
123 szValueName,
124 0,
125 NULL,
126 (LPBYTE)pszData,
127 &dwDataSize) == ERROR_SUCCESS)
128 {
129 INT nReturnValue;
130
131 /* Quote the file name if required */
132 if(*pszData != '\"')
133 {
134 *(--pszData) = '\"';
135 pszData[dwDataSize / sizeof(TCHAR)] = '\"';
136 pszData[(dwDataSize / sizeof(TCHAR)) + 1] = 0;
137 }
138
139 nReturnValue = RunControlPanel(pszData);
140 HeapFree(hProcessHeap,
141 0,
142 pszData);
143 RegCloseKey(hKey);
144
145 return nReturnValue;
146 }
147
148 HeapFree(hProcessHeap,
149 0,
150 pszData);
151 }
152 }
153
154 RegCloseKey(hKey);
155 }
156
157 /* It's none of the known parameters, so interpret the parameter as the file name of a control panel applet */
158 return RunControlPanel(lpCmdLine);
159 }