How to get the path to my executable ?
09/07/03 22:54
I need to access the system relatively to the current executable path: how ?
In a Cocoa application, the NSBundle class gives the answer:
[[NSBundle mainBundle] executablePath] returns a NSString containing your executable path.
There is also [[NSBundle mainBundle] bundlePath] returning the path to the bundle.
In a tool, under 10.2.x and greater:
use NSGetExecutablePath(char *inPathBuffer,size_t *inOutBufferSize): you pass a pointer to a character buffer and a pointer to a size_t variable with the size of the buffer and the function returns the path to the calling executable and sets the size_t parameter to the length of the returned path.
Under 10.1.x or if your code needs to run on both 10.1x and 10.2.x, see the getpath.c example on Apple developer site.
[[NSBundle mainBundle] executablePath] returns a NSString containing your executable path.
There is also [[NSBundle mainBundle] bundlePath] returning the path to the bundle.
In a tool, under 10.2.x and greater:
use NSGetExecutablePath(char *inPathBuffer,size_t *inOutBufferSize): you pass a pointer to a character buffer and a pointer to a size_t variable with the size of the buffer and the function returns the path to the calling executable and sets the size_t parameter to the length of the returned path.
Under 10.1.x or if your code needs to run on both 10.1x and 10.2.x, see the getpath.c example on Apple developer site.