Get Machine id of MAC OS X - c++

I want machine unique id such as processor id, hdd id, uuid of MAC PC through c++ program.
Can anyone please tell me how it implements?
Thanks.

only about 7 years later, but here's an answer to those stumbling across this that we've been using.
It uses the IOPlatformExpertDevice class to access the Mac Serial number/hardware uuid
There are two ways to do this, the first uses C++, the second python. I have personally used the second way, and can verify it fetches the hardware uuid as given by System Information.
First method, not tested by myself, but uses the same class so has at least the potential to work, see https://gist.github.com/tmiz/1294978 for a routine in C++ on how to retrieve the "serial number" which is not be the same as the hardware uuid from system information, but from some tweaking, you should be able to get the hardware uuid.
Second method (see python code below), in python, which uses the ioreg command, which is executed via a separate process, then the results processed with a regular expression to get the uuid. This method definitely retrieves the hardware uuid as I've checked it with the System Information app in macos 10.14 and previous versions of 10.13 and 10.12.
May these methods serve you well, they do not return the mac address and as such should function well as a uuid for the machine, not just the network interface.
Finally you can read about ioreg here -> http://www.manpagez.com/man/8/ioreg/ and the I/O Kit more generally here -> https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/Families_Ref/Families_Ref.html#//apple_ref/doc/uid/TP0000021-BABHIGFE
import platform, re, os
os_type = platform.system()
if os_type == 'Darwin':
machine_uuid_str = ''
p = os.popen('ioreg -rd1 -c IOPlatformExpertDevice | grep -E \'(UUID)\'', "r")
while 1:
line = p.readline()
if not line: break
machine_uuid_str += line
match_obj = re.compile('[A-Z,0-9]{8,8}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{12,12}')
results = match_obj.findall(machine_uuid_str)
return results[0]

Outside of a few ancient processors, x86 CPUs do not have software-visible serial numbers.
Apple recommends that you use the MAC address of the computer's primary network interface (i.e, the onboard Ethernet controller if present, or the wireless interface otherwise) as a unique identifier for the system. Sample code for doing this is available in Apple's Validating Mac App Store Receipts documentation (under "Get the Computer's GUID").

Related

Receiving back string of lenght 0 from os.popen('cmd').read()

I am working with a command line tool called 'ideviceinfo' (see https://github.com/libimobiledevice) to help me to quickly get back serial, IMEI and battery health information from the iOS device I work with daily. It executes much quicker than Apple's own 'cfgutil' tools.
Up to know I have been able to develop a more complicated script than the one shown below in PyCharm (my main IDE) to assign specific values etc to individual variables and then to use something like to pyclip and pyautogui to help automatically paste these into the fields of the database app we work with. I have also been able to use the simplified version of the script both in Mac OS X terminal and in the python shell without any hiccups.
I am looking to use AppleScript to help make running the script as easy as possible.
When I try to use Applescript's "do shell script 'python script.py'" I just get back a string of lenght zero when I call 'ideviceinfo'. The exact same thing happens when I try to build an Automator app with a 'Run Shell Script' component for "python script.py".
I have tried my best to isolate the problem down. When other more basic commands such as 'date' are called within the script they return valid strings.
#!/usr/bin/python
import os
ideviceinfoOutput = os.popen('ideviceinfo').read()
print ideviceinfoOutput
print len (ideviceinfoOutput)
boringExample = os.popen('date').read()
print boringExample
print len (boringExample)
I am running Mac OS X 10.11 and am on Python 2.7
Thanks.
I think I've managed to fix it on my own. I just need to be far more explicit about where the 'ideviceinfo' binary (I hope that's the correct term) was stored on the computer.
Changed one line of code to
ideviceinfoOutput = os.popen('/usr/local/bin/ideviceinfo').read()
and all seems to be OK again.

detecting if program is installed on machine

