📄 req.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
Module Name:
req.c
Abstract:
Power Manager (PM) util to set a device power requirement.
Notes:
Use in conjunction with the other PM utils.
Revision History:
--*/
#include <windows.h>
#include <msgqueue.h>
#include <pm.h>
extern int CreateArgvArgc(TCHAR *pProgName, TCHAR *argv[20], TCHAR *pCmdLine);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, int nCmShow)
{
WCHAR device[1024] = {0};
LPWSTR pDevice = &device[0];
DWORD dwDeviceState = 0;
DWORD dwDeviceFlags = 0;
WCHAR state[1024] = {0};
LPWSTR pState = &state[0];
DWORD dwStateFlags = 0;
HANDLE h = NULL;
int release = 0;
WCHAR szEventName[MAX_PATH];
TCHAR *argv[20];
int argc;
DWORD dwErr=0;
BOOL fGotState = FALSE;
UNREFERENCED_PARAMETER(hInst);
UNREFERENCED_PARAMETER(hPrevInst);
UNREFERENCED_PARAMETER(nCmShow);
//
// parse command line
//
argc = CreateArgvArgc(TEXT( "PMREQ" ), argv, lpCmdLine);
switch (argc) {
case 2:
if (!swscanf(argv[1], TEXT("%x"), &h))
dwErr = 1;
release = 1;
break;
case 4:
if (!swscanf(argv[1], TEXT("%s"), &device))
dwErr = 1;
if (!swscanf(argv[2], TEXT("%x"), &dwDeviceState))
dwErr = 1;
if (!swscanf(argv[3], TEXT("%x"), &dwDeviceFlags))
dwErr = 1;
break;
case 6:
if (!swscanf(argv[1], TEXT("%s"), &device))
dwErr = 1;
if (!swscanf(argv[2], TEXT("%x"), &dwDeviceState))
dwErr = 1;
if (!swscanf(argv[3], TEXT("%x"), &dwDeviceFlags))
dwErr = 1;
if (!swscanf(argv[4], TEXT("%s"), &state))
dwErr = 1;;
if (!swscanf(argv[5], TEXT("%x"), &dwStateFlags))
dwErr = 1;
fGotState = TRUE;
break;
default:
dwErr = 1;
break;
}
if (dwErr) {
RETAILMSG(1, (TEXT("PMREQ <Device> <DeviceState> <DeviceFlags> [[<SystemState>] <SystemFlags>]\n")));
RETAILMSG(1, (TEXT(" Places a specific power requirement on a device.\n")));
RETAILMSG(1, (TEXT(" <Device> names the deive, e.g. PDX1:\n")));
RETAILMSG(1, (TEXT(" <DeviceState> specifies the device state (Dx), e.g. 4\n")));
RETAILMSG(1, (TEXT(" <DeviceFlags> specifies the device flags in hex, e.g. POWER_NAME = 0x1\n")));
RETAILMSG(1, (TEXT(" <SystemState> (OPTIONAL) names the system power state, e.g. LowPowerAC\n")));
RETAILMSG(1, (TEXT(" <SystemFlags> (OPTIONAL) specifies the system power state flags, e.g. POWER_FORCE = 0x1000\n")));
RETAILMSG(1, (TEXT("Device names may be qualified with the device's class GUID. This is\n")));
RETAILMSG(1, (TEXT("required for devices that are not members of the default power-manageable\n")));
RETAILMSG(1, (TEXT("device class\n")));
RETAILMSG(1, (TEXT("PMREQ <handle>\n")));
RETAILMSG(1, (TEXT(" Releases a previously established requirement\n")));
RETAILMSG(1, (TEXT(" where <handle> is the return HANDLE from a previous invocation of PMREQ\n\n")));
RETAILMSG(1, (TEXT(" Example: PMREQ PDX1: 1 1 (requires that PDX1: be at D1 or higher in all system power states)\n")));
RETAILMSG(1, (TEXT(" PMREQ PDX1: 1 1 SystemIdle 0 (requires that PDX1: be at D1 or higher in the SystemIdle system power state\n")));
RETAILMSG(1, (TEXT(" PMREQ PDX1: 1 1001 (requires that PDX1: be at D1 or higher in all system power states,\n")));
RETAILMSG(1, (TEXT(" even when the system is suspended; not all drivers actually stay powered\n")));
RETAILMSG(1, (TEXT(" during suspend)\n")));
RETAILMSG(1, (TEXT(" PMREQ {A32942B7-920C-486b-B0E6-92A702A99B35}\\PDX1: 1 1 (requires that PDX1: be at D1 or higher in all system power states)\n")));
RETAILMSG(1, (TEXT(" PMREQ {98C5250D-C29A-4985-AE5F-AFE5367E5006}\\NE20001 0 1 (requires that NDIS miniport NE20001 be at D0 in all system power states)\n")));
exit(1);
}
if (!release) {
// set the requirement
if(!fGotState) {
h = SetPowerRequirement(device, dwDeviceState, dwDeviceFlags, NULL, 0);
} else {
h = SetPowerRequirement(device, dwDeviceState, dwDeviceFlags, state, dwStateFlags);
}
if (h) {
HANDLE hevWakeEvent;
DWORD dwProcess = GetCurrentProcessId();
// create an event so we can be signaled to wake up and release the requirement
wsprintf(szEventName, _T("PMREQ_Wake_Event_%x"), dwProcess);
hevWakeEvent = CreateEvent(NULL, FALSE, FALSE, szEventName);
if(hevWakeEvent != NULL) {
RETAILMSG(1, (TEXT("Use 'PMREQ %x' to release the requirement\n"), dwProcess));
WaitForSingleObject(hevWakeEvent, INFINITE);
CloseHandle(hevWakeEvent);
} else {
RETAILMSG(1, (TEXT("PMREQ!CreateEvent('%s') failed %d\n"), szEventName));
}
// release the requirement
dwErr = ReleasePowerRequirement(h);
if(dwErr != ERROR_SUCCESS) {
RETAILMSG(1, (TEXT("PMREQ!ReleasePowerRequirement(0x%08x) failed %d\n"), h, dwErr));
} else {
RETAILMSG(1, (TEXT("PMREQ!ReleasePowerRequirement(0x%08x) succeeded\n"), h));
}
} else {
dwErr = GetLastError();
RETAILMSG(1, (TEXT("PMREQ!SetPowerRequirement(%s, %x, 0x%x, %s, 0x%x) ERROR:%d\n"),
device, dwDeviceState, dwDeviceFlags, state, dwStateFlags, dwErr));
}
} else {
HANDLE hevWakeEvent;
// signal an event to wake up the process holding a requirement
wsprintf(szEventName, _T("PMREQ_Wake_Event_%x"), h);
hevWakeEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, szEventName);
if(hevWakeEvent == NULL) {
RETAILMSG(1, (TEXT("PMREQ!OpenEvent('%s') failed %d\r\n"), szEventName, GetLastError()));
} else {
SetEvent(hevWakeEvent);
CloseHandle(hevWakeEvent);
}
}
return 0;
}
// EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -