I create a kill switch in my application so that only one instance can run on the same computer at a time. I accomplish this by sending messages between the process of the running application and the process of the new instance of the application:
[DllImport("user32.dll", EntryPoint = "PostMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern int PostMessage(int hwnd, int wMsg, int wParam, int lParam);
I cannot use Process.Kill because if another user starts the application and the current user does not have sufficient privileges, I get problems.
When starting 2 instances under the same user account, there are no problems. Messages are sent correctly and received correctly. However, when starting one instance from one user account, when switching users and starting the second instance, my first instance does not receive messages.
Here is my logic for subscribing to window messages:
var wih = new WindowInteropHelper(this); var hwndSource = HwndSource.FromHwnd(wih.Handle); var hwndSourceHook = new HwndSourceHook(HookHandler); if (hwndSource != null) hwndSource.AddHook(hwndSourceHook);
And here is my hook handler:
private IntPtr HookHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { handled = false; switch (msg) { case 0x400:
Here's the logic for sending a message:
private void SendString() { //create byte array byte[] ba = null; //encode string to byte array if (object.ReferenceEquals(enc, Encoding.UTF8)) { ba = Encoding.UTF8.GetBytes(lParam); } else if (object.ReferenceEquals(enc, Encoding.Unicode)) { ba = Encoding.Unicode.GetBytes(lParam); } else if (object.ReferenceEquals(enc, Encoding.ASCII)) { ba = Encoding.ASCII.GetBytes(lParam); } else { ba = Encoding.Default.GetBytes(lParam); } int i = 0; for (i = 0; i <= ba.Length - 1; i++) { //start post message PostMessage(hwnd, wMsg, wParam, ba[i]); } //post a terminator message to destination window PostMessage(hwnd, wMsg, wParam, 0); }
No Win32 errors are set by the PostMessage function. I cannot find any documentation related to sending messages between processes through user accounts. Is this something that really cannot be done?