嘿,我在C#中发现了这个代码here,它允许使用SendMessage API从运行的2个.net应用程序来回发送消息。在测试时,C#代码中的一切都很顺利,但我需要将它转换为VB.net。vb.net应用程序之间的通信
所以我用网上的C# - > VB.net转换器和得到这个:
进口System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private Const RF_TESTMESSAGE As Integer = &HA123
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function SendMessage(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
Public Sub New()
InitializeComponent()
Me.Text = String.Format("This process ID: {0}", Process.GetCurrentProcess().Id)
End Sub
<STAThread()> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim proc As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(proc.ProcessName)
If processes.Length > 1 Then
For Each p As Process In processes
If p.Id <> proc.Id Then
SendMessage(p.MainWindowHandle, RF_TESTMESSAGE, IntPtr.Zero, IntPtr.Zero)
End If
Next
Else
MessageBox.Show("No other running applications found.")
End If
End Sub
Protected Overrides Sub WndProc(ByRef message As Message)
If message.Msg = RF_TESTMESSAGE Then
ListBox1.Items.Add("Received message RF_TESTMESSAGE")
End If
MyBase.WndProc(message)
End Sub
End Class
但是,使用上面的代码都为我的应用程序似乎不当它到达时产生任何sendmessage动作如果processes.Length> 1 Then。它总是告诉我找不到其他正在运行的应用程序。即使我有两个应用程序正如我在C#示例中一样运行。
我一定错过了转换时没有带过的东西。任何帮助将是伟大的解决这个问题!
'两个应用程序正在运行' - 同一程序的两个实例或不同的程序?顺便说一句,你为什么不使用WCF? –
好主,学习如何编写C#代码将花费的时间少于获取转换后的垃圾运行时间。这是我的建议,对于VB.NET程序员来说,理解C#是一个相当困难的要求。 –
您提供的博客文章链接在此处不起作用。也许是时候考虑使用像WCF这样的一流的进程间通信方法,或者甚至是Remoting。另请参阅http://stackoverflow.com/questions/2082450/communication-between-c-sharp-applications-the-easy-way –