-2

Is the Win32 function PostMessage user session or machine based? I want to use it for sending information to a single instance application, which also run on terminals server with multiple users. So if this will only work machine based, it will cause problems.

Implementation:

[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

Thank you very much!

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
BendEg
  • 20,098
  • 17
  • 57
  • 131

2 Answers2

1

You can't have a single-instance program which simultaneously shows windows on multiple terminal services sessions, or communciates using windows messages with other sessions.

If your question is, how can you ensure that your program only communicates with applications in the same session, the answer is you don't need to do anything.

Each Windows program is restricted to using Windows USER32 functions on a single session, so PostMessage will only send messages to windows in the same session.

Note that a single session can host processes belonging to different users - that's what the "Run As" functionality does. There are some restrictions on those processes communicating with each other though.

If your question is, how to allow a single instance of the program to be used simultaneously by users in different sessions, the answer is you must split your process into two.

A Service, and Client applications which run for each user in their own session. I would recommend the client activate and communicate with the service using DCOM. This can be a two-way channel so the service can send commands to the client applications too, once the communication is set up.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • The question reads to me more like the OP was concerned about possible interference between sessions and seeking ways to *avoid* communicating across sessions. But maybe they'll add some comments that clarify this. – Damien_The_Unbeliever Dec 30 '15 at 08:54
  • @Damien_The_Unbeliever, maybe. Hard to tell sometimes. – Ben Dec 30 '15 at 08:55
  • I want the opposit of what you said. I just want to send the commands within the user Session – BendEg Dec 30 '15 at 10:47
  • Well it still answers the question! You can't postmessage between sessions even if you want to, so you have nothing to worry about. – Ben Dec 30 '15 at 10:49
  • Ok, would you like to formulate this more clearer? :) Than i will accept the answer +1 :) Thanks a lot – BendEg Dec 30 '15 at 13:30
1

Is the Win32 function PostMessage user session or machine based?

Neither. Messages are passed between processes within a particular desktop. See the MSDN section on Desktops.

However, since each desktop is within a particular Remote Desktop (aka terminal services) session, it is never possible to send a message to a process that is in a different session.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158