OSDN Git Service

AppliStation-all,ウイルススキャン対応
[applistation/AppliStation.git] / na-get-lib / NaGet.Net / DownloadScanner.cs
1 using System;\r
2 using System.Reflection;\r
3 using System.Collections.Generic;\r
4 using System.Runtime.InteropServices;\r
5 using System.Runtime.InteropServices.ComTypes;\r
6 using NaGet.InteropServices;\r
7 \r
8 namespace NaGet.Net\r
9 {\r
10         public class DownloadScanner : IDisposable\r
11         {\r
12                 #region COMInterop\r
13                 [Flags()]\r
14                 private enum MSOAVINFOFLAG : uint {\r
15                         fPath = 1,\r
16                         fReadOnlyRequest = 2,\r
17                         fInstalled = 4,\r
18                         fHttpDownload = 8,\r
19                 }\r
20                 \r
21                 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]\r
22                 private struct MSOAVINFO {\r
23                         public int cbsize;\r
24 \r
25                         [MarshalAs(UnmanagedType.U4)]\r
26                         public MSOAVINFOFLAG uFlags;\r
27                         \r
28                         public IntPtr hwnd;\r
29 \r
30                         [MarshalAs(UnmanagedType.LPWStr)]\r
31                         public string pwzFullPath;\r
32                         [MarshalAs(UnmanagedType.LPWStr)]\r
33                         public string pwzHostName;\r
34                         [MarshalAs(UnmanagedType.LPWStr)]\r
35                         public string pwzOrigURL;\r
36                 }\r
37                 \r
38                 [ComImport()]\r
39                 [Guid("56FFCC30-D398-11D0-B2AE-00A0C908FA49")]\r
40                 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\r
41                 private interface IOfficeAntiVirus {\r
42                         void Scan(ref MSOAVINFO pmsoavinfo);\r
43                 }\r
44                 #endregion\r
45                 \r
46                 /// <summary>\r
47                 /// ウイルススキャナーに渡すプログラム・ホスト名。\r
48                 /// </summary>\r
49                 public string HostName {\r
50                         get {\r
51                                 // 実行アセンブリ名を返す\r
52                                 return Assembly.GetExecutingAssembly().GetName().FullName;\r
53                         }\r
54                 }\r
55                 \r
56                 private List<IOfficeAntiVirus> scanners;\r
57                 \r
58                 public DownloadScanner()\r
59                 {\r
60                         ComDirectAccess.CoInitialize(IntPtr.Zero);\r
61                 }\r
62                 \r
63                 public void Dispose()\r
64                 {\r
65                         if ((scanners != null) && (scanners.Count > 0)) {\r
66                                 foreach (IOfficeAntiVirus i in scanners) {\r
67                                         Marshal.ReleaseComObject(i);\r
68                                 }\r
69                                 scanners.Clear();\r
70                         }\r
71                         \r
72                         ComDirectAccess.CoUninitialize();\r
73                 }\r
74                 \r
75                 public bool HasScanner {\r
76                         get { return scanners.Count > 0; }\r
77                 }\r
78                 \r
79                 public void Init()\r
80                 {\r
81                         scanners = new List<IOfficeAntiVirus>();\r
82                         \r
83                         Guid IID_MSOfficeAntiVirus = new Guid(((GuidAttribute) Attribute.GetCustomAttribute(typeof(IOfficeAntiVirus), typeof(GuidAttribute))).Value);\r
84                         \r
85                         using (GuidEnumeratorForCategories guids = new GuidEnumeratorForCategories(IID_MSOfficeAntiVirus)) {\r
86                                 foreach (Guid guid in guids) {\r
87                                         IOfficeAntiVirus oav = ComDirectAccess.CreateInstance<IOfficeAntiVirus>(guid, ComDirectAccess.CLSCTX.CLSCTX_INPROC_SERVER);\r
88                                         \r
89                                         scanners.Add(oav);\r
90                                 }\r
91                         }\r
92                 }\r
93                 \r
94                 public bool Scan(string path, string origin)\r
95                 {\r
96                         if (scanners.Count <= 0) {\r
97                                 return false;\r
98                         }\r
99                         \r
100                         MSOAVINFO info = new MSOAVINFO();\r
101                         info.cbsize = Marshal.SizeOf(info);\r
102                         info.uFlags = MSOAVINFOFLAG.fPath | MSOAVINFOFLAG.fHttpDownload;\r
103                         info.hwnd = IntPtr.Zero;\r
104                         info.pwzFullPath = path;\r
105                         info.pwzHostName = HostName;\r
106                         info.pwzOrigURL = origin;\r
107                         \r
108                         foreach (IOfficeAntiVirus i in scanners) {\r
109                                 i.Scan(ref info);\r
110                         }\r
111                         return true;\r
112                 }\r
113         }\r
114 }\r