OSDN Git Service

na-get-lib,\83インストール・アンインストール時にプロセスの優先順位を下げるようにした・。
[applistation/AppliStation.git] / na-get-lib / NaGet.InteropServices / DllAccess.cs
1 using System;\r
2 using System.Reflection;\r
3 using System.Runtime.InteropServices;\r
4 \r
5 namespace NaGet.InteropServices\r
6 {\r
7         /// <summary>\r
8         /// DLLへの動的アクセスを行うクラス\r
9         /// </summary>\r
10         public class DllAccess : IDisposable\r
11         {\r
12                 #region Win32 API\r
13                 \r
14                 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]\r
15                 private extern static IntPtr LoadLibrary(string lpFileName);\r
16                 \r
17                 [DllImport("kernel32.dll")]\r
18                 private extern static bool FreeLibrary(IntPtr hModule);\r
19                 \r
20                 [DllImport("kernel32.dll", CharSet=CharSet.Ansi)]\r
21                 private extern static IntPtr GetProcAddress(IntPtr hModule, string lpProcName);\r
22                 \r
23                 #endregion\r
24                 \r
25                 private IntPtr hModule = IntPtr.Zero;\r
26                 \r
27                 /// <summary>\r
28                 /// コンストラクタ。DLLを読み込む\r
29                 /// </summary>\r
30                 /// <param name="dllName">\r
31                 /// DLLの名称。直接Win32APIのLoadLibraryにわたされる\r
32                 /// </param>\r
33                 public DllAccess(string dllName)\r
34                 {\r
35                         hModule = LoadLibrary(dllName);\r
36                         if (hModule == IntPtr.Zero) {\r
37                                 Exception innerEx = null;\r
38                                 try {\r
39                                         innerEx = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());\r
40                                 } catch {\r
41                                 }\r
42                                 throw new DllNotFoundException("Failed to LoadLibrary " + dllName, innerEx);\r
43                         }\r
44                 }\r
45                 \r
46                 /// <summary>\r
47                 /// 関数をdelegateに変換して得る\r
48                 /// </summary>\r
49                 /// <param name="strProcName">\r
50                 /// 関数名\r
51                 /// </param>\r
52                 /// <param name="type">\r
53                 /// 関数の型。戻り値のdelegateのタイプ\r
54                 /// </param>\r
55                 /// <returns>\r
56                 /// delegateに変換された関数\r
57                 /// </returns>\r
58                 public Delegate GetFunction(string strProcName, Type type)\r
59                 {\r
60                         IntPtr pFunc = GetProcAddress(hModule, strProcName);\r
61                         if (pFunc == IntPtr.Zero) {\r
62                                 int result = Marshal.GetHRForLastWin32Error();\r
63                                 throw Marshal.GetExceptionForHR(result);\r
64                         }\r
65                         \r
66                         return Marshal.GetDelegateForFunctionPointer(pFunc, type);\r
67                 }\r
68                 \r
69                 /// <summary>\r
70                 /// DLLの読み込みを閉じる\r
71                 /// </summary>\r
72                 public void Dispose()\r
73                 {\r
74                         if (hModule != IntPtr.Zero) {\r
75                                 FreeLibrary(hModule);\r
76                                 hModule = IntPtr.Zero;\r
77                         }\r
78                 }\r
79         }\r
80 }\r