2014-01-14 2 views
0

안녕하세요,이 코드는 모두 here입니다. SendMessage API를 사용하여 실행중인 2 개의 .net 응용 프로그램에서 메시지를 앞뒤로 보낼 수 있습니다. 테스트 할 때 모든 것이 C# 코드에서 잘되지만 VB.net으로 변환해야했습니다. 에 보이는 나던 모두 내 애플 리케이션을위한 위의 코드를 사용하여, 그러나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 

:> VB.net 변환이있어 -

그래서 내가 온라인 C# .NET을 사용 에 도달하면 sendmessage 액션을 생성합니다. processes.Length> 1 Then. 항상 알려줍니다 실행중인 다른 응용 프로그램이 없습니다. 비록 내가 C# 예제에서 실행 한 두 앱을 모두 가지고있다.

나는 변환 할 때 나르 지 않은 것을 놓치고 있어야합니다. 이 문제를 해결하는 데 도움이 될 것입니다!

+1

'두 앱이 모두 실행 중입니다.'- 동일한 프로그램 또는 두 가지 프로그램의 두 인스턴스가 있습니까? BTW, 왜 WCF를 사용하지 않습니까? –

+2

좋은 주님, C# 코드를 작성하는 방법은 변환 된 쓰레기를 실행하는 것보다 시간을 절약 할 것입니다. 어느 것이 좋습니다, C#은 요즘 VB.NET 프로그래머에게는 꽤 어려운 요구 사항입니다. –

+0

제공된 블로그 게시물 링크가 여기에서 작동하지 않습니다. 아마도 WCF 또는 심지어 Remoting과 같은 일류 프로세스 간 통신 방법을 사용할 때가 왔습니다. @stackoverflow.com/questions/2082450/communication-between-c-sharp-applications-the-ewayway –

답변

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에서 코드를 실행하면, 현대적인 스튜디오 버전은 일반적으로 너무 정상적으로 작동해야 IDE 의 두 인스턴스를 실행 DEVENV.EXE로 표시 양해하여 주시기 바랍니다. 하나! Visual Studio에는 자체 응용 프로그램 (실행 가능) 이름으로 코드를 실행하는 옵션이 있습니다. Project Properties ->Debug으로 가면 Enable the Visual Studio hosting process이라는 옵션이 있습니다. 이 확인란을 비활성화하면 컴파일 된 실행 파일 이름으로 코드가 실행됩니다.

관련 문제