Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoqtmac

macrumors newbie
Original poster
Mar 15, 2011
1
0
Hi,
I am very much new to mac programming. I need to get volume uuid of the device(hard disk where my mac installed) through a c/c++ program. How can i do this? I googled regarding this but i am getting commends like "ioreg" , "diskutil". But all these are commands to run in terminal. But i need to achieve this from a program. A sample piece of code is really helpful.

Thank you in advance.
 
Use NSTask

Hi,

bash script which return disk UUID will look something like

Code:
/usr/sbin/diskutil info / | /usr/bin/awk '$0 ~ /UUID/ { print $3 }'

save it to file with shebang at top -> #!/bin/sh
and then use NSTask to run it and get returned value. Do you know how?

Another example (written in mail):

Code:
NSTask *task;
	task = [[NSTask alloc] init];
	[task setLaunchPath: @"/usr/sbin/diskutil"];
	
	NSArray *arguments;
	arguments = [NSArray arrayWithObjects:@"info",@"-plist",@"/",nil];
	[task setArguments: arguments];
	
	NSPipe *pipe = [NSPipe pipe];
	[task setStandardOutput: pipe];
	
	NSFileHandle *file = [pipe fileHandleForReading];
	[task launch];

NSData *data = [file readDataToEndOfFile];
    
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                    propertyListFromData:data
                                    mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                    format:&format errorDescription:&errorDesc];

NSString *diskUUID = [temp objectForKey:@"VolumeUUID"];
[task release];

hope it helps.

Les
 
If this was in Obj-C it's simpler to just get it from the IOKit framework. I think there was an easy way of getting it via the DiskArbitration framework but it may have been deprecated. I don't remember.
 
You can use the DiskArbitration framework and kDADiskDescriptionVolumeUUIDKey.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.