Skip navigation.
 
mlRe: Finding out executable location from a c program
FROM : Clark Cox
DATE : Tue Nov 20 03:09:36 2007

On Nov 19, 2007 2:55 PM, Scott Stevenson <<email_removed>> wrote:
>
> On Nov 19, 2007, at 8:51 AM, Christiaan Hofman wrote:
>
> >> I can't think of a way of doing it. It's normal practise to put
> >> support files in a location like /usr/local/lib or similar rather
> >> than with the executable on Unix. On a Mac the solution is normally
> >> to package it inside the app bundle and use the API to access that.
> >
> > I also wouldn't know. There should be a way though, as internally
> > NSBundle does.
>
> I believe CFBundle is open source.


Why bother with CFBundle's source? Just *use* CFBundle. (composed in
e-mail, but should give a basic idea):

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

char *GetExecutableLocation() {
    CFBundleRef bundle          = CFBundleGetMainBundle();
    CFURLRef    executableURL  = CFBundleCopyExecutableURL(bundle);
    CFStringRef executablePath  =
CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
    CFIndex    maxLength      =
CFStringGetMaximumSizeOfFileSystemRepresentation(executablePath);
    char        *result        = malloc(maxLength);

    if(result) {
        if(!CFStringGetFileSystemRepresentation(executablePath,
result, maxLength)) {
            free(result);
            result = NULL;
        }
    }

    CFRelease(executablePath);
    CFRelease(executableURL);

    return result;
}


int main() {
    char    *path  = GetExecutableLocation();
    printf("path = \"%s\"\n", path);
    free(path);

    return 0;
}



int main() {
    char    *path  = GetExecutableLocation();
    printf("path = \"%s\"\n", path);
    free(path);

    return 0;
}


--
Clark S. Cox III
<email_removed>

Related mailsAuthorDate
mlFinding out executable location from a c program Antti Karanta Nov 19, 17:06
mlRe: Finding out executable location from a c program I. Savant Nov 19, 17:12
mlRe: Finding out executable location from a c program Antti Karanta Nov 19, 17:24
mlRe: Finding out executable location from a c program Paul Sargent Nov 19, 17:37
mlRe: Finding out executable location from a c program I. Savant Nov 19, 17:44
mlRe: Finding out executable location from a c program Christiaan Hofman Nov 19, 17:51
mlRe: Finding out executable location from a c program Roy Lovejoy Nov 19, 23:53
mlRe: Finding out executable location from a c program Scott Stevenson Nov 19, 23:55
mlRe: Finding out executable location from a c program Clark Cox Nov 20, 03:09
mlRe: Finding out executable location from a c program Scott Stevenson Nov 20, 06:31
mlRe: Finding out executable location from a c program Clark S. Cox III Nov 20, 06:37
mlRe: Finding out executable location from a c program Alastair Houghton Nov 20, 13:36
mlRe: Finding out executable location from a c program Antti Karanta Nov 23, 21:22
mlRe: Finding out executable location from a c program Antti Karanta Nov 23, 21:52
mlRe: Finding out executable location from a c program Douglas Davidson Nov 26, 19:44
mlRe: Finding out executable location from a c program Alastair Houghton Nov 27, 13:11