Yeah right, its very simple, You can do it from control panel, Your laptop does it, I have a cheap laptop, blah blah blah…

Blv me when I say that I cannot do it on any of my 4 laptops. I can make it to sleep, hibernate or even turn off. But one thing I cannot do on any of my windows 7 laptops is to make them lock windows on closing lid.

I also thought that it should be easy, should be some option in control panel, should be some utility available if I google. But guess what, nothing at all. Nothing in entire world wide web. I am not saying that I am an expert in search but I couldn’t find anything to perform this simple task for me.

At the end, I had to write it myself. A small piece of c# code, doing just that. Nothing fancy and, to my amazement, very very easy to implement. I’ve seen that people want to do it but nobody knows how to do it. It’s not because its hard, but only because its not very well known and properly documented. I intend to keep it same and have no intention of writing a whole tutorial on how to write this tool.

However, I can mention that all one has to do is register a service or application to listen to power setting change event and be sure to register for lid switch change event. That’s it.

If you don’t want to do it yourself but need this tweak, ask me and I’ll email it to you Dowload it from here.

And Microsoft!!! Thanks for at least providing the infrastructure to do it but a documentation would have helped even more.

Note: I would like to mention that this tool works only on Windows 7 and Vista. However, I have not tested it for XP and have reports that it does not work for XP.

Code (as lot of people want to understand how it works):

You need only do the following. This is not exact code but just idea about what you need and how to use it.

  1. Declare IDs
    static Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
    const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
    const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001;
    // This structure is sent when the PBT_POWERSETTINGSCHANGE message is sent.
    // It describes the power setting that has changed and contains data about the change
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    internal struct POWERBROADCAST_SETTING
    public Guid PowerSetting;
    public uint DataLength;
    public byte Data;
  2. Declare the Native Function we need to use
    [DllImport(@"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification",CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient,ref Guid PowerSettingGuid,Int32 Flags);
  3. This is how you register for lid switch change
    private void RegisterForPowerNotifications(IntPtr hwnd)
    IntPtr hLidSwitch = RegisterPowerSettingNotification(hwnd, ref GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
  4. RegisterForPowerNotifications(this.Handle); //call this function to register for power notification
  5. override wnd proc:
    protected override void WndProc(ref Message m)
    base.WndProc(ref m);
    if (m.Msg == WM_POWERBROADCAST)
    catch (Exception)
  6. Rest of the code
    private void PowerSettingChange(Message m)
    m.LParam, typeof(POWERBROADCAST_SETTING));
    if (ps.DataLength == Marshal.SizeOf(typeof(Int32)) && ps.PowerSetting == GUID_LIDSWITCH_STATE_CHANGE)
    //use some logic to lock workstation. You can uSe the below functions
    public static extern void LockWorkStation(); private void LockSystem()

private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient,ref Guid PowerSettingGuid,Int32 Flags);

This entry was posted on Friday, December 10th, 2010 at 10:05 pm and is filed under Technical, Tools & Techs. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

145 comments so far


If you could mail me that piece of software then you’re my hero! I’ve been looking for ages for this tweak. And if it’s to much to ask could you also mail me the source code because I wanted to program it myself but didn’t know where to start so I’d really like to know how you did this. Thanks!

December 23rd, 2010 at 5:43 am

awesome! could you please email me this tweak? you’d be my hero!
it would be nice if you could also add the source code because i was about to make myself but i didn’t know where to start. but if you don’t want to to add the source code, i understand. thanks

December 23rd, 2010 at 1:29 pm

lol i didnt see my first so i thought it was gone so i re-posted. sorry :)

December 23rd, 2010 at 1:30 pm

wow… hero stuff is kinda cool. I would have emailed you the tool anyways :)
btw, about comments, these were pending approval.

December 23rd, 2010 at 7:44 pm

Please email me the tweak! thanks.

January 27th, 2011 at 5:13 am

Hey, could you please email me the tweak i really need it, or at least let me know if you have it. thanks in advance.

February 8th, 2011 at 1:52 pm