Say I have an application I write, that relies for some task on an externat app (lets call it "tool") that is installed on my machine. In my program, I call it with system( "tool myarguments" ); , works fine.
Now, I want to distribute my app. Of course, the end-user might not have "tool" installed on his machine, so I would like my app to check this, and printout a message for the user. So my question is:
Is there a portable way to check for the existence of an app on the machine? (assuming we know its name and it is accessible through the machine's shell).
Additional information: First idea was to check the existence of the binary file, but:
This is platform dependent,
depending on how it has been installed (build from sources, installed through package,...), it might not always be in the same place, although it can be accessed through local path.
My first opinion on this question is "No", but maybe somebody has an idea ?
Reference: system()
Related: stackoverflow.com/questions/7045879
If you use the Qt toolkit, QProcess may help you.
Edit: and look for QProcess::error() return value: if it is QProcess::FailedToStart , then either it is not installed, or you have insufficient permissions.
If running the tool without argument has no side-effect, and is expected to return an exit code of 0, you can use system("tool") to check tool's existence.
You can check whether the command has been found by checking system's return value like this:
int ret = system("tool");
if (ret != 0) {
std::cout << "tool is not here, move along\n";
}
It is portable in the sense that system is expected to return 0 if all goes well and the command return status is 0 too.
For example, on Linux, running system("non_existing_command") returns 0x7F00 (same type of value as returned by wait()).
On Windows, it returns 1 instead.

C/C++ - HWInfo - libhd - How to get the name list of all available devices?

On Linux, one can use command "hwinfo" (after installing it) in Terminal to print a lot of device information. While this is nice, I'd also like to utilize libhd (included in hwinfo) to get device information, device names to be exact. How can one get the name list of all available devices and then print the name list, in C/C++?
From your comment above... If you just wanna know the number of elements from a struct, let's say your struct is hd_data_t, you may use
int nSize = sizeof(hd_data_t)/sizeof(hd_data_t[0]);

Getting Mac Address in Groovy

Just want to ask if there's a way of getting your local machine's Mac address as String.
I need to save my local machine's mac address into a domain class. I tried using this code to get my Mac address:
String address = "ifconfig".execute().text()
But this line it also returns a lot of details about my ip address, all I need to get is the Mac Address which is found after the substring "HWaddr". I'm thinking if i could extract this substring using regex but I am not sure how to do it.
You can get the MAC address for an interface using java.net.NetworkInterface. Note that it is possible to have multiple hardware network interfaces, so it's possible to have more than one MAC address. In addition, most machines will have at least one interface without a hardware address: the loopback interface.
This will get a list of all the MAC addresses as Strings, including nulls for interfaces without a MAC address:
import java.net.NetworkInterface
def macs = NetworkInterface.networkInterfaces.collect { iface ->
iface.hardwareAddress?.encodeHex().toString()
}
You can use the NetworkInterface to get this information, especially the getHardwareAddress() method which return a byte array

Why does ICU's Locale::getDefault() return "root"?

Using the ICU library with C++ I'm doing:
char const *lang = Locale::getDefault().getLanguage();
If I write a small test program and run it on my Mac system, I get en for lang. However, inside a larger group project I'm working on, I get root. Anybody have any idea why? I did find this:
http://userguide.icu-project.org/locale/resources
so my guess is that, when running under the larger system, some ICU resources aren't being found, but I don't know what resources, why they're not being found, or how to fix it.
Additional Information
/usr/bin/locale returns:
LANG="en_US.ISO8859-1"
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL="C"
If I write a small C program:
char const *lang = setlocale( LC_ALL, "" ):
I get en_US.ISO8859-1.
OS: Mac OS X 10.6.4 (Snow Leopard)
ICU version: 4.3.4 (latest available via MacPorts).
A little help? Thanks.
root is surely an odd default locale - you don't see many native root-speakers these days.
But seriously, is it safe to assume on the larger system that someone hasn't called one of the variants of setDefault("root")?
What does something like /usr/bin/locale return on this system (if you can run that)?
ICU 4.4 now has a test program called 'icuinfo', does it also return root as the default locale?
What OS/platform is this on, and which version of ICU?