How to Read System Information in C++

Get Linux system information in C

Linux exposes a lot of information under /proc. You can read the data from there. For example, fopen the file at /proc/cpuinfo and read its contents.

How to get system information in windows with c++

You can use the GetSystemInfo function to obtain hardware information such as the OEM identifier, processor type, page size, and so on.

#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

void main()
{
SYSTEM_INFO siSysInfo;

// Copy the hardware information to the SYSTEM_INFO structure.

GetSystemInfo(&siSysInfo);

// Display the contents of the SYSTEM_INFO structure.

printf("Hardware information: \n");
printf(" OEM ID: %u\n", siSysInfo.dwOemId);
printf(" Number of processors: %u\n",
siSysInfo.dwNumberOfProcessors);
printf(" Page size: %u\n", siSysInfo.dwPageSize);
printf(" Processor type: %u\n", siSysInfo.dwProcessorType);
printf(" Minimum application address: %lx\n",
siSysInfo.lpMinimumApplicationAddress);
printf(" Maximum application address: %lx\n",
siSysInfo.lpMaximumApplicationAddress);
printf(" Active processor mask: %u\n",
siSysInfo.dwActiveProcessorMask);
}

References:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724423(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724381(v=vs.85).aspx

http://www.cplusplus.com/forum/windows/124624/

How to take system information by OS in C

Get a list of processes with EnumProcesses(), which you can then iterate to get information on the individual processes using the Process API functions such as GetModuleBaseName() as described in the example at https://learn.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes

How do I read system information in C++?

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.

Get PC (system) information on a Windows machine

WMI is what you're looking for.

http://www.codeproject.com/KB/cs/EverythingInWmi02.aspx

Let me add the link to Part 3 too, which concentrates on hardware via WMI

http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx

MSDN is also a great resource for WMI scopes...

http://msdn.microsoft.com/en-us/library/aa394554(v=vs.85).aspx

How to read all of STDOUT produced by system() call into a socket written in C?

The way your code is sending and reading strings is not sufficient.

TCP is a byte stream. There is no 1-to-1 relationship between sends and reads. As such, the sender MUST either:

  1. send the string length before sending the string's data.
  2. send a unique terminator after the string data.

And the receiver MUST either:

  1. read the length then read the specified amount of data.
  2. read until the terminator is reached.

Also, send()/write() and recv()/read() can return fewer bytes than requested, so they need to be called in loops (or, in the case of recv(), you can use the MSG_WAITALL flag).

Try something more like this instead:

// common functions ...

bool sendRaw(int sock, void *data, size_t len)
{
char *ptr = (char*) data;
while (len > 0) {
int sent = send(sock, ptr, len, MSG_NOSIGNAL);
if (sent < 0) return false;
ptr += sent;
len -= sent;
}
return true;
}

int recvRaw(int sock, void *data, size_t len)
{
char *ptr = (char*) data;
while (len > 0) {
int recvd = recv(sock, ptr, len, MSG_NOSIGNAL);
if (recvd <= 0) return recvd;
ptr += recvd;
len -= recvd;
}
return 1;
}

bool sendUInt32(int sock, uint32_t value)
{
value = htonl(value);
return sendRaw(sock, &value, sizeof(value));
}

uint32_t recvUInt32(int sock)
{
uint32_t value;
if (recvRaw(sock, &value, sizeof(value)) <= 0) return -1;
return ntohl(value);
}

bool sendString(int sock, const char *str)
{
uint32_t len = strlen(str);
if (!sendUInt32(sock, len)) return false;
return sendRaw(sock, str, len);

/* alternatively:
return sendRaw(sock, str, strlen(len) + 1);
*/
}

/*
bool grow(char **str, size_t *cap, size_t stepBy)
{
size_t newcap = cap + stepBy;
char *newstr = (char*) realloc(*str, newcap);
if (!newstr) return false;
*str = newstr;
*cap = newcap;
return true;
}
*/

char* recvString(int sock)
{
uint32_t len = recvUInt32(sock);
if (len == -1) return NULL;

char *str = (char*) malloc(len+1);
if (!str) return NULL;

if (recvRaw(sock, str, len) <= 0){
free(str);
return NULL;
}

str[len] = '\0';
return str;

/* alternatively:

char ch, *str = NULL;
size_t len = 0, cap = 0;

do{
if (recvRaw(sock, &ch, 1) <= 0){
free(str);
return NULL;
}

if (ch == '\0') break;

if (len == cap){
if (!grow(&str, &cap, 256)){
free(str);
return NULL;
}
}

str[len++] = ch;
}
while (1);

if (len == cap){
if (!grow(&str, &cap, 1)){
free(str);
return NULL;
}
}

str[len] = '\0';
return str;
*/
}
// server ...

char *command;

while ((command = recvString(sock)) != NULL){
// ...
system(command);
free(command);

// read from command's stdout until finished ...

if (!sendString(sock, output, outputLength)) break;
}
// client ...

if (sendString(sock, command)){
char *output = recvString(sock);
if (output){
//print the output somewhere
free(output);
}
}

Alternatively, if you don't know the length of the command's response ahead of time, and/or don't want to buffer it all in a single memory buffer, then you can read it in chunks, sending each chunk as you go, eg:

// common functions, see above ...

typedef struct _chunk
{
uint8_t size;
char data[256];
} chunk;

bool sendChunk(int sock, const chunk *chk)
{
uint8_t size = chk ? chk->size : 0;
if (!sendRaw(sock, &size, 1)) return false;
if (chk) return sendRaw(sock, chk->data, size);
return true;
}

bool recvChunk(int sock, chunk *chk)
{
if (recvRaw(sock, &(chk->size), 1) <= 0) return false;
if (chk->size) return recvRaw(sock, chk->data, chk->size);
return true;
}
// server ...

bool sendOutput(int sock)
{
chunk chk;
int size;

do{
// read from command's stdout ...
size = read(..., chk.data, sizeof(chk.data));
if (size <= 0) break;

chk.size = (uint8_t) size;
if (!sendChunk(sock, &chk)) return false;
}
while(1);

// tell client the data is finished ...
return sendChunk(sock, NULL);
}

char *command;

while ((command = recvString(sock)) != NULL){
// ...
system(command);
free(command);
if (!sendOutput(sock)) break;
}
// client ...

if (sendString(sock, command)){
chunk chk;
do{
if (!recvChunk(sock, &chk)) break;
if (chk.size == 0) break;
//print the chk.data somewhere
}
while (1);
}

Get system information using c#

You can use following to get Manufacturer name, add a reference to System.Management.

 System.Management.SelectQuery query = new System.Management.SelectQuery(@"Select * from Win32_ComputerSystem");

//initialize the searcher with the query it is supposed to execute
using (System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query))
{
//execute the query
foreach (System.Management.ManagementObject process in searcher.Get())
{
//print system info
process.Get();
Console.WriteLine("/*********Operating System Information ***************/");
Console.WriteLine("{0}{1}", "System Manufacturer:", process["Manufacturer"]);
Console.WriteLine("{0}{1}", " System Model:", process["Model"]);

}
}

