加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何从一个Delphi程序的一个实例发送一个字符串到另一个?

发布时间:2020-12-15 10:17:05 所属栏目:大数据 来源:网络整理
导读:从我的程序的一个实例发送一个字符串到我的程序的另一个实例的最好和最简单的方法是什么?接收程序必须执行一个过程,使用接收到的字符串作为参数。 我开始阅读DDE,但我感到困惑。我还有什么其他选择,最简单的方法是什么? 解决方法 使用命名管道,但我会
从我的程序的一个实例发送一个字符串到我的程序的另一个实例的最好和最简单的方法是什么?接收程序必须执行一个过程,使用接收到的字符串作为参数。

我开始阅读DDE,但我感到困惑。我还有什么其他选择,最简单的方法是什么?

解决方法

使用命名管道,但我会推荐Russell Libby的命名管道组件。有一个TPipeClient和TPipeServer组件。

截至(2013-10-04)Francoise Piette and arno.garrels@gmx.de updated this source code使用Delphi 7编译为XE5(早期版本可能会编译,但未经测试),并将其放在这里:
http://www.overbyte.be/frame_index.html?redirTo=/blog_source_code.html

这两个组件使得命名管道变得非常简单,命名管道非常适合进程间通信(IPC)。

His website is here.寻找“Pipes.zip”。来源的描述是://说明:为Delphi设置客户端和服务器命名的管道组件,以及//控制台管道重定向组件。

此外,Russell帮助我在Experts-Exchange上使用旧版本的这个组件,在控制台应用程序中通过命名管道发送/接收消息。这可能有助于作为使用他的组件让您开始运行的指南。请注意,在VCL应用程序或服务中,您不需要像我在此控制台应用程序中编写自己的消息循环。

program CmdClient;
{$APPTYPE CONSOLE}

uses
  Windows,Messages,SysUtils,Pipes;

type
  TPipeEventHandler =  class(TObject)
  public
     procedure  OnPipeSent(Sender: TObject; Pipe: HPIPE; Size: DWORD);
  end;

procedure TPipeEventHandler.OnPipeSent(Sender: TObject; Pipe: HPIPE; Size: DWORD);
begin
  WriteLn('On Pipe Sent has executed!');
end;

var
  lpMsg:         TMsg;
  WideChars:     Array [0..255] of WideChar;
  myString:      String;
  iLength:       Integer;
  pcHandler:     TPipeClient;
  peHandler:     TPipeEventHandler;

begin

  // Create message queue for application
  PeekMessage(lpMsg,WM_USER,PM_NOREMOVE);

  // Create client pipe handler
  pcHandler:=TPipeClient.CreateUnowned;
  // Resource protection
  try
     // Create event handler
     peHandler:=TPipeEventHandler.Create;
     // Resource protection
     try
        // Setup clien pipe
        pcHandler.PipeName:='myNamedPipe';
        pcHandler.ServerName:='.';
        pcHandler.OnPipeSent:=peHandler.OnPipeSent;
        // Resource protection
        try
           // Connect
           if pcHandler.Connect(5000) then
           begin
              // Dispatch messages for pipe client
              while PeekMessage(lpMsg,PM_REMOVE) do DispatchMessage(lpMsg);
              // Setup for send
              myString:='the message I am sending';
              iLength:=Length(myString) + 1;
              StringToWideChar(myString,wideChars,iLength);
              // Send pipe message
              if pcHandler.Write(wideChars,iLength * 2) then
              begin
                 // Flush the pipe buffers
                 pcHandler.FlushPipeBuffers;
                 // Get the message
                 if GetMessage(lpMsg,pcHandler.WindowHandle,0) then DispatchMessage(lpMsg);
              end;
           end
           else
              // Failed to connect
              WriteLn('Failed to connect to ',pcHandler.PipeName);
        finally
           // Show complete
           Write('Complete...');
           // Delay
           ReadLn;
        end;
     finally
        // Disconnect event handler
        pcHandler.OnPipeSent:=nil;
        // Free event handler
        peHandler.Free;
     end;
  finally
     // Free pipe client
     pcHandler.Free;
  end;

end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读