急!找不到类型或命名空间名称“POWER_POLICY”(是否缺少 using 指令或程序集引用?)

blegend 2014-06-25 04:32:20
[DllImport("powrprof.dll", SetLastError = true, EntryPoint = "WritePwrScheme")]
static extern bool WritePwrSchemeS(ref uint puiID, [MarshalAs(UnmanagedType.LPWStr)] String sName,
[MarshalAs(UnmanagedType.LPWStr)] String sDesc, ref POWER_POLICY PowerPolicy);
想做一个电源管理的程序,引用powrprof.dll,但是提示错误“找不到类型或命名空间名称“POWER_POLICY”(是否缺少 using 指令或程序集引用?) ”,命名空间已经引用了 System.Runtime.InteropServices;请问是什么问题
...全文
166 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
blegend 2014-07-06
  • 打赏
  • 举报
回复
非常非常感谢,之前的问题解决了,但是现在成功运行之后 acTime = PwrPolicy.user.VideoTimeoutAc;得到的返回值一直都是2,这个语句不是应该获取显示器关闭时间的吗,能再请教一下我哪里有问题
save4me 2014-06-28
  • 打赏
  • 举报
回复
参考: PowerSchemeXP.cs DllImport的函数参数修饰符漏掉了ref和out,加上就可以了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PowerApp
{
	internal enum POWER_ACTION
	{
		PowerActionNone = 0,
		PowerActionReserved,
		PowerActionSleep,
		PowerActionHibernate,
		PowerActionShutdown,
		PowerActionShutdownReset,
		PowerActionShutdownoff,
		PowerActionWarmEject
	}

	internal enum SYSTEM_POWER_STATE
	{
		PowerSystemUnspecified=0,
		PowerSystemWorking=1,
		PowerSystemSleep1=2,
		PowerSystemSleep2=3,
		PowerSystemSleep3=4,
		PowerSystemHibernate=5,
		PowerSystemShutdown=6,
		PowerSystemMaximum=7
	}
	
	internal struct POWER_ACTION_POLICY
	{
		POWER_ACTION Action;
		ulong Flags;
		ulong EventCode;
	}
	
	internal struct MACHINE_POWER_POLICY
	{
		ulong Revision;
		SYSTEM_POWER_STATE MinSleepAc;
		SYSTEM_POWER_STATE MinSleepDc;
		SYSTEM_POWER_STATE ReducedLatencySleepAc;
		SYSTEM_POWER_STATE ReducedLatencySleepDc;
		public ulong DozeTimeoutAc;
		public ulong DozeTimeoutDc;
		ulong DozeS4TimeoutAc;
		ulong DozeS4TimeoutDc;
		ulong MinThrottleAc;
		char MinThrottleDc;
		// UCHAR pad1[2];
		POWER_ACTION_POLICY OverThrottledAc;
		POWER_ACTION_POLICY OverThrottledDc;
	}
	
	internal struct USER_POWER_POLICY
	{
		ulong Revision;

		POWER_ACTION_POLICY IdleAc;
		POWER_ACTION_POLICY IdleDc;
		int IdleTimeoutAc;
		int IdleTimeoutDc;
		char IdleSensitivityAc;
		char IdleSensitivityDc;
		char ThrottlePolicyAc;
		char ThrottlePolicyDc;
		SYSTEM_POWER_STATE MaxSleepAc;
		SYSTEM_POWER_STATE MaxSleepDc;
		//ulong Reserved[2];
		public int VideoTimeoutAc;
		public int VideoTimeoutDc;
		public int SpindownTimeoutAc;
		int SpindownTimeoutDc;
		Boolean OptimizeForPowerAc;
		Boolean OptimizeForPowerDc;
		char FanThrottleToleranceAc;
		char FanThrottleToleranceDc;
		char ForcedThrottleAc;
		char ForcedThrottleDc;
	}
	
	internal struct POWER_POLICY
	{
		public USER_POWER_POLICY user;
		public MACHINE_POWER_POLICY mach;
	}

	class Monitor
	{
		[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern bool GetActivePwrScheme(ref uint puiID);

		[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern bool ReadPwrScheme(uint uiID, out POWER_POLICY pPowerPolicy);
		
		[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
		internal static extern bool WritePwrScheme(ref uint puiID, string lpszName, string lpszDescription, ref POWER_POLICY pPowerPolicy);

		public int MonitorPerform()
		{
			int acTime=0;
			uint uiID = 0;
			POWER_POLICY PwrPolicy=new POWER_POLICY();
			
			GetActivePwrScheme(ref uiID);
			
			ReadPwrScheme(uiID, out PwrPolicy);
			acTime = PwrPolicy.user.VideoTimeoutDc;
			
			return acTime;
		}
	}
}
blegend 2014-06-27
  • 打赏
  • 举报
回复
大家帮个忙,急求啊,我现在把结构体引入了,可以正常调用POWER_POLICY 了,不过没有成功获取到电源管理的数据,不知道哪里不对 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { internal enum POWER_ACTION { PowerActionNone = 0, PowerActionReserved, PowerActionSleep, PowerActionHibernate, PowerActionShutdown, PowerActionShutdownReset, PowerActionShutdownoff, PowerActionWarmEject } internal enum SYSTEM_POWER_STATE {PowerSystemUnspecified=0,PowerSystemWorking=1,PowerSystemSleep1=2,PowerSystemSleep2=3,PowerSystemSleep3=4,PowerSystemHibernate=5,PowerSystemShutdown=6,PowerSystemMaximum=7} internal struct POWER_ACTION_POLICY { POWER_ACTION Action; ulong Flags; ulong EventCode; } internal struct MACHINE_POWER_POLICY { ulong Revision; SYSTEM_POWER_STATE MinSleepAc; SYSTEM_POWER_STATE MinSleepDc; SYSTEM_POWER_STATE ReducedLatencySleepAc; SYSTEM_POWER_STATE ReducedLatencySleepDc; ulong DozeTimeoutAc; ulong DozeTimeoutDc; ulong DozeS4TimeoutAc; ulong DozeS4TimeoutDc; ulong MinThrottleAc; char MinThrottleDc; // UCHAR pad1[2]; POWER_ACTION_POLICY OverThrottledAc; POWER_ACTION_POLICY OverThrottledDc; } internal struct USER_POWER_POLICY { ulong Revision; POWER_ACTION_POLICY IdleAc; POWER_ACTION_POLICY IdleDc; int IdleTimeoutAc; int IdleTimeoutDc; char IdleSensitivityAc; char IdleSensitivityDc; char ThrottlePolicyAc; char ThrottlePolicyDc; SYSTEM_POWER_STATE MaxSleepAc; SYSTEM_POWER_STATE MaxSleepDc; //ulong Reserved[2]; public int VideoTimeoutAc; int VideoTimeoutDc; public int SpindownTimeoutAc; int SpindownTimeoutDc; Boolean OptimizeForPowerAc; Boolean OptimizeForPowerDc; char FanThrottleToleranceAc; char FanThrottleToleranceDc; char ForcedThrottleAc; char ForcedThrottleDc; } internal struct POWER_POLICY { public USER_POWER_POLICY user; public MACHINE_POWER_POLICY mach; } class Monitor { [DllImport("powrprof.dll", SetLastError = true, EntryPoint = "WritePwrScheme")] internal static extern bool WritePwrScheme(ref uint puiID, [MarshalAs(UnmanagedType.LPWStr)] String sName,[MarshalAs(UnmanagedType.LPWStr)] String sDesc, ref POWER_POLICY PowerPolicy); [DllImport("powrprof.dll", SetLastError = true, EntryPoint = "WritePwrScheme")] internal static extern bool GetActivePwrScheme(uint puiID); [DllImport("powrprof.dll", SetLastError = true, EntryPoint = "WritePwrScheme")] internal static extern bool ReadPwrScheme(uint uiID, POWER_POLICY PowerPolicy); public int monitorperform() { int actime=0; uint uiID = 0; POWER_POLICY PwrPolicy=new POWER_POLICY(); GetActivePwrScheme(uiID); ReadPwrScheme(uiID,PwrPolicy); actime = PwrPolicy.user.VideoTimeoutAc; return actime; } } }

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