PowerRegisterSuspendResumeNotification - provided callback function doesn't work as expected

Proposed PowerRegisterSuspendResumeNotification - provided callback function doesn't work as expected

  • Monday, July 09, 2012 12:50 PM
     
      Has Code

    I register my application to receive notification when the system is suspended or resumed. MSDN documentation

    Function I'd like to be executed after application receives notification (I tried both void and void CALLBACK and both work same way):

    void isConnectedStandby()
    {
        printf( "ConnectedStandby Request");
    }
    

    1st case - I provide pointer to the isConnectedStandby function, but system treats as a double pointer to the function - it calls an address which is under this callback pointer.

    HPOWERNOTIFY RegistrationHandle;
    
    PowerRegisterSuspendResumeNotification(
          DEVICE_NOTIFY_CALLBACK,
          &isConnectedStandby,
          &RegistrationHandle
    );
    

    2nd case - here I provide as follows (this way my function code is executed):

    typedef void (*StatusFunction_t)(); 
    StatusFunction_t StatusFunction = isConnectedStandby;
    HPOWERNOTIFY RegistrationHandle;
    
        PowerRegisterSuspendResumeNotification(
          DEVICE_NOTIFY_CALLBACK,
          &isConnectedStandby,
          &RegistrationHandle
    );
    

    System calls not only mine function, but all addresses after the first one (if I provide an array of functions, it executes one after another to crash when there is no valid code available)

    What is the correct way to use this function?

All Replies

  • Monday, July 09, 2012 1:50 PM
     
     

    Wow, I don't know if I have seen worse docs than are here.  It says it takes a callback function, but lists the type of the parameter as a HANDLE, and doesn't give any sort of documentation on what the signature of the function should be.

    Since you say that the second version works, have you tried creating an array of function pointers and null terminating it?  (Note: that I would never guess that usage at all from the docs, but it is the only guess I have).

    If that doesn't work, I am honestly not sure where you put in a request to fix the documentation.  Perhaps as community content on that page.

  • Monday, July 09, 2012 2:13 PM
     
     
    When it hits NULL app crashes.
  • Tuesday, July 10, 2012 7:06 AM
    Moderator
     
     

    Hello,

     

    Thanks for your feedback, I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

  • Thursday, July 12, 2012 10:18 AM
     
     
    Any good news?
  • Thursday, July 12, 2012 10:58 AM
     
     

    The callback functions probably are stdcall, like other Windows callbacks. By default VC++ uses cdecl.

    -- pa

  • Thursday, July 12, 2012 11:27 AM
     
     
    Due to bug in powrprof.h (http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/f2475251-97f4-45fb-8362-f64301f27e13) you have to use stdcall as your default calling convention anyway. (VC++, project properties->C++ -> Advanced -> calling convention -> stdcall).
  • Monday, July 30, 2012 6:45 AM
     
     
    @Jacky Wu: bug in powerprof.h has nothing to do with PowerRegisterSuspendResumeNotification function which doesn't work as expected. After setting calling convention you are able to compile your code, but PowerRegisterSuspendResumeNotification function is still not fully functional.
  • Monday, July 30, 2012 3:43 PM
     
     

    So, can you make a *small* example app that reproes the problem so they can look at it?

    -- pa

  • Thursday, August 02, 2012 10:15 AM
     
     Proposed Has Code

    We have solution finally.

    Function declaration (must be static ULONG with 3 parameters as you can see below):

    static ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting);
    
    ULONG isConnectedStandby(PVOID Context, ULONG Type, PVOID Setting)
    {
        printf( "ConnectedStandby Request");
    	return 0;
    }

    Istead of providing callback function directly to PowerRegisterSuspendResumeNotification we have to provide struct  _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS filled with our functions address :

    static _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS testCallback = {
    	isConnectedStandby,
    	nullptr
    	};
    HPOWERNOTIFY RegistrationHandle;
    
    PowerRegisterSuspendResumeNotification(
      DEVICE_NOTIFY_CALLBACK,
      &testCallback,
      &RegistrationHandle
    );

    MSDN documentation did not mention any of those information.


  • Wednesday, August 22, 2012 12:02 AM
     
     

    Do you have sample C# code (or a link) to receive the callback for PowerRegisterSuspendResumeNotification in c#?
  • Monday, October 22, 2012 8:41 AM
     
     

    thanks for your solution.

    As what I understanding, connected-standby mode is different with suspend mode.

    When the device enter connected-standby mode, the callback function not work on my side.

    But I can get the notification from callback when device enter suspend or hibernation.

    • Proposed As Answer by pianovv Monday, October 22, 2012 8:42 AM
    • Unproposed As Answer by pianovv Monday, October 22, 2012 8:42 AM
    •  
  • Monday, November 05, 2012 11:38 AM
     
     
    I am trying to understand connected standby function. I need to create event handler for system resume. any idea what apis to be used?