1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
22 // You should have received a copy of the GNU General public License along
23 // with this program. if not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Diagnostics;
31 using System.Runtime.InteropServices;
32 using System.Threading;
33 using System.Windows.Forms;
37 public static class Win32Api
39 #region "先行起動プロセスをアクティブにする"
41 public static void WakeupWindow(IntPtr hWnd)
43 // メイン・ウィンドウが最小化されていれば元に戻す
46 ShowWindowAsync(hWnd, SW_RESTORE);
50 SetForegroundWindow(hWnd);
53 // 外部プロセスのメイン・ウィンドウを起動するためのWin32 API
54 [DllImport("user32.dll")]
55 private extern static bool SetForegroundWindow(
59 [DllImport("user32.dll")]
60 private extern static bool ShowWindowAsync(
64 // 指定されたウィンドウが最小化( アイコン化)されているかどうかを調べる
65 [DllImport("user32.dll")]
66 private extern static bool IsIconic(
69 // ShowWindowAsync関数のパラメータに渡す定義値
70 private const int SW_RESTORE = 9; // 画面を元の大きさに戻す
72 // 実行中の同じアプリケーションのプロセスを取得する
73 public static Process GetPreviousProcess()
75 var curProcess = Process.GetCurrentProcess();
78 return Process.GetProcessesByName(curProcess.ProcessName)
79 .Where(p => p.Id != curProcess.Id)
80 .FirstOrDefault(p => string.Compare(p.MainModule.FileName, curProcess.MainModule.FileName, StringComparison.OrdinalIgnoreCase) == 0);
89 #region "タスクトレイアイコンのクリック"
90 // 指定されたクラス名およびウィンドウ名と一致するトップレベルウィンドウのハンドルを取得します
91 [DllImport("user32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
92 private extern static IntPtr FindWindow(
93 [MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
94 [MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
96 // 指定された文字列と一致するクラス名とウィンドウ名文字列を持つウィンドウのハンドルを返します
97 [DllImport("user32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
98 private extern static IntPtr FindWindowEx(
101 [MarshalAs(UnmanagedType.LPTStr)] string lpsz1,
102 [MarshalAs(UnmanagedType.LPTStr)] string lpsz2);
104 // 指定されたウィンドウへ、指定されたメッセージを送信します
105 [DllImport("user32.dll")]
106 private extern static IntPtr SendMessage(
112 // SendMessageで送信するメッセージ
113 private enum Sm_Message : int
115 WM_USER = 0x400, //ユーザー定義メッセージ
116 TB_GETBUTTON = WM_USER + 23, //ツールバーのボタン取得
117 TB_BUTTONCOUNT = WM_USER + 24, //ツールバーのボタン(アイコン)数取得
118 TB_GETBUTTONINFO = WM_USER + 65, //ツールバーのボタン詳細情報取得
120 TCM_FIRST = 0x1300, //タブコントロールメッセージ
121 TCM_SETMINTABWIDTH = TCM_FIRST + 49, //タブアイテムの最小幅を設定
124 [StructLayout(LayoutKind.Sequential, Pack = 1)]
125 private struct TBBUTTON
128 public IntPtr idCommand;
131 public byte bReserved0;
132 public byte bReserved1;
137 [StructLayout(LayoutKind.Sequential)]
138 private struct TBBUTTONINFO
142 public Int32 idCommand;
147 public IntPtr lParam;
148 public IntPtr pszText;
149 public Int32 cchText;
151 // TBBUTTONINFOのlParamでポイントされるアイコン情報(PostMessageで使用)
152 [StructLayout(LayoutKind.Sequential)]
153 private struct TRAYNOTIFY
157 public UInt32 uCallbackMessage;
158 public UInt32 dwDummy1;
159 public UInt32 dwDummy2;
162 // TBBUTTONINFOに指定するマスク情報
164 private enum ToolbarButtonMask : int
170 // 指定されたウィンドウを作成したスレッドの ID を取得します
171 [DllImport("user32.dll", SetLastError = true)]
172 private static extern int GetWindowThreadProcessId(
174 out int lpdwProcessId);
176 // 指定したプロセスIDに対するプロセスハンドルを取得します
177 [DllImport("kernel32.dll")]
178 private static extern IntPtr OpenProcess(
179 ProcessAccess dwDesiredAccess,
180 [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
183 // OpenProcessで指定するアクセス権
185 private enum ProcessAccess : int
187 /// <summary>Specifies all possible access flags for the process object.</summary>
188 AllAccess = CreateThread | DuplicateHandle | QueryInformation | SetInformation | Terminate | VMOperation | VMRead | VMWrite | Synchronize,
189 /// <summary>Enables usage of the process handle in the CreateRemoteThread function to create a thread in the process.</summary>
191 /// <summary>Enables usage of the process handle as either the source or target process in the DuplicateHandle function to duplicate a handle.</summary>
192 DuplicateHandle = 0x40,
193 /// <summary>Enables usage of the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object.</summary>
194 QueryInformation = 0x400,
195 /// <summary>Enables usage of the process handle in the SetPriorityClass function to set the priority class of the process.</summary>
196 SetInformation = 0x200,
197 /// <summary>Enables usage of the process handle in the TerminateProcess function to terminate the process.</summary>
199 /// <summary>Enables usage of the process handle in the VirtualProtectEx and WriteProcessMemory functions to modify the virtual memory of the process.</summary>
201 /// <summary>Enables usage of the process handle in the ReadProcessMemory function to read from the virtual memory of the process.</summary>
203 /// <summary>Enables usage of the process handle in the WriteProcessMemory function to write to the virtual memory of the process.</summary>
205 /// <summary>Enables usage of the process handle in any of the wait functions to wait for the process to terminate.</summary>
206 Synchronize = 0x100000,
208 // 指定したプロセスの仮想アドレス空間にメモリ領域を確保
209 [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
210 private static extern IntPtr VirtualAllocEx(
214 AllocationTypes flAllocationType,
215 MemoryProtectionTypes flProtect);
219 private enum AllocationTypes : uint
228 WriteWatch = 0x200000,
229 LargePages = 0x20000000,
231 // アロケートしたメモリに対する保護レベル
233 private enum MemoryProtectionTypes : uint
237 ExecuteReadWrite = 0x40,
238 ExecuteWriteCopy = 0x80,
243 GuardModifierflag = 0x100,
244 NoCacheModifierflag = 0x200,
245 WriteCombineModifierflag = 0x400,
247 // オープンしているカーネルオブジェクトのハンドルをクローズします
248 [DllImport("kernel32.dll", SetLastError = true)]
249 private static extern bool CloseHandle(IntPtr hHandle);
251 // 指定されたプロセスの仮想アドレス空間内のメモリ領域を解放またはコミット解除します
252 [DllImport("kernel32.dll")]
253 private static extern bool VirtualFreeEx(
257 MemoryFreeTypes dwFreeType);
261 private enum MemoryFreeTypes
265 //指定したプロセスのメモリ領域にデータをコピーする
266 [DllImport("kernel32.dll", SetLastError = true)]
267 private static extern bool WriteProcessMemory(
269 IntPtr lpBaseAddress,
270 ref TBBUTTONINFO lpBuffer,
272 out int lpNumberOfBytesWritten);
274 //指定したプロセスのメモリ領域のデータを呼び出し側プロセスのバッファにコピーする
275 [DllImport("kernel32.dll", SetLastError = true)]
276 private static extern bool ReadProcessMemory(
278 IntPtr lpBaseAddress,
281 out int lpNumberOfBytesRead);
283 //メッセージをウィンドウのメッセージ キューに置き、対応するウィンドウがメッセージを処理するのを待たずに戻ります
284 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
285 private static extern bool PostMessage(
291 //PostMessageで送信するメッセージ
292 private enum PM_Message : uint
294 WM_LBUTTONDOWN = 0x201, //左マウスボタン押し下げ
295 WM_LBUTTONUP = 0x202, //左マウスボタン離し
299 /// タブコントロールのアイテムの最小幅を設定します
301 /// <param name="tabControl">対象となるタブコントロール</param>
302 /// <param name="width">アイテムの最小幅。-1 を指定するとデフォルトの幅が使用されます</param>
303 /// <returns>設定前の最小幅</returns>
304 public static int SetMinTabWidth(TabControl tabControl, int width)
306 return (int)SendMessage(tabControl.Handle, (int)Sm_Message.TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)width);
310 public static bool ClickTasktrayIcon(string tooltip)
312 const string TRAY_WINDOW = "Shell_TrayWnd";
313 const string TRAY_NOTIFYWINDOW = "TrayNotifyWnd";
314 const string TRAY_PAGER = "SysPager";
315 const string TOOLBAR_CONTROL = "ToolbarWindow32";
317 var taskbarWin = FindWindow(TRAY_WINDOW, null);
318 if (taskbarWin.Equals(IntPtr.Zero)) return false;
320 var trayWin = FindWindowEx(taskbarWin, IntPtr.Zero, TRAY_NOTIFYWINDOW, null);
321 if (trayWin.Equals(IntPtr.Zero)) return false;
322 //SysPagerの有無確認。(XP/2000はSysPagerあり)
323 var tempWin = FindWindowEx(trayWin, IntPtr.Zero, TRAY_PAGER, null);
324 if (tempWin.Equals(IntPtr.Zero)) tempWin = trayWin;
325 //タスクトレイがツールバーで出来ているか確認
327 var toolWin = FindWindowEx(tempWin, IntPtr.Zero, TOOLBAR_CONTROL, null);
328 if (toolWin.Equals(IntPtr.Zero)) return false;
329 //タスクトレイのプロセス(Explorer)を取得し、外部から参照するために開く
331 GetWindowThreadProcessId(toolWin, out expPid);
332 var hProc = OpenProcess(ProcessAccess.VMOperation | ProcessAccess.VMRead | ProcessAccess.VMWrite, false, expPid);
333 if (hProc.Equals(IntPtr.Zero)) return false;
335 //プロセスを閉じるためにtry-finally
338 var tbButtonLocal = new TBBUTTON(); //本プロセス内のタスクバーボタン情報作成(サイズ特定でのみ使用)
339 //Explorer内のタスクバーボタン格納メモリ確保
340 var ptbSysButton = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
341 if (ptbSysButton.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
344 var tbButtonInfoLocal = new TBBUTTONINFO(); //本プロセス内ツールバーボタン詳細情報作成
345 //Explorer内のタスクバーボタン詳細情報格納メモリ確保
346 var ptbSysInfo = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
347 if (ptbSysInfo.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
350 const int titleSize = 256; //Tooltip文字列長
351 var title = ""; //Tooltip文字列
352 //共有メモリにTooltip読込メモリ確保
353 var pszTitle = Marshal.AllocCoTaskMem(titleSize);
354 if (pszTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
357 //Explorer内にTooltip読込メモリ確保
358 var pszSysTitle = VirtualAllocEx(hProc, IntPtr.Zero, (IntPtr)titleSize, AllocationTypes.Reserve | AllocationTypes.Commit, MemoryProtectionTypes.ReadWrite);
359 if (pszSysTitle.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
363 var iCount = (int)SendMessage(toolWin, (int)Sm_Message.TB_BUTTONCOUNT, new IntPtr(0), new IntPtr(0));
365 for (var i = 0; i < iCount; i++)
367 var dwBytes = 0; //読み書きバイト数
368 var tbButtonLocal2 = new TBBUTTON(); //ボタン情報
369 var tbButtonInfoLocal2 = new TBBUTTONINFO(); //ボタン詳細情報
371 var ptrLocal = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonLocal));
372 if (ptrLocal.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
375 Marshal.StructureToPtr(tbButtonLocal, ptrLocal, true); //共有メモリ初期化
376 //ボタン情報取得(idCommandを取得するため)
379 (int)Sm_Message.TB_GETBUTTON,
382 //Explorer内のメモリを共有メモリに読み込み
387 (IntPtr)Marshal.SizeOf(tbButtonLocal),
390 tbButtonLocal2 = (TBBUTTON)Marshal.PtrToStructure(
396 Marshal.FreeCoTaskMem(ptrLocal); //共有メモリ解放
399 //ボタン詳細情報を取得するためのマスク等を設定
400 tbButtonInfoLocal.cbSize = Marshal.SizeOf(tbButtonInfoLocal);
401 tbButtonInfoLocal.dwMask = (int)(ToolbarButtonMask.TBIF_COMMAND | ToolbarButtonMask.TBIF_LPARAM | ToolbarButtonMask.TBIF_TEXT);
402 tbButtonInfoLocal.pszText = pszSysTitle; //Tooltip書き込み先領域
403 tbButtonInfoLocal.cchText = titleSize;
404 //マスク設定等をExplorerのメモリへ書き込み
408 ref tbButtonInfoLocal,
409 (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
414 (int)Sm_Message.TB_GETBUTTONINFO,
415 tbButtonLocal2.idCommand,
417 //共有メモリにボタン詳細情報を読み込む領域確保
418 var ptrInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbButtonInfoLocal));
419 if (ptrInfo.Equals(IntPtr.Zero)) return false; //共有メモリ確保失敗
422 Marshal.StructureToPtr(tbButtonInfoLocal, ptrInfo, true); //共有メモリ初期化
423 //Explorer内のメモリを共有メモリに読み込み
428 (IntPtr)Marshal.SizeOf(tbButtonInfoLocal),
431 tbButtonInfoLocal2 = (TBBUTTONINFO)Marshal.PtrToStructure(
433 typeof(TBBUTTONINFO));
437 Marshal.FreeCoTaskMem(ptrInfo); //共有メモリ解放
439 //Tooltipの内容をExplorer内のメモリから共有メモリへ読込
440 ReadProcessMemory(hProc, pszSysTitle, pszTitle, (IntPtr)titleSize, out dwBytes);
442 title = Marshal.PtrToStringAnsi(pszTitle, titleSize);
444 //Tooltipが指定文字列を含んでいればクリック
445 if (title.Contains(tooltip))
447 //PostMessageでクリックを送るために、ボタン詳細情報のlParamでポイントされているTRAYNOTIFY情報が必要
448 var tNotify = new TRAYNOTIFY();
449 var tNotify2 = new TRAYNOTIFY();
451 var ptNotify = Marshal.AllocCoTaskMem(Marshal.SizeOf(tNotify));
452 if (ptNotify.Equals(IntPtr.Zero)) return false; //メモリ確保失敗
455 Marshal.StructureToPtr(tNotify, ptNotify, true); //初期化
459 tbButtonInfoLocal2.lParam,
461 (IntPtr)Marshal.SizeOf(tNotify),
464 tNotify2 = (TRAYNOTIFY)
465 Marshal.PtrToStructure(
471 Marshal.FreeCoTaskMem(ptNotify); //共有メモリ解放
473 //クリックするためには通知領域がアクティブでなければならない
474 SetForegroundWindow(tNotify2.hWnd);
476 PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONDOWN);
477 PostMessage(tNotify2.hWnd, tNotify2.uCallbackMessage, (IntPtr)tNotify2.uID, (IntPtr)PM_Message.WM_LBUTTONUP);
485 VirtualFreeEx(hProc, pszSysTitle, (IntPtr)titleSize, MemoryFreeTypes.Release); //メモリ解放
490 Marshal.FreeCoTaskMem(pszTitle); //共有メモリ解放
495 VirtualFreeEx(hProc, ptbSysInfo, (IntPtr)Marshal.SizeOf(tbButtonInfoLocal), MemoryFreeTypes.Release); //メモリ解放
500 VirtualFreeEx(hProc, ptbSysButton, (IntPtr)Marshal.SizeOf(tbButtonLocal), MemoryFreeTypes.Release); //メモリ解放
505 CloseHandle(hProc); //Explorerのプロセス閉じる
511 public static bool FlashMyWindow(IntPtr hwnd,
512 FlashSpecification flashType,
515 var fInfo = new FLASHWINFO();
516 fInfo.cbSize = Convert.ToInt32(Marshal.SizeOf(fInfo));
518 fInfo.dwFlags = (int)FlashSpecification.FlashAll;
519 fInfo.uCount = flashCount;
522 return FlashWindowEx(ref fInfo);
525 public enum FlashSpecification : uint
527 FlashStop = FLASHW_STOP,
528 FlashCaption = FLASHW_CAPTION,
529 FlashTray = FLASHW_TRAY,
530 FlashAll = FLASHW_ALL,
531 FlashTimer = FLASHW_TIMER,
532 FlashTimerNoForeground = FLASHW_TIMERNOFG,
534 /// http://www.atmarkit.co.jp/fdotnet/dotnettips/723flashwindow/flashwindow.html
535 [DllImport("user32.dll")]
536 private static extern bool FlashWindowEx(
537 ref FLASHWINFO FWInfo);
540 private struct FLASHWINFO
542 public Int32 cbSize; // FLASHWINFO構造体のサイズ
543 public IntPtr hwnd; // 点滅対象のウィンドウ・ハンドル
544 public Int32 dwFlags; // 以下の「FLASHW_XXX」のいずれか
545 public Int32 uCount; // 点滅する回数
546 public Int32 dwTimeout; // 点滅する間隔(ミリ秒単位)
550 private const Int32 FLASHW_STOP = 0;
552 private const Int32 FLASHW_CAPTION = 0x1;
554 private const Int32 FLASHW_TRAY = 0x2;
555 // タスクバー・ボタンとタイトルバーを点滅させる
556 private const Int32 FLASHW_ALL = 0x3;
557 // FLASHW_STOPが指定されるまでずっと点滅させる
558 private const Int32 FLASHW_TIMER = 0x4;
559 // ウィンドウが最前面に来るまでずっと点滅させる
560 private const Int32 FLASHW_TIMERNOFG = 0xC;
563 [DllImport("user32.dll")]
564 public static extern bool ValidateRect(
568 #region "スクリーンセーバー起動中か判定"
569 [DllImport("user32", CharSet = CharSet.Auto)]
570 private static extern int SystemParametersInfo(
575 // returns non-zero value if function succeeds
577 //スクリーンセーバーが起動中かを取得する定数
578 private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
580 public static bool IsScreenSaverRunning()
582 var isRunning = false;
583 SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0);
589 [DllImport("user32")]
590 private static extern int RegisterHotKey(IntPtr hwnd, int id,
591 int fsModifiers, int vk);
592 [DllImport("user32")]
593 private static extern int UnregisterHotKey(IntPtr hwnd, int id);
594 [DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
595 private static extern ushort GlobalAddAtom([MarshalAs(UnmanagedType.LPTStr)] string lpString);
596 [DllImport("kernel32")]
597 private static extern ushort GlobalDeleteAtom(ushort nAtom);
599 private static int registerCount = 0;
600 // register a global hot key
601 public static int RegisterGlobalHotKey(int hotkeyValue, int modifiers, Form targetForm)
606 // use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs)
608 var atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + targetForm.Name + registerCount.ToString();
609 hotkeyID = GlobalAddAtom(atomName);
612 throw new Exception("Unable to generate unique hotkey ID. Error code: " +
613 Marshal.GetLastWin32Error().ToString());
616 // register the hotkey, throw if any error
617 if (RegisterHotKey(targetForm.Handle, hotkeyID, modifiers, hotkeyValue) == 0)
619 throw new Exception("Unable to register hotkey. Error code: " +
620 Marshal.GetLastWin32Error().ToString());
626 // clean up if hotkey registration failed
627 UnregisterGlobalHotKey(hotkeyID, targetForm);
632 // unregister a global hotkey
633 public static void UnregisterGlobalHotKey(ushort hotkeyID, Form targetForm)
637 UnregisterHotKey(targetForm.Handle, hotkeyID);
638 // clean up the atom list
639 GlobalDeleteAtom(hotkeyID);
645 #region "プロセスのProxy設定"
646 [DllImport("wininet.dll", SetLastError =true)]
647 private static extern bool InternetSetOption(IntPtr hInternet,
650 int lpdwBufferLength);
652 private struct INTERNET_PROXY_INFO : IDisposable
654 public int dwAccessType;
656 public IntPtr proxyBypass;
658 public void Dispose()
663 private void Dispose(bool disposing)
665 if (proxy != IntPtr.Zero) Marshal.FreeHGlobal(proxy);
666 if (proxyBypass != IntPtr.Zero) Marshal.FreeHGlobal(proxyBypass);
670 private static void RefreshProxySettings(string strProxy)
672 const int INTERNET_OPTION_PROXY = 38;
673 //const int INTERNET_OPEN_TYPE_PRECONFIG = 0; //IE setting
674 const int INTERNET_OPEN_TYPE_DIRECT = 1; //Direct
675 const int INTERNET_OPEN_TYPE_PROXY = 3; //Custom
677 INTERNET_PROXY_INFO ipi;
679 // Filling in structure
680 if (!string.IsNullOrEmpty(strProxy))
682 ipi.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
683 ipi.proxy = Marshal.StringToHGlobalAnsi(strProxy);
684 ipi.proxyBypass = Marshal.StringToHGlobalAnsi("local");
686 else if (strProxy == null)
689 var p = WebRequest.GetSystemWebProxy();
690 if (p.IsBypassed(new Uri("http://www.google.com/")))
692 ipi.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
693 ipi.proxy = IntPtr.Zero;
694 ipi.proxyBypass = IntPtr.Zero;
698 ipi.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
699 ipi.proxy = Marshal.StringToHGlobalAnsi(p.GetProxy(new Uri("http://www.google.com/")).Authority);
700 ipi.proxyBypass = Marshal.StringToHGlobalAnsi("local");
705 ipi.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
706 ipi.proxy = IntPtr.Zero;
707 ipi.proxyBypass = IntPtr.Zero;
713 var pIpi = Marshal.AllocCoTaskMem(Marshal.SizeOf(ipi));
714 if (pIpi.Equals(IntPtr.Zero)) return;
717 // Converting structure to IntPtr
718 Marshal.StructureToPtr(ipi, pIpi, true);
719 var ret = InternetSetOption(IntPtr.Zero,
720 INTERNET_OPTION_PROXY,
722 Marshal.SizeOf(ipi));
726 Marshal.FreeCoTaskMem(pIpi);
735 private static void RefreshProxyAccount(string username, string password)
737 const int INTERNET_OPTION_PROXY_USERNAME = 43;
738 const int INTERNET_OPTION_PROXY_PASSWORD = 44;
740 if (!string.IsNullOrEmpty(username) || !string.IsNullOrEmpty(password))
742 var ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, IntPtr.Zero, 0);
743 ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, IntPtr.Zero, 0);
747 var pUser = Marshal.StringToBSTR(username);
748 var pPass = Marshal.StringToBSTR(password);
751 var ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_USERNAME, pUser, username.Length + 1);
752 ret = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY_PASSWORD, pPass, password.Length + 1);
756 Marshal.FreeBSTR(pUser);
757 Marshal.FreeBSTR(pPass);
762 public static void SetProxy(HttpConnection.ProxyType pType, string host, int port, string username, string password)
767 case HttpConnection.ProxyType.IE:
770 case HttpConnection.ProxyType.None:
773 case HttpConnection.ProxyType.Specified:
774 proxy = host + (port > 0 ? ":" + port.ToString() : "");
777 RefreshProxySettings(proxy);
778 RefreshProxyAccount(username, password);