怎样修改CPU个数呢

各位大神,我想用sysctlbyname来hookCPU的个数,可是到这里就不会了,我该怎么改变参数呢,小白一枚请赐教:pray:
int (*old_sysctlbyname)(const char *, void *, size_t *, void *, size_t);
int new_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen){

    NSString* nameStr=[NSString stringWithUTF8String:name];
    int ret=old_sysctlbyname(name,oldp,oldlenp,newp,newlen);
    return ret;    

}
MSHookFunction(&sysctlbyname, &new_sysctlbyname, (void**)&old_sysctlbyname);

可以这样:

int (*old_sysctlbyname)(const char *, void *, size_t *, void *, size_t);
int new_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
{
    int result = old_sysctlbyname(name, oldp, oldlenp, newp, newlen);
    if(strcmp(name, "hw.ncpu") == 0)
    {
        int cpuCount = 6;
        NSString* DeviceType = @"想要改的机型";
        if (DeviceType)
        {
            // iphone 4S
            if ([DeviceType rangeOfString:@"iPhone4"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone 5
            else if ([DeviceType rangeOfString:@"iPhone5"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone 5S
            else if ([DeviceType rangeOfString:@"iPhone6"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone 6P
            else if ([DeviceType rangeOfString:@"iPhone7,1"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone 6
            else if ([DeviceType rangeOfString:@"iPhone7,2"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone6s
            else if ([DeviceType rangeOfString:@"iPhone8,1"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone6s P
            else if ([DeviceType rangeOfString:@"iPhone8,2"].location != NSNotFound)
            {
                cpuCount = 2;
            }
            // iphone7
            else if ([DeviceType rangeOfString:@"iPhone9,1"].location != NSNotFound)
            {
                cpuCount = 4;
            }
            // iphone7 P
            else if ([DeviceType rangeOfString:@"iPhone9,2"].location != NSNotFound)
            {
                cpuCount = 4;
            }
            // iphone8
            else if ([DeviceType rangeOfString:@"iPhone10,1"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            // iphone9 P
            else if ([DeviceType rangeOfString:@"iPhone10,2"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            // iphonex
            else if ([DeviceType rangeOfString:@"iPhone10,3"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            // iphone8
            else if ([DeviceType rangeOfString:@"iPhone10,4"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            // iphone9 P
            else if ([DeviceType rangeOfString:@"iPhone10,5"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            // iphonex
            else if ([DeviceType rangeOfString:@"iPhone10,6"].location != NSNotFound)
            {
                cpuCount = 6;
            }
            else
            {
                cpuCount = 6;
            }
        }

         if (NULL != oldp)
         {
            NSLog(@"new_sysctlbyname hw.ncpu = %d", *((int*)oldp));
            memcpy(oldp, &cpuCount, sizeof(cpuCount));
            NSLog(@"new_sysctlbyname hw.ncpu modify = %d", *((int*)oldp));
         }
         

    }

    return result;    
}

MSHookFunction((void* )sysctlbyname, (void * )new_sysctlbyname, (void** )&old_sysctlbyname);

感谢分享。论坛有Markdown支持可以用它把代码段包起来

第一次发帖没有注意,现在我改过来了 :grinning: