📄 form1.vb
字号:
Imports System.messaging
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Threading
Public Class Form1
Private mqLocal As MessageQueue
Private mqRemote As MessageQueue
Private listening As Boolean
Private Delegate Sub AppendToListBoxDelegate(ByVal newItem As String)
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function ActivateDevice(ByVal lpszDevKey As String, ByVal dwClientInfo As Integer) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As Integer
End Function
Private Sub ReceiveThread()
Dim formatter As New XmlMessageFormatter(New Type() {GetType(Prospect)})
Me.mqLocal.Formatter = formatter
Do While Me.listening
Dim p As Prospect = DirectCast(Me.mqLocal.Receive.Body, Prospect)
MyBase.Invoke(New AppendToListBoxDelegate(AddressOf Me.AppendToListBox), New Object() {p.ToString})
Loop
End Sub
Private Shared Function ShellWait(ByVal app As String, ByVal args As String) As Integer
If Not File.Exists(app) Then
Return -1
End If
Dim p As Process = Process.Start(app, args)
p.WaitForExit()
Return p.ExitCode
End Function
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
Try
Dim p As New Prospect
p.Name = Me.txtName.Text
p.Company = Me.txtCompany.Text
p.Number = Me.txtNumber.Text
Me.mqRemote.Send(p)
Me.mqLocal.Send(p)
Me.txtName.Text = ""
Me.txtCompany.Text = ""
Me.txtNumber.Text = ""
Catch mqe As MessageQueueException
MessageBox.Show(mqe.ToString)
End Try
End Sub
Private Sub AppendToListBox(ByVal newItem As String)
Me.listBox1.Items.Add(newItem)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitMsmq()
If Not MessageQueue.Exists(Me.mqLocal.Path) Then
MessageQueue.Create(Me.mqLocal.Path)
End If
Dim t As New Thread(New ThreadStart(AddressOf Me.ReceiveThread))
t.Start()
End Sub
Private Shared Sub InitMsmq()
Dim msmqadmPath As String = "\Windows\msmqadm.exe"
If (ShellWait(msmqadmPath, "status") < 0) Then
Dim apppath As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly.GetName.CodeBase)
If (Environment.OSVersion.Version.Major < 5) Then
File.Copy(Path.Combine(apppath, "msmqadm.exe"), "\Windows\msmqadm.exe")
File.Copy(Path.Combine(apppath, "msmqadmext.dll"), "\Windows\msmqadmext.dll")
File.Copy(Path.Combine(apppath, "msmqd.dll"), "\Windows\msmqd.dll")
File.Copy(Path.Combine(apppath, "msmqrt.dll"), "\Windows\msmqrt.dll")
Else
Form1.ShellWait("\Windows\wceload.exe", ("""" & Path.Combine(apppath, "msmq.arm.cab") & """"))
End If
Form1.ShellWait(msmqadmPath, "register cleanup")
Form1.ShellWait(msmqadmPath, "register install")
Form1.ShellWait(msmqadmPath, "register")
Form1.ShellWait(msmqadmPath, "enable binary")
Form1.CloseHandle(Form1.ActivateDevice("Drivers\BuiltIn\MSMQD", 0))
If (Form1.ShellWait(msmqadmPath, "status") < 0) Then
Throw New ApplicationException("Failed to register MSMQ")
End If
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -