internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbSizeFileInfo, int uFlags);
public static Icon GetWindowIcon(IntPtr handle, bool small) { int processId; NativeMethods.GetWindowThreadProcessId(handle, out processId); var hInstance = NativeMethods.OpenProcess(0, false, processId); var fileName = new StringBuilder(256); var ret = NativeMethods.GetModuleFileName(IntPtr.Zero, fileName, fileName.Capacity); if (ret <= 0) { return null; } if (ret > fileName.Capacity) { throw new PathTooLongException("Assembly name is longer than MAX_PATH."); } var shinfo = new SHFILEINFO(); NativeMethods.SHGetFileInfo(fileName.ToString(), 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | (small ? SHGFI_SMALLICON : SHGFI_LARGEICON)); NativeMethods.CloseHandle(hInstance); if (shinfo.Icon != IntPtr.Zero) { return Icon.FromHandle(shinfo.Icon); } var hParent = handle; while (hParent != IntPtr.Zero) { var hIcon = NativeMethods.SendMessage(hParent, (int)WM.GETICON, small ? ICON_SMALL : ICON_BIG, 0); if (hIcon != IntPtr.Zero) { return Icon.FromHandle(hIcon); } hParent = NativeMethods.GetParent(hParent); } return null; }