Including MSMQ as a prerequisite for my application

后端 未结 3 1384
醉梦人生
醉梦人生 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条回答
  •  遥遥无期
    2020-12-07 19:53

    Thank You!! Here's the VB.Net version for anyone who is interested.

    Option Explicit On
    Option Strict On
    
    Imports System.Diagnostics.Process
    Imports System.IO
    Imports System.Text
    'Required in all cases when calling API functions
    Imports System.Runtime.InteropServices
    Imports System.Configuration.Install.Installer
    
     _
    Public Class msmqInstaller
        Inherits System.Configuration.Install.Installer
    
        Private Declare Function LoadLibrary Lib "kernel32" (ByVal lpFileName As String) As IntPtr`enter code here`
         _
         Public Shared Function FreeLibrary(ByVal hModule As IntPtr) As Boolean
            ' Leave function empty - DLLImport attribute 
            ' forces calls to LoadLibrary to
            ' be forwarded to LoadLibrary in KERNEL32.DLL
        End Function
    
        Public Const MAX_PATH As Integer = 256
        ' Dim testKernel As loadlibrary
        Dim p As New Process
        '  Dim startInfo As New ProcessStartInfo("sysocmgr.exe", "/i:sysoc.inf /u:\"" + fileName + " \ "")
        Dim fileName As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "MSMQAnswer.ans")
        Dim writer As New StreamWriter(fileName)
    
        ' Override the 'Install' method of the Installer class. When overridden in a derived class, performs the installation.
        'You must override the Install and Uninstall methods to add the code to perform your custom installation steps.
        Public Overrides Sub Install(ByVal mySavedState As IDictionary)
            MyBase.Install(mySavedState)
            Dim loaded As Boolean = False
            Dim fileName As String
            Dim writer As StreamWriter
            Dim p As Process
    
            Try
                Dim handle As IntPtr = LoadLibrary("Mqrt.dll")
                If handle = IntPtr.Zero Or handle.ToInt32 = 0 Then
                    loaded = False
                Else
                    loaded = True
                    FreeLibrary(handle)
                End If
            Catch ex As Exception
                loaded = False
            End Try
    
            If Not loaded = True Then
                If Environment.OSVersion.Version.Major < 6 Then ' windows xp or earlier
                    fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "MSMQAnswer.ans")
                    writer = New System.IO.StreamWriter(fileName)
                    Using writer
                        writer.WriteLine("[Version]")
                        '  writer.WriteLine("Signature = \"$Windows NT$\"")
                        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")
                    End Using
                    p = New System.Diagnostics.Process()
                    Using p
                        Dim startInfo As New ProcessStartInfo("sysocmgr.exe", "/i:sysoc.inf /u:\" + fileName + " \ ")
                        p.StartInfo = startInfo
                        p.Start()
                        p.WaitForExit()
                    End Using
                Else 'windows vista or later, server 03 
                    p = New System.Diagnostics.Process
                    Using p
                        Dim startInfo As New ProcessStartInfo("ocsetup.exe", "MSMQ-Container;MSMQ-Server /passive")
                        p.StartInfo = startInfo
                        p.Start()
                        p.WaitForExit()
                    End Using
                End If
            End If
    
    
    
        End Sub
    
    End Class
    

提交回复
热议问题