I’d really appreciate to see this tweak.

February 15th, 2011 at 3:25 pm

Hey, can you mail me this tool, please? Thanks in advance.

February 16th, 2011 at 7:42 am

hi there,
i’ve send you an email about the tool you send me. the problem i’m having is that it blocks my pc from hibernating when i close the lid after i press hibernate.any idea how to solve this problem. (pls don’t say: don’t close the lid while hibernating) =) thanx

February 16th, 2011 at 2:01 pm

Charles, Rafael, Daryel… You must have got the tweak by now. Sorry for delay. I am out and have imited access to internet.

thanks for pointing it out. I’ll look into it as soon as I get back (in few weeks).

February 18th, 2011 at 2:59 pm

I would love to have a copy of this if you could I too cannot believe no one has built this into windows it seems so logical …… oh wait thats probably why ;)

March 11th, 2011 at 12:17 pm

Sure Paul, I’ll send it to you ASAP. Please not that this tool has a bug, read comment by stijn to know about it.
sorry guys, I still don’t have time to fix it. I’ll remain busy and away from home for few more months. After I am back, I’ll try to fix it ASAP.

EDIT: Paul, I tried to email you but mail bounced back.

March 11th, 2011 at 1:36 pm

It’s ridiculous that this isn’t built into the system. Please could you email me the tweak I’m no good at coding :(

May 16th, 2011 at 6:53 pm

I’ve wondered why MS doesn’t implement this feature as well. I’d love to have your tweak emailed to me if you could. Thanks!

May 20th, 2011 at 1:52 pm

mail it to me, please!

May 20th, 2011 at 5:04 pm

Pls mail it to me. In dire need of it.

May 30th, 2011 at 3:26 pm

Can you mail it to me, please?
Thanks a lot!

May 30th, 2011 at 7:48 pm
Muhammad Moazzam

wow, can i also have it please? :)

June 2nd, 2011 at 7:50 am

I could use this utility, I am a c# developer as well, and much like yourself I have been all over the web looking for this functionality, you sir seem to be the only one who has put something like this together. So congrats on that, and please help.

June 7th, 2011 at 5:10 pm

Pls mail it to me. thks

June 8th, 2011 at 5:16 am

Can you send me a copy as well. This would be incredibly useful.

June 8th, 2011 at 10:54 pm

This seems like an awesome utility could you please e-mail this because it seems to be the only way without me messing with programming (I’m not great put lightly…) I got this link from

but you may want to repost with an updated link… the old one 404′s out… :)

June 11th, 2011 at 2:28 am

Could you send me a copy please.Thanks.

June 17th, 2011 at 6:10 am

Desperate to have this feature and see how you did it. Please could you send me a copy of the source code?

June 17th, 2011 at 3:51 pm

I really appreciate your interest Dimitri but I don’t give away source code. However, I can guide you to how it’s done. All I want is to make sure that you have some effort on your own rather than just looking at source. I hope you understand :)

June 17th, 2011 at 4:15 pm

That’s cool also, thanks :)

June 17th, 2011 at 4:41 pm

Please send this to me too thanks

June 19th, 2011 at 5:08 am

I’d really appreciate this feature! Please send an e-mail when you get a chance. Thank you.

June 20th, 2011 at 12:17 am

Can you send me the tool for locking a laptop on lid close please? I think it’s awesome that you were able to figure this out. If you shared it with me, I’d really appreciate it, thanks!

June 21st, 2011 at 5:03 am

Assalam Alaikum Bhai.

I would be very thankfull to have this problem reliever sw.

June 25th, 2011 at 9:09 am


I know it’s kind of old, but I’m also interested in this. I’m primarily interested in the source code as I am myself a coder, and I just don’t have the time or the will to find this out on my own.


July 2nd, 2011 at 5:05 pm

Sounds awesome – if you could email me the tweak that would be greatly appreciated! Thanks!

July 5th, 2011 at 8:17 pm

Can you pls mail it to me too? Thanks in advance!

July 11th, 2011 at 5:58 am
Ryan Mitchell

