aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/internetFeatures.cpp
blob: c2c7bd52b582a4617c6f880706b7af8d1ef3a478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <main.h>
#include <internetFeatures.h>
#include <shlwapi.h>
#include <strsafe.h>

#define REGISTRY_FEATURE_CONTROL		L"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl"

InternetFeatures::InternetFeatures()
	: module(NULL), loadResult(S_FALSE), 
	  CoInternetSetFeatureEnabled(NULL),
	  CoInternetIsFeatureEnabled(NULL),
	  processName_(NULL)
{
}

InternetFeatures::~InternetFeatures()
{
	if (NULL != module)
	{
		FreeLibrary(module);
	}

	if (NULL != processName_)
		free(processName_);
}

HRESULT InternetFeatures::LoadModule()
{
	if (S_FALSE != loadResult) 
		return loadResult;

	module = LoadLibrary(L"UrlMon.dll");
	if (NULL == module)
	{
		DWORD errorCode = GetLastError();
		loadResult = HRESULT_FROM_WIN32(errorCode);
		return loadResult;
	}
	else 
	{
		loadResult = S_OK;
	}

	CoInternetSetFeatureEnabled = (COINTERNETSETFEATUREENABLED)GetProcAddress(module, "CoInternetSetFeatureEnabled");
	CoInternetIsFeatureEnabled = (COINTERNETISFEATUREENABLED)GetProcAddress(module, "CoInternetIsFeatureEnabled");

	return loadResult;
}

const wchar_t *InternetFeatures::GetProcessName()
{
	if (NULL == processName_)
	{
		wchar_t buffer[2*MAX_PATH] = {0};
		unsigned long length = GetModuleFileNameW(NULL, buffer, ARRAYSIZE(buffer));
		if (0 == length)
			return NULL;

		const wchar_t *fileName = PathFindFileName(buffer);
		length = lstrlenW(fileName);
		processName_ = (wchar_t *)calloc((length + 1), sizeof(wchar_t));
		if (NULL == processName_)
			return NULL;

		memcpy(processName_, fileName, sizeof(wchar_t)*length);
		processName_[length]=L'\0';
	}

	return processName_;
}

HRESULT InternetFeatures::SetEnabled(INTERNETFEATURELIST FeatureEntry, DWORD dwFlags, BOOL fEnable)
{
	HRESULT hr = LoadModule();
	if (FAILED(hr)) return hr;
	if (NULL == CoInternetSetFeatureEnabled) return E_NOINTERFACE;
    return CoInternetSetFeatureEnabled(FeatureEntry, dwFlags, fEnable);
}

HRESULT InternetFeatures::IsEnabled(INTERNETFEATURELIST FeatureEntry, DWORD dwFlags)
{
	HRESULT hr = LoadModule();
	if (FAILED(hr)) return hr;
	if (NULL == CoInternetIsFeatureEnabled) return E_NOINTERFACE;
	return CoInternetIsFeatureEnabled(FeatureEntry, dwFlags);
}

HRESULT InternetFeatures::SetDWORDFeature(const wchar_t *featureName, BOOL perUser, unsigned long value)
{
	if (NULL == featureName 
		|| L'\0' == *featureName)
	{
		return E_INVALIDARG;
	}

	const wchar_t *processName = GetProcessName();
	if (NULL == processName)
		return E_UNEXPECTED;

	HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
	HKEY key = NULL;
	unsigned long disposition = 0;
	long errorCode = 0;
	HRESULT result = S_OK;

	wchar_t buffer[MAX_PATH] = {0};
	if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer), 
							  REGISTRY_FEATURE_CONTROL L"\\" L"%s", 
							  featureName)))
	{
		return E_OUTOFMEMORY;
	}

	errorCode = RegCreateKeyEx(rootKey, buffer, 
						  NULL, NULL, REG_OPTION_VOLATILE, KEY_WRITE, 
						  NULL, &key, &disposition);

	if (ERROR_SUCCESS != errorCode)
		return HRESULT_FROM_WIN32(errorCode);

	errorCode = RegSetValueEx(key, processName, 0, REG_DWORD, (const BYTE*)&value, 
							  sizeof(unsigned long));

	if (ERROR_SUCCESS != errorCode)
	{
		result = HRESULT_FROM_WIN32(errorCode);
	}
	else
	{
		result = S_OK;
	}

	RegCloseKey(key);

	return result;
}

HRESULT InternetFeatures::GetDWORDFeature(const wchar_t *featureName, BOOL perUser, unsigned long *value)
{
	if (NULL == featureName 
		|| L'\0' == *featureName)
	{
		return E_INVALIDARG;
	}

	if (NULL == value)
		return E_POINTER;

	const wchar_t *processName = GetProcessName();
	if (NULL == processName)
		return E_UNEXPECTED;

	HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
	HKEY key = NULL;
	long errorCode = 0;
	HRESULT result = S_OK;
	unsigned long valueType = 0;
	unsigned long valueSize = 0;

	wchar_t buffer[MAX_PATH] = {0};
	if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer), 
							  REGISTRY_FEATURE_CONTROL L"\\" L"%s", 
							  featureName)))
	{
		return E_OUTOFMEMORY;
	}

	errorCode = RegOpenKeyEx(rootKey, buffer, 0, KEY_READ, &key);
	if (ERROR_SUCCESS != errorCode)
		return HRESULT_FROM_WIN32(errorCode);

	valueSize = sizeof(unsigned long);

	errorCode = RegQueryValueEx(key, processName, 0, &valueType, 
								(BYTE*)value, &valueSize);

	if (ERROR_SUCCESS != errorCode)
	{
		result = HRESULT_FROM_WIN32(errorCode);
	}
	else
	{
		if (REG_DWORD != valueType)
			result = E_UNEXPECTED;
		else
			result = S_OK;
	}

	RegCloseKey(key);

	return result;
}

void InternetFeatures::DeleteFeature(const wchar_t *featureName, BOOL perUser)
{
	if (NULL == featureName 
		|| L'\0' == *featureName)
	{
		return;
	}

	const wchar_t *processName = GetProcessName();
	if (NULL == processName)
		return;

	HKEY rootKey = (FALSE != perUser) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
	HKEY key = NULL;
	long errorCode = 0;

	wchar_t buffer[MAX_PATH] = {0};
	if (FAILED(StringCchPrintf(buffer, ARRAYSIZE(buffer), 
							  REGISTRY_FEATURE_CONTROL L"\\" L"%s", 
							  featureName)))
	{
		return;
	}

	errorCode = RegOpenKeyEx(rootKey, buffer, 0, KEY_WRITE, &key);
	if (ERROR_SUCCESS != errorCode)
		return;

	RegDeleteValue(key, processName);
	RegCloseKey(key);
}