Including MSMQ as a prerequisite for my application

后端 未结 3 1383
醉梦人生
醉梦人生 2020-12-07 19:34

I\'m working on an application that uses MSMQ for interprocess communication, and I need the setup project to be able to install the service if it isn\'t already. I\'ve chec

3条回答
  •  萌比男神i
    2020-12-07 19:56

    Discovered the answer on my own...the windows component installer is not crippled by the typical inability to install more than one MSI at any given time, so I'm able to use a custom installer action to execute a command line script to install MSMQ.

    Here's my Installer class (your options may obviously vary):

    public partial class MSMQInstaller : Installer
    {
        public MSMQInstaller()
        {
            InitializeComponent();
        }
    
        [DllImport("kernel32")]
        static extern IntPtr LoadLibrary(string lpFileName);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool FreeLibrary(IntPtr hModule);
    
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
    
            bool loaded;
    
            try
            {
                IntPtr handle = LoadLibrary("Mqrt.dll");
    
                if (handle == IntPtr.Zero || handle.ToInt32() == 0)
                {
                    loaded = false;
                }
                else
                {
                    loaded = true;
    
                    FreeLibrary(handle);
                }
            }
            catch
            {
                loaded = false;
            }
    
            if (!loaded)
            {
                if (Environment.OSVersion.Version.Major < 6) // Windows XP or earlier
                {
                    string fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "MSMQAnswer.ans");
    
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName))
                    {
                        writer.WriteLine("[Version]");
                        writer.WriteLine("Signature = \"$Windows NT$\"");
                        writer.WriteLine();
                        writer.WriteLine("[Global]");
                        writer.WriteLine("FreshMode = Custom");
                        writer.WriteLine("MaintenanceMode = RemoveAll");
                        writer.WriteLine("UpgradeMode = UpgradeOnly");
                        writer.WriteLine();
                        writer.WriteLine("[Components]");
                        writer.WriteLine("msmq_Core = ON");
                        writer.WriteLine("msmq_LocalStorage = ON");
                    }
    
                    using (System.Diagnostics.Process p = new System.Diagnostics.Process())
                    {
                        System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo("sysocmgr.exe", "/i:sysoc.inf /u:\"" + fileName + "\"");
    
                        p.StartInfo = start;
    
                        p.Start();
                        p.WaitForExit();
                    }
                }
                else // Vista or later
                {
                    using (System.Diagnostics.Process p = new System.Diagnostics.Process())
                    {
                        System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo("ocsetup.exe", "MSMQ-Container;MSMQ-Server /passive");
    
                        p.StartInfo = start;
    
                        p.Start();
                        p.WaitForExit();
                    }
                }
            }
        }
    }
    

提交回复
热议问题