Is there any C++ API available to know the OS description? - c++

I am working on tools that will be used on the multiple OSes and platforms. These tools will provide the details of the OS on which it is running, like 32-bit or 64-bit OS, exact version of Linux, Solaris, or other OS.
One way I am thinking is of using the "uname -a" command to extract the OS information on a Linux/Unix-based OS. Please suggest me that if it is efficient to call the command from the program.
Is there any API available or is there any workaround that I can implement?
Note that I also do need to check for OS information on the Windows.

Why don't you simply wrap the OS detection with #define and each time call the ideal function so as to return a meaningful string in every case?
Some examples of system function to get information about the current OS (such as the running version):
In Windows the function is:
GetVersionEx
FreeBSD (and unixes I guess): uname

The C++ Standard Library provides no way of doing this, so you are at the mercy of operating system specific features. And even then, there is no general method, as utilities like "uname" are not supported on Windows and will give differently formatted results on different UNIX flavours.

You can give a try to sysinfo() call (from sys/systeminfo.h on Unix).
Also regarding "uname -a" efficiency, it depends where are you going to use it. In normal circumstances (if your application is not time/CPU critical) performance of "uname -a" should be acceptable.
There is also a function, "uname()", present in the sys/systeminfo.h file, which may help you.

There is no standard way to do this. But you can apply fingerprinting techniques to find out the target OS.
These are generally used to guess remote machine OS. You can look at Nmap also. It does various types of fingerprintings.

I would think that uname -a would be a good way, but only for *nixes.

Related

Linux c++: apis vs /proc files?

Im working on an app to collect and send various bits of system info (partition space/free, laptop battery info, etc). Im not having much success getting this information in the form of direct c++ api.. though its all available via files in /proc (or similar).
So - I'm wondering whether reading/parsing these files in my c++ app is the appropriate way to get this info or should I keep trying to discover APIs? ( NOTE: I am working with statvfs ).
So far it looks like it's easier to gather this sort of info in Win32. Seems strange.
It is best practice by far to stick with an API in the following order of precedence.
Your language API (not much help for you here, but say for strings, a C99 string function is better to use than a library string facility specified by a Posix or other OS standard.)
Posix operating software APIs
Documented kernel API's
Undocumented kernel APIs (at least these will break, say, ioctl users if they change, so they probably won't change)
/proc
/dev/kmem, /dev/mem
There is no reason to believe that /proc trolling will be portable or even the same from release to release. Not every system will even have a /proc mounted!
Having said all that, it is much easier to just scrape stuff off of /proc and if it's the only available interface then you should go ahead and use it.qa
Finally, the ordering of the last two isn't completely clear, because /proc isn't available for a post-mortem kernel crash dump analysis, but tools that can peek in the core dump will still work.
I though that /proc was the API (everything is a file...)
As you have noticed, a lot of Linux systems information is in /proc. And you're correct that there often isn't a C API for retrieving that information (though there is usually a shell command if you're inclined to stick with bash instead of C++). In the worst-case scenario, you might be stuck parsing through /proc, though you might be able to get some sample code in the form of open-source shell commands for the particular item you want.

moving code from unix to windows xp

i have code written in c++. its a console app that takes an input and displays output. Now i can just give my a.out to someone without giving them the code and it should work on another unix system. but what if they have windows environment. I would like to learn how to make dll for them so they can run that.
also, if they were going to use it as part of another program I guess i would need to make an api or function for them. But i am not sure how that works with dlls as i have never done this before.
Now i can just give my a.out to
someone without giving them the code
and it should work on another unix
system.
No it shouldn't and almost certainly won't. Executables are not typically portable across even closely related operating systems, and most flavours of Unix are not too closely related. You need to compile your application for the specific (and "unix" is not specific enough) target OSs you are interested in.
You need to recompile your application for Windows, either on a Windows machine or by using a crosscompiler. This requires that all routines which you use need to be available under Windows, too.
Either you wrote your application from scratch using portable libraries (read: no unix/posix system calls), or you will get problems porting your code to run under Windows. Cygwin can probably help, check it out.
If you say its a pure console app, I assume you're using std::cout and std::cin or other stuff from the C++ standard library. These are indeed universally available on every C++ implementation.

portable system/process information library

I need to retrieve process information in a C/C++ program. I need at least basic things like CPU% and memory usage, but additional details would be useful as well.
The problem is that I need to use this information in a portable program, that will run on multiple platforms: windows, linux, MAC and possibly Solaris too.
Is there a library that I can use or do I need to write my own HAL for the different platforms? I tried to look on google, but I couldn't see anything obvious.
I can't help you with a complete solution, but here's a link to my CPULoadMeter class, which you can use to poll CPU usage information over time. It works under MacOS/X, Windows, and Linux (and possibly other Unixy-OS's that have /proc/stat also, I don't know). Perhaps you can use it as a starting point.
https://public.msli.com/lcs/muscle/muscle/util/CPULoadMeter.h
https://public.msli.com/lcs/muscle/muscle/util/CPULoadMeter.cpp
This information must be retrieved from the OS.
By definition this is non portable, but there are a couple of OS abstraction layers out there. The one that springs to mind is ACE.

Is there a cross-platform way of getting a list of running applications?

I need to get a list of applications that are currently running so that my C++ application and make them take focus.
Has anybody done this?
There is no real cross platform way of doing that. The whole concept of processes, applications, etc. is an operating system specific concept. If you use a certain library to solve the issue, you are not really cross platform, you are limited to the platforms that are supported by this library. E.g. Qt is not universal cross platform, it runs on a lot of platforms, but not on every known one and on platforms where it won't run, a Qt solution won't work. Most UNIX like platforms support the POSIX API (some more, some less) and if you limit yourself to POSIX functions, the solution will work in Linux, BSD, Mac OS X, Solaris, and similar OSes. It will not work on Windows, though. Microsoft decided to drop POSIX support (not that their POSIX support was great to begin with), however Cygwin brings back POSIX support to Windows (Cygwin emulates a complete Linux glibc API on top of Windows). The problem is that not even POSIX is really offering a set of functions to solve your problem here - the way how a POSIX tool like ps gets process information is entirely different on a Linux system compared to a BSD system for example.
The second issue is that you are talking "focus". Focus is something that does not apply to applications. A background application that has no UI and no windows cannot have "focus". What would "focus" mean for such an application? So you are not really interested in a list of running applications, but in a list of running UI applications that have windows and whose windows may get focus. An entirely different thing. The windows systems are even more different between different platforms and POSIX ignores UIs altogether.
Also you have a Visual-C++ tag on your question, so how cross platform must your code really be, as Visual-C++ is a Windows only thing, isn't it? What platforms are you really trying to support (please update your question accordingly), since I doubt there is any better solution than writing a different piece of code for every single supported platform.
First: Applications do not have focus; windows do. Secondly: some windows will not accept focus. So, I don't think it's literally possible.

How do I read system information in C++?

I'm trying to get information like OS version, hard disk space, disk space available, and installed RAM on a Linux system in C++. I know I can use system() to run different Linux commands and capture their output (which is what I'm currently doing) but I was wondering if there's a better way? Is there something in the C++ standard library that I can use to get information from the operating system?
If you are using *nix commands via system.
Then do man scroll to the bottom of the man page and it will usually show you what relevant C system calls are related.
Example: man uname:
SEE ALSO
uname(2), getdomainname(2), gethostname(2)
Explanation of numbers:
(1): User UNIX Command
(2): Unix and C system calls
(3): C Library routines
(4): Special file names
(5): File formats
(6):
(7):
(8): System admin commands
So if you are using system("uname"). From the man page you can see that there is also a uname C system call (uname(2)). So you can now do a 'man 2 uname' to get information about how to use the C system call uname.
There is nothing in the C++ Standard library for these purposes. The library you could use is libhal, which abstracts the view of programs to the hardware, collecting various informations from /proc, /sys and others. HAL, scroll down, there seems to be an unofficial C++ binding available too (haven't tested it though, while libhal works also fine for C++ programs). Use the command lshal to display all device informations available to HAL.
If you don't want to use HAL as litb suggests, you can read things straight out of the /proc filesystem, provided it's there on your system. This isn't the most platform-independent way of doing things, and in many cases you'll need to do a little parsing to pick apart the files.
I think HAL abstracts a lot of these details for you, but just know that you can read it straight from /proc if using a library isn't an option.
System information is by definition not portable, so there is no standard solution. Your best bet is using a library that does most of the work for you. One such cross platform library (unlike hal, which is currently Linux specific) is SIGAR API, which is open source BTW. I've used it in a C++ project without much trouble (the installation is a bit non-standard but can be figured out easily)