如果您不想使用网络传输,那么进行跨会话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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
348 次 |
| 最近记录: |