Determining if an app is running in 64-bit mode
OK, time for more geek-talk. I’ve spent a couple of late nights fussing with fat binaries, bundles, and mach-o architecture API’s to try and find a way to determine if an app is running in 64 bit mode. Surprisingly, I couldn’t just Google it and get an easy solution.
Looking at the darwin source, I found there’s a sysctl() token to get this information. This code will return the processor type (including the 64-bit flag). Note that it uses the sysctlbyname_with_pid() function in Apple’s Universal Binary Programming Guidelines:
cpu_type_t GetProcessArchitecture(pid_t pid)
{
cpu_type_t cputype;
size_t cpusz = sizeof(cputype);
// Default values
#if __i386__
cputype = CPU_TYPE_X86;
#else
cputype = CPU_TYPE_POWERPC;
#endif
if (sysctlbyname_with_pid("sysctl.proc_cputype", pid,
&cputype, &cpusz, NULL, 0) == -1)
{
fprintf(stderr, "proc_cputype: sysctlbyname_with_pid failed:"
"%s\n", strerror(errno));
}
return cputype;
}
