在苹果iOS平台中获取当前程序进程的进程名等信息

本文由EasyDarwin开源团队成员Penggy供稿;

Objective-C 提供 NSProcessInfo 这个类来获取当前 APP 进程信息, 然而我们的静态库是 pure C++ 工程. 那么如何在 cpp 中调用 Objective-C 呢 ? 这个问题涉及 C++ 和 Objective-C 混编.

  1. 我们把获取进程信息的函数int ios_process_name(char* buf, int len);声明在 ios_process_info.h 文件中
  2. 在 ios_process_info.mm 文件中通过调用 NSProcessInfo 实现函数ios_process_name
  3. 在 .cpp 文件中 include "ios_process_info.h", 调用函数 ios_process_name

ios_process_info.h


//
//  ios_process_info.h
//  libEasyRTSPClient
//
//  Created by Penggy 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.mm


//
//  ios_process_info.m
//  libEasyRTSPClient
//
//  Created by Penggy 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;
}

xxx.cpp


#ifdef __MACH__
#include "ios_process_info.h"
 int ret = ios_process_name(szProcName, sizeof(szProcName));
#else
 //TODO 其它平台获取当前进程名称
#endif


获取更多信息

邮件:support@easydarwin.org

WEB:www.EasyDarwin.org

Copyright © EasyDarwin.org 2012-2016

EasyDarwin



posted @ 2016-09-21 10:44  Babosa  阅读(364)  评论(0编辑  收藏  举报