当前位置: 首页 > wzjs >正文

广州网站推广联盟wordpress根据用户显示文章

广州网站推广联盟,wordpress根据用户显示文章,公司企业文化墙设计方案,南城网站建设公司策划要向已运行的进程发送特定命令(如/exit),而不是启动新进程,需要使用进程间通信(IPC)机制。以下是几种常见的实现方法:一、使用命名管道(Named Pipes)如果ABC.EXE支持通过…

要向已运行的进程发送特定命令(如/exit),而不是启动新进程,需要使用进程间通信(IPC)机制。以下是几种常见的实现方法:

一、使用命名管道(Named Pipes)

如果ABC.EXE支持通过命名管道接收命令,可以这样实现:

1. 在 ABC.EXE 中添加管道服务器代码
' ABC.EXE 中添加以下代码(需在启动时运行)
Imports System.IO.PipesPublic Sub StartPipeServer()Dim thread As New Threading.Thread(AddressOf PipeServerThread)thread.IsBackground = Truethread.Start()
End SubPrivate Sub PipeServerThread()TryWhile TrueUsing server As New NamedPipeServerStream("ABCControlPipe")Console.WriteLine("等待客户端连接...")server.WaitForConnection()Using reader As New StreamReader(server)Dim command As String = reader.ReadLine()Console.WriteLine($"收到命令: {command}")' 处理命令If command = "/exit" Then' 执行退出逻辑Application.Exit()End IfEnd UsingEnd UsingEnd WhileCatch ex As ExceptionConsole.WriteLine($"管道服务器错误: {ex.Message}")End Try
End Sub
2. 在监控程序中发送命令

vb

Imports System.IO.PipesPublic Sub SendCommandToRunningProcess(command As String)TryUsing client As New NamedPipeClientStream(".", "ABCControlPipe", PipeDirection.Out)client.Connect(2000)  ' 等待2秒连接Using writer As New StreamWriter(client)writer.WriteLine(command)writer.Flush()Console.WriteLine($"命令已发送: {command}")End UsingEnd UsingCatch ex As ExceptionConsole.WriteLine($"发送命令失败: {ex.Message}")End Try
End Sub' 使用示例
SendCommandToRunningProcess("/exit")

二、使用命令行参数 + 进程查找

如果ABC.EXE支持通过创建特定文件或命令行参数触发行为,可以这样实现:

1. 修改 ABC.EXE 定期检查命令文件

vb

' 在ABC.EXE中添加定时检查逻辑
Private Sub StartCommandChecker()Dim timer As New Timer(1000)  ' 每秒检查一次AddHandler timer.Elapsed, AddressOf CheckForCommandstimer.Start()
End SubPrivate Sub CheckForCommands(sender As Object, e As ElapsedEventArgs)Dim commandFile As String = Path.Combine(Application.StartupPath, "command.txt")If File.Exists(commandFile) ThenTryDim command As String = File.ReadAllText(commandFile)File.Delete(commandFile)  ' 处理后删除If command = "/exit" ThenApplication.Exit()End IfCatch ex As ExceptionConsole.WriteLine($"读取命令文件失败: {ex.Message}")End TryEnd If
End Sub
2. 在监控程序中创建命令文件

vb

Public Sub SendCommandToRunningProcess(command As String)Dim commandFile As String = Path.Combine(Path.GetDirectoryName(Process.GetProcessesByName("ABC").FirstOrDefault()?.MainModule.FileName),"command.txt")TryFile.WriteAllText(commandFile, command)Console.WriteLine($"命令已写入文件: {command}")Catch ex As ExceptionConsole.WriteLine($"发送命令失败: {ex.Message}")End Try
End Sub

三、使用 Windows 消息(适用于有窗口的进程)

如果ABC.EXE有隐藏窗口,可以发送自定义 Windows 消息:

1. 在 ABC.EXE 中注册并处理自定义消息

vb

Imports System.Runtime.InteropServicesPublic Class MainForm' 注册自定义消息Private Const WM_USER As Integer = &H400Private Const WM_EXIT_COMMAND As Integer = WM_USER + 100<DllImport("user32.dll", CharSet:=CharSet.Auto)>Private Shared Function RegisterWindowMessage(ByVal lpString As String) As IntegerEnd FunctionPrivate exitMessageId As Integer = RegisterWindowMessage("ABC_EXIT_COMMAND")Protected Overrides Sub WndProc(ByRef m As Message)If m.Msg = exitMessageId OrElse m.Msg = WM_EXIT_COMMAND Then' 处理退出命令Application.Exit()ReturnEnd IfMyBase.WndProc(m)End Sub
End Class
2. 在监控程序中发送消息

vb

