Jordan Paris
Jordan Paris

Reputation: 53

How to use the Windows Power Management API in VB.net

I have a program i am creating which integrates a feature that will tell you your current power scheme and has the option to set a pre-configured .pow file. This is very easy to do if i am using the powercfg command from command prompt and i already have that integrated into the program using

Process.start"cmd", "/k powercfg... etc. etc." 

I would much rather be able to use the windows API to call this setting so that i do not have to run a command window every time i want to update this setting and so that i might be able to keep the output of the current scheme dynamic.

From my understanding VB.net does not have the direct ability to use Windows APIs in this way. Doing some research i found that C++ can. I am wondering if there is a way to create a DLL file i can call to with my VB.net script using C++. Or if there is an easier solution to this i would love to know.

Upvotes: 0

Views: 1369

Answers (2)

Jordan Paris
Jordan Paris

Reputation: 53

I got it all figured out thanks to Joshua. To get the current active scheme...

 Try
        Dim searcher As New ManagementObjectSearcher(
                "root\CIMV2\power",
                "SELECT * FROM Win32_PowerPlan WHERE IsActive = 'True'")
        For Each queryObj As ManagementObject In searcher.Get()
            TextBox1.Text = queryObj("ElementName")
        Next

Upvotes: 1

Joshua Dannemann
Joshua Dannemann

Reputation: 2080

There are a few options at your disposal. I think using WMI in your VB.NET project is probably best. You will need to add references to the System.Management and System.Management.Instrumentation namespaces. Please refer to this page on MSDN for details on working with the code:

https://msdn.microsoft.com/en-us/library/ms257340(v=vs.80).aspx

Although the following is a C# example, it should provide you with a good idea about how to write queries against WMI in .NET, and it is also somewhat specific to the goals that you have in mind.

Is there any API function to get the battery level in Windows Desktop Apps?

Hope this helps.

Upvotes: 1

Related Questions