Skip to content

Commit 7553864

Browse files
committed
move InternetCloseHandle to finally block
1 parent 8034464 commit 7553864

File tree

1 file changed

+88
-92
lines changed

1 file changed

+88
-92
lines changed

FlashpointSecurePlayer/FlashpointProxy.cs

Lines changed: 88 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPer
121121
internetPerConnOptionList.dwOptionCount = (uint)internetPerConnOptionListOptions.Length;
122122
internetPerConnOptionList.dwOptionError = 0;
123123

124-
// query internet options
125-
bool result = InternetQueryOption(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref internetPerConnOptionList, ref internetPerConnOptionListSize);
126-
127-
if (!result) {
124+
if (!InternetQueryOption(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, ref internetPerConnOptionList, ref internetPerConnOptionListSize)) {
128125
throw new FlashpointProxyException("Could not query the Internet Options.");
129126
}
130127
} catch {
@@ -134,134 +131,133 @@ private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPer
134131
}
135132

136133
public static void Enable(string proxyServer) {
137-
IntPtr internetHandle = IntPtr.Zero;
138-
internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, IntPtr.Zero, IntPtr.Zero, 0);
134+
IntPtr internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, IntPtr.Zero, IntPtr.Zero, 0);
139135

140136
if (internetHandle == IntPtr.Zero) {
141137
throw new FlashpointProxyException("Could not open the Internet Handle.");
142138
}
143139

144-
// initialize a INTERNET_PER_CONN_OPTION_LIST instance
145-
INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST();
146-
uint internetPerConnOptionListSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
140+
try {
141+
// initialize a INTERNET_PER_CONN_OPTION_LIST instance
142+
INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST();
143+
uint internetPerConnOptionListSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
147144

148-
// create two options
149-
INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2];
145+
// create two options
146+
INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2];
150147

151-
// set PROXY flags
152-
internetPerConnOptionListOptions[0] = new INTERNET_PER_CONN_OPTION {
153-
dwOption = INTERNET_PER_CONN_OPTION_OPTION.INTERNET_PER_CONN_FLAGS
154-
};
148+
// set PROXY flags
149+
internetPerConnOptionListOptions[0] = new INTERNET_PER_CONN_OPTION {
150+
dwOption = INTERNET_PER_CONN_OPTION_OPTION.INTERNET_PER_CONN_FLAGS
151+
};
155152

156-
internetPerConnOptionListOptions[0].Value.dwValue = (uint)INTERNET_PER_CONN_FLAGS_VALUEFlags.PROXY_TYPE_PROXY;
157-
158-
// set proxy name
159-
internetPerConnOptionListOptions[1] = new INTERNET_PER_CONN_OPTION {
160-
dwOption = INTERNET_PER_CONN_OPTION_OPTION.INTERNET_PER_CONN_PROXY_SERVER
161-
};
153+
internetPerConnOptionListOptions[0].Value.dwValue = (uint)INTERNET_PER_CONN_FLAGS_VALUEFlags.PROXY_TYPE_PROXY;
162154

163-
internetPerConnOptionListOptions[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
155+
// set proxy name
156+
internetPerConnOptionListOptions[1] = new INTERNET_PER_CONN_OPTION {
157+
dwOption = INTERNET_PER_CONN_OPTION_OPTION.INTERNET_PER_CONN_PROXY_SERVER
158+
};
164159

165-
// allocate memory for the INTERNET_PER_CONN_OPTION_LIST Options
166-
internetPerConnOptionList.pOptions = Marshal.AllocCoTaskMem(Marshal.SizeOf(internetPerConnOptionListOptions[0]) + Marshal.SizeOf(internetPerConnOptionListOptions[1]));
160+
internetPerConnOptionListOptions[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
167161

168-
try {
169-
IntPtr internetPerConnOptionListOptionPointer = internetPerConnOptionList.pOptions;
162+
// allocate memory for the INTERNET_PER_CONN_OPTION_LIST Options
163+
internetPerConnOptionList.pOptions = Marshal.AllocCoTaskMem(Marshal.SizeOf(internetPerConnOptionListOptions[0]) + Marshal.SizeOf(internetPerConnOptionListOptions[1]));
170164

171-
// marshal data from a managed object to unmanaged memory
172-
for (int i = 0; i < internetPerConnOptionListOptions.Length; i++) {
173-
Marshal.StructureToPtr(internetPerConnOptionListOptions[i], internetPerConnOptionListOptionPointer, false);
174-
internetPerConnOptionListOptionPointer = (IntPtr)((long)internetPerConnOptionListOptionPointer + Marshal.SizeOf(internetPerConnOptionListOptions[i]));
175-
}
165+
try {
166+
IntPtr internetPerConnOptionListOptionPointer = internetPerConnOptionList.pOptions;
176167

177-
// fill the internetPerConnOptionList structure
178-
internetPerConnOptionList.dwSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
168+
// marshal data from a managed object to unmanaged memory
169+
for (int i = 0; i < internetPerConnOptionListOptions.Length; i++) {
170+
Marshal.StructureToPtr(internetPerConnOptionListOptions[i], internetPerConnOptionListOptionPointer, false);
171+
internetPerConnOptionListOptionPointer = (IntPtr)((long)internetPerConnOptionListOptionPointer + Marshal.SizeOf(internetPerConnOptionListOptions[i]));
172+
}
179173

180-
// NULL == LAN, otherwise connectoid name
181-
internetPerConnOptionList.pszConnection = IntPtr.Zero;
174+
// fill the internetPerConnOptionList structure
175+
internetPerConnOptionList.dwSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
182176

183-
// set two options
184-
internetPerConnOptionList.dwOptionCount = (uint)internetPerConnOptionListOptions.Length;
185-
internetPerConnOptionList.dwOptionError = 0;
177+
// NULL == LAN, otherwise connectoid name
178+
internetPerConnOptionList.pszConnection = IntPtr.Zero;
186179

187-
// allocate memory for the INTERNET_PER_CONN_OPTION_LIST
188-
IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem((int)internetPerConnOptionListSize);
180+
// set two options
181+
internetPerConnOptionList.dwOptionCount = (uint)internetPerConnOptionListOptions.Length;
182+
internetPerConnOptionList.dwOptionError = 0;
189183

190-
try {
191-
// marshal data from a managed object to unmanaged memory
192-
Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true);
184+
// allocate memory for the INTERNET_PER_CONN_OPTION_LIST
185+
IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem((int)internetPerConnOptionListSize);
193186

194-
// set the options on the connection
195-
bool result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize);
196-
197-
if (!InternetCloseHandle(internetHandle)) {
198-
throw new FlashpointProxyException("Could not close the Internet Handle.");
199-
}
187+
try {
188+
// marshal data from a managed object to unmanaged memory
189+
Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true);
200190

201-
// throw an exception if this operation failed
202-
if (!result) {
203-
throw new FlashpointProxyException("Could not set the Internet Options.");
191+
// set the options on the connection
192+
if (!InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize)) {
193+
throw new FlashpointProxyException("Could not set the Internet Options.");
194+
}
195+
} finally {
196+
// free the allocated memory
197+
Marshal.FreeCoTaskMem(internetPerConnOptionListPointer);
204198
}
205199
} finally {
206-
// free the allocated memory
207-
Marshal.FreeCoTaskMem(internetPerConnOptionListPointer);
200+
Marshal.FreeCoTaskMem(internetPerConnOptionList.pOptions);
208201
}
209202
} finally {
210-
Marshal.FreeCoTaskMem(internetPerConnOptionList.pOptions);
203+
if (!InternetCloseHandle(internetHandle)) {
204+
throw new FlashpointProxyException("Could not close the Internet Handle.");
205+
}
211206
}
212207
}
213208

