我有一个C ++应用程序,我在Mac OS Bundle中重新分发。如何通过打开MyApp.app
- 捆绑包或执行MyApp.app/Contents/MacOS/myapp
来检查我的应用是否已启动时检入代码?
我试过了:
#include "CoreFoundation/CoreFoundation.h"
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyBundleURL(mainBundle);
CFStringRef str = CFURLCopyFileSystemPath( resourcesURL, kCFURLPOSIXPathStyle );
CFRelease(resourcesURL);
char* path = malloc(sizeof(char) * 1024);
CFStringGetCString( str, path, FILENAME_MAX, kCFStringEncodingASCII );
CFRelease(str);
printf("NS: %s\n", path);
这总是给我MyApp.app
的路径。我试过这个:
#include <mach-o/dyld.h>
char pathbuf[PATH_MAX + 1];
char real_executable[PATH_MAX + 1];
char *bundle_id;
uint32_t bufsize = sizeof(pathbuf);
_NSGetExecutablePath( pathbuf, &bufsize);
printf("NS: %s\n", pathbuf);
始终打印可执行文件的路径。