#4 s3n0
este doplnim mnou mierne poupraveny spominany C++ kod demonstrujuci zapnutie a vypnutie monitoru ako aj nacitania "power saving" aktualneho rezimu monitoru... jedna sa tu o upraveny koniec kodu resp. jedna sa o hlavny vykonavaci program - main{} , kde pridavam Sleep-prikazy, nakolko nejaky cas trva, kym sa vypne monitor a zaroven "nabehne" idle rezim stav... takze su tam tie 3 sekundy vyckavacej doby, aby sa aktivoval IDLE rezim monitoru, po jeho samotnom vypnuti a aby ukazala hodnota cislo 4 platna uz pre uspany monitor
// monitor_info.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <setupapi.h>
#ifdef _MSC_VER
#pragma comment(lib, "Setupapi.lib")
#endif
#include <list>
#include <vector>
#include <string>
using namespace std;
// my StlPort configured w/o iostreams at the moment
#include <stdio.h>
//------------------------------------------------------------------------------
struct DevData
{
std::wstring Description; // SPDRP_DEVICEDESC
CM_POWER_DATA PowerData; // SPDRP_DEVICE_POWER_DATA
DevData()
{
PowerData.PD_Size = sizeof(PowerData);
}//constructor
};//DevData
//------------------------------------------------------------------------------
bool GetDeviceRegString(HDEVINFO infoSet, SP_DEVINFO_DATA *pinfoData,
DWORD prop, wstring &val)
{
DWORD req_sz = 0;
BOOL res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, 0, 0, &req_sz);
if (!res && (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
if (GetLastError() != ERROR_INVALID_DATA)
{
printf("SetupDiGetDeviceRegistryPropertyW() failed, le = %u",
GetLastError());
}//if
return false;
}//if
vector<wchar_t> vec_buff; // in case we have to go to the heap
wchar_t auto_buff[512];
DWORD buff_sz;
wchar_t *buff;
if (req_sz > sizeof(auto_buff))
{
vec_buff.reserve(req_sz/2 + 2);
buff = &vec_buff[0];
buff_sz = req_sz;
}//if
else
{
buff = auto_buff;
buff_sz = sizeof(auto_buff);
}//else
res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, (PBYTE)buff, buff_sz, 0);
if (!res)
{
DWORD le = GetLastError();
if (le != ERROR_INVALID_DATA)
{
printf("SetupDiGetDeviceRegistryPropertyW(%d) "
"failed, le = %u",
prop, le);
return false;
}//if
// return empty string on ERROR_INVALID_DATA
val.erase(val.begin(), val.end());
return true;
}//else
val.assign(buff, req_sz);
return true;
}//GetDeviceRegString
//------------------------------------------------------------------------------
template <typename T>
bool GetDeviceRegData(HDEVINFO infoSet, SP_DEVINFO_DATA *pinfoData,
DWORD prop, T &val)
{
SetLastError(0);
BOOL res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, (PBYTE)&val,
sizeof(val), 0);
DWORD le = GetLastError();
if (!res || (le == ERROR_INVALID_DATA))
{
if (le != ERROR_INVALID_DATA)
printf("GetDeviceRegData() failed, le = %u", le);
return false;
}//if
return true;
}//GetDeviceRegData
//------------------------------------------------------------------------------
void ListDeviceClassData(const GUID *classGuid, std::list<DevData> &devList)
{
devList.clear();
const DWORD flags = DIGCF_PRESENT;
HDEVINFO infoSet = SetupDiGetClassDevsW(classGuid, 0, 0, flags);
if (infoSet == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs() failed, le = %u",
GetLastError());
return;
}//if
SP_DEVINFO_DATA infoData;
infoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD n;
for (n = 0; SetupDiEnumDeviceInfo(infoSet, n, &infoData); ++n)
{
DevData dd;
if (GetDeviceRegString(infoSet, &infoData, SPDRP_DEVICEDESC,
dd.Description) &&
GetDeviceRegData(infoSet, &infoData, SPDRP_DEVICE_POWER_DATA,
dd.PowerData))
{
devList.push_back(dd);
}//if
}//for
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
printf("Last call to SetupDiEnumDeviceInfo(%d) failed, le = %u",
n, GetLastError());
}//if
SetupDiDestroyDeviceInfoList(infoSet);
}//ListDeviceClassData
//------------------------------------------------------------------------------
void print_monitor_info()
{
const GUID MonitorClassGuid =
{0x4d36e96e, 0xe325, 0x11ce,
{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};
list<DevData> monitors;
ListDeviceClassData(&MonitorClassGuid, monitors);
printf("# Monitors = %d\n", monitors.size());
list<DevData>::iterator it = monitors.begin(),
it_end = monitors.end();
for (; it != it_end; ++it)
{
const char *off_msg = "";
if (it->PowerData.PD_MostRecentPowerState > PowerDeviceD0)
off_msg = ", *** Sleeping State ***";
printf("[%ls]\n"
" PowerState = %d%s\n",
it->Description.c_str(),
it->PowerData.PD_MostRecentPowerState,
off_msg);
}//for
putchar('\n');
}//print_monitor_info
//------------------------------------------------------------------------------
int main()
{
printf("\n\n\n\n*** Current Status of Monitor(s) ***\n\n");
print_monitor_info();
Sleep(3000);
printf("\n\n\n\n*** Turning OFF Monitor(s) ***\n\n");
fflush(stdout);
Sleep(500); // don't use mouse or keyboard
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
Sleep(3000);
print_monitor_info();
Sleep(3000);
printf("\n\n\n\n*** Turning ON Monitor(s) ***\n\n");
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1);
Sleep(3000);
print_monitor_info();
Sleep(3000);
return 0;
}//main
//------------------------------------------------------------------------------
co sa tyka pouzitia v CMD scriptoch, tak samozrejme som si musel upravit main{} proceduru... a vyuzivam tam pridanu globalnu premennu int stav_napajania... fakt primitivizmus, ale nemam cas fakt uz na to hehe... nechcelo sa mi trapit sa s tym dalsie hodiny :D tak som to vyriesil naozaj velmi razne a jednoducho :D vid kod:
// monitor_info.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <setupapi.h>
#ifdef _MSC_VER
#pragma comment(lib, "Setupapi.lib")
#endif
#include <list>
#include <vector>
#include <string>
using namespace std;
// my StlPort configured w/o iostreams at the moment
#include <stdio.h>
int stav_napajania = 0;
//------------------------------------------------------------------------------
struct DevData
{
std::wstring Description; // SPDRP_DEVICEDESC
CM_POWER_DATA PowerData; // SPDRP_DEVICE_POWER_DATA
DevData()
{
PowerData.PD_Size = sizeof(PowerData);
}//constructor
};//DevData
//------------------------------------------------------------------------------
bool GetDeviceRegString(HDEVINFO infoSet, SP_DEVINFO_DATA *pinfoData,
DWORD prop, wstring &val)
{
DWORD req_sz = 0;
BOOL res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, 0, 0, &req_sz);
if (!res && (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
if (GetLastError() != ERROR_INVALID_DATA)
{
printf("SetupDiGetDeviceRegistryPropertyW() failed, le = %u",
GetLastError());
}//if
return false;
}//if
vector<wchar_t> vec_buff; // in case we have to go to the heap
wchar_t auto_buff[512];
DWORD buff_sz;
wchar_t *buff;
if (req_sz > sizeof(auto_buff))
{
vec_buff.reserve(req_sz/2 + 2);
buff = &vec_buff[0];
buff_sz = req_sz;
}//if
else
{
buff = auto_buff;
buff_sz = sizeof(auto_buff);
}//else
res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, (PBYTE)buff, buff_sz, 0);
if (!res)
{
DWORD le = GetLastError();
if (le != ERROR_INVALID_DATA)
{
printf("SetupDiGetDeviceRegistryPropertyW(%d) "
"failed, le = %u",
prop, le);
return false;
}//if
// return empty string on ERROR_INVALID_DATA
val.erase(val.begin(), val.end());
return true;
}//else
val.assign(buff, req_sz);
return true;
}//GetDeviceRegString
//------------------------------------------------------------------------------
template <typename T>
bool GetDeviceRegData(HDEVINFO infoSet, SP_DEVINFO_DATA *pinfoData,
DWORD prop, T &val)
{
SetLastError(0);
BOOL res = SetupDiGetDeviceRegistryPropertyW(infoSet, pinfoData, prop,
0, (PBYTE)&val,
sizeof(val), 0);
DWORD le = GetLastError();
if (!res || (le == ERROR_INVALID_DATA))
{
if (le != ERROR_INVALID_DATA)
printf("GetDeviceRegData() failed, le = %u", le);
return false;
}//if
return true;
}//GetDeviceRegData
//------------------------------------------------------------------------------
void ListDeviceClassData(const GUID *classGuid, std::list<DevData> &devList)
{
devList.clear();
const DWORD flags = DIGCF_PRESENT;
HDEVINFO infoSet = SetupDiGetClassDevsW(classGuid, 0, 0, flags);
if (infoSet == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs() failed, le = %u",
GetLastError());
return;
}//if
SP_DEVINFO_DATA infoData;
infoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD n;
for (n = 0; SetupDiEnumDeviceInfo(infoSet, n, &infoData); ++n)
{
DevData dd;
if (GetDeviceRegString(infoSet, &infoData, SPDRP_DEVICEDESC,
dd.Description) &&
GetDeviceRegData(infoSet, &infoData, SPDRP_DEVICE_POWER_DATA,
dd.PowerData))
{
devList.push_back(dd);
}//if
}//for
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
printf("Last call to SetupDiEnumDeviceInfo(%d) failed, le = %u",
n, GetLastError());
}//if
SetupDiDestroyDeviceInfoList(infoSet);
}//ListDeviceClassData
//------------------------------------------------------------------------------
void print_monitor_info()
{
const GUID MonitorClassGuid =
{0x4d36e96e, 0xe325, 0x11ce,
{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};
list<DevData> monitors;
ListDeviceClassData(&MonitorClassGuid, monitors);
stav_napajania = 0;
printf("# Monitors = %d\n", monitors.size());
list<DevData>::iterator it = monitors.begin(),
it_end = monitors.end();
for (; it != it_end; ++it)
{
const char *off_msg = "";
if (it->PowerData.PD_MostRecentPowerState > PowerDeviceD0)
off_msg = ", *** Sleeping State ***";
printf("[%ls]\n"
" PowerState = %d%s\n",
it->Description.c_str(),
it->PowerData.PD_MostRecentPowerState,
off_msg);
stav_napajania = it->PowerData.PD_MostRecentPowerState;
}//for
putchar('\n');
}//print_monitor_info
//------------------------------------------------------------------------------
int main()
{
printf("\n\n*** Current Status of Monitor(s) ***\n\n");
print_monitor_info();
printf("\nEXIT CODE = %d \n", stav_napajania);
// Sleep(3000);
// printf("\n\n\n\n*** Turning OFF Monitor(s) ***\n\n");
// fflush(stdout);
// Sleep(500); // don't use mouse or keyboard
// SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
// Sleep(3000);
// print_monitor_info();
// Sleep(3000);
// printf("\n\n\n\n*** Turning ON Monitor(s) ***\n\n");
// SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1);
// Sleep(3000);
// print_monitor_info();
// Sleep(3000);
return stav_napajania;
}//main
//------------------------------------------------------------------------------