Send message to a Windows process (not its main window)

后端 未结 3 1071
执念已碎
执念已碎 2020-12-02 18:07

I have an application that on a subsequent start detects if there\'s a process with the same name already running and, if so, activates the running app\'s window and then ex

3条回答
  •  死守一世寂寞
    2020-12-02 18:19

    Named Pipes can be used for this. It might be the more acceptable method with .net.You can define a service in the main application that accepts a message from the calling application. Here's a sample of the service, in vb. It calls the main app and passes a string to it, in this case, a filename. It also returns a string, but any parameters can be used here.

    Public Class PicLoadService : Implements IMainAppPicLoad
    
    Public Function LoadPic(ByVal fName As String) As String Implements IMainAppPicLoad.LoadPic
    ' do some stuff here.
    LoadPic = "return string"
    End Function
    
    End Class
    

    The setup in the calling application is a little more involved. The calling and main application can be the same application.

    Imports System.Diagnostics
    Imports System.ServiceModel
    Imports System.IO
    Imports vb = Microsoft.VisualBasic
    
    Module MainAppLoader
    
    Sub Main()
    
    Dim epAddress As EndpointAddress
    Dim Client As picClient
    Dim s As String
    Dim loadFile As String
    Dim procs() As Process
    Dim processName As String = "MainApp"
    
    loadFile = "" ' filename to load
    
    procs = Process.GetProcessesByName(processName)
    
    If UBound(procs) >= 0 Then
      epAddress = New EndpointAddress("net.pipe://localhost/MainAppPicLoad")
      Client = New picClient(New NetNamedPipeBinding, epAddress)
      s = Client.LoadPic(loadFile)
    End If
    
    End Sub
    
     _
    Partial Public Class picClient
        Inherits System.ServiceModel.ClientBase(Of IMainAppPicLoad)
        Implements IMainAppPicLoad
    
        Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
            MyBase.New(binding, remoteAddress)
        End Sub
    
        Public Function LoadPic(ByVal fName As String) As String Implements IMainAppPicLoad.LoadPic
            Return MyBase.Channel.LoadPic(fName)
        End Function
    
    End Class
    
    ' from here down was auto generated by svcutil.
    ' svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config http://localhost:8000/MainAppPicLoad
    ' Some has been simplified after auto code generation.
     _
    Public Interface IMainAppPicLoad
       _
      Function LoadPic(ByVal fName As String) As String
    End Interface
    
     _
    Public Interface IMainAppPicLoadChannel
      Inherits IMainAppPicLoad, System.ServiceModel.IClientChannel
    End Interface
    
     _
    Partial Public Class IMainAppPicLoadClient
      Inherits System.ServiceModel.ClientBase(Of IMainAppPicLoad)
      Implements IMainAppPicLoad
    
      Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(binding, remoteAddress)
      End Sub
    
      Public Function LoadPic(ByVal fName As String) As String Implements IMainAppPicLoad.LoadPic
        Return MyBase.Channel.LoadPic(fName)
      End Function
    End Class
    
    End Module
    
     Public Interface IMainAppPicLoad
     Function LoadPic(ByVal fName As String) As String
    End Interface
    

提交回复
热议问题