OSDN Git Service

na-get-lib,チケット #36130 に関連して、更新パッケージから非対応パッケージを除外するようにした
[applistation/AppliStation.git] / na-get-lib / NaGet.InteropServices / PEFileInfoUtils.cs
1 using System;\r
2 using System.IO;\r
3 using System.Runtime.InteropServices;\r
4 \r
5 namespace NaGet.InteropServices\r
6 {       \r
7         /// <summary>\r
8         /// PE\83t\83@\83C\83\8b\82Ì\8eí\97Þ\r
9         /// </summary>\r
10         public enum PEFileType : uint\r
11         {\r
12                 /// <summary>\r
13                 /// \95s\96¾\82È\8c`\8e®\81A\82 \82é\82¢\82ÍPE\83t\83@\83C\83\8b\83w\83b\83_\82ð\8e\9d\82½\82È\82¢\r
14                 /// </summary>\r
15                 Unknown = 0,\r
16                 /// <summary>\r
17                 /// Windows\83R\83\93\83\\81[\83\8b\83A\83v\83\8a\83P\81[\83V\83\87\83\93\r
18                 /// </summary>\r
19                 WinConsole = 1,\r
20                 /// <summary>\r
21                 /// Windows GUI\83A\83v\83\8a\83P\81[\83V\83\87\83\93\r
22                 /// </summary>\r
23                 WinGUI = 2,\r
24                 /// <summary>\r
25                 /// MS-DOS(\8by\82Ñ\83R\83}\83\93\83h\83v\83\8d\83\93\83v\83g)\82Å\82ÌCOM\83t\83@\83C\83\8b\r
26                 /// </summary>\r
27                 MSDosCom = 3,\r
28         }\r
29         \r
30         public sealed class PEFileInfoUtils\r
31         {\r
32                 // \8cÄ\82Ñ\8fo\82µ\8bÖ\8e~\r
33                 private PEFileInfoUtils()\r
34                 {\r
35                 }\r
36                 \r
37                 /// <summary>\r
38                 /// \93n\82³\82ê\82½\8eÀ\8ds\83t\83@\83C\83\8b\82Ì\8eí\97Þ\82ð\95Ô\82·\81B\93à\95\94\82ÅSHGetFileInfo\82ð\8eg\97p\81B\r
39                 /// </summary>\r
40                 /// <param name="path">\8eÀ\8ds\83t\83@\83C\83\8b(*.exe,*.dll)\82Ö\82Ì\83p\83X</param>\r
41                 /// <returns>\8eÀ\8ds\83t\83@\83C\83\8b\82Ì\8eí\97Þ</returns>\r
42                 public static PEFileType GetPEFileType(string path)\r
43                 {\r
44                         PEFileType fileType;\r
45                         \r
46                         try {\r
47                                 SHFILEINFO info = new SHFILEINFO();\r
48                                 int type = (int) shGetFileInfo(path, 0, ref info, SHGFI.ExeType);\r
49                                 \r
50                                 const int MZ = 0x5a4d;\r
51                                 const int NE = 0x504e;\r
52                                 const int PE = 0x4550;\r
53                                 switch (type)\r
54                                 {\r
55                                         case MZ:\r
56                                                 fileType = PEFileType.MSDosCom;\r
57                                                 break;\r
58                                         case PE:\r
59                                                 fileType = PEFileType.WinConsole;\r
60                                                 break;\r
61                                         default:\r
62                                                 int loWord = type & 0xffff;\r
63                                                 int hiWord = (type >> 16) & 0xffff;\r
64                                                 \r
65                                                 if (((loWord == PE) || (loWord == NE)) &&\r
66                                                     ((hiWord == 0x0300) || (hiWord == 0x0350) || (hiWord == 0x0400)))\r
67                                                 {\r
68                                                         fileType = PEFileType.WinGUI;\r
69                                                 } else {\r
70                                                         fileType = PEFileType.Unknown;\r
71                                                 }\r
72                                                 break;\r
73                                 }\r
74                         } catch (FileNotFoundException e) {\r
75                                 throw new FileNotFoundException(e.Message, e);\r
76                         } catch (IOException) {\r
77                                 fileType = PEFileType.Unknown;\r
78                         }\r
79                         \r
80                         return fileType;\r
81                 }\r
82                 \r
83                 #region SHGetFileInfo\r
84                 \r
85                 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]\r
86                 private struct SHFILEINFO\r
87                 {\r
88                         public IntPtr hIcon;\r
89                         public IntPtr iIcon;\r
90                         public uint dwAttributes;\r
91                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]\r
92                         public string szDisplayName;\r
93                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]\r
94                         public string szTypeName;\r
95                 }\r
96                 \r
97                 private enum SHGFI : uint\r
98                 {\r
99                         Icon = 256,\r
100                         DisplayName = 512,\r
101                         TypeName = 1024,\r
102                         Attributes = 2048,\r
103                         IconLocation = 4096,\r
104                         ExeType = 8192,\r
105                         SysIconIndex = 16384,\r
106                         LinkOverlay = 32768,\r
107                         Selected = 65536,\r
108                         AttrSpecified = 131072,\r
109                         LargeIcon = 0,\r
110                         SmallIcon = 1,\r
111                         OpenIcon = 2,\r
112                         PIDL = 8,\r
113                         UseFileAttributes = 16\r
114                 }\r
115                 \r
116                 [DllImport("shell32.dll", CharSet=CharSet.Auto)]\r
117                 private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);\r
118 \r
119                 /// <summary>\r
120                 /// SHGetFileInfo\82ð\8cÄ\82Ñ\8fo\82·\95Ö\97\98\83\81\83\\83b\83h\81B\83t\83@\83C\83\8b\93Ç\82Ý\8d\9e\82Ý\82È\82Ç\82É\8e¸\94s\82µ\82½\82è\81A\r
121                 /// \8eÀ\8ds\83t\83@\83C\83\8b\82Å\82È\82¢\8fê\8d\87\82ÍIOException\82ð\93\8a\82°\82Ü\82·\81B\r
122                 /// </summary>\r
123                 /// <param name="pszPath">\83t\83@\83C\83\8b\83p\83X</param>\r
124                 /// <param name="dwFileAttributes">\83t\83@\83C\83\8b\91®\90«</param>\r
125                 /// <param name="psfi">\83t\83@\83C\83\8b\8fî\95ñ</param>\r
126                 /// <param name="uFlags">\83t\83\89\83O</param>\r
127                 private static IntPtr shGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, SHGFI uFlags)\r
128                 {\r
129                         if (! File.Exists(pszPath)) {\r
130                                 throw new FileNotFoundException(null, pszPath);\r
131                         }\r
132                         \r
133                         IntPtr hSuccess = SHGetFileInfo(pszPath, dwFileAttributes, ref psfi, (uint)Marshal.SizeOf(psfi), (uint) uFlags);\r
134                         if (hSuccess == IntPtr.Zero) {\r
135                                 throw new IOException(string.Format("Maybe {0} is not a executable file.", pszPath));\r
136                         }\r
137                         return hSuccess;\r
138                 }\r
139                 \r
140                 #endregion\r
141         }\r
142 }\r