HCL
HCL

Reputation: 36775

Limiting the scope of a NamedPipeServerStream

I would like to use NamedPipeServerStream to do some small IPC between instances of an app. However I only want to target the server if it is on the same Terminal Server Session as the client (If the app is run in a TS-environment).
An idea to achieve this would be to use the SID of the logon user and concat this with the Pipes name. Hover I think this will give problems if the user is loged on from two different locations and it also seems not very neat to me.

Is there a possiblity to give NamedPipeServerStream a scope in which it responds to calls and to set this scope so that the RDP-Session builds the border? Or has someone a good idea how to limit the scope/achieve the desired behaviour?

Upvotes: 2

Views: 1020

Answers (1)

Alois Kraus
Alois Kraus

Reputation: 13545

If you want to limit it to one session only you can create a named mutex within the local namespace to limit its visibility within your session. When a client wants to connect it can create the same mutex and if it gets as last error code ERROR_ALREADY_EXISTS back it knows that within the current session a pipe server is.

You can get the current session number of your process via ProcessIdToSessionId so you can create a named pipe server with the session id in its name.

[DllImport("kernel32.dll",SetLastError=true)]
static extern int ProcessIdToSessionId(int pid, ref int session);

Addenum: To get your current Session Id you can simply use Process.GetCurrentProcess().SessionId instead of the PInvoke signature defined above.

Upvotes: 3

Related Questions