【问题标题】:How to sent text string to service?如何将文本字符串发送到服务?
【发布时间】:2013-04-18 10:20:01
【问题描述】:

我有一个桌面应用程序和一项服务。如何将字符串从桌面应用程序发送到我的服务并在服务中处理?

我不想使用套接字,因为它可能被 Windows 防火墙阻止。

【问题讨论】:

  • 您可以使用基于文件的命令,当文件被修改/创建时,读取它,处理它,删除它/更改其中的标志。
  • 如果启用了文件或打印机共享(端口 445),您可以使用命名管道
  • 防火墙不会阻止本地通信。无论如何,您最好的选择是共享内存 + 适当的同步事件。
  • 试过 Cromis IPC 吗?围绕命名管道的以 Delphi 为中心的库。或者,如果真的需要高吞吐量多线程数据流量,则可以使用共享内存和命名信号量的组合(如 Firebird XNet)。

标签: delphi service


【解决方案1】:

如果您不想使用网络传输,那么进行跨会话 IPC 的最简单方法可能是使用命名管道。需要注意的主要事情是在创建命名管道时需要提供安全属性。如果不这样做,您将无法在跨会话通信中取得成功。我这样做的代码如下所示:

var
  SA: TSecurityAttributes;
....
SA.nLength := SizeOf(SA);
SA.bInheritHandle := True;
ConvertStringSecurityDescriptorToSecurityDescriptor(
  'D:(A;OICI;GRGW;;;AU)',//discretionary ACL to allow read/write access for authenticated users
  SDDL_REVISION_1,
  SA.lpSecurityDescriptor,
  nil
);
FPipe := CreateNamedPipe(
  '\\.\pipe\MyPipeName',
  PIPE_ACCESS_DUPLEX,
  PIPE_TYPE_MESSAGE or PIPE_READMODE_MESSAGE or PIPE_WAIT,
  PIPE_UNLIMITED_INSTANCES,
  0,//don't care about buffer sizes, let system decide
  0,//don't care about buffer sizes, let system decide
  100,//timout (ms), used by clients, needs to cover the time between DisconnectNamedPipe and ConnectNamedPipe
  @SA
);
LocalFree(HLOCAL(SA.lpSecurityDescriptor));
if FPipe=ERROR_INVALID_HANDLE then begin
  ;//deal with error
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2015-10-20
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多