ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/Win32/libWin32/Win32/Threading/ProcessIconEx.cs
Revision: 107
Committed: Thu May 10 11:16:03 2012 UTC (10 years, 10 months ago) by william
File size: 2311 byte(s)
Log Message:

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.Drawing;
7
8 namespace libWin32.Win32.Threading
9 {
10 public class ProcessIconEx
11 {
12 [DllImport("Shell32", CharSet = CharSet.Auto)]
13 private static unsafe extern int ExtractIconEx(
14 string lpszFile,
15 int nIconIndex,
16 IntPtr[] phIconLarge,
17 IntPtr[] phIconSmall,
18 int nIcons);
19 [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
20 private static unsafe extern int DestroyIcon(IntPtr hIcon);
21 public static Icon ExtractIConFromFile(string file, bool large)
22 {
23 unsafe
24 {
25 int readIconCount = 0;
26 IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero };
27 IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero };
28
29 try
30 {
31 if (large)
32 readIconCount = ExtractIconEx(file, 0, hIconEx, hDummy, 1);
33 else
34 readIconCount = ExtractIconEx(file, 0, hDummy, hIconEx, 1);
35
36 if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
37 {
38 // GET FIRST EXTRACTED ICON
39 Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone();
40
41 return extractedIcon;
42 }
43 else // NO ICONS READ
44 return null;
45 }
46 catch (Exception ex)
47 {
48 /* EXTRACT ICON ERROR */
49
50 // BUBBLE UP
51 throw new ApplicationException("Could not extract icon", ex);
52 }
53 finally
54 {
55 // RELEASE RESOURCES
56 foreach (IntPtr ptr in hIconEx)
57 if (ptr != IntPtr.Zero)
58 DestroyIcon(ptr);
59
60 foreach (IntPtr ptr in hDummy)
61 if (ptr != IntPtr.Zero)
62 DestroyIcon(ptr);
63 }
64 }
65 }
66 }
67 }