Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateInParameters, SetPropertyValue Wont work without : using (WmiMethod setMethod = obj.GetMethod("method name")) #45

Closed
haseakash opened this issue Dec 30, 2024 · 8 comments · Fixed by #47

Comments

@haseakash
Copy link

I have following code

using System.Management;

    public static void SetLidCloseAction(int actionValue)
    {

        // Define the namespace for power settings (this is part of standard WMI API)
        string namespacePath = @"root\cimv2\power";

        // GUID for lid close action setting (this GUID is standard across Windows versions)
        string lidCloseGuid = "{5ca83367-6e45-459f-a27b-476b1d01c936}";

        // WMI query to retrieve current power settings related to lid close action
        string query = $"SELECT * FROM Win32_PowerSettingDataIndex WHERE InstanceID LIKE '%{lidCloseGuid}%'";

        // Connect to the WMI namespace and execute the query
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(namespacePath, query);
        ManagementObjectCollection queryCollection = searcher.Get();

        // Update the setting for each matching power setting
        foreach (ManagementObject powerSetting in queryCollection)
        {
            // Set the desired lid close action (0: Do Nothing, 1: Sleep, 2: Shutdown, 3: Hibernate)
            powerSetting["SettingIndexValue"] = actionValue;

            // Apply the setting to the system
            powerSetting.Put();
        }
    }

I converted it to following using WmiLight ,

using WmiLight;

    public static void SetLidCloseAction(int actionValue)
    {
        // GUID for lid close action setting (this GUID is standard across Windows versions)
        string lidCloseGuid = "{5ca83367-6e45-459f-a27b-476b1d01c936}";

        // WMI query to retrieve current power settings related to lid close action
        string query = $"SELECT * FROM Win32_PowerSettingDataIndex WHERE InstanceID LIKE '%{lidCloseGuid}%'";


        using (WmiConnection connection = new WmiConnection(@"\\.\root\cimv2\power"))
        {
            // Query the WmiMonitorBrightnessMethods class to get access to the method
            foreach (var obj in connection.CreateQuery(query))
            {
                using (WmiMethod setMethod = obj.GetMethod("")) // May be it has no method
                {
                    MessageBox.Show("Method Found");

                    using (WmiMethodParameters parameters = setMethod.CreateInParameters())
                    {
                        // Set the required parameter for the method
                        parameters.SetPropertyValue("SettingIndexValue", actionValue);

                        // Execute the method to commit the change
                        uint result = obj.ExecuteMethod<uint>(setMethod, parameters, out _);

                        if (result != 0)
                            throw new Exception($"Failed to set lid close action: {result}");
                    }

                }
            }
        }
    }

And Calling in my main Form As,

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                  SystemPowerSettingsWmi.SetLidCloseAction(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
}

Problem is as i know ,
i just want to apply

        foreach (ManagementObject powerSetting in queryCollection)
        {
            powerSetting["SettingIndexValue"] = actionValue;
            powerSetting.Put();
        }

with WmiLight .

As i know this does not depend on any Method Name. It i think directly set as PropertyValue.
But in WmiLight ,
SetPropertyValue will not works if there is no Method Name
i think it still required using (WmiMethod setMethod = obj.GetMethod("method name"))

May be im wrong. I dident find a solution , i tried all names on google etc . and chnage but still dosent work for me.
Can you fix to use SetPropertyValue without the obj.GetMethod("method name")) if any dosent have any method name?

And please tell me how to convert my code in WMILight ?

@MartinKuschnik
Copy link
Owner

You are assigning values to WMI objects. This feature is not fully implemented yet, as there has been no feature request for it until now.

If you need this feature, I can provide a new version with it implemented within the week.

@haseakash
Copy link
Author

@MartinKuschnik : Thanks man. Your response is very fast. I like that. Also,
WmiLight is very good and portable solution for all.
You need to focus on users can easily convert their any WMI code into WmiLight.

Love from India <3

Hats off to your work man... !

@haseakash
Copy link
Author

@MartinKuschnik : Also for your work,

I have GlobalSign digital signature for DLL and exe. For 3 Year.

Name as my personal name.

I can sign your DLL without any cost. If you need, I can anytime do it for you, man !

I'm waiting for new release.... !!!

@MartinKuschnik
Copy link
Owner

@haseakash Please have a look to Version 6.10.0 that addes the Put method.

The follwoing code should work:

public static void SetLidCloseAction(int actionValue)
{
    // GUID for lid close action setting (this GUID is standard across Windows versions)
    string lidCloseGuid = "{5ca83367-6e45-459f-a27b-476b1d01c936}";

    // WMI query to retrieve current power settings related to lid close action
    string query = $"SELECT * FROM Win32_PowerSettingDataIndex WHERE InstanceID LIKE '%{lidCloseGuid}%'";

    using (WmiConnection connection = new WmiConnection(@"\\.\root\cimv2\power"))
    {
        // Query the WmiMonitorBrightnessMethods class to get access to the method
        foreach (WmiObject obj in connection.CreateQuery(query))
        {
            obj.SetPropertyValue("SettingIndexValue", actionValue);

            obj.Put();
        }
    }
}

@haseakash
Copy link
Author

haseakash commented Jan 2, 2025 via email

@haseakash
Copy link
Author

haseakash commented Jan 3, 2025 via email

@MartinKuschnik
Copy link
Owner

The following code workes for me:

public static void SetScreenBrightness(uint brightnessLevel)
{
    // Ensure brightness level is within valid range
    brightnessLevel = Math.Max(0, Math.Min(100, brightnessLevel));

    using (WmiConnection connection = new WmiConnection(@"\\.\root\wmi"))
    {
        // Query the WmiMonitorBrightnessMethods class to get access to the method
        foreach (var obj in connection.CreateQuery("SELECT * FROM WmiMonitorBrightnessMethods"))
        {
            using (WmiMethod setBrightnessMethod = obj.GetMethod("WmiSetBrightness"))
            using (WmiMethodParameters parameters = setBrightnessMethod.CreateInParameters())
            {
                parameters.SetPropertyValue("Timeout", "0"); // Timeout in seconds (0 = immediate)
                parameters.SetPropertyValue("Brightness", brightnessLevel.ToString());

                int? result = obj.ExecuteMethod<int?>(setBrightnessMethod, parameters, out _);

                if (result != null && result != 0)
                    throw new Exception($"Failed to set brightness. Error code: {result}");
            }
        }
    }
}

@haseakash
Copy link
Author

haseakash commented Jan 3, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment