Question

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.

Was it helpful?

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.

OTHER TIPS

hdiutil uses DiskImages.framework; unfortunately, the framework is private (undocumented, no headers), but if you're feeling adventurous, you can try to use it anyway.

hdiutil has a -plist argument that causes it to format its output as a standard OS X plist. See this for info on processing the output from hdiutil; because you'll likely have to examine the output a bit to find what you need, it may be easy to do this initially with something like python. Then see here for some suggestions on parsing plists in C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top