214209
public static void Disable() {
215-
IntPtr internetHandle = IntPtr.Zero;
216-
internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, IntPtr.Zero, IntPtr.Zero, 0);
210+
IntPtr internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, IntPtr.Zero, IntPtr.Zero, 0);
217211

218212
if (internetHandle == IntPtr.Zero) {
219213
throw new FlashpointProxyException("Could not open the Internet Handle.");
220214
}
221215

222-
// initialize a INTERNET_PER_CONN_OPTION_LIST instance
223-
INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST();
224-
uint internetPerConnOptionListSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
225-
226-
// create two options
227-
INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2];
228-
229-
GetSystemProxy(ref internetPerConnOptionList, ref internetPerConnOptionListOptions);
230-
231216
try {
232-
// allocate memory
233-
IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem((int)internetPerConnOptionListSize);
234-
235-
try {
236-
// convert structure to IntPtr
237-
Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true);
217+
// initialize a INTERNET_PER_CONN_OPTION_LIST instance
218+
INTERNET_PER_CONN_OPTION_LIST internetPerConnOptionList = new INTERNET_PER_CONN_OPTION_LIST();
219+
uint internetPerConnOptionListSize = (uint)Marshal.SizeOf(internetPerConnOptionList);
238220

239-
// set internet options
240-
bool result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize);
221+
// create two options
222+
INTERNET_PER_CONN_OPTION[] internetPerConnOptionListOptions = new INTERNET_PER_CONN_OPTION[2];
241223

242-
// notify the system that the registry settings have been changed and cause
243-
// the proxy data to be reread from the registry for a handle
244-
if (result) {
245-
result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
246-
}
247-
248-
if (result) {
249-
result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
250-
}
224+
GetSystemProxy(ref internetPerConnOptionList, ref internetPerConnOptionListOptions);
251225

252-
if (!InternetCloseHandle(internetHandle)) {
253-
throw new FlashpointProxyException("Could not close the Internet Handle.");
254-
}
255-
256-
if (!result) {
257-
throw new FlashpointProxyException("Could not set the Internet Options.");
226+
try {
227+
// allocate memory
228+
IntPtr internetPerConnOptionListPointer = Marshal.AllocCoTaskMem((int)internetPerConnOptionListSize);
229+
230+
try {
231+
// convert structure to IntPtr
232+
Marshal.StructureToPtr(internetPerConnOptionList, internetPerConnOptionListPointer, true);
233+
234+
// set internet options
235+
bool result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, internetPerConnOptionListPointer, internetPerConnOptionListSize);
236+
237+
// notify the system that the registry settings have been changed and cause
238+
// the proxy data to be reread from the registry for a handle
239+
if (result) {
240+
result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
241+
242+
if (result) {
243+
result = InternetSetOption(internetHandle, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
244+
}
245+
}
246+
247+
if (!result) {
248+
throw new FlashpointProxyException("Could not set the Internet Options.");
249+
}
250+
} finally {
251+
// free the allocated memory
252+
Marshal.FreeCoTaskMem(internetPerConnOptionListPointer);
258253
}
259254
} finally {
260-
// free the allocated memory
261-
Marshal.FreeCoTaskMem(internetPerConnOptionListPointer);
255+
Marshal.FreeCoTaskMem(internetPerConnOptionList.pOptions);
262256
}
263257
} finally {
264-
Marshal.FreeCoTaskMem(internetPerConnOptionList.pOptions);
258+
if (!InternetCloseHandle(internetHandle)) {
259+
throw new FlashpointProxyException("Could not close the Internet Handle.");
260+
}
265261
}
266262
}
267263
}

0 commit comments

Comments
 (0)