PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   疑難雜症區 (https://www.pcdvd.com.tw/forumdisplay.php?f=34)
-   -   問一個關於 Objective-C 與 C 的問題 (https://www.pcdvd.com.tw/showthread.php?t=1119251)

Goemon 2016-12-25 10:40 PM

問一個關於 Objective-C 與 C 的問題
 
例如我編譯的 xxx.app 底下有兩個文件例如 cat 與 readme.txt
我打算在yyyy/xxx.app程式啟動的時候執行
$ yyyy/xxx.app/ls -l /yyyy/xxx.app/readme.txt

yyyy 是隨機數值

在 Objective-C 上該怎麼寫


在 C 上面執行shell我目前知道用

mianbundle Patch = yyyy

file_ls = mianbundle Patch + "/ls"
file_readme = mianbundle Patch + "/readme.txt"

代碼:
    pid_t pid;
    char *argv[] = {
        file_ls,
        "-l",
        file_readme,
        NULL
    };


但是這個 mianbundle Patch 我不知道怎麼寫

Goemon 2016-12-26 12:53 AM

那換個方式問好了

代碼:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSLog(@“\n\App Path: %@", bundlePath);

NSString *exec_launch = [[NSBundle mainBundle] pathForResource:@"launch" ofType:@""];
NSLog(@"\n\app Path: %@", exec_launch);


換成 C++ 寫法 該怎麼寫

打工吧魔王大人 2016-12-26 06:03 AM

不明覺厲
只好頂帖.
:eek:

Goemon 2016-12-26 07:50 AM

引用:
作者打工吧魔王大人
不明覺厲
只好頂帖.
:eek:


感謝頂帖, 我寫好了. :D

代碼:
#include "CoreFoundation/CoreFoundation.h"

    // get app mainBundle
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyBundleURL(mainBundle);
    CFStringRef str = CFURLCopyFileSystemPath( resourcesURL, kCFURLPOSIXPathStyle );
    CFRelease(resourcesURL);
    char* app_bundle  = malloc(sizeof(char) * 1024);

    CFStringGetCString( str, app_bundle, FILENAME_MAX, kCFStringEncodingASCII );
    CFRelease(str);
 
    printf("App mainBundle: %s\n", app_bundle);

    // get app mainBundle .app/launch
    const char *file_launch = "/launch";
    char *exec_launch;
    exec_launch = malloc(strlen(app_bundle) + strlen(file_launch) + 1);
    
    strcpy(exec_launch, app_bundle);
    strcat(exec_launch, file_launch);
    
    printf("exec_launch: %s\n", exec_launch);


所有的時間均為GMT +8。 現在的時間是12:28 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。