如何发送文本字符串到服务? [英] How to sent text string to service?
问题描述
我有一个桌面应用程序和一项服务.如何将字符串从桌面应用程序发送到服务中并在服务中处理?
I have a desktop application and a service. How can i send string from desktop application into my service and handle it in a service?
我不想使用套接字,因为它可能被Windows防火墙阻止.
I don't want use sockets because it may be blocked by Windows Firewall.
推荐答案
如果您不想使用网络传输,那么进行跨会话IPC的最简单方法可能就是使用命名管道.需要注意的主要事情是,在创建命名管道时,您将需要提供安全属性.否则,您将无法在跨会话通信中取得成功.我这样做的代码如下:
If you don't want to use network transport then probably the simplest way to do cross-session IPC is to use a named pipe. The main thing to take care over is that you will need to supply security attributes when creating the named pipe. Without doing so you won't succeed in cross-session communications. My code to do so looks like this:
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;
这篇关于如何发送文本字符串到服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!