在纯C中获取NSHomeDirectory()

huangapple go评论63阅读模式
英文:

Get NSHomeDirectory() in pure C

问题

我正在尝试在纯C中获取NSHomeDirectory()

#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CoreFoundation.h>

void testFunc() {
    printf(NSHomeDirectory()); 
}
英文:

I am trying to get the NSHomeDirectory() in pure C

#include &lt;CoreFoundation/CFBundle.h&gt;
#include &lt;CoreFoundation/CoreFoundation.h&gt;

void testFunc() {
    printf(NSHomeDirectory()); 
}

答案1

得分: 3

你需要将UTF8String消息发送给NSHomeDirectory()返回的NSString对象以获取C字符串。要在纯C中执行此操作(已测试):

#include <objc/runtime.h>
#include <objc/message.h>
void *NSHomeDirectory();
void testFunc() {
    puts(((const char *(*)(void *, SEL))objc_msgSend)(NSHomeDirectory(), sel_getUid("UTF8String")));
    // 这里也使用puts()而不是printf()
}

*注意: 使用-framework Cocoa编译。

英文:

You need to send the UTF8String message to the NSString object returned by NSHomeDirectory() to obtain a C string. To do this in pure C (Tested):

#include &lt;objc/runtime.h&gt;
#include &lt;objc/message.h&gt;
void *NSHomeDirectory();
void testFunc() {
    puts(((const char *(*)(void *, SEL))objc_msgSend)(NSHomeDirectory(), sel_getUid(&quot;UTF8String&quot;)));
    // Also use puts() instead of printf() here
}

Note: Compile using -framework Cocoa

huangapple
  • 本文由 发表于 2023年4月20日 09:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定