2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
theactiveactor
  • 7,314
  • 15
  • 52
  • 60

3 Answers3

3

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.

Dewayne Christensen
  • 2,084
  • 13
  • 15
3

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.

Community
  • 1
  • 1
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Hi, I'm just doing some research in hdiutil private framworks in order to attach DMG file programmatically (file that represent disk image with since HFS+ partition). I wonder if you happen to dig a little inside DiskImages framework and look for the right method ? thanks – Zohar81 Feb 28 '18 at 19:28
  • @Zohar81 you'll want class-dump for that: http://stevenygard.com/projects/class-dump/ – alfwatt Apr 20 '18 at 02:15
1

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++.

Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151