Add native helpers to get the CPU name
This commit is contained in:
parent
7d8a77db05
commit
1aeca9de1c
3 changed files with 69 additions and 0 deletions
|
|
@ -111,6 +111,32 @@ std::string getOperatingSystemName()
|
||||||
std::unique_ptr<char[]> valueUTF8(stringToUTF8(valueW.get()));
|
std::unique_ptr<char[]> valueUTF8(stringToUTF8(valueW.get()));
|
||||||
return valueUTF8.get();
|
return valueUTF8.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getProcessorName()
|
||||||
|
{
|
||||||
|
LSTATUS status;
|
||||||
|
HKEY key = nullptr;
|
||||||
|
const WCHAR keyPath[] = L"Hardware\\Description\\System\\CentralProcessor\\0";
|
||||||
|
const WCHAR valueName[] = L"ProcessorNameString";
|
||||||
|
const char fallbackName[] = "Unknown";
|
||||||
|
|
||||||
|
status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_QUERY_VALUE, &key);
|
||||||
|
if (status != ERROR_SUCCESS)
|
||||||
|
return fallbackName;
|
||||||
|
|
||||||
|
DWORD valueSize = 32768 * sizeof(WCHAR);
|
||||||
|
std::unique_ptr<WCHAR[]> valueW(new WCHAR[(valueSize / sizeof(WCHAR)) + 1]());
|
||||||
|
DWORD valueType;
|
||||||
|
status = RegQueryValueExW(
|
||||||
|
key, valueName, nullptr,
|
||||||
|
&valueType, reinterpret_cast<LPBYTE>(valueW.get()), &valueSize);
|
||||||
|
RegCloseKey(key);
|
||||||
|
if (status != ERROR_SUCCESS || (valueType != REG_SZ && valueType != REG_EXPAND_SZ))
|
||||||
|
return fallbackName;
|
||||||
|
|
||||||
|
std::unique_ptr<char[]> valueUTF8(stringToUTF8(valueW.get()));
|
||||||
|
return valueUTF8.get();
|
||||||
|
}
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
// implemented in NativeHelpers.mm
|
// implemented in NativeHelpers.mm
|
||||||
#else
|
#else
|
||||||
|
|
@ -120,6 +146,8 @@ std::string getOperatingSystemName()
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <regex>
|
||||||
|
#include <fstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
extern "C" { extern char **environ; }
|
extern "C" { extern char **environ; }
|
||||||
|
|
@ -247,4 +275,25 @@ std::string getOperatingSystemName()
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getProcessorName()
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
std::string line;
|
||||||
|
std::ifstream in("/proc/cpuinfo", std::ios::binary);
|
||||||
|
std::regex re("^model name\\s*:\\s*(.*)");
|
||||||
|
|
||||||
|
line.reserve(256);
|
||||||
|
|
||||||
|
while (name.empty() && std::getline(in, line) && !line.empty()) {
|
||||||
|
std::smatch match;
|
||||||
|
if (std::regex_match(line, match, re))
|
||||||
|
name = match[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name.empty())
|
||||||
|
name = "Unknown";
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ bool openDirectoryInExplorer(const char *filename);
|
||||||
bool openURLWithExternalProgram(const char *url);
|
bool openURLWithExternalProgram(const char *url);
|
||||||
bool askQuestion(const char *text);
|
bool askQuestion(const char *text);
|
||||||
std::string getOperatingSystemName();
|
std::string getOperatingSystemName();
|
||||||
|
std::string getProcessorName();
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(__APPLE__)
|
#if !defined(_WIN32) && !defined(__APPLE__)
|
||||||
bool isZenityAvailable();
|
bool isZenityAvailable();
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@
|
||||||
#import <AppKit/AppKit.h>
|
#import <AppKit/AppKit.h>
|
||||||
#import <CoreServices/CoreServices.h>
|
#import <CoreServices/CoreServices.h>
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
static bool openFileWithApplication(const char *fileName, NSString *application)
|
static bool openFileWithApplication(const char *fileName, NSString *application)
|
||||||
{
|
{
|
||||||
|
|
@ -59,4 +62,20 @@ std::string getOperatingSystemName()
|
||||||
NSString *osVersion = [[NSProcessInfo processInfo] operatingSystemVersionString];
|
NSString *osVersion = [[NSProcessInfo processInfo] operatingSystemVersionString];
|
||||||
return [[NSString stringWithFormat:@"%@ %@", osName, osVersion] UTF8String];
|
return [[NSString stringWithFormat:@"%@ %@", osName, osVersion] UTF8String];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string getProcessorName()
|
||||||
|
{
|
||||||
|
char nameBuf[256];
|
||||||
|
size_t size = sizeof(nameBuf);
|
||||||
|
const char* fallbackName = "Unknown";
|
||||||
|
|
||||||
|
if (sysctlbyname("machdep.cpu.brand_string", nameBuf, &size, nullptr, 0) == -1)
|
||||||
|
return fallbackName;
|
||||||
|
|
||||||
|
size = strnlen(nameBuf, sizeof(nameBuf));
|
||||||
|
if (size == 0)
|
||||||
|
return fallbackName;
|
||||||
|
|
||||||
|
return std::string(nameBuf, size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue