How to sent text string to service?

帅比萌擦擦* 提交于 2021-02-17 06:41:33

问题


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?

I don't want use sockets because it may be blocked by Windows Firewall.


回答1:


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;


来源:https://stackoverflow.com/questions/16080311/how-to-sent-text-string-to-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!