Jim Lawless' Blog


Locking a Windows Session

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

Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C

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



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Checking Shift States with DEBUG

A TCP Command Line Interface in Rhino JavaScript

Stacking Images with PerlMagick

WSH2EXE - The Final Chapter

BPL: Batch Programming Language Interpreter

Obfuscated Ruby

A Simple Media Control Interface Script Processor

Pi Day Meets the HTML5 Canvas

Removing IE Popups in C

Yet Another Enhanced Echo Command


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service