Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@grawity
Created February 23, 2012 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grawity/1892562 to your computer and use it in GitHub Desktop.
Save grawity/1892562 to your computer and use it in GitHub Desktop.
Workstation unlocker
#pragma once
#define WIN32_LEAN_AND_MEAN
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include "stdafx.h"
#define WM_LOGONNOTIFY 0x004c
#define LN_UNLOCK_WORKSTATION 0x0006
void showError()
{
LPTSTR pBuf;
DWORD nBytes = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), LANG_USER_DEFAULT, (LPWSTR)&pBuf, 4096, NULL);
if (nBytes)
wprintf(L"Error: %s", pBuf);
else
wprintf(L"Failed to format error message: %08x\n", GetLastError());
}
BOOL CALLBACK enumWndProc(HWND hWnd, HWND *hSasWindow)
{
LPTSTR lpClassName[64];
if (!GetClassName(hWnd, (LPTSTR)&lpClassName, 63))
return TRUE;
if (wcscmp((LPTSTR)lpClassName, L"SAS window class") != 0)
return TRUE;
*hSasWindow = hWnd;
return FALSE;
}
int _tmain(int argc, _TCHAR* argv[])
{
HDESK hDesktop = NULL;
HWND hSasWindow = NULL;
hDesktop = OpenDesktop(L"Winlogon", 0, FALSE, DESKTOP_READOBJECTS | DESKTOP_ENUMERATE);
if (!hDesktop) {
showError();
return 1;
}
EnumDesktopWindows(hDesktop, (WNDENUMPROC)enumWndProc, (LPARAM)&hSasWindow);
if (!hSasWindow) {
wprintf(L"Error: WinLogon window not found.\n");
return 1;
}
PostMessage(hSasWindow, WM_LOGONNOTIFY, LN_UNLOCK_WORKSTATION, 0);
wprintf(L"Success: Unlock message sent to %p\n", hSasWindow);
return 0;
}
@grawity
Copy link
Author

grawity commented May 7, 2014

Works on Windows XP when run with some psexec magic (tell psexec to run it on the secure logon desktop).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment