Jump to content
LaptopVideo2Go Forums

Close laptop lid code


Recommended Posts

Hey guys,

(skip this if you dont care about the backstory :) )

Last wensday i received my inspiron 1720 and well i wanted to install windows xp, So i did and i download nvidia drivers like i always do and it didnt work well after a bit of googling i came here downloaded the moddef inf and it worked perfectly. But later i noticed that my monitor didnt turn off when i closed it after yet again googling i found out that its becuase im using these drivers not made by dell.

Now maby theres a solution to this and i didnt find any so i just created my own and i tought i'd share it with you all.

This code is writen in c# and will cost you your standby function of your laptop.

It works like this, I configured my laptop to go into standby when i close the lid my program will then cancel the standby and turn off the monitor instead any mouse or keyboard movement will turn the monitor back on.

//create an override function which receives messages send to your program
protected override void WndProc(ref Message m)
	{

		base.WndProc(ref m);

		switch (m.Msg)
		{
			//Powerbroadcast messages
			case WM_POWERBROADCAST:
				{
					switch (m.WParam.ToInt32())
					{
						//if msg equals request for standby
						case (int)PBT.PBT_APMQUERYSUSPEND:
							{
								//check if its an hibernate or standby request
								if (m.LParam == (IntPtr)0x1)
								{
									//Set bool to true so our program doesnt cancel hibernate
									hibernate = true;
									//start a 10 second timer which sets hibernate to false again so after the hibernate if you close the lid it will turn of the monitor again
									resethib.Start();
								}
								//check if a hibernate request was made
								if (hibernate == false)
								{
										//create a deny to standby request
										IntPtr deny = new IntPtr(BROADCAST_QUERY_DENY);
										//send the deny command back
										m.Result = deny;
										//Turn off the monitor
										PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
									}
								}
								break;
							}

						default:
							break;
					}
					break;
				}
			default:
				break;
		}
	}

Well thats about it, if you dont understand how this works or have any other questions just send me an email/pm and ill see if i can help you.

Link to comment
Share on other sites

  • 1 month later...
Guest monmon

Hi.. I'd just like to ask what you mean by "if (m.LParam == (IntPtr)0x1)".

Is it to check whether it's a hibernate request or a standby request? Can it be used to differentiate hibernate from standby?

I'm creating a program that logs power management functions so I need to distinguish between the 2(standby and hibernate).

Thanks!! :)

Hey guys,

(skip this if you dont care about the backstory :) )

Last wensday i received my inspiron 1720 and well i wanted to install windows xp, So i did and i download nvidia drivers like i always do and it didnt work well after a bit of googling i came here downloaded the moddef inf and it worked perfectly. But later i noticed that my monitor didnt turn off when i closed it after yet again googling i found out that its becuase im using these drivers not made by dell.

Now maby theres a solution to this and i didnt find any so i just created my own and i tought i'd share it with you all.

This code is writen in c# and will cost you your standby function of your laptop.

It works like this, I configured my laptop to go into standby when i close the lid my program will then cancel the standby and turn off the monitor instead any mouse or keyboard movement will turn the monitor back on.

//create an override function which receives messages send to your program
protected override void WndProc(ref Message m)
[tab][/tab]   {

[tab][/tab][tab][/tab]  base.WndProc(ref m);

[tab][/tab][tab][/tab]  switch (m.Msg)
[tab][/tab][tab][/tab]  {
[tab][/tab][tab][/tab][tab][/tab] //Powerbroadcast messages
[tab][/tab][tab][/tab][tab][/tab] case WM_POWERBROADCAST:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]{
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	switch (m.WParam.ToInt32())
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	{
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   //if msg equals request for standby
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   case (int)PBT.PBT_APMQUERYSUSPEND:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] //check if its an hibernate or standby request
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] if (m.LParam == (IntPtr)0x1)
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]//Set bool to true so our program doesnt cancel hibernate
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]hibernate = true;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]//start a 10 second timer which sets hibernate to false again so after the hibernate if you close the lid it will turn of the monitor again
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]resethib.Start();
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] //check if a hibernate request was made
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] if (hibernate == false)
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] {
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//create a deny to standby request
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	IntPtr deny = new IntPtr(BROADCAST_QUERY_DENY);
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//send the deny command back
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	m.Result = deny;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	//Turn off the monitor
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]	PostMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]}
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] }
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab] break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  }

[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]   default:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab][tab][/tab]  break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	}
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]	break;
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]}
[tab][/tab][tab][/tab][tab][/tab] default:
[tab][/tab][tab][/tab][tab][/tab][tab][/tab]break;
[tab][/tab][tab][/tab]  }
[tab][/tab]   }

Well thats about it, if you dont understand how this works or have any other questions just send me an email/pm and ill see if i can help you.

Link to comment
Share on other sites

The following line in the driver inf is supposed to do the same thing:

HKLM,"System\CurrentControlSet\Control\GraphicsDrivers\LidCloseSetPower"

In fact, nVidia is now adding it to thier infs, years after we started doing it.

However, it may not work for everyone.

Link to comment
Share on other sites

I also noticed this line which may be involved in this issue:

HKR,, LidBehavior, %REG_DWORD%, 0x11

Link to comment
Share on other sites

You're absolutely right - it's supposed to. Unfortunately, Microsoft did something in XP Service Pack 2 (and SP3 and Vista) that stopped that from working. That's when we had to resort to the LidCloseSetPower hack.

As long as we're talking about LCD/lid behavior, there's another catche that snags most people: Even if the LCD shuts off when you close the lid, any activity that affects the touchpad or external mouse will turn the LCD back on again. Since the lid is already shut, most people aren't aware that it's on and running down the battery.

Always use the Windows power settings to shurt off the monitor after some period of inactivity.

Link to comment
Share on other sites

As long as we're talking about LCD/lid behavior, there's another catche that snags most people: Even if the LCD shuts off when you close the lid, any activity that affects the touchpad or external mouse will turn the LCD back on again. Since the lid is already shut, most people aren't aware that it's on and running down the battery.

It doesn't go back on with my system.....

Link to comment
Share on other sites

It probably doesn't happen with all systems. Just something worth checking.

Link to comment
Share on other sites

Also, for some people 0x11 doesn't work. I have to set it to 0x10 to keep the backlight from coming on again right after it goes off.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...