How to create/insert WMI object?

倖福魔咒の 提交于 2019-12-23 05:14:18

问题


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



来源:https://stackoverflow.com/questions/4442672/how-to-create-insert-wmi-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!