/UpdaterService/MSI.Interop/SummaryInformation.cs

# · C# · 297 lines · 281 code · 16 blank · 0 comment · 4 complexity · 13961f04d5ffc056fcc11679dd887e73 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Pahvant.MSI
  5. {
  6. public class SummaryInformation : IDisposable
  7. {
  8. public enum Properties
  9. {
  10. CodePage = 1,
  11. Title = 2,
  12. Subject = 3,
  13. Author = 4,
  14. Keywords = 5,
  15. Comments = 6,
  16. Template = 7,
  17. LastAuthor = 8,
  18. Revision = 9,
  19. Printed = 11,
  20. Created = 12,
  21. Saved = 13,
  22. Pages = 14,
  23. Words = 15,
  24. Characters = 16,
  25. Application = 18,
  26. Security = 19
  27. }
  28. [Flags]
  29. public enum SourceType
  30. {
  31. ShortFileNames = 0x00000001,
  32. Compressed = 0x00000002,
  33. AdminImage = 0x00000004
  34. }
  35. #region Construction/Destruction
  36. public SummaryInformation(Database db, int count)
  37. {
  38. IntPtr handle;
  39. TR(MsiGetSummaryInformation(db.Handle, null, count, out handle));
  40. m_handle = new MSIHandle(handle);
  41. }
  42. public SummaryInformation(string path, int count)
  43. {
  44. IntPtr handle;
  45. TR(MsiGetSummaryInformation(IntPtr.Zero, path, count, out handle));
  46. m_handle = new MSIHandle(handle);
  47. }
  48. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  49. private static extern UInt32 MsiGetSummaryInformation(IntPtr database,
  50. string path, int count, out IntPtr handle);
  51. #endregion
  52. #region Methods
  53. public void Persist()
  54. {
  55. TR(MsiSummaryInfoPersist(m_handle.Handle));
  56. }
  57. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  58. private static extern UInt32 MsiSummaryInfoPersist(IntPtr handle);
  59. #endregion
  60. #region GetProperty
  61. public string GetProperty(Properties which)
  62. {
  63. int integerData;
  64. FILETIME dateTimeData;
  65. VarEnum dataType;
  66. int stringDataLength = 0;
  67. TR(MsiSummaryInfoGetProperty(m_handle.Handle, (int) which,
  68. out dataType, out integerData, out dateTimeData, null,
  69. ref stringDataLength));
  70. StringBuilder stringData = new StringBuilder(stringDataLength+1);
  71. stringDataLength = stringData.Capacity;
  72. TR(MsiSummaryInfoGetProperty(m_handle.Handle, (int) which,
  73. out dataType, out integerData, out dateTimeData, stringData,
  74. ref stringDataLength));
  75. return stringData.ToString();
  76. }
  77. public int GetIntegerProperty(Properties which)
  78. {
  79. int integerData;
  80. FILETIME dateTimeData;
  81. int stringDataLength = 0;
  82. VarEnum dataType;
  83. TR(MsiSummaryInfoGetProperty(m_handle.Handle, (int) which,
  84. out dataType, out integerData, out dateTimeData, null,
  85. ref stringDataLength));
  86. return integerData;
  87. }
  88. public DateTime GetDateTimeProperty(Properties which)
  89. {
  90. int integerData;
  91. FILETIME dateTimeData;
  92. int stringDataLength = 0;
  93. VarEnum dataType;
  94. TR(MsiSummaryInfoGetProperty(m_handle.Handle, (int) which,
  95. out dataType, out integerData, out dateTimeData, null,
  96. ref stringDataLength));
  97. return ToDateTime(dateTimeData);
  98. }
  99. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  100. private static extern UInt32 MsiSummaryInfoGetProperty(
  101. IntPtr handle, int which, out VarEnum dataType,
  102. out int integerData, out FILETIME dateTimeData,
  103. StringBuilder stringData, ref int stringDataLength);
  104. #endregion
  105. #region SetProperty
  106. public void SetProperty(Properties which, string data)
  107. {
  108. TR(MsiSummaryInfoSetProperty(m_handle.Handle, (int) which,
  109. (int) VarEnum.VT_LPSTR, 0, IntPtr.Zero, data));
  110. }
  111. public void SetIntegerProperty(Properties which, int data)
  112. {
  113. TR(MsiSummaryInfoSetProperty(m_handle.Handle, (int) which,
  114. (int) VarEnum.VT_I4, data, IntPtr.Zero, null));
  115. }
  116. public void SetDateTimeProperty(Properties which, DateTime data)
  117. {
  118. FILETIME ft = FromDateTime(data);
  119. TR(MsiSummaryInfoSetProperty(m_handle.Handle, (int) which,
  120. (int) VarEnum.VT_FILETIME, 0, ref ft, null));
  121. }
  122. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  123. private static extern UInt32 MsiSummaryInfoSetProperty(
  124. IntPtr handle, int propertyId, int dataType,
  125. int integerData, IntPtr dateTimeData, string stringData);
  126. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  127. private static extern UInt32 MsiSummaryInfoSetProperty(
  128. IntPtr handle, int propertyId, int dataType,
  129. int integerData, ref FILETIME dateTimeData, string stringData);
  130. #endregion
  131. private static FILETIME FromDateTime(DateTime time)
  132. {
  133. FILETIME val = new FILETIME();
  134. long ticks = time.ToFileTime();
  135. val.dwLowDateTime = (int) (ticks & 0xFFFFFFFFL);
  136. val.dwHighDateTime = (int) (ticks >> 32);
  137. return val;
  138. }
  139. private static DateTime ToDateTime(FILETIME time)
  140. {
  141. return new DateTime(
  142. (((long) time.dwHighDateTime) << 32) + time.dwLowDateTime);
  143. }
  144. #region Synthesized Properties
  145. #region PropertyCount
  146. public int PropertyCount
  147. {
  148. get
  149. {
  150. int count;
  151. TR(MsiSummaryInfoGetPropertyCount(m_handle.Handle, out count));
  152. return count;
  153. }
  154. }
  155. [DllImport(Installer.MSI_DLL, CharSet = CharSet.Auto)]
  156. private static extern UInt32 MsiSummaryInfoGetPropertyCount(IntPtr handle,
  157. out int count);
  158. #endregion
  159. public int CodePage
  160. {
  161. get { return GetIntegerProperty(Properties.CodePage); }
  162. set { SetIntegerProperty(Properties.CodePage, value); }
  163. }
  164. public string Title
  165. {
  166. get { return GetProperty(Properties.Title); }
  167. set { SetProperty(Properties.Title, value); }
  168. }
  169. public string Subject
  170. {
  171. get { return GetProperty(Properties.Subject); }
  172. set { SetProperty(Properties.Subject, value); }
  173. }
  174. public string Author
  175. {
  176. get { return GetProperty(Properties.Author); }
  177. set { SetProperty(Properties.Author, value); }
  178. }
  179. public string Keywords
  180. {
  181. get { return GetProperty(Properties.Keywords); }
  182. set { SetProperty(Properties.Keywords, value); }
  183. }
  184. public string Comments
  185. {
  186. get { return GetProperty(Properties.Comments); }
  187. set { SetProperty(Properties.Comments, value); }
  188. }
  189. public string Template
  190. {
  191. get { return GetProperty(Properties.Template); }
  192. set { SetProperty(Properties.Template, value); }
  193. }
  194. public string LastAuthor
  195. {
  196. get { return GetProperty(Properties.LastAuthor); }
  197. set { SetProperty(Properties.LastAuthor, value); }
  198. }
  199. public string Revision
  200. {
  201. get { return GetProperty(Properties.Revision); }
  202. set { SetProperty(Properties.Revision, value); }
  203. }
  204. public DateTime Printed
  205. {
  206. get { return GetDateTimeProperty(Properties.Printed); }
  207. set { SetDateTimeProperty(Properties.Printed, value); }
  208. }
  209. public DateTime Created
  210. {
  211. get { return GetDateTimeProperty(Properties.Created); }
  212. set { SetDateTimeProperty(Properties.Created, value); }
  213. }
  214. public DateTime Saved
  215. {
  216. get { return GetDateTimeProperty(Properties.Saved); }
  217. set { SetDateTimeProperty(Properties.Saved, value); }
  218. }
  219. public int Pages
  220. {
  221. get { return GetIntegerProperty(Properties.Pages); }
  222. set { SetIntegerProperty(Properties.Pages, value); }
  223. }
  224. public int Words
  225. {
  226. get { return GetIntegerProperty(Properties.Words); }
  227. set { SetIntegerProperty(Properties.Words, value); }
  228. }
  229. public bool ShortFileNames
  230. {
  231. get { return 0 != (Words & (int) SourceType.ShortFileNames); }
  232. }
  233. public bool Compressed
  234. {
  235. get { return 0 != (Words & (int) SourceType.Compressed); }
  236. }
  237. public bool AdministrativeImage
  238. {
  239. get { return 0 != (Words & (int) SourceType.AdminImage); }
  240. }
  241. public int Characters
  242. {
  243. get { return GetIntegerProperty(Properties.Characters); }
  244. set { SetIntegerProperty(Properties.Characters, value); }
  245. }
  246. public string Application
  247. {
  248. get { return GetProperty(Properties.Application); }
  249. set { SetProperty(Properties.Application, value); }
  250. }
  251. public string Security
  252. {
  253. get { return GetProperty(Properties.Security); }
  254. set { SetProperty(Properties.Security, value); }
  255. }
  256. #endregion
  257. private static void TR(UInt32 result)
  258. {
  259. Installer.TR(result);
  260. }
  261. private MSIHandle m_handle;
  262. #region IDisposable Members
  263. private void Dispose(bool disposing)
  264. {
  265. if (disposing)
  266. {
  267. GC.SuppressFinalize(this);
  268. m_handle.Dispose();
  269. }
  270. }
  271. public void Dispose()
  272. {
  273. Dispose(true);
  274. }
  275. ~SummaryInformation()
  276. {
  277. Dispose(false);
  278. }
  279. #endregion
  280. }
  281. }