ReactOS 0.4.15-dev-8145-ga541a46
control.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS System Control Panel
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: ReactOS System Control Panel
5 * COPYRIGHT: Copyright 2004 Gero Kuehn (reactos.filter@gkware.com)
6 * Copyright 2008 Colin Finck (colin@reactos.org)
7 * Copyright 2014 Hermès Bélusca-Maïto (hermes.belusca-maito@reactos.org)
8 */
9
10#include <stdio.h>
11
12#define WIN32_NO_STATUS
13#define COBJMACROS
14
15
16#include <windef.h>
17#include <winbase.h>
18#include <winuser.h>
19#include <winreg.h>
20#include <shellapi.h>
21#include <strsafe.h>
22#include <objbase.h>
23#include <shobjidl.h>
24#include <shlguid.h>
25
26#include "resource.h"
27
28#define MAX_VALUE_NAME 16383
29
30/*
31 * Macro for calling "rundll32.exe"
32 * According to MSDN, ShellExecute returns a value greater than 32
33 * if the operation was successful.
34 */
35#define RUNDLL(param) \
36 ((INT_PTR)ShellExecuteW(NULL, L"open", L"rundll32.exe", (param), NULL, SW_SHOWDEFAULT) > 32)
37
38VOID
41
42static BOOL
44{
45 if (*Arg == '/' || *Arg == '-')
46 {
47 return !lstrcmpiW(Arg+1, Switch);
48 }
49 return FALSE;
50}
51
52static HRESULT
54{
56 if (SUCCEEDED(hr))
57 {
58 IOpenControlPanel *pOCP;
59 hr = CoCreateInstance(&CLSID_OpenControlPanel, NULL, CLSCTX_INPROC_SERVER,
60 &IID_IOpenControlPanel, (void**)&pOCP);
61 if (SUCCEEDED(hr))
62 {
63 hr = IOpenControlPanel_Open(pOCP, Name, Page, NULL);
64 IOpenControlPanel_Release(pOCP);
65 }
67 }
68 return hr;
69}
70
71static INT
72OpenShellFolder(LPWSTR lpFolderCLSID)
73{
74 WCHAR szParameters[MAX_PATH];
75
76 /*
77 * Open a shell folder using "explorer.exe". The passed CLSIDs
78 * are all subfolders of the "Control Panel" shell folder.
79 */
80 StringCbCopyW(szParameters, sizeof(szParameters), L"/n,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}");
81 StringCbCatW(szParameters, sizeof(szParameters), lpFolderCLSID);
82
84 L"open",
85 L"explorer.exe",
86 szParameters,
87 NULL,
88 SW_SHOWDEFAULT) > 32;
89}
90
91static INT
93{
94 WCHAR szParameters[MAX_PATH];
95 StringCchCopyW(szParameters, ARRAYSIZE(szParameters), L"shell32.dll,Control_RunDLL ");
96 if (FAILED(StringCchCatW(szParameters, ARRAYSIZE(szParameters), lpCmd)))
97 return 0;
98
99 return RUNDLL(szParameters);
100}
101
102INT
103WINAPI
105 HINSTANCE hPrevInstance,
106 LPWSTR lpCmdLine,
107 INT nCmdShow)
108{
109 HKEY hKey;
110 LPWSTR *argv;
111 int argc;
112
113 /* Show the control panel window if no argument or "panel" was passed */
114 if (*lpCmdLine == 0 || !_wcsicmp(lpCmdLine, L"panel"))
115 return OpenShellFolder(L"");
116
117 /* Map legacy control panels */
118 if (!_wcsicmp(lpCmdLine, L"sticpl.cpl")) lpCmdLine = (LPWSTR) L"scannercamera";
119
120 /* Check one of the built-in control panel handlers */
121 if (!_wcsicmp(lpCmdLine, L"admintools")) return OpenShellFolder(L"\\::{D20EA4E1-3957-11d2-A40B-0C5020524153}");
122 else if (!_wcsicmp(lpCmdLine, L"color")) return RunControlPanel(L"desk.cpl,,2");
123 else if (!_wcsicmp(lpCmdLine, L"date/time")) return RunControlPanel(L"timedate.cpl");
124 else if (!_wcsicmp(lpCmdLine, L"desktop")) return RunControlPanel(L"desk.cpl");
125 else if (!_wcsicmp(lpCmdLine, L"folders")) return RUNDLL(L"shell32.dll,Options_RunDLL");
126 else if (!_wcsicmp(lpCmdLine, L"fonts")) return OpenShellFolder(L"\\::{D20EA4E1-3957-11d2-A40B-0C5020524152}");
127 else if (!_wcsicmp(lpCmdLine, L"infrared")) return RunControlPanel(L"irprops.cpl");
128 else if (!_wcsicmp(lpCmdLine, L"international")) return RunControlPanel(L"intl.cpl");
129 else if (!_wcsicmp(lpCmdLine, L"keyboard")) return RunControlPanel(L"main.cpl @1");
130 else if (!_wcsicmp(lpCmdLine, L"mouse")) return RunControlPanel(L"main.cpl @0");
131 else if (!_wcsicmp(lpCmdLine, L"netconnections")) return OpenShellFolder(L"\\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}");
132 else if (!_wcsicmp(lpCmdLine, L"netware")) return RunControlPanel(L"nwc.cpl");
133 else if (!_wcsicmp(lpCmdLine, L"ports")) return RunControlPanel(L"sysdm.cpl,,1");
134 else if (!_wcsicmp(lpCmdLine, L"printers")) return OpenShellFolder(L"\\::{2227A280-3AEA-1069-A2DE-08002B30309D}");
135 else if (!_wcsicmp(lpCmdLine, L"scannercamera")) return OpenShellFolder(L"\\::{E211B736-43FD-11D1-9EFB-0000F8757FCD}");
136 else if (!_wcsicmp(lpCmdLine, L"schedtasks")) return OpenShellFolder(L"\\::{D6277990-4C6A-11CF-8D87-00AA0060F5BF}");
137 else if (!_wcsicmp(lpCmdLine, L"telephony")) return RunControlPanel(L"telephon.cpl");
138 else if (!_wcsicmp(lpCmdLine, L"userpasswords")) return RunControlPanel(L"nusrmgr.cpl"); /* Graphical User Account Manager */
139 else if (!_wcsicmp(lpCmdLine, L"userpasswords2")) return RUNDLL(L"netplwiz.dll,UsersRunDll"); /* Dialog based advanced User Account Manager */
140
141 /* https://learn.microsoft.com/en-us/windows/win32/shell/executing-control-panel-items#windows-vista-canonical-names */
142 argv = CommandLineToArgvW(lpCmdLine, &argc);
143 if (argv)
144 {
145 UINT argi = 0;
146 HRESULT hr = -1;
147 if (argc >= 2 && IsSwitch(L"name", argv[argi + 0]))
148 {
149 LPCWSTR pszPage = NULL;
150 if (argc >= 4 && IsSwitch(L"page", argv[argi + 2]))
151 {
152 pszPage = argv[argi + 3];
153 }
154 hr = OpenControlPanelItem(argv[argi + 1], pszPage);
155 }
157 if (hr != -1)
158 {
159 return SUCCEEDED(hr);
160 }
161 }
162
163 /* It is none of them, so look for a handler in the registry */
165 L"Software\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls",
166 0,
168 &hKey) == ERROR_SUCCESS)
169 {
170 DWORD dwIndex;
171
172 for (dwIndex = 0; ; ++dwIndex)
173 {
174 DWORD dwDataSize;
175 DWORD dwValueSize = MAX_VALUE_NAME;
176 WCHAR szValueName[MAX_VALUE_NAME];
177
178 /* Get the value name and data size */
180 dwIndex,
181 szValueName,
182 &dwValueSize,
183 0,
184 NULL,
185 NULL,
186 &dwDataSize) != ERROR_SUCCESS)
187 {
188 break;
189 }
190
191 /* Check if the parameter is the value name */
192 if (!_wcsicmp(lpCmdLine, szValueName))
193 {
194 /*
195 * Allocate memory for the data plus two more characters,
196 * so we can quote the file name if required.
197 */
198 LPWSTR pszData;
199 pszData = HeapAlloc(GetProcessHeap(),
200 0,
201 dwDataSize + 2 * sizeof(WCHAR));
202 ++pszData;
203
204 /*
205 * This value is the one we are looking for, so get the data.
206 * It is the path to a .cpl file.
207 */
209 szValueName,
210 0,
211 NULL,
212 (LPBYTE)pszData,
213 &dwDataSize) == ERROR_SUCCESS)
214 {
215 INT nReturnValue;
216
217 /* Quote the file name if required */
218 if (*pszData != L'\"')
219 {
220 *(--pszData) = L'\"';
221 pszData[dwDataSize / sizeof(WCHAR)] = L'\"';
222 pszData[(dwDataSize / sizeof(WCHAR)) + 1] = 0;
223 }
224
225 nReturnValue = RunControlPanel(pszData);
226 HeapFree(GetProcessHeap(), 0, pszData);
228
229 return nReturnValue;
230 }
231
232 HeapFree(GetProcessHeap(), 0, pszData);
233 }
234 }
235
237 }
238
239 /*
240 * It's none of the known parameters, so interpret the parameter
241 * as the file name of a control panel applet.
242 */
243 return RunControlPanel(lpCmdLine);
244}
static int argc
Definition: ServiceArgs.c:12
HWND hWnd
Definition: settings.c:17
static BOOL IsSwitch(LPCWSTR Switch, LPCWSTR Arg)
Definition: control.c:43
static HRESULT OpenControlPanelItem(LPCWSTR Name, LPCWSTR Page)
Definition: control.c:53
static INT OpenShellFolder(LPWSTR lpFolderCLSID)
Definition: control.c:72
#define RUNDLL(param)
Definition: control.c:35
static INT RunControlPanel(LPCWSTR lpCmd)
Definition: control.c:92
#define MAX_VALUE_NAME
Definition: control.c:28
INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nCmdShow)
Definition: control.c:104
VOID WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
Definition: control.c:1172
#define RegCloseKey(hKey)
Definition: registry.h:49
HINSTANCE hInstance
Definition: charmap.c:19
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2830
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4261
HRESULT WINAPI DECLSPEC_HOTPATCH CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
Definition: compobj.c:3325
HRESULT WINAPI CoInitialize(LPVOID lpReserved)
Definition: compobj.c:1964
void WINAPI DECLSPEC_HOTPATCH CoUninitialize(void)
Definition: compobj.c:2067
HINSTANCE hInst
Definition: dxdiag.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FAILED(hr)
Definition: intsafe.h:51
#define argv
Definition: mplay32.c:18
unsigned int UINT
Definition: ndis.h:50
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
_In_ PVOID _Out_opt_ BOOLEAN _Out_opt_ PPFN_NUMBER Page
Definition: mm.h:1306
#define L(x)
Definition: ntvdm.h:50
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
LPWSTR *WINAPI CommandLineToArgvW(LPCWSTR lpCmdline, int *numargs)
Definition: shell32_main.c:80
HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
Definition: shlexec.cpp:2486
HRESULT hr
Definition: shlfolder.c:183
STRSAFEAPI StringCchCatW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:325
STRSAFEAPI StringCbCopyW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:166
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
STRSAFEAPI StringCbCatW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:342
Definition: ftp_var.h:139
int32_t INT_PTR
Definition: typedefs.h:64
unsigned char * LPBYTE
Definition: typedefs.h:53
int32_t INT
Definition: typedefs.h:58
#define WINAPI
Definition: msvc.h:6
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define SW_SHOWDEFAULT
Definition: winuser.h:780
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185