none
Wake remote display RRS feed

  • Question

  • I have the following code that allows my to control the power of the monitor.

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace MonPwr
    {
        public partial class HiddenForm : Form
        {
            // ReSharper disable InconsistentNaming
            private const int WM_SYSCOMMAND = 0x0112;
            private const int SC_MONITORPOWER = 0xF170;
            // ReSharper restore InconsistentNaming
    
            [DllImport("user32.dll")]
            private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    
            public HiddenForm(bool power)
            {
                InitializeComponent();
                SendMessage(Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, power ? -1 : 2);
                Load += HiddenForm_Load;
            }
    
            private void HiddenForm_Load(object sender, EventArgs e)
            {
                Close();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                Visible = false;
                ShowInTaskbar = false;
                base.OnLoad(e);
            }
        }
    }

    This code work fine if I execute it locally, but if I copy the application to a remote machine and execute it using the Win32_Process.Create() method, it doesn't do anything. I can see the process fleetingly appear in Task Manager, although nothing happens. If I execute the process locally on that same machine, again it works fine.

    I've outputted the return code from SendMessage() to a text file, and it is returning 0.

    Is this because the process is running non-interactively?

    I've also tried using Scheduled Tasks and PSExec without success.


    • Edited by sw3103 Tuesday, March 10, 2015 3:28 PM
    Tuesday, March 10, 2015 3:27 PM

Answers

All replies

  • Not authoritative, but looks like this developer went down the same path. See the 'Architecture' section

    http://www.codeproject.com/Articles/44738/Turn-Off-Monitor-when-PC-is-Locked

    This is really a Win32 question, so I'd post there, e.g. https://social.msdn.microsoft.com/Forums/en-US/home?category=windowsdesktopdev

    • Marked as answer by sw3103 Wednesday, March 11, 2015 4:55 PM
    Tuesday, March 10, 2015 9:25 PM
  • Hi sw3103,

    I have tested your code on remote machine, yes it shows nothing. Very wierd. Never seen that. I doubt Win32SendMessage has some limitation.

    After search with other way to implement this. Using lockworkstation (user32) both works on local machine and remote machine. Please try and test.

      [DllImport("user32.dll", SetLastError = true)]
            static extern bool LockWorkStation();
       private void HiddenForm_Load(object sender, EventArgs e)
            {
               // Close();
                LockWorkStation();
            }

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 11, 2015 7:55 AM
    Moderator
  • Thank you. I read the Architecture section, and it's headed me down the correct path to getting this process running in the active session.

    I have managed to get this working using PsExec.exe with the -i switch. Not sure why it didn't work for me previously, but it's all working now.

    Wednesday, March 11, 2015 4:55 PM