Imports System.Runtime.InteropServicesPublic Sub SendExitMessageToProcess()Dim processes() As Process = Process.GetProcessesByName("ABC")If processes.Length > 0 ThenDim mainWindowHandle As IntPtr = processes(0).MainWindowHandleIf mainWindowHandle <> IntPtr.Zero Then' 注册相同的消息IDDim exitMessageId As Integer = RegisterWindowMessage("ABC_EXIT_COMMAND")' 发送消息PostMessage(mainWindowHandle, exitMessageId, IntPtr.Zero, IntPtr.Zero)Console.WriteLine("退出消息已发送")ElseConsole.WriteLine("未找到主窗口句柄")End IfEnd If
End Sub<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function RegisterWindowMessage(ByVal lpString As String) As Integer
End Function<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function

四、总结

选择哪种方法取决于ABC.EXE的实现:

  1. 优先使用命名管道:灵活且安全,需修改ABC.EXE代码。
  2. 命令文件方式:简单但有延迟(取决于检查频率)。
  3. Windows 消息:适用于有窗口的进程。

如果无法修改ABC.EXE,可考虑使用外部工具(如 AutoHotkey 发送模拟按键),但可靠性较低。

分享

如果多个进程,管道名称是否要不一样?

是的,如果需要同时管理多个进程实例,每个进程的管道名称必须唯一。否则,不同进程会监听同一个管道,导致命令混淆或无法正确识别目标进程。以下是几种实现多进程管道通信的方案:

一、基于进程 ID(PID)的唯一管道名称

最简单的方法是将进程 ID(PID)作为管道名称的一部分:

1. 在 ABC.EXE 中创建唯一管道

vb

Imports System.IO.PipesPublic Class ABCApplicationPrivate pipeServer As NamedPipeServerStreamPrivate pipeName As StringPublic Sub StartPipeServer()' 使用进程ID生成唯一管道名称pipeName = $"ABCControlPipe_{Process.GetCurrentProcess().Id}"' 创建并启动管道服务器pipeServer = New NamedPipeServerStream(pipeName, PipeDirection.InOut)Dim thread As New Threading.Thread(AddressOf HandleIncomingConnections)thread.IsBackground = Truethread.Start()End SubPrivate Sub HandleIncomingConnections()TryWhile TruepipeServer.WaitForConnection()' 处理命令...Using reader As New StreamReader(pipeServer)Dim command As String = reader.ReadLine()If command = "/exit" Then' 优雅退出逻辑Application.Exit()End IfEnd Using' 重置管道以接收下一个连接pipeServer.Disconnect()End WhileCatch ex As Exception' 错误处理End TryEnd Sub
End Class
2. 在监控程序中发送命令到特定进程
Imports System.IO.PipesPublic Sub SendCommandToProcess(pid As Integer, command As String)Dim pipeName As String = $"ABCControlPipe_{pid}"TryUsing client As New NamedPipeClientStream(".", pipeName, PipeDirection.Out)client.Connect(2000)  ' 2秒超时Using writer As New StreamWriter(client)writer.WriteLine(command)writer.Flush()End UsingEnd UsingCatch ex As ExceptionConsole.WriteLine($"发送命令到进程 {pid} 失败: {ex.Message}")End Try
End Sub' 示例:向所有ABC进程发送退出命令
Public Sub SendExitToAllProcesses()Dim processes() As Process = Process.GetProcessesByName("ABC")For Each proc In processesSendCommandToProcess(proc.Id, "/exit")Next
End Sub

二、使用中央管道管理器(更高级的方案)

如果进程数量较多或需要更复杂的管理,可以实现一个中央管道管理器:

1. 中央管理器设计
Imports System.Collections.ConcurrentPublic Class PipeManagerPrivate Shared pipeMap As New ConcurrentDictionary(Of Integer, NamedPipeServerStream)' 注册新进程的管道Public Shared Function RegisterProcess(pid As Integer) As StringDim pipeName As String = $"ABCControlPipe_{Guid.NewGuid()}"  ' 使用GUID确保唯一性Dim server As New NamedPipeServerStream(pipeName, PipeDirection.InOut)pipeMap.TryAdd(pid, server)' 启动监听线程Dim thread As New Threading.Thread(Sub() HandleConnections(pid, server))thread.IsBackground = Truethread.Start()Return pipeNameEnd Function' 处理管道连接Private Shared Sub HandleConnections(pid As Integer, server As NamedPipeServerStream)TryWhile pipeMap.ContainsKey(pid)server.WaitForConnection()' 处理命令...Using reader As New StreamReader(server)Dim command As String = reader.ReadLine()' 根据PID转发命令到对应进程End Usingserver.Disconnect()End WhileCatch ex As Exception' 错误处理End TryEnd Sub' 向特定进程发送命令Public Shared Sub SendCommand(pid As Integer, command As String)If pipeMap.TryGetValue(pid, out Dim server) Then' 通过管道发送命令...End IfEnd Sub
End Class
2. 进程端注册

vb

