如何创建/插入WMI对象?

发布于 2024-10-08 02:11:35 字数 137 浏览 1 评论 0原文

我正在开发一个用 C# 管理 Windows 电源计划的软件,通过 ManagementObjet 可以轻松获取电源计划并设置其设置。但我想创建一个新的电源计划,换句话说,创建一个新的 WMI 对象,但我不知道该怎么做。

有谁知道如何创建它吗?

I'm doing a software that manages the Windows power plans in C#, and to get the Power Plans and set it's settings is easy by the ManagementObjet. But I want to create a new Power Plan, in other words, create a new WMI object, and I don't know how to do that.

Do any one knows how to create it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2024-10-15 02:11:35

您无法在 WMI 中执行此操作。您可以按照此处所述使用 Win32 API 进行电源方案管理来创建您的计划,然后使用 WMI 监视/管理它。

要创建电源方案,您需要
首先复制现有方案
使用 PowerDuplicateScheme
函数,指定 GUID
您希望将您的新计划作为基础的计划
计划。您应该复制其中之一
内置方案并修改
根据您的需要进行电源设置。

You can't do this in WMI. You can use the Win32 APIs for Power Scheme Management as described here to create your plan, and then monitor/manage it using WMI.

To create a power scheme, you need to
first duplicate an existing scheme by
using the PowerDuplicateScheme
function, specifying the GUID of the
scheme you wish to base your new
scheme upon. You should copy one of
the built-in schemes and modify the
power settings to your needs.

秋叶绚丽 2024-10-15 02:11:35

现在它正在工作......请按照以下方式操作:

using System.Runtime.InteropServices;


[DllImport("powrprof.dll", EntryPoint = "PowerDuplicateScheme", SetLastError = true)]
        public static extern UInt32 PowerDuplicateScheme(IntPtr RootPowerKey, ref Guid SrcSchemeGuid, ref IntPtr DstSchemeGuid);


public static Guid createNewPowerPlan()
{
    Guid result = new Guid();
    IntPtr RetrPointer = IntPtr.Zero;

    // Attempt to duplicate the 'Balanced' Power Scheme.
    NativeMethods.PowerDuplicateScheme(IntPtr.Zero, ref VISA_PM_BASIC_SCHEMES.BALANCED, ref RetrPointer);

    if (RetrPointer != IntPtr.Zero)
    {
        // Function returns a pointer-to-memory, marshal back to our Guid variable.
        result = (Guid)Marshal.PtrToStructure(RetrPointer, typeof(Guid));
    }

    return result;
}

感谢您的帮助

Now it's working... follow bellow how I done it:

using System.Runtime.InteropServices;


[DllImport("powrprof.dll", EntryPoint = "PowerDuplicateScheme", SetLastError = true)]
        public static extern UInt32 PowerDuplicateScheme(IntPtr RootPowerKey, ref Guid SrcSchemeGuid, ref IntPtr DstSchemeGuid);


public static Guid createNewPowerPlan()
{
    Guid result = new Guid();
    IntPtr RetrPointer = IntPtr.Zero;

    // Attempt to duplicate the 'Balanced' Power Scheme.
    NativeMethods.PowerDuplicateScheme(IntPtr.Zero, ref VISA_PM_BASIC_SCHEMES.BALANCED, ref RetrPointer);

    if (RetrPointer != IntPtr.Zero)
    {
        // Function returns a pointer-to-memory, marshal back to our Guid variable.
        result = (Guid)Marshal.PtrToStructure(RetrPointer, typeof(Guid));
    }

    return result;
}

Thanks for your help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文