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