How to create/insert WMI object?

2019-06-08 16:33发布

问题:

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?

回答1:

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.



回答2:

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



标签: c# windows wmi