@@ -101,10 +101,11 @@ private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPer
101101
102102 // allocate a block of memory of the options
103103 internetPerConnOptionList . pOptions = Marshal . AllocCoTaskMem ( Marshal . SizeOf ( internetPerConnOptionListOptions [ 0 ] ) + Marshal . SizeOf ( internetPerConnOptionListOptions [ 1 ] ) ) ;
104+
104105 IntPtr internetPerConnOptionPointer = internetPerConnOptionList . pOptions ;
105106
106107 // marshal data from a managed object to unmanaged memory
107- for ( int i = 0 ; i < internetPerConnOptionListOptions . Length ; i ++ ) {
108+ for ( int i = 0 ; i < internetPerConnOptionListOptions . Length ; i ++ ) {
108109 Marshal . StructureToPtr ( internetPerConnOptionListOptions [ i ] , internetPerConnOptionPointer , false ) ;
109110 internetPerConnOptionPointer = ( IntPtr ) ( ( int ) internetPerConnOptionPointer + Marshal . SizeOf ( internetPerConnOptionListOptions [ i ] ) ) ;
110111 }
@@ -121,7 +122,7 @@ private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPer
121122
122123 // query internet options
123124 bool result = InternetQueryOption ( IntPtr . Zero , INTERNET_OPTION . INTERNET_OPTION_PER_CONNECTION_OPTION , ref internetPerConnOptionList , ref internetPerConnOptionListSize ) ;
124-
125+
125126 if ( ! result ) {
126127 throw new FlashpointProxyException ( "Could not query the Internet Options." ) ;
127128 }
@@ -158,44 +159,50 @@ public static void Enable(string proxyServer) {
158159
159160 // allocate memory for the INTERNET_PER_CONN_OPTION_LIST Options
160161 internetPerConnOptionList . pOptions = Marshal . AllocCoTaskMem ( Marshal . SizeOf ( internetPerConnOptionListOptions [ 0 ] ) + Marshal . SizeOf ( internetPerConnOptionListOptions [ 1 ] ) ) ;
161- IntPtr internetPerConnOptionListOptionPointer = internetPerConnOptionList . pOptions ;
162-
163- // marshal data from a managed object to unmanaged memory
164- for ( int i = 0 ; i < internetPerConnOptionListOptions . Length ; i ++ ) {
165- Marshal . StructureToPtr ( internetPerConnOptionListOptions [ i ] , internetPerConnOptionListOptionPointer , false ) ;
166- internetPerConnOptionListOptionPointer = ( IntPtr ) ( ( long ) internetPerConnOptionListOptionPointer + Marshal . SizeOf ( internetPerConnOptionListOptions [ i ] ) ) ;
167- }
168-
169- // fill the internetPerConnOptionList structure
170- internetPerConnOptionList . dwSize = ( uint ) Marshal . SizeOf ( internetPerConnOptionList ) ;
171-
172- // NULL == LAN, otherwise connectoid name
173- internetPerConnOptionList . pszConnection = IntPtr . Zero ;
174-
175- // set two options
176- internetPerConnOptionList . dwOptionCount = ( uint ) internetPerConnOptionListOptions . Length ;
177- internetPerConnOptionList . dwOptionError = 0 ;
178-
179- // allocate memory for the INTERNET_PER_CONN_OPTION_LIST
180- IntPtr internetPerConnOptionListPointer = Marshal . AllocCoTaskMem ( ( int ) internetPerConnOptionListSize ) ;
181-
182- // marshal data from a managed object to unmanaged memory
183- Marshal . StructureToPtr ( internetPerConnOptionList , internetPerConnOptionListPointer , true ) ;
184162
185- // set the options on the connection
186- bool result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_PER_CONNECTION_OPTION , internetPerConnOptionListPointer , internetPerConnOptionListSize ) ;
187-
188- // free the allocated memory
189- Marshal . FreeCoTaskMem ( internetPerConnOptionList . pOptions ) ;
190- Marshal . FreeCoTaskMem ( internetPerConnOptionListPointer ) ;
191-
192- if ( ! InternetCloseHandle ( internetHandle ) ) {
193- throw new FlashpointProxyException ( "Could not close the Internet Handle." ) ;
194- }
195-
196- // throw an exception if this operation failed
197- if ( ! result ) {
198- throw new FlashpointProxyException ( "Could not set the Internet Options." ) ;
163+ try {
164+ IntPtr internetPerConnOptionListOptionPointer = internetPerConnOptionList . pOptions ;
165+
166+ // marshal data from a managed object to unmanaged memory
167+ for ( int i = 0 ; i < internetPerConnOptionListOptions . Length ; i ++ ) {
168+ Marshal . StructureToPtr ( internetPerConnOptionListOptions [ i ] , internetPerConnOptionListOptionPointer , false ) ;
169+ internetPerConnOptionListOptionPointer = ( IntPtr ) ( ( long ) internetPerConnOptionListOptionPointer + Marshal . SizeOf ( internetPerConnOptionListOptions [ i ] ) ) ;
170+ }
171+
172+ // fill the internetPerConnOptionList structure
173+ internetPerConnOptionList . dwSize = ( uint ) Marshal . SizeOf ( internetPerConnOptionList ) ;
174+
175+ // NULL == LAN, otherwise connectoid name
176+ internetPerConnOptionList . pszConnection = IntPtr . Zero ;
177+
178+ // set two options
179+ internetPerConnOptionList . dwOptionCount = ( uint ) internetPerConnOptionListOptions . Length ;
180+ internetPerConnOptionList . dwOptionError = 0 ;
181+
182+ // allocate memory for the INTERNET_PER_CONN_OPTION_LIST
183+ IntPtr internetPerConnOptionListPointer = Marshal . AllocCoTaskMem ( ( int ) internetPerConnOptionListSize ) ;
184+
185+ try {
186+ // marshal data from a managed object to unmanaged memory
187+ Marshal . StructureToPtr ( internetPerConnOptionList , internetPerConnOptionListPointer , true ) ;
188+
189+ // set the options on the connection
190+ bool result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_PER_CONNECTION_OPTION , internetPerConnOptionListPointer , internetPerConnOptionListSize ) ;
191+
192+ if ( ! InternetCloseHandle ( internetHandle ) ) {
193+ throw new FlashpointProxyException ( "Could not close the Internet Handle." ) ;
194+ }
195+
196+ // throw an exception if this operation failed
197+ if ( ! result ) {
198+ throw new FlashpointProxyException ( "Could not set the Internet Options." ) ;
199+ }
200+ } finally {
201+ // free the allocated memory
202+ Marshal . FreeCoTaskMem ( internetPerConnOptionListPointer ) ;
203+ }
204+ } finally {
205+ Marshal . FreeCoTaskMem ( internetPerConnOptionList . pOptions ) ;
199206 }
200207 }
201208
@@ -216,35 +223,40 @@ public static void Disable() {
216223
217224 GetSystemProxy ( ref internetPerConnOptionList , ref internetPerConnOptionListOptions ) ;
218225
219- // allocate memory
220- IntPtr internetPerConnOptionListPointer = Marshal . AllocCoTaskMem ( ( int ) internetPerConnOptionListSize ) ;
221-
222- // convert structure to IntPtr
223- Marshal . StructureToPtr ( internetPerConnOptionList , internetPerConnOptionListPointer , true ) ;
224-
225- // set internet options
226- bool result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_PER_CONNECTION_OPTION , internetPerConnOptionListPointer , internetPerConnOptionListSize ) ;
227-
228- // free the allocated memory
229- Marshal . FreeCoTaskMem ( internetPerConnOptionList . pOptions ) ;
230- Marshal . FreeCoTaskMem ( internetPerConnOptionListPointer ) ;
231-
232- // notify the system that the registry settings have been changed and cause
233- // the proxy data to be reread from the registry for a handle
234- if ( result ) {
235- result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_SETTINGS_CHANGED , IntPtr . Zero , 0 ) ;
236- }
237-
238- if ( result ) {
239- result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_REFRESH , IntPtr . Zero , 0 ) ;
240- }
241-
242- if ( ! InternetCloseHandle ( internetHandle ) ) {
243- throw new FlashpointProxyException ( "Could not close the Internet Handle." ) ;
244- }
245-
246- if ( ! result ) {
247- 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+
243+ if ( result ) {
244+ result = InternetSetOption ( internetHandle , INTERNET_OPTION . INTERNET_OPTION_REFRESH , IntPtr . Zero , 0 ) ;
245+ }
246+
247+ if ( ! InternetCloseHandle ( internetHandle ) ) {
248+ throw new FlashpointProxyException ( "Could not close the Internet Handle." ) ;
249+ }
250+
251+ if ( ! result ) {
252+ throw new FlashpointProxyException ( "Could not set the Internet Options." ) ;
253+ }
254+ } finally {
255+ // free the allocated memory
256+ Marshal . FreeCoTaskMem ( internetPerConnOptionListPointer ) ;
257+ }
258+ } finally {
259+ Marshal . FreeCoTaskMem ( internetPerConnOptionList . pOptions ) ;
248260 }
249261 }
250262 }
0 commit comments