Jim Lawless' Blog


Locking a Windows Session

Originally published on: Wed, 02 Dec 2009 03:11:19 +0000

Several years ago, a friend and I discussed the topic of a program that would, after a period of inactivity, issue the equivalent function of locking one's Windows session via CTRL-ALT-DEL followed by a click on the appropriate "lock" button. The screen-saver was supposed to fulfill this role, but the thought-process was that someone could disable their screen-saver.

I did a little investigation into the matter and could not find a way to lock the session via a program. Years later, a function was added to user32.dll that could be invoked to provide this functionality. The C program below attempts to find this function in User32 dynamically and then invokes it. If not found, an error message will be displayed.

lock.c


// Lock a Windows session.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>

#pragma comment(lib,"user32.lib")

int main(int argc,char **argv) {
   FARPROC lockWorkStation;
   HANDLE user32;
   user32=LoadLibrary("user32.dll");
   lockWorkStation=GetProcAddress(user32,"LockWorkStation");
   if(lockWorkStation!=NULL)
      (*lockWorkStation)();
   else
      MessageBox(NULL,
         "You don't have LockWorkStation in User32",
         "",MB_OK);

}

The source and sample executable file for lock can be downloaded in a single archive at: http://www.mailsend-online.com/wp/lock.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Structuring my Thinking
Next post:Invoking the Default Windows Screen-Saver


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Computers I Have Known

Checking Shift States with DEBUG

Command-Line Image Format Conversion

Open Source Licenses

Obfuscated Perl

Book Review : Using Google App Engine

WSH2EXE part 1

Flirting with Forth

Speeding up JRuby with NailGun

BBS Fun in the Eighties


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer