如何从进程pid中获取其他应用程序的目录路径?
iOS 上似乎没有 proc_pidpath 调用。
以下适用于 iOS 并使用sysctl
像 Activity Monitor Touch 这样的应用程序正在 App Store 中使用,因此 Apple 应该可以接受。但是,一旦获得该路径,您打算对其进行的操作可能不会被 Apple 接受。如果您不打算将应用程序提交到应用程序商店,那么这可能不是问题。
- (NSString *)pathFromProcessID:(NSUInteger)pid {
// First ask the system how big a buffer we should allocate
int mib[3] = {CTL_KERN, KERN_ARGMAX, 0};
size_t argmaxsize = sizeof(size_t);
size_t size;
int ret = sysctl(mib, 2, &size, &argmaxsize, NULL, 0);
if (ret != 0) {
NSLog(@"Error '%s' (%d) getting KERN_ARGMAX", strerror(errno), errno);
return nil;
}
// Then we can get the path information we actually want
mib[1] = KERN_PROCARGS2;
mib[2] = (int)pid;
char *procargv = malloc(size);
ret = sysctl(mib, 3, procargv, &size, NULL, 0);
if (ret != 0) {
NSLog(@"Error '%s' (%d) for pid %d", strerror(errno), errno, pid);
free(procargv);
return nil;
}
// procargv is actually a data structure.
// The path is at procargv + sizeof(int)
NSString *path = [NSString stringWithCString:(procargv + sizeof(int))
encoding:NSASCIIStringEncoding];
free(procargv);
return(path);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)