Hello, I would greatly appreciate it if you send it to Me, ThankYou!

July 11th, 2011 at 5:59 am

I am really sorry about not mentioning the target Os in my post. I would like to mention that this toolw orks only on Windows 7 and may work on Vista as well. However, I have not tested it for XP and have reports that it does not work for XP. I’ll update the post with this information.

July 11th, 2011 at 11:30 am

Hi, I’m desperately looking for a solution for this problem, and you seem to be almost the only person with the perfect tool for it.

If you still have it, I would very much appreciate it if you could send it to me.

With regards, Mike

July 12th, 2011 at 7:59 pm

Would love to have this app. Can’t believe its not built in. Thanks.

July 14th, 2011 at 1:29 pm
Luke Maw


I’d really love this application too! Would you e able to email it to me please?

Many thanks in anticipation!


July 22nd, 2011 at 6:43 pm

Hey, this should totally be a windows intergreated feature, can you send it to me?

ill give you my redundant email to send it to just incase spammers are watching lol


July 24th, 2011 at 5:40 pm

Looks like I am not the only one anymore with a solution. Based on my not so good programming skills, I can advise you to look at other solutions if you have problem with mine. For an alternative, look at comment by JonusC. I don’t know if my post helped him or not and I don’t want to take any credit at all. But apparently, he was able to write his own tool. I am assuming, again based on my skills, that his tool is probably better than mine. Check that if you like.

July 25th, 2011 at 3:27 pm
James Hyde

Hi, would definitely like to try this program. Could you send to – also, could you tell me – what programming language did you code this in?

Cheers, James

August 10th, 2011 at 9:59 pm

Thanks for creating it. Can you send me a copy of this utility?

August 21st, 2011 at 4:17 am

great job out there! im interested at your program. could you send it to my email? thanks!

September 12th, 2011 at 9:42 pm

Please send me a copy of this tool. Thanks.

September 25th, 2011 at 10:05 pm

This is EXACTLY what I have been looking all over for. I would be very appreciative if you could e-mail the tweak please. I don’t know why Microsoft wouldn’t incorporate this feature in the first place. Thank you for making and distributing this!

October 28th, 2011 at 5:22 pm
Roger Mihay

Would you please send me a copy. Thanks for your work and willingness to share.

November 11th, 2011 at 12:03 pm
Jere S

Please send me copy.

December 4th, 2011 at 8:33 am

Please send me copy. Thanks!

December 4th, 2011 at 8:34 am

Hi! could you send me your tool too please? ^^

December 4th, 2011 at 4:50 pm

Hello, I checked out Jonus’ site and it appears that some dumbass has hacked it and the tweak is unavailble. I really need this tweak and found your question on Microsoft’s website. Any way you would make an exception and send me the tweak? Thanks

December 10th, 2011 at 12:12 pm

Please, mail this to me!

December 10th, 2011 at 12:50 pm

I have 3 requests.

1) Maybe you should stop directing people to Jonus’ site, as it appears it may have malicious intent.

2) Could you send me your Lid locking utility?

3) Are you willing to send me the C# source code, so maybe I could make it better?


January 2nd, 2012 at 12:27 am

Please email me the tweak! thanks.

January 6th, 2012 at 3:50 pm

could you email this to me please

January 19th, 2012 at 3:41 pm
Dominik Stephen

Please, send me your tweak.

January 25th, 2012 at 10:37 am

Can you send me the program/the source?
Im a visual studio c# developer & I’ll be happy to see the code.


February 2nd, 2012 at 5:44 am

Like u said nothing on the entire World Wide Web! Please i would like to have this software, please send it to my inbox.

Thank you!

February 3rd, 2012 at 5:18 am

Can’t beleive something this basic is missing from win7, it just seems so obvious. It would be greatly appreciated if you could email me the utility. Jonus’s website is still offline due to hackers.

February 12th, 2012 at 7:34 am

link sent to all who waited. Sorry for delay. Those who asked for source, sorry for not sending. Don’t have time to dig it out right now. Sorry!

