OK, have to answer my own question. Here's what I was able to find out:
It is easy to get what power action will be performed when lid is closed. Here's the code (it must be run from a user-mode process though):
void getLidClosedAction()
{
GUID* pGuidActivePwrSchm = NULL;
DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
if(dwR == ERROR_SUCCESS)
{
DWORD val;
val = -1;
dwR = PowerReadACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, &val);
if(dwR == ERROR_SUCCESS)
{
_tprintf(L"Lid closed action: ");
switch(val)
{
case 0:
_tprintf(L"Do nothing\n");
break;
case 1:
_tprintf(L"Sleep\n");
break;
case 2:
_tprintf(L"Hibernate\n");
break;
case 3:
_tprintf(L"Shut-down\n");
break;
default:
_tprintf(L"Unknown value=%d\n", val);
break;
}
}
else
{
_tprintf(L"PowerReadACValueIndex error=%d\n", dwR);
}
if(pGuidActivePwrSchm)
{
LocalFree(pGuidActivePwrSchm);
pGuidActivePwrSchm = NULL;
}
}
else
{
_tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
}
}
And it is also easy to set the power action when the lid is closed (again the code must run in the user-mode process -- otherwise you'll need to get the power scheme GUID using means other than PowerGetActiveScheme
call):
BOOL setLidClosedAction(DWORD dwVal)
{
//'dwVal' = can be one of:
// 0 = do nothing
// 1 = sleep
// 2 = hibernate
// 3 = shut-down
BOOL bRes = FALSE;
GUID* pGuidActivePwrSchm = NULL;
DWORD dwR = PowerGetActiveScheme(NULL, &pGuidActivePwrSchm);
if(dwR == ERROR_SUCCESS)
{
dwR = PowerWriteACValueIndex(NULL, pGuidActivePwrSchm, &GUID_SYSTEM_BUTTON_SUBGROUP, &GUID_LIDCLOSE_ACTION, dwVal);
if(dwR == ERROR_SUCCESS)
{
bRes = TRUE;
}
else
{
_tprintf(L"PowerWriteACValueIndex error=%d\n", dwR);
}
if(pGuidActivePwrSchm)
{
LocalFree(pGuidActivePwrSchm);
pGuidActivePwrSchm = NULL;
}
}
else
{
_tprintf(L"PowerGetActiveScheme error=%d\n", dwR);
}
return bRes;
}
This applies to the power/sleep button action as well. The GUIDs in GUID_SYSTEM_BUTTON_SUBGROUP
are as such:
GUID_POWERBUTTON_ACTION = power button
GUID_SLEEPBUTTON_ACTION = sleep button
GUID_USERINTERFACEBUTTON_ACTION = sometimes another OEM sleep button
Unfortunately I was not able to see how to set up a custom power action for those events.