Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:21:27+00:00 2026-06-05T16:21:27+00:00

I am trying to detect when the laptop lid opens and closes, should be

  • 0

I am trying to detect when the laptop lid opens and closes, should be real simple. I can register that event properly it seems, but then I don’t get notification when I close my laptop window.

Here’s the DLL Import

(DLL code: http://www.pinvoke.net/default.aspx/user32/registerpowersettingnotification.html )
(GUID_LIDCLOSE_ACTION: http://social.msdn.microsoft.com/Forums/en-US/tabletandtouch/thread/0bbf90be-9322-47fb-bfa4-016b57211b3a )

[DllImport(@"User32", SetLastError = true, 
  EntryPoint = "RegisterPowerSettingNotification",
  CallingConvention = CallingConvention.StdCall)]

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

static Guid GUID_LIDCLOSE_ACTION =
    new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 
             0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);

private const int WM_POWERBROADCAST = 0x0218;
private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
const int PBT_POWERSETTINGCHANGE = 0x8013; // DPPE

[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct POWERBROADCAST_SETTING
{
    public Guid PowerSetting;
    public uint DataLength;
    public byte Data;
}

And then here is how I am registering the GUID_LIDCLOSE_ACTION event:

private void registerLidClosedNotification()
{
    IntPtr hWnd = this.Handle;
    IntPtr ret = RegisterPowerSettingNotification(hWnd, 
                           ref GUID_LIDCLOSE_ACTION,
                           DEVICE_NOTIFY_WINDOW_HANDLE);

    Debug.WriteLine("Registered: " + ret.ToString());
    Debug.WriteLIne("LastError:" + Marshal.GetLastWin32Error().ToString());
}

Here’s the output from that:

Registered: 6867560

LastError:0

Looks good to me.

Then where I’m supposed to recieve the message:

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {

            Debug.WriteLine("Entered: WndProc");  // we never make it even this far!

So why isn’t it making it to the WndProc function if it is registered :[

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:21:30+00:00Added an answer on June 5, 2026 at 4:21 pm

    I think what you’ve done with the RegisterPowerSettingNotification is cause Windows to include messages for WM_POWERBROADCAST to your WndProc, but you still need to actually hook the WndProc as well.

    It looks like you’re using WinForms (because your sample has “this.Handle”), in which case you can just override the protected WndProc method on your Form.

    If you’re using WPF, then you can do this by getting an HwndSource for the root Window and then calling AddHook.

    You’ll want to read up on the documentation about the WndProc in WinForms (http://msdn.microsoft.com/en-us/library/system.windows.forms.form.wndproc.aspx). It will get called a lot, so you’ll want to only run your code when it’s the message you care about, and unconditionally call base.WndProc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to detect and remove those fields that after a sync are still
I'm trying to detect change event for a select list as if from a
I am trying to detect which web in sharepoint that the user is looking
I'm trying to detect when the onresize event ends in a browser. If I
In trying to detect a right mouse click with jquery, I noticed that the
i am trying to detect file creation in a folder but WatchService can't detect
I am trying to detect .net framework 2.0 service pack 2. But it seems
I am trying to detect if a certain webkit event ' webkitAnimationEnd ' is
I am trying to detect a scroll event on Windows Mobile IE 6 However,
Trying to detect left click vs right click (without using jQuery!) and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.