February 19th, 2012 at 2:06 pm

I have been looking for such a utility for some time. Can you please send me link. thanks.

February 24th, 2012 at 3:20 am

Can you mail me either or both of the programs? edpb @

February 27th, 2012 at 4:25 am
Ishfaq Ul Haque

Dear AK,

This is what I wanted from so long. Thanks Allah, you have the solution. Please email me the application.

Fe’ Amanillah.


March 5th, 2012 at 4:44 am

hi. can I get this one too? i got windows 8 CP, just want to see if this works. thanks. :)

March 16th, 2012 at 8:17 pm

If you are not working for any future releases, can you send me the source of the program? Please?

March 22nd, 2012 at 3:47 pm
Eric Boos

Hi, hope you are still reading this. I really need this tweak if possible. I found you thru your reply on the Microsoft site. Thanks!

March 27th, 2012 at 8:09 pm
Dave Davis

Can I get a copy too?

March 29th, 2012 at 11:19 pm

Those wanting the source, I will be writing a post soon containing and explaining the source code. I will also link the utility to that post so that you dont need to wait for my email to get your hands on it.
Thanks for understanding and being patient.

April 3rd, 2012 at 6:09 pm

I want it! please email to me

April 4th, 2012 at 9:10 pm
Anders Andersen

If you are still providing the utility, I would love to have a copy. It is exactly what I am looking for!

April 6th, 2012 at 6:03 am

I’d really like to have a copy of the program on my own. Please…

April 18th, 2012 at 11:01 am

1) Go to regedit > HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\Power
2) Right click > New > DWORD (32 bits)
3) Rename it to AwayModeEnabled
4) Select it > Right click > Change value to 1 (Hexadecimal)
5) Go to the Power Options settings page and change the lid close action to sleep.

That worked for me!

April 25th, 2012 at 1:11 am

I’d appreciate this utility too. I’m also using Win-7, so what you’ve got should work for me. Thanks and good work.

May 2nd, 2012 at 10:45 am
Mark Thompson

Please could you provide me with your utility or link where I can download.

May 9th, 2012 at 9:50 am

Could you send this great utility to me too in email?


May 10th, 2012 at 12:59 pm

Could you please email me your utility? The source code would be great too!

Thank you very much!!!

May 17th, 2012 at 4:02 pm

Hi, hope you are still reading this. I really need this Great Utility possible.

June 1st, 2012 at 10:35 am
g b

hi, i’m looking for this tweak for years…
could you please send me a copy??
thank you

June 1st, 2012 at 12:43 pm

Hi there AK, I’m really looking a lot around for a tweak like this and I’ve found your blog.
I see the last comment was on 3rd April and no more news, could you please send me the utility or the source code (or both if u like)? I’m a C# developer and I can build it myself if you don’t mind.

If you can’t send me the utility please provide some news about a release date, thanks, regards

June 6th, 2012 at 3:15 am
Harsh Vardhan verma

Please send me ur utility. Thanx

June 10th, 2012 at 6:40 am

Can you please send me a copy too?

June 10th, 2012 at 10:50 pm

Please send me ur utility.


June 17th, 2012 at 5:28 pm

Please, send me your utility.

June 18th, 2012 at 7:18 am

I would love a copy, Thanks

June 25th, 2012 at 12:32 am

Its a good tool… mail me. must come with name of pakistani person to share it on web and keep it up.

madrid, spain

June 25th, 2012 at 9:19 am

Can you email me the program.

Also I like it to go to sleep when not connected to power, but when it is plugged in I just want it to lock when I shut the lid, can this program distinguish between plugged in and not plugged in? Or does it do it for both?

June 30th, 2012 at 11:06 am

You’re a genious !! Can you please email me this tweak? Thank you :D

July 3rd, 2012 at 4:53 pm
Always happy

hai… could u please mail me the tweak.. ur discovery is amazing.. thanks in advance :):)

July 5th, 2012 at 9:56 am

I would like to use this utility.
Please send ASAP….Thanks!

