简体   繁体   中英

using C++ on Mac, how to check if the machine is a laptop or desktop?

On PC, there's a way to do as this post suggests How to check the machine type? laptop or desktop?

I need to do the same on a Mac. Thanks in advance.

One way of achieving this is to get machine model:

#include <sys/sysctl.h>

size_t len = 0;
sysctlbyname("hw.model", NULL, &len, NULL, 0);

char* model = (char*)malloc(len + 1);
memset(model, 0, len + 1);

sysctlbyname("hw.model", model, &len, NULL, 0);
printf("%s", model);
free(model)

which on my MBP prints "MacBookPro5,5". Other models include "MacBookAir", "iMac", "Macmini", "MacPro". So, if there is "book" in the model name then it's likely a laptop:

if (strcasestr("book", model)) {
    // This is laptop
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM