0

I'm writing a program that returns some physical information of the computer. I have seen that in Windows there exists the command

WMIC CPU GET /FORMAT:LIST

That command returns the whole information about the process, I need to use that information from a C program. I mean, I have to run the C program and show that information.

I'm a beginner in C.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631

2 Answers2

3

For viewing purpose you can use this:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    system("WMIC CPU GET /FORMAT:LIST");
    return 0;
}

And if you want use this information then here the solution: https://stackoverflow.com/a/28093714/2317535

Use popen instead of system. See example here https://msdn.microsoft.com/en-us/library/96ayss4b.aspx

char   psBuffer[128];
FILE   *pPipe;

if( (pPipe = _popen( "set PATH=%PATH%;C:/Program Files (x86)/myFolder/bin", "rt" )) == NULL )
    exit( 1 );

then

while(fgets(psBuffer, 128, pPipe)) {
    printf(psBuffer);
}

if (feof( pPipe))
    printf( "\nProcess returned %d\n", _pclose( pPipe ) );
Community
  • 1
  • 1
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • Why do you say to use popen instead of system? (besides you quoting that stackoverflow answer you link to that says those words) – barlop Aug 29 '15 at 01:02
  • Because output of a system call redirect to stdout. To reuse the output popen can help. – ashiquzzaman33 Aug 29 '15 at 01:10
  • Yes, [popen()](http://linux.die.net/man/3/popen) is the best solution here, because it makes it easy to read the output from wmic. Don't forget the "pclose()" when you're done (as illustrated above)! – paulsm4 Aug 29 '15 at 01:16
0

Fair warning: the way presented here is not really an ‘easy’ solution.

WMIC is a command that accesses WMI, Windows Management Instrumentation. WMIC CPU GET /FORMAT:LIST presumably gets the CPUs; on the level of WMI itself, you’re probably just trying to get all instances of Win32_Processor.

WMI can be accessed through COM. To do so, you’d start by using CoCreateInstance to create an IWbemLocator, then calling ConnectServer to get an IWbemServices, on which you could run ExecQuery to query for Win32_Processors.

Unfortunately, COM is not very easy to access from C, but it is doable. (Unfortunately I could not find any page on MSDN about it; that CodeProject article is the best I could find.)

I’m not particularly familiar with any of these technologies in great depth, but if you research them all, that’s how you’d connect them together to get that information natively rather than by calling out to an external command.

icktoofay
  • 126,289
  • 21
  • 250
  • 231