Josh Sobel
Josh Sobel

Reputation: 1328

Enumerating battery devices c++ windows

I am using windows api to get various hardware info such as cpu usage and battery info. I have been trying to get the names of the names of the battery devices by following this guide http://msdn.microsoft.com/en-us/library/windows/desktop/bb204769%28v=vs.85%29.aspx but i am stuck when i get to here.

#define INITGUID
#include<windows.h>
#include<batclass.h>
#include<setupapi.h>
#include<devguid.h>

using namespace std;

int main()
{
HDEVINFO hdev = SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY, 0, 0, DIGCF_PRESENT 
| DIGCF_DEVICEINTERFACE);
}

but i am getting the following error

[Linker error] C:\Users\Owner\AppData\Local\Temp\ccTMeaf9.o:Untitled1.cpp:(.text+0x28): undefined reference to `__imp_SetupDiGetClassDevsA' collect2: ld returned 1 exit status

i am a beginner in c++ so i might be missing something obvious but this is how they do it in the guide. Any advice would be great.

Upvotes: 1

Views: 1947

Answers (3)

johnfound
johnfound

Reputation: 7061

The following code compiles here, on DevCPP:

#define INITGUID
#include <windows.h>
#include<ddk\batclass.h>
#include<setupapi.h>

int main() {
{
    HDEVINFO hdev = SetupDiGetClassDevs(&GUID_DEVICE_BATTERY, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
}

Note GUID_DEVICE_BATTERY.

In order to get proper linking, you have to add to the project options|Parameters|Linker the library libsetupapi.a

The program is not tested to run. It only compiles properly.

Upvotes: 0

HerrJoebob
HerrJoebob

Reputation: 2313

The documentation lied, that GUID is actually defined in devguid.h. Also (if you're not already doing this in another source file) you need to

#define INITGUID

before including BatClass.h and devguid.h. You'll get an undefined symbol error at link time if you don't have this once in your project, and a multiple-defined symbol error at link time if you have more than one.

Upvotes: 3

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

It's defined in devguid.h in my mingw/GCC headers and my old copy of MSSDK.

Try adding this:

#include <devguid.h>

Upvotes: 0

Related Questions