3 using System.Runtime.InteropServices;
\r
5 namespace NaGet.InteropServices
\r
8 /// PE
\83t
\83@
\83C
\83\8b\82Ì
\8eí
\97Þ
\r
10 public enum PEFileType : uint
\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
17 /// Windows
\83R
\83\93\83\
\81[
\83\8b\83A
\83v
\83\8a\83P
\81[
\83V
\83\87\83\93\r
21 /// Windows GUI
\83A
\83v
\83\8a\83P
\81[
\83V
\83\87\83\93\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
30 public sealed class PEFileInfoUtils
\r
32 //
\8cÄ
\82Ñ
\8fo
\82µ
\8bÖ
\8e~
\r
33 private PEFileInfoUtils()
\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
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
44 PEFileType fileType;
\r
47 SHFILEINFO info = new SHFILEINFO();
\r
48 int type = (int) shGetFileInfo(path, 0, ref info, SHGFI.ExeType);
\r
50 const int MZ = 0x5a4d;
\r
51 const int NE = 0x504e;
\r
52 const int PE = 0x4550;
\r
56 fileType = PEFileType.MSDosCom;
\r
59 fileType = PEFileType.WinConsole;
\r
62 int loWord = type & 0xffff;
\r
63 int hiWord = (type >> 16) & 0xffff;
\r
65 if (((loWord == PE) || (loWord == NE)) &&
\r
66 ((hiWord == 0x0300) || (hiWord == 0x0350) || (hiWord == 0x0400)))
\r
68 fileType = PEFileType.WinGUI;
\r
70 fileType = PEFileType.Unknown;
\r
74 } catch (FileNotFoundException e) {
\r
75 throw new FileNotFoundException(e.Message, e);
\r
76 } catch (IOException) {
\r
77 fileType = PEFileType.Unknown;
\r
83 #region SHGetFileInfo
\r
85 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
\r
86 private struct SHFILEINFO
\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
97 private enum SHGFI : uint
\r
103 IconLocation = 4096,
\r
105 SysIconIndex = 16384,
\r
106 LinkOverlay = 32768,
\r
108 AttrSpecified = 131072,
\r
113 UseFileAttributes = 16
\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
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
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
129 if (! File.Exists(pszPath)) {
\r
130 throw new FileNotFoundException(null, pszPath);
\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