Get Linux system info with Lazarus and FreePascal
Asked Answered
U

2

8

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?

Ulloa answered 12/11, 2011 at 16:35 Comment(2)
On Linux you will need to call into libc. I don't have any idea whether or not the FPC libraries wrap that up for you. I would hope and expect that they do.Overpay
Yet you don't want to use the "libc" unit in freepascal, as that's depreciated.Declivous
E
5

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.

Extinction answered 14/11, 2011 at 10:15 Comment(2)
There is LSB however, tools like "lsb_release" exist on at least some distributions.Cammycamomile
There are certainly ways, but they are all hard work. Point of the message is that it is going to be harder than Windows, and won't be a one-off effort. For every additional attribute there might be work involved. First think long and hard about your requirements.Extinction
D
2
  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.

Declivous answered 3/2, 2012 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.