1

Is there any way to run command prompt commands from within a C application ? I want to check the manufacturer serial number of the hard drive using wmic diskdrive get serialnumber command.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
Asus gates
  • 380
  • 1
  • 5
  • 20

3 Answers3

2

There many functions that allow you to call external program from C, some of them "standard", some of them are specific to Windows.

But in your scenario I would recommend you use the WMI API provided by Windows, you can find examples for this already on stackoverflow:

How to obtain data from WMI using a C Application? how to run a wmi query in c

I didn't provide examples but rather linked to already existing answers on SO because I don't think it makes sense to copy what is already there.

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • From the MSDN Reference to the WMI COM Api, it says this: "Note WMI only supports C++ development using Microsoft Visual C++ version 6.0 and later development systems. " – Magisch Oct 16 '15 at 06:29
  • Please check the link provided above. It shows an example which is plain C, no C++ involved. It seems that COM is only one alternative to access WMI. – Marged Oct 16 '15 at 06:44
0

You could try using popen :-

#include<stdio.h>
#include<string.h>

int main()
{
   char c;
   FILE *file = popen("wmic diskdrive get serialnumber", "r");
   if (file) {
     while ((c = getc(file)) != EOF)
        putchar(c);
    fclose(file);
   }
   return 0;
}

Since this is a file object you can now easily write it to a file if you want, and retrieve it at will too.

Rahul Nori
  • 698
  • 6
  • 17
-1

Yes you can. It is however, considered bad practice to do so, as it makes your app needlessly platform dependant and generates overhead.

Like so

#include <stdio.h>
#include <string.h>
void main ()
{
    system("wmic diskdrive get serialnumber");
}

Read more on it here Regarding output (quoted from the link above):

The value returned is -1 on error, and the return status of the command otherwise.

You can also use the ReadConsoleOutputCharacter function (link to the msdn reference inbound) to read console output.

Before you try and fetch such data with the command prompt however, consider using an API or any other means to retrieve the data, because by calling the command prompt, you are adding ridiculous overhead to your application that is unnecessary.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • 1
    And how can the output be retrieved ? – Marged Oct 16 '15 at 06:14
  • from the link I linked, regarding output: "The value returned is -1 on error, and the return status of the command otherwise." – Magisch Oct 16 '15 at 06:15
  • Nope. The output of `wmic` is something like `SerialNumber Z81EFB1 E6503403 F123123`. This won't fit into an `int` – Marged Oct 16 '15 at 06:17
  • 3
    If you're coding for a specific platform, it's not bad practice to use the features of that platform. Checking for manufacturer and serial number is probably going to be platform dependent anyway, so you pretty much throw out complete portability. But if there's a way to do it through the WinAPI (there probably is), then that's definitely better. – PC Luddite Oct 16 '15 at 06:20
  • Further digging says that there is a WIM api, but that it only really works in c++ – Magisch Oct 16 '15 at 06:23
  • So you could probably create a c++ wrapper dll that exports functions to call it in c but thats alot of work – Magisch Oct 16 '15 at 06:23
  • How do you have `ReadConsoleOutputCharacter` read the console output of a foreign process ? I think this won`t work. Redirecting stdout of that process to your calling process and then parsing it might be a solution. – Marged Oct 16 '15 at 06:49
  • Yup `ReadConsoleOutputCharacter` tipped the balance. It's only so much to _answer_ a misguided question helpfully, but to send them so far into the woods that they will starve a horrible death is just not nice. -1 – sehe Jan 29 '16 at 22:38
  • The WMI API looks to be COM. The very point of COM is to be middle ware. Of course you can consume this from C. (It won't be nice to do, but ... well that's [tag:c]) – sehe Jan 29 '16 at 22:39