dlopen调用私有函数用theos编译不通过

想获取手机的序列号,但是代码在Xcode中编译能通过,并且可以正常运行,但是在theos编译却不通过,希望大神能帮帮忙指出该怎么解决,谢谢

代码如下

- (NSString *) serialNumber
{
    NSString *serialNumber = nil;
    
    void *IOKit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW);
    if (IOKit)
    {
        mach_port_t *kIOMasterPortDefault = dlsym(IOKit, "kIOMasterPortDefault");
        CFMutableDictionaryRef (*IOServiceMatching)(const char *name) = dlsym(IOKit, "IOServiceMatching");
        mach_port_t (*IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, "IOServiceGetMatchingService");
        CFTypeRef (*IORegistryEntryCreateCFProperty)(mach_port_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options) = dlsym(IOKit, "IORegistryEntryCreateCFProperty");
        kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, "IOObjectRelease");
        
        if (kIOMasterPortDefault && IOServiceGetMatchingService && IORegistryEntryCreateCFProperty && IOObjectRelease)
        {
            mach_port_t platformExpertDevice = IOServiceGetMatchingService(*kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
            if (platformExpertDevice)
            {
                CFTypeRef platformSerialNumber = IORegistryEntryCreateCFProperty(platformExpertDevice, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0);
                if (CFGetTypeID(platformSerialNumber) == CFStringGetTypeID())
                {
                    
                    serialNumber = [NSString stringWithString:(__bridge NSString*)platformSerialNumber];
                    
                    CFRelease(platformSerialNumber);
                }

                IOObjectRelease(platformExpertDevice);
            }
        }
        dlclose(IOKit);
    }
    [_showLabel setText:serialNumber];
    return serialNumber;
}

编译报错如下
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/include/dlfcn.h:37:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/include/sys/cdefs.h:680:2: error:
Unsupported architecture
#error Unsupported architecture
^
1 error generated.
Compiling RootViewController.mm…
RootViewController.mm:40:22: error: cannot initialize a variable of type ‘mach_port_t *’ (aka ‘unsigned int *’) with an
rvalue of type ‘void *’
mach_port_t *kIOMasterPortDefault = dlsym(IOKit, “kIOMasterPortDefault”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RootViewController.mm:41:34: error: cannot initialize a variable of type ‘CFMutableDictionaryRef (*)(const char )’ with
an rvalue of type ‘void *’
CFMutableDictionaryRef (*IOServiceMatching)(const char *name) = dlsym(IOKit, “IOServiceMatching”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RootViewController.mm:42:23: error: cannot initialize a variable of type ‘mach_port_t (*)(mach_port_t, CFDictionaryRef)’
with an rvalue of type ‘void *’
…IOServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching) = dlsym(IOKit, “IOServiceGetMatchingService”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RootViewController.mm:43:21: error: cannot initialize a variable of type 'CFTypeRef (
)(mach_port_t, CFStringRef,
CFAllocatorRef, uint32_t)’ with an rvalue of type ‘void *’
…IORegistryEntryCreateCFProperty)(mach_port_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options) = dlsym(IOKit, “IORegistryEntryCreateCFProperty”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RootViewController.mm:44:25: error: cannot initialize a variable of type ‘kern_return_t (*)(mach_port_t)’ with an rvalue
of type ‘void *’
kern_return_t (*IOObjectRelease)(mach_port_t object) = dlsym(IOKit, “IOObjectRelease”);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 errors generated.
make[2]: *** [obj/RootViewController.mm.f6ced082.o] Error 1
make[1]: *** [internal-application-all_] Error 2
make: *** [serial.all.application.variables] Error 2

1 个赞