PROPVARIANT.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.InteropServices;
  4. namespace Standard;
  5. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  6. [StructLayout(LayoutKind.Explicit)]
  7. internal class PROPVARIANT : IDisposable
  8. {
  9. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  10. public VarEnum VarType
  11. {
  12. get
  13. {
  14. return (VarEnum) this.vt;
  15. }
  16. }
  17. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  18. public string GetValue()
  19. {
  20. if (this.vt == 31)
  21. {
  22. return Marshal.PtrToStringUni(this.pointerVal);
  23. }
  24. return null;
  25. }
  26. public void SetValue(bool f)
  27. {
  28. this.Clear();
  29. this.vt = 11;
  30. this.boolVal = (short) (f ? -1 : 0);
  31. }
  32. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  33. public void SetValue(string val)
  34. {
  35. this.Clear();
  36. this.vt = 31;
  37. this.pointerVal = Marshal.StringToCoTaskMemUni(val);
  38. }
  39. public void Clear()
  40. {
  41. PROPVARIANT.NativeMethods.PropVariantClear(this);
  42. }
  43. public void Dispose()
  44. {
  45. this.Dispose(true);
  46. GC.SuppressFinalize(this);
  47. }
  48. ~PROPVARIANT()
  49. {
  50. this.Dispose(false);
  51. }
  52. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "disposing")]
  53. private void Dispose(bool disposing)
  54. {
  55. this.Clear();
  56. }
  57. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  58. [FieldOffset(0)]
  59. private ushort vt;
  60. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  61. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  62. [FieldOffset(8)]
  63. private IntPtr pointerVal;
  64. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  65. [FieldOffset(8)]
  66. private byte byteVal;
  67. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  68. [FieldOffset(8)]
  69. private long longVal;
  70. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  71. [FieldOffset(8)]
  72. private short boolVal;
  73. private static class NativeMethods
  74. {
  75. [DllImport("ole32.dll")]
  76. internal static extern HRESULT PropVariantClear(PROPVARIANT pvar);
  77. }
  78. }