Link to home
Create AccountLog in
Avatar of FamousMortimer
FamousMortimerFlag for United States of America

asked on

VB.NET - Sleep/Wake Monitor from Windows Service Application

Hi Experts,

The title says it all pretty much.

I am trying to create a windows service that can sleep and wake a monitor.

Thanks
-fm
Avatar of OriNetworks
OriNetworks

Simple google search...

http://stackoverflow.com/questions/3594465/send-display-to-sleep-mode-in-c-sharp

public partial class Form1 : Form
{
    private int SC_MONITORPOWER = 0xF170;
    private uint WM_SYSCOMMAND = 0x0112;

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
    }
}

Open in new window

Avatar of FamousMortimer

ASKER

Well I am using VB and i do not have any forms so this needs to be run from a module.  I have the equivalent VB code

<Runtime.InteropServices.DllImport("user32.dll")> Public Function SendMessage(hWnd As Integer, hMsg As Integer, wParam As Integer, lParam As Integer) As Integer
    End Function

SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, -1)

Open in new window


This does not work in the module, especially because Me.Handle is not valid in a module.
ASKER CERTIFIED SOLUTION
Avatar of FamousMortimer
FamousMortimer
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
it works