Objective-C 提供 NSProcessInfo 这个类来获取当前 APP 进程信息, 然而咱们的静态库是 pure C++ 工程. 那么如何在 cpp 中调用 Objective-C 呢 ? 这个问题涉及 C++ 和 Objective-C 混编.ios
int ios_process_name(char* buf, int len);
声明在 ios_process_info.h 文件中ios_process_name
include "ios_process_info.h"
, 调用函数 ios_process_name
// // ios_process_info.h // libEasyRTSPClient // // Created by 吴鹏 on 16/9/20. // Copyright © 2016年 org.easydarwin. All rights reserved. // #ifndef ios_process_info_h #define ios_process_info_h int ios_process_name(char* buf, int len); #endif /* ios_process_info_h */
// // ios_process_info.m // libEasyRTSPClient // // Created by 吴鹏 on 16/9/20. // Copyright © 2016年 org.easydarwin. All rights reserved. // #import <Foundation/Foundation.h> #import <Foundation/NSProcessInfo.h> #import "ios_process_info.h" int ios_process_name(char* buf, int len){ NSString *pname = [[NSProcessInfo processInfo] processName]; if(!pname){ return -1; } strncpy(buf,[pname UTF8String],len-1); buf[len-1] = 0; return 0; }
#ifdef __MACH__ #include "ios_process_info.h" int ret = ios_process_name(szProcName, sizeof(szProcName)); #else //TODO 其它平台获取当前进程名称 #endif