July 9th, 2012 at 5:17 pm

Can you email me the utility? thanks

July 17th, 2012 at 5:36 pm

This lid lock issue has been making me crazy for years. Tiago’s solution did work for me (thanks Tiago), however I’d still like to try the program you wrote, could you please send it to me?

July 19th, 2012 at 2:49 am

Hello. I would like it, too.

July 19th, 2012 at 7:25 pm

Very interesting, exactly what I need! Please send it to me.

July 22nd, 2012 at 2:20 pm
Sanjan Patel

send it to my mail id

July 24th, 2012 at 4:58 am

Very good utility.
Please mail me.

July 24th, 2012 at 5:55 am

Can you send me your utility? I think this is just what I’m looking for. Thank You!

July 24th, 2012 at 2:37 pm
simon r

hello, I’d be very happy if you could send me the program!



July 30th, 2012 at 4:39 pm

Waiting for the Post having source code & utility …

July 31st, 2012 at 12:46 pm

Hi! I’d like to get this little programm. Can you please send it?

August 3rd, 2012 at 5:12 pm

can I get it please, also is there a fix for it, as it blocks pc from hibernating when the lid is closed after pressing hibernate.

August 3rd, 2012 at 8:32 pm

I would love to get this for my new laptop, please!

Thanks a bunch

August 8th, 2012 at 4:08 am

hey,i have followed Tiago’s steps but it didn’t work well, so pls send me ur utility s/w, pls
i wil thankful of yours..

August 10th, 2012 at 3:16 am

how about instead of wasting our time begging for an email some on upload it somewhere?

August 13th, 2012 at 5:16 am

Please, send me your app!
Thanks in advance!!!

August 14th, 2012 at 3:20 am

Thought that I was the minority… May I get this program of yours too?

August 16th, 2012 at 10:55 am

Another copy for me, please.

August 19th, 2012 at 10:03 am

it would be great if you could email me this utility. I tried Tiago’s version and it still just puts the computer to sleep. This would be great for computers with fingerprint scanners since I can have it be secure while not having to type a password.

August 21st, 2012 at 8:32 pm

Could you please send me a copy of the file? I would greatly appreciate it. Thank you for creating this.

August 22nd, 2012 at 10:45 am
Sandeep Kumar

hey nice work,plz mail me and can you plz guide me how to work with such kind of things like making a twick on your own.

August 23rd, 2012 at 3:47 am
The Nash

I would love a copy.

August 31st, 2012 at 12:32 am

Thank you AK for making this utility. If you can, please send it my inbox. If you could also send the source code for your program as I m a programming student and can try to make it myself.

Thank you.

August 31st, 2012 at 5:09 am

Hello, I have also been looking for a solution to this. Could you please share your application?
Thanks a lot in advance.

September 1st, 2012 at 3:41 pm

could u please mail me the program
thankx in advance

September 2nd, 2012 at 11:33 am
another ones

Any chance you could send a built copy of it to me as well?

September 10th, 2012 at 10:18 am

I would love this log in app!

September 13th, 2012 at 10:45 pm

Sorry to all who requested but never got an email. Life is busy and I am tired.

Now, you dont need to email me for the tool. You can just download it from the post. I have also added the code for people who want.

I am done witht his post. but feel free to leave comments anyways.

September 17th, 2012 at 2:56 pm

thanx maan

September 19th, 2012 at 12:28 pm

