OSDN Git Service

na-get-lib,チケット #36130 に関連して、更新パッケージから非対応パッケージを除外するようにした
[applistation/AppliStation.git] / na-get-lib / NaGet.InteropServices / ComDirectAccess.cs
1 using System;\r
2 using System.Runtime.InteropServices;\r
3 using System.Runtime.InteropServices.ComTypes;\r
4 \r
5 namespace NaGet.InteropServices\r
6 {\r
7         /// <summary>\r
8         /// COMをComInteropを経由せずにいじるための関数集\r
9         /// </summary>\r
10         public sealed class ComDirectAccess\r
11         {\r
12                 [DllImport("ole32.dll")]\r
13                 public static extern int CoInitialize(IntPtr pvReserved);\r
14                 \r
15                 [DllImport("ole32.dll")]\r
16                 public static extern void CoUninitialize();\r
17                 \r
18                 [DllImport("ole32.dll", ExactSpelling=true, PreserveSig=false)]\r
19                 [return: MarshalAs(UnmanagedType.Interface)]\r
20                 static extern object CoCreateInstance(\r
21                         [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,\r
22                         [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,\r
23                         CLSCTX dwClsContext,\r
24                         [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);\r
25                 \r
26                 [Flags()]\r
27                 public enum CLSCTX : uint\r
28                 {\r
29                     CLSCTX_INPROC_SERVER    = 0x1,\r
30                     CLSCTX_INPROC_HANDLER   = 0x2,\r
31                     CLSCTX_LOCAL_SERVER     = 0x4,\r
32                     CLSCTX_INPROC_SERVER16  = 0x8,\r
33                     CLSCTX_REMOTE_SERVER    = 0x10,\r
34                     CLSCTX_INPROC_HANDLER16 = 0x20,\r
35                     CLSCTX_RESERVED1        = 0x40,\r
36                     CLSCTX_RESERVED2        = 0x80,\r
37                     CLSCTX_RESERVED3        = 0x100,\r
38                     CLSCTX_RESERVED4        = 0x200,\r
39                     CLSCTX_NO_CODE_DOWNLOAD = 0x400,\r
40                     CLSCTX_RESERVED5        = 0x800,\r
41                     CLSCTX_NO_CUSTOM_MARSHAL    = 0x1000,\r
42                     CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000,\r
43                     CLSCTX_NO_FAILURE_LOG       = 0x4000,\r
44                     CLSCTX_DISABLE_AAA      = 0x8000,\r
45                     CLSCTX_ENABLE_AAA       = 0x10000,\r
46                     CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000,\r
47                     CLSCTX_INPROC           = CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,\r
48                     CLSCTX_SERVER           = CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,\r
49                     CLSCTX_ALL              = CLSCTX_SERVER|CLSCTX_INPROC_HANDLER,\r
50                 }\r
51                 \r
52                 /// <summary>\r
53                 /// 初期化禁止\r
54                 /// </summary>\r
55                 private ComDirectAccess()\r
56                 {\r
57                 }\r
58                 \r
59                 /// <summary>\r
60                 /// COMオブジェクトを生成する。CoCreateInstanceのラッパメソッド\r
61                 /// </summary>\r
62                 /// <param name="rclsid">オブジェクトのCLSID</param>\r
63                 /// <param name="pUnkOuter">アグリゲートオブジェクト</param>\r
64                 /// <param name="dwClsContext">コンテキスト</param>\r
65                 /// <param name="riid">オブジェクトのIID</param>\r
66                 /// <returns>生成されたCOMオブジェクト</returns>\r
67                 public static object CreateInstance(Guid rclsid, object pUnkOuter, CLSCTX dwClsContext, Guid riid)\r
68                 {\r
69                         return CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid);\r
70                 }\r
71                 \r
72                 /// <summary>\r
73                 /// COMオブジェクトを生成する。COMオブジェクトの型はテンプレートTから自動取得・キャストされる。\r
74                 /// </summary>\r
75                 /// <param name="clsid">オブジェクトのCLSID</param>\r
76                 /// <param name="dwClsContext">コンテキスト</param>\r
77                 /// <returns>生成されたCOMオブジェクト</returns>\r
78                 public static T CreateInstance<T>(Guid clsid, CLSCTX dwClsContext)\r
79                 {\r
80                         Guid riid = new Guid(((GuidAttribute) Attribute.GetCustomAttribute(typeof(T), typeof(GuidAttribute))).Value);\r
81                         return (T) CreateInstance(clsid, null, dwClsContext, riid);\r
82                 }\r
83         }\r
84 }\r