Gre*_*ndt 5 c++builder firemonkey
在这个问题的答案中:登录用户接受的答案使用看起来像这样的Delphi代码来访问Cocoa函数NSUserName.
function NSUserName: Pointer; cdecl; external '/System/Library/Frameworks/Foundation.framework/Foundation' name _PU +'NSUserName';
Run Code Online (Sandbox Code Playgroud)
你会如何在C++ Builder中做到这一点?
这回答了问题并且值得赏金
解决方案是使用 dlopen 和 dlsym 在 C++ 中导入 NSUserName:
void* (*NSUserName)();
String UserName;
void *hLib = dlopen("/System/Library/Frameworks/Foundation.framework/Foundation", RTLD_GLOBAL);
if(hLib)
{
NSUserName = (void*(*)())dlsym(hLib, "NSUserName");
CFStringRef srUserName = (CFStringRef)NSUserName();
if(srUserName)
{
UserName = CFStringGetCStringPtr(srUserName, 0);
}
dlclose(hLib);
}
Run Code Online (Sandbox Code Playgroud)
通过添加头文件,可以直接在 C++ Builder 中使用 NSString(Cocoa 类型):
#include <Macapi.Foundation.hpp> // note that this will cause 8080 warnings if you have this warning turned on (unused variables)
Run Code Online (Sandbox Code Playgroud)
现在我可以使用 NSString 代替 CFStringRef (核心基础类型):
UserName = TNSString::Wrap(NSUserName())->UTF8String();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
412 次 |
| 最近记录: |