Hello! It`s a very nice program, but is there any way to turn it back? to be normal again? thanks.

September 23rd, 2012 at 11:51 am
awais rana

bro its working awsome u solved my problem thnkxx gr8 wrk yrrrr

September 30th, 2012 at 3:09 am

Thank you for this, AK!

I do have one remark though; When I run the .exe it appears to create an invisible window called “Form1″ which shows up in both the task manager and the alt-tab list. Visual Studio leftover maybe?

October 11th, 2012 at 5:54 pm

forgot to mention: running Windows 7 SP1 x64

October 11th, 2012 at 5:55 pm

I would like a copy of this! This sounds like everything I have been searching for!

October 15th, 2012 at 9:15 pm

please mail it to

October 22nd, 2012 at 2:04 am

After posting my previous comments I decided to get to work myself and as a result, I built an application in VB.NET based on AK’s. Apart from fixing the problem with the invisible window, I also added a tray icon and an option to run at boot.
You can download it at

AK, if me using your code is a problem to you, please send me an email and I will take the link down.

December 1st, 2012 at 6:55 am

No, not a problem at all. I am really glad that you tried something and it worked. Thanks for sharing.

December 1st, 2012 at 2:43 pm

hey could I have you lock utility please thank you

December 6th, 2012 at 7:05 pm

it doesn’t work for me. help please?

December 8th, 2012 at 3:42 pm

Please, send me your app!


December 9th, 2012 at 11:42 am
John T

Cool I downloaded it and it worked perfect for windows 8. Is there a way to disable it so I can do so if needed? Thanks!

December 12th, 2012 at 9:31 pm

Thanks for this! Both AK’s and Num’s work on Win 8 x64.

December 23rd, 2012 at 9:18 pm
Jaswinder Singh

Thank you soo soo much , i was looking for it over a week , atlast go it …..
IT works On WINDOWS 8 also ……

January 5th, 2013 at 9:20 am

Thanks I was searching months for this in the settings.

January 5th, 2013 at 1:01 pm
Omar Yasser

Please email me this program, I was searching for this a while ago :D

March 1st, 2013 at 11:32 am
Mark William DIano

Hey, can I have this program as well? I really helps me I thought this was already built in on laptops. My first time having one and I was kinda disappointed lol

March 4th, 2013 at 11:49 pm
Raphaël Mélotte

Could you please send me a copy of this program?
I’ve been looking for a no-GUI/light solution for a while since Microsoft doesn’t give any solution.

Thanks in advance, Raphaël

March 22nd, 2013 at 7:07 am

Thanks so much…just what I have been looking for!

April 19th, 2013 at 5:11 am

Would love a copy please!

April 25th, 2013 at 4:51 pm

Thanx a lot… so many years and yet not implemented by MS…

May 5th, 2013 at 10:52 am

this a very good software, and i can make my laptop more security. thanks very much.

May 14th, 2013 at 12:56 am

pleaseeee will you email it to little old me i so so need it would be so greatful thankyouuu xxxx

June 6th, 2013 at 12:29 am
Henry Lee

can i know how to set this tool to run on startup??
please emal me way to do it i am new here

June 16th, 2013 at 4:02 am
Mithilesh Domala

Hey, please can you mail me a copy of your software…. I’m too lazy to lock the windows and then close the lid, also it disrupts the continuity…

June 29th, 2013 at 4:02 am

Dude you are my personal hero at the moment. Thanks for this tool and shame on microsoft for not implementing it …

November 16th, 2013 at 12:15 pm
Nozir Ahmod

thanks for this great work!! but it doesn’t work in Windows 8. please, release a tool for W8 32bit &64bit both.

November 22nd, 2013 at 11:02 am
Nozir Ahmod

got it! it works on windows 8 too. but there are some options lack, like start-up option, manage window, settings etc.

November 22nd, 2013 at 11:18 am
Nozir Ahmod

Ok, I got a GUI based tool “LidLock” &has more options but I like your tool specially. cause that’s GUI based but it’s Icon is stand by in the taskbar.

November 22nd, 2013 at 11:36 am

2 Trackbacks/Pings

  1. Lock Windows Laptop By Just Closing Its Lid - The Bloggie Man Wordpress Edition    May 06 2013 / 6am:

    [...] I stumbled upon AK’s Blog, where he made a small program which did the same what I wanted, it worked in the background and [...]

  2. Anonymous    Aug 09 2013 / 4am:

    [...] this is posted here Lock windows when laptop lid is closed… | AK's Blog But i want windows update to add lock windows option to when laptop lid is closed option [...]

Leave a reply

Name (*)
Mail (will not be published) (*)