Search code examples
c++macoshdiutil

C++ interface for hdiutil on Mac


Does a system call or library exist that would allow my C++ code to use hdiutil on Mac OS X. My code needs to mount an available .dmg file and then manipulate what's inside.


Solution

  • If you can use Objective-C++, you can use NSTask to run command line tools:

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/usr/bin/hdiutil"];
    [task setArguments:
        [NSArray arrayWithObjects: @"attach", @"/path/to/dmg/file", nil]];
    [task launch];
    [task waitUntilExit];
    if (0 != [task terminationStatus])
        NSLog(@"Mount failed.");
    [task release];
    

    If you need to use "plain" C++, you can use system():

    if (0 != system("/usr/bin/hdiutil attach /path/to/dmg/file"))
        puts("Mount failed.");
    

    or fork()/exec().

    You'll need to double-check whether hdiutil actually returns 0 for success or not.