views:

606

answers:

4

Is there any way to get the status of the Battery (charging/discharging/Current Charge/Capacity/etc...) from a C++ program in a portable manner between Windows & Linux. I'm doing a small program for my own use and require some help with it. Kindly look over the description of the program on my blog:


The JB Journals

Also, is there any way to actually see if a computer is currently connected to the internet. I'm doing this currently by requesting www.google.com and seeing if I get a "503 Error" in return. Any better way of doing this? I'm using libCurl for the webinterface part of my program, but would like to know if there are better alternatives...

+4  A: 
  1. There is no portable way to get battery status between windows and linux. You have to query absolutely different APIs. You'll have to see if someone has a wrapper written for this, but likely you'll need to do it yourself. (Maybe you were asking if there are any wrappers or libraries?)

  2. There are plenty of ways to see if a computer is connected to the internet, but they basically all boil down to pinging a host that is probably up (like google). I use ping instead of http connect. There are various C libraries for doing your own ping (without calling out to a program.)

Christopher
ping doesn't protect against false positives due to dns redirects.
e5
sorry I forgot to mention that. Yes, I am looking for a library of sorts. Currently, I'm using a DBus connection to the GNOME Power Management Applet, and I'm looking for some way to get the same functionality (signals on BatteryStatusChange) on Windows.
Asad Jibran Ahmed
A: 

to test for internet connection, check your ip address. if you have 0.0.0.0 in linux, or 169.254.x.y in windows, then no

ifconfig (linux)
ipconfig (windows)
Finer Recliner
That's not a good check. THat only checks to see if you have an IP address, and if you are connected to some local network. Not the internet at large.
Christopher
Since I'm connecting through a WiMax device, I'm actaully connected to the Wateen network all the time. I have to actually log in to gain access to the internet. So I do have an IP at all times. Any other way?
Asad Jibran Ahmed
+1  A: 

In regards to your second question about programatically determining if you are connected to the internet. Looking for a 503 on a google.com request is not foolproof. Many wifi networks will redirect traffic or otherwise supply incorrect dns names to prevent you from connecting to the internet without their authorization.

For example if you don't pay your comcast bill, comcast rewrites all dns responses to point you to an ip address that tells you to pay your comcast bill. From an http layer perspective no error has occurred, you have successfully completed a http request against google.com. Unfortunately the content of the page is different.

e5
A: 

As you might have deduced from the various answers, the "connected to the internet" question is malformed. There is nothing you can do internal to the computer to tell with certainty what kind of network it is connected to. You can have a valid IP but be on a private network. ICMP pings may be filtered by a firewall but HTTP will not. HTTP to arbitrary hosts might be dynamically routed to a local "pay now" account management application. The list of failure modes goes on and on. If you really need to know whether you can get to a given service on the internet, you need to try to connect to that service.

I can't speak to power management issues in windows, but in linux reading the battery state is as simple as opening up the files under /proc/acpi/battery (there may be several battery directories, each one containing "state" and "info" files with plaintext, human-readable fields.

Andy Ross