System.Management.ManagementObjectSearcher searcher1 = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
System.Management.ManagementObjectCollection collection = searcher1.Get();

foreach (ManagementObject obj in collection)
{
if ( ((string[])obj["BIOSVersion"]).Length > 1)
Console.WriteLine("BIOS VERSION: " + ((string[])obj["BIOSVersion"])[0] + " - " + ((string[])obj["BIOSVersion"])[1]);
else
Console.WriteLine("BIOS VERSION: " + ((string[])obj["BIOSVersion"])[0]);
}

SOURCE

read system call: When input has more bytes than count argument, excess bytes overflow to shell and get executed as next command

Your program only reads 20 bytes. Whatever there is to be read after those 20 bytes, from whatever the program's standard input is connected to, stays there. Be that input from the terminal, a pipe buffer, or a file.

If you used the stdio input functions (fgets(), fread() etc.) instead,
they would ask the OS for a larger block of data (usually 4096 B with glibc on Linux), so the issue wouldn't come up with such a short input.

To get everything there is, you need to loop, reading everything until EOF and, since you're implementing tee, also copy all of it to standard output and the output file.

i.e. something in this direction:

#include<unistd.h>
#include<stdio.h>

int main(void)
{
/* setup ... */
char buf[1024];
while(1) {
int n = read(fd, buf, 1024);
if (n == 0)
break; /* EOF */
if (n == -1) {
perror("read");
return 1;
}
write(STDOUT_FILENO, buf, n);
write(outfd, buf, n);
}
return 0;
}

But check for errors on the write() calls, too. Also, technically, write() may return without writing everything you asked, i.e. write(outfd, buf, n) may write less than the n bytes you asked. But the cases where that happens are somewhat rare.



Related Topics



Leave a reply



Submit