How to create/insert WMI object?

2019-06-08 15:44发布

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?

标签: c# windows wmi
2条回答
看我几分像从前
2楼-- · 2019-06-08 16:39

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.

查看更多
ら.Afraid
3楼-- · 2019-06-08 16:40

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

查看更多
登录 后发表回答