' 在ABC.EXE启动时
Public Sub Main()' 向管理器注册自己并获取唯一管道名称Dim pipeName As String = PipeManager.RegisterProcess(Process.GetCurrentProcess().Id)' 保存管道名称用于后续通信My.Settings.PipeName = pipeNameMy.Settings.Save()' 其他初始化逻辑...
End Sub

三、关键注意事项

  1. 管道名称唯一性

    • 确保每个进程的管道名称包含唯一标识符(如 PID、GUID)。
    • 避免使用硬编码的固定名称。
  2. 资源清理

    • 进程退出时,确保关闭并释放管道资源。
    • 可以通过Process.Exited事件监听进程退出,清理对应的管道。
  3. 安全性考虑

    • 对管道名称进行权限控制,防止未授权访问。
    • 考虑使用加密或身份验证增强安全性(如使用PipeSecurity)。
  4. 错误处理

    • 处理管道连接超时、断开等异常情况。
    • 实现重试机制,确保命令可靠发送。

四、总结

通过为每个进程分配唯一的管道名称,可以有效管理多个进程实例。选择哪种方案取决于具体需求:

  • PID 方案:简单直接,适用于大多数场景。
  • 中央管理器:适合复杂系统,提供更灵活的控制和监控能力。

无论采用哪种方式,核心原则是确保管道名称的唯一性和通信的可靠性。


文章转载自:

http://PNN3JtWM.Lbpqk.cn
http://88jucamT.Lbpqk.cn
http://MOwjnlsy.Lbpqk.cn
http://Z9OmoPDz.Lbpqk.cn
http://NfO4OwrW.Lbpqk.cn
http://eR5EGzI7.Lbpqk.cn
http://dtMF7aHm.Lbpqk.cn
http://MpzyGCSX.Lbpqk.cn
http://CJXhAmDD.Lbpqk.cn
http://ZkgQj3pa.Lbpqk.cn
http://cc1JyAXj.Lbpqk.cn
http://mu0NbJTI.Lbpqk.cn
http://ebvBXeKZ.Lbpqk.cn
http://5jD7Al3S.Lbpqk.cn
http://YUSyT2vI.Lbpqk.cn
http://xtPVSZT2.Lbpqk.cn
http://SZZ4jUCD.Lbpqk.cn
http://yDDK6S69.Lbpqk.cn
http://69LEG9Cc.Lbpqk.cn
http://oO0wQUD9.Lbpqk.cn
http://mHllNthz.Lbpqk.cn
http://IHMvOg9m.Lbpqk.cn
http://Jzv4Iq64.Lbpqk.cn
http://dlB78RdF.Lbpqk.cn
http://M7k5xCIc.Lbpqk.cn
http://8qMf1bh2.Lbpqk.cn
http://Snd3sTHa.Lbpqk.cn
http://NCCbpNPv.Lbpqk.cn
http://fP4I025o.Lbpqk.cn
http://W8582VKE.Lbpqk.cn
http://www.dtcms.com/wzjs/709552.html

相关文章:

  • 怎样建立网站邢台建设银行网站
  • 单页网站 seo黄州做网站的
  • 网站里面内外链接如何做做聚美优品网站得多少钱
  • 网站建设拓扑图苏州app定制
  • 网站搭建设计 是什么意思海口网红图书馆
  • 织梦网站搬迁个人怎么做网站
  • 挣钱网站一小时两百做博客网站要什么技术
  • 网站开发语言作用python 创建wordpress
  • wordpress详细安装教程广东seo课程
  • 常德做网站建设的公司学习网站建设的步骤
  • 福州网页建站维护有哪些怎么自己设计logo
  • 网站建设招标提问域名安装wordpress
  • 建网站找兴田德润天津 公司网站建设
  • 什么是网络营销产生的现实原因天津seo公司网站
  • 客户网站建设确认书建湖做网站需要多少钱
  • 做最好的在线中文绅士本子阅读网站如何用 ftp上传网站
  • 不用域名访问网站网站内链怎么优化
  • 网站建设语言环境深圳专业画册设计机构
  • 网站建设的合同 体会大连零基础网站建设培训电话
  • 网站购物建设实训心得体会a做片手机免费网站有哪些
  • 长治网站设计制作网站用visual做的网站
  • html5手机网站教程wordpress登陆ip唯一
  • 合肥网站 技术支持 上诚科技打代码怎么做网站
  • 交通运输网站建设的方案网站建设的要素
  • 青浦手机网站制作有哪些做分析图用的地图网站
  • 宁波有没有开发网站的公司友情链接交换网址大全
  • 中国万网怎么自己做网站采集侠 wordpress
  • 网站建设的意义以及重要性北京网络科技有限公司
  • 网站建设的报告wordpress超详细教程视频教程
  • 网站优化能发外链的gvm网站大全开发公司和物业公司