|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Deephaven.ExcelAddInInstaller.CustomActions { |
| 7 | + public class MsiSession { |
| 8 | + public class NativeMethods { |
| 9 | + public const ulong WS_VISIBLE = 0x10000000L; |
| 10 | + |
| 11 | + public const int GWL_STYLE = -16; |
| 12 | + |
| 13 | + // Declare the delegate for EnumWindows callback |
| 14 | + public delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam); |
| 15 | + |
| 16 | + // Import the user32.dll library |
| 17 | + [DllImport("user32.dll")] |
| 18 | + public static extern int EnumWindows(EnumWindowsCallback callback, int lParam); |
| 19 | + |
| 20 | + [DllImport("user32.dll")] |
| 21 | + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); |
| 22 | + |
| 23 | + [DllImport("user32.dll", SetLastError = true)] |
| 24 | + public static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex); |
| 25 | + |
| 26 | + [DllImport("msi.dll", CharSet = CharSet.Unicode)] |
| 27 | + public static extern uint MsiGetProperty( |
| 28 | + int hInstall, |
| 29 | + string szName, |
| 30 | + StringBuilder szValueBuf, |
| 31 | + ref uint pcchValueBuf); |
| 32 | + |
| 33 | + [DllImport("msi.dll", CharSet = CharSet.Unicode)] |
| 34 | + public static extern uint MsiSetProperty(int hInstall, string szName, string szValue); |
| 35 | + |
| 36 | + [DllImport("msi.dll", CharSet = CharSet.Unicode)] |
| 37 | + public static extern int MsiCreateRecord(uint cParams); |
| 38 | + |
| 39 | + [DllImport("msi.dll", CharSet = CharSet.Unicode)] |
| 40 | + public static extern uint MsiRecordSetString(int hRecord, uint iField, string szValue); |
| 41 | + |
| 42 | + [DllImport("msi.dll", CharSet = CharSet.Unicode)] |
| 43 | + public static extern int MsiProcessMessage(int hInstall, uint eMessageType, int hRecord); |
| 44 | + } |
| 45 | + |
| 46 | + public enum InstallMessage : uint { |
| 47 | + FATALEXIT = 0x00000000, // premature termination, possibly fatal OOM |
| 48 | + ERROR = 0x01000000, // formatted error message |
| 49 | + WARNING = 0x02000000, // formatted warning message |
| 50 | + USER = 0x03000000, // user request message |
| 51 | + INFO = 0x04000000, // informative message for log |
| 52 | + FILESINUSE = 0x05000000, // list of files in use that need to be replaced |
| 53 | + RESOLVESOURCE = 0x06000000, // request to determine a valid source location |
| 54 | + OUTOFDISKSPACE = 0x07000000, // insufficient disk space message |
| 55 | + ACTIONSTART = 0x08000000, // start of action: action name & description |
| 56 | + ACTIONDATA = 0x09000000, // formatted data associated with individual action item |
| 57 | + PROGRESS = 0x0A000000, // progress gauge info: units so far, total |
| 58 | + COMMONDATA = 0x0B000000, // product info for dialog: language Id, dialog caption |
| 59 | + INITIALIZE = 0x0C000000, // sent prior to UI initialization, no string data |
| 60 | + TERMINATE = 0x0D000000, // sent after UI termination, no string data |
| 61 | + SHOWDIALOG = 0x0E000000, // sent prior to display or authored dialog or wizard |
| 62 | + } |
| 63 | + |
| 64 | + private IntPtr mMsiWindowHandle = IntPtr.Zero; |
| 65 | + |
| 66 | + private bool EnumWindowCallback(IntPtr hwnd, int lParam) { |
| 67 | + uint wnd_proc = 0; |
| 68 | + NativeMethods.GetWindowThreadProcessId(hwnd, out wnd_proc); |
| 69 | + |
| 70 | + if (wnd_proc == lParam) { |
| 71 | + UInt32 style = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE); |
| 72 | + if ((style & NativeMethods.WS_VISIBLE) != 0) { |
| 73 | + mMsiWindowHandle = hwnd; |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + public IntPtr MsiHandle { get; private set; } |
| 82 | + |
| 83 | + public string CustomActionData { get; private set; } |
| 84 | + |
| 85 | + public MsiSession(string aMsiHandle) { |
| 86 | + if (string.IsNullOrEmpty(aMsiHandle)) |
| 87 | + throw new ArgumentNullException(); |
| 88 | + |
| 89 | + int msiHandle = 0; |
| 90 | + if (!int.TryParse(aMsiHandle, out msiHandle)) |
| 91 | + throw new ArgumentException("Invalid msi handle"); |
| 92 | + |
| 93 | + MsiHandle = new IntPtr(msiHandle); |
| 94 | + |
| 95 | + string allData = GetProperty("CustomActionData"); |
| 96 | + CustomActionData = allData.Split(new char[] { '|' }).First(); |
| 97 | + } |
| 98 | + |
| 99 | + public string GetProperty(string aProperty) { |
| 100 | + // Get buffer size |
| 101 | + uint pSize = 0; |
| 102 | + StringBuilder valueBuffer = new StringBuilder(); |
| 103 | + NativeMethods.MsiGetProperty(MsiHandle.ToInt32(), aProperty, valueBuffer, ref pSize); |
| 104 | + |
| 105 | + // Get property value |
| 106 | + pSize++; // null terminated |
| 107 | + valueBuffer.Capacity = (int)pSize; |
| 108 | + NativeMethods.MsiGetProperty(MsiHandle.ToInt32(), aProperty, valueBuffer, ref pSize); |
| 109 | + |
| 110 | + return valueBuffer.ToString(); |
| 111 | + } |
| 112 | + |
| 113 | + public void SetProperty(string aProperty, string aValue) { |
| 114 | + NativeMethods.MsiSetProperty(MsiHandle.ToInt32(), aProperty, aValue); |
| 115 | + } |
| 116 | + |
| 117 | + public void Log(string aMessage, InstallMessage aMessageType) { |
| 118 | + int hRecord = NativeMethods.MsiCreateRecord(1); |
| 119 | + NativeMethods.MsiRecordSetString(hRecord, 0, "[1]"); |
| 120 | + NativeMethods.MsiRecordSetString(hRecord, 1, aMessage); |
| 121 | + NativeMethods.MsiProcessMessage(MsiHandle.ToInt32(), (uint)aMessageType, hRecord); |
| 122 | + } |
| 123 | + |
| 124 | + public IntPtr GetMsiWindowHandle() { |
| 125 | + string msiProcId = GetProperty("CLIENTPROCESSID"); |
| 126 | + if (string.IsNullOrEmpty(msiProcId)) |
| 127 | + return IntPtr.Zero; |
| 128 | + |
| 129 | + IntPtr handle = new IntPtr(Convert.ToInt32(msiProcId)); |
| 130 | + mMsiWindowHandle = IntPtr.Zero; |
| 131 | + NativeMethods.EnumWindows(EnumWindowCallback, (int)handle); |
| 132 | + |
| 133 | + return mMsiWindowHandle; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments