问题
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