Recientemente he comprobado este código nuevamente desde previous blog post para asegurarme de que funciona en las versiones de Windows XP a 7, x86 y x64 y lo limpié un poco.
Aquí es el último código minimalista que comprueba si la estación de trabajo está bloqueada y si el salvapantallas está corriendo envuelto en dos fáciles de usar métodos estáticos:
using System;
using System.Runtime.InteropServices;
namespace BrutalDev.Helpers
{
public static class NativeMethods
{
// Used to check if the screen saver is running
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(uint uAction,
uint uParam,
ref bool lpvParam,
int fWinIni);
// Used to check if the workstation is locked
[DllImport("user32", SetLastError = true)]
private static extern IntPtr OpenDesktop(string lpszDesktop,
uint dwFlags,
bool fInherit,
uint dwDesiredAccess);
[DllImport("user32", SetLastError = true)]
private static extern IntPtr OpenInputDesktop(uint dwFlags,
bool fInherit,
uint dwDesiredAccess);
[DllImport("user32", SetLastError = true)]
private static extern IntPtr CloseDesktop(IntPtr hDesktop);
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SwitchDesktop(IntPtr hDesktop);
// Check if the workstation has been locked.
public static bool IsWorkstationLocked()
{
const int DESKTOP_SWITCHDESKTOP = 256;
IntPtr hwnd = OpenInputDesktop(0, false, DESKTOP_SWITCHDESKTOP);
if (hwnd == IntPtr.Zero)
{
// Could not get the input desktop, might be locked already?
hwnd = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP);
}
// Can we switch the desktop?
if (hwnd != IntPtr.Zero)
{
if (SwitchDesktop(hwnd))
{
// Workstation is NOT LOCKED.
CloseDesktop(hwnd);
}
else
{
CloseDesktop(hwnd);
// Workstation is LOCKED.
return true;
}
}
return false;
}
// Check if the screensaver is busy running.
public static bool IsScreensaverRunning()
{
const int SPI_GETSCREENSAVERRUNNING = 114;
bool isRunning = false;
if (!SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRunning, 0))
{
// Could not detect screen saver status...
return false;
}
if (isRunning)
{
// Screen saver is ON.
return true;
}
// Screen saver is OFF.
return false;
}
}
}
ACTUALIZACIÓN: Código actualizado en base a sugerencias de la comentarios
Cuando la estación de trabajo está bloqueada entonces el método OpenInputDesktop no devuelve un mango, de manera que podamos caer de nuevo en -OpenDesktop para un mango para asegurarse de que está bloqueado por intentar cambiar. Si no está bloqueado, su escritorio predeterminado no se activará porque OpenInputDesktop devolverá un identificador válido para el escritorio que está viendo.
Has publicado un enlace no válido –
Aha, deshabilitaron el hotlinking. ¿Esto funciona? http://images.google.com/imgres?imgurl=http://blog.fatbusinessman.com/blog-post-images/msn-pics-in-popups.png&imgrefurl=http://blog.fatbusinessman.com/ archives/2005/04/07/msn-messenger-7-review/& usg = __ Ed4o5d78BK01ZJ3K6penXkWgsJc = & h = 287 & w = 341 & sz = 21 & hl = es & start = 0 & zoom = 1 & tbnid = _fUzeg729w42dM: & tbnh = 157 & tbnw = 187 & prev =/imágenes% 3Fq% 3Dmsn% 2Bmessenger % 2Baster% 26hl% 3Den% 26biw% 3D1600% 26bih% 3D775% 26tbs% 3Disch: 1 & itbs = 1 & iact = rc & dur = 529 & ei = jQS7TJnID9ug4QasnODRDQ & oei = jQS7TJnID9ug4QasnODRDQ & esq = 1 & page = 1 & ndsp = 33 & ved = 1t: 429, r: 11, s: 0 & tx = 146 & ty = 89 – Pieter