2014-01-14 235 views
0

嘿,我在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#示例中一样运行。

我一定错过了转换时没有带过的东西。任何帮助将是伟大的解决这个问题!

+1

'两个应用程序正在运行' - 同一程序的两个实例或不同的程序?顺便说一句,你为什么不使用WCF? –

+2

好主,学习如何编写C#代码将花费的时间少于获取转换后的垃圾运行时间。这是我的建议,对于VB.NET程序员来说,理解C#是一个相当困难的要求。 –

+0

您提供的博客文章链接在此处不起作用。也许是时候考虑使用像WCF这样的一流的进程间通信方法,或者甚至是Remoting。另请参阅http://stackoverflow.com/questions/2082450/communication-between-c-sharp-applications-the-easy-way –

回答

0

您收到“找不到其他正在运行的应用程序”消息的事实表明此位代码存在问题。点击这里,查看我的笔记:

Dim proc As Process = Process.GetCurrentProcess() 
    //This gets an array of all processes currently running with the same name 
    // as this application. 
    Dim processes As Process() = Process.GetProcessesByName(proc.ProcessName) 

    //If there is only one process with this application's name, then it's 
    // THIS process, so there aren't any others by the same name running 
    // right now. 
    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 
     //Thus, this gets called because there are not other applications with 
     //the same name. 
     MessageBox.Show("No other running applications found.") 
    End If 

请注意,当你在environement运行代码,现代演播室版本通常显示为devenv.exe的,所以运行IDE 的两个实例应正常工作。然而! Visual Studio中有一个选项可以在其自己的应用程序(可执行文件)名称下运行代码。如果转到Project Properties - >Debug,则会出现一个标记为Enable the Visual Studio hosting process的选项。禁用该复选框将导致代码在其编译的可执行文件的名称下运行。

相关问题