网上的获取MAC地址代码 打包dylib错误

msngll 发布于 2013/08/24 17:19
阅读 154
收藏 0
在XCODE下编译测试通过 打包DYLIB 出现错误 :

“if ((buf = malloc(len)) == NULL) {” error: invalid conversion from 'void*' to 'char*
求高人指点解决方案 谢谢

#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
- (NSString *) macaddid{ 
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if ((mib[5] = if_nametoindex("en0")) == 0) { printf("Error: if_nametoindex error\n");
return NULL;
} 
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { printf("Error: sysctl, take 1\n");
return NULL;
} 
if ((buf = malloc(len)) == NULL) { printf("Could not allocate memory. error!\n");
return NULL;
} 
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { printf("Error: sysctl, take 2");
free(buf);
return NULL;
} 
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return outstring;}
加载中
返回顶部
顶部