Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Linux system info with Lazarus and FreePascal

I am really new to Lazarus/FreePascal and I have no prior Delphi experience. I want to be able to get OS version information and user account type on both WIndows and Linux. I saw that there is a Windows system API, but could not find anything similar for Linux.

Is there a Linux system API for Lazarus/FreePascal and if so, how can I use it?

like image 493
Shade Avatar asked Nov 12 '11 16:11

Shade


2 Answers

There is no such unified point on Linux, for which you could write one header and then keep using it. Moreover, since many parts of Linux are independently versioned, there is no such thing as a single version.

That being said, kernel version can be gotten via baseunix.fpuname(). Distribution version is hard, since each distribution stores his version info in a different way.

First step would be to identify the information you need, and a rough idea where to get it (e.g. system libraries like libc, additional info like sysctl and /proc, and if higher level systems on desktop systems allow to query such info, e.g. via DBUS). Those references won't be in Pascal always. Then ask specific questions here, or on the Lazarus forums/maillists.

But there is no WMI like functionality that always works and is pretty invariant. Linux is and remains a hacker and tweaker OS fragmented into separate distributions with few hard securities. DBUS and HAL were going in that direction, but versionitis and the transition to a new library made this route less universal.

like image 109
Marco van de Voort Avatar answered Sep 30 '22 12:09

Marco van de Voort


  1. There is a "Unix" unit, you can check if what you want is in there.

I want to be able to get OS version information

What does this mean exactly? You can use the output of "uname -a" (Called from TProcess), depending on your needs. There are also files like /etc/redhat-release, etc., but they may vary by distribution.

and user account type on both Windows and Linux.

Probably what you want in Unix is to see what groups the user is in. (There are various ways to do this).

I saw that there is a Windows system API, but could not find anything similar for Linux.

Linux tends to follow the "Everything is a file" approach, so somewhere there is usually a file (or pseudo-file) you can read with the information you want. In general, Unix is more flexible than Windows. For example, in Windows there is probably an API call to get the number of processors or speed in Mhz. In Linux, you can look in /dev/cpuinfo and parse that info as a text file.

There are various flavors of Unix, and even within Linux, various distributions. You can of course narrow the scope of what you want to do by supporting only certain distributions for the time being.

like image 25
Noah Avatar answered Sep 30 '22